File: energy.cc

package info (click to toggle)
achilles 2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 528 kB
  • ctags: 387
  • sloc: sh: 2,600; cpp: 2,067; makefile: 45
file content (103 lines) | stat: -rw-r--r-- 2,227 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* 

Copyright (C) 2000 Matthew Danish

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include"energy.h"
#include"vector.h"
#include"defines.h"
#include<stdio.h>
#include<iostream.h>

EnergyClass::EnergyClass() :
  food(0),
  health(0),
  fcap(0),
  hcap(0)
{}

EnergyClass::EnergyClass(VectorClass &s) :
  food(0),
  health(MIN_ECAP + (s.X() * s.Y() * s.Z() - MIN_VOLUME) * (MAX_ECAP - MIN_ECAP) / (MAX_VOLUME - MIN_VOLUME)),
  fcap(health),
  hcap(health)
{
#ifdef _DEBUG
  printf("HCAP: %f\n",hcap);
#endif
}

EnergyClass::~EnergyClass() {}

double EnergyClass::Food() {
  return food;
}

double EnergyClass::Health() {
  return health;
}

bool EnergyClass::SetCap(VectorClass &s) {
  hcap = fcap = MIN_ECAP + (s.X() * s.Y() * s.Z() - MIN_VOLUME) * (MAX_ECAP - MIN_ECAP) * (MAX_VOLUME - MIN_VOLUME);

  if(health > hcap) health = hcap;
  if(food > fcap) food = fcap;
  return true;
}

bool EnergyClass::TakeDamage(double dmg) {
  health-=dmg;
  if(health<=0) {
    health = 0;
    return false;  // dead!
  } else
    return true;
}

bool EnergyClass::UseEnergy(double amt) {
  if(food-amt < 0)
    return false;  // no can do..
  else {
    food-=amt;
    return true;
  }
}

double EnergyClass::Regen(double meta) {
  double amt = (hcap-health)/hcap * (fcap-food)/fcap * food * meta;
  food-=amt;
  health+=amt;
  if(health>hcap) health=hcap;
  if(food<0) food=0;
  return amt;
}

bool EnergyClass::EatFood(double f) {
  food+=f;
  if(f>fcap) f=fcap;
  if(f<0) f=0;
  return true;
}

double EnergyClass::HealthCap() {
  return hcap;
}

double EnergyClass::FoodCap() {
  return fcap;
}