UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cp_hospital.cpp
Go to the documentation of this file.
1 
7 /*
8 Copyright (C) 2002-2020 UFO: Alien Invasion.
9 
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 See the GNU General Public License for more details.
20 
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 
25 */
26 
27 #include "../../cl_shared.h"
28 #include "cp_campaign.h"
29 #include "cp_hospital.h"
30 
31 #define GET_HP_HEALING( ab ) (1 + (ab) * 15/MAX_SKILL)
32 
33 static void HOS_HealWounds (character_t* chr, int healing)
34 {
35  woundInfo_t& wounds = chr->wounds;
36 
37  for (int bodyPart = 0; bodyPart < chr->teamDef->bodyTemplate->numBodyParts(); ++bodyPart) {
38  if (wounds.treatmentLevel[bodyPart] <= 0)
39  continue;
40  wounds.treatmentLevel[bodyPart] = std::max(0, wounds.treatmentLevel[bodyPart] - healing);
41  }
42 }
43 
51 bool HOS_HealCharacter (character_t* chr, bool hospital)
52 {
53  assert(chr);
54  float healing = ccs.curCampaign->healingRate;
55 
56  if (hospital) {
57  healing *= GET_HP_HEALING(chr->score.skills[ABILITY_POWER]);
58  HOS_HealWounds(chr, healing);
59  }
60 
61  if (chr->HP < chr->maxHP) {
62  /* if the character has less that 100 hitpoints, he will be disadvantaged by using the percentage
63  * method of allocating hitpoints. So just give the character "healing" as Hitpoints, otherwise
64  * allocate "healing" as a percentage of the characters total hitpoints. */
65  if (chr->maxHP < INITIAL_HP)
66  chr->HP = std::min(chr->HP + static_cast<int>(healing), chr->maxHP);
67  else
68  chr->HP = std::min(chr->HP + static_cast<int>(((healing / 100.0f) * chr->maxHP)), chr->maxHP);
69 
70  if (chr->HP == chr->maxHP)
71  return false;
72  return true;
73  }
74  return false;
75 }
76 
82 void HOS_HospitalRun (void)
83 {
84  for (int i = 0; i < MAX_EMPL; i++) {
86  E_Foreach(type, employee) {
87  if (!employee->isHired())
88  continue;
89  const bool hospital = B_GetBuildingStatus(employee->baseHired, B_HOSPITAL);
90  HOS_HealCharacter(&(employee->chr), hospital);
91  CHRSH_UpdateImplants(employee->chr);
92  }
93  }
94 }
95 
96 float HOS_GetInjuryLevel (const character_t& chr)
97 {
98  float injuryLevel = 0.0f;
99 
100  const BodyData* const bt = chr.teamDef->bodyTemplate;
101  for (int i = 0; i < bt->numBodyParts(); ++i) {
102  const float woundLevel = chr.wounds.treatmentLevel[i];
103  if (woundLevel * 0.5f >= bt->woundThreshold(i) * chr.maxHP)
104  injuryLevel += woundLevel / chr.maxHP;
105  }
106 
107  return injuryLevel;
108 }
109 
110 bool HOS_NeedsHealing (const character_t& chr)
111 {
112  return HOS_GetInjuryLevel(chr) > 0.0001f || chr.HP < chr.maxHP;
113 }
114 
115 #ifdef DEBUG
116 
119 static void HOS_HealAll_f (void)
120 {
121  if (cgi->Cmd_Argc() < 2) {
122  cgi->Com_Printf("Usage: %s <baseIDX>\n", cgi->Cmd_Argv(0));
123  return;
124  }
125  base_t* base = B_GetBaseByIDX(atoi(cgi->Cmd_Argv(1)));
126  if (!base) {
127  cgi->Com_Printf("Invalid base idx\n");
128  return;
129  }
130 
131  for (int type = 0; type < MAX_EMPL; type++) {
132  E_Foreach(type, employee) {
133  if (!employee->isHiredInBase(base))
134  continue;
135  employee->chr.HP = employee->chr.maxHP;
136  }
137  }
138 }
139 
143 static void HOS_HurtAll_f (void)
144 {
145  if (cgi->Cmd_Argc() < 2) {
146  cgi->Com_Printf("Usage: %s <baseIDX> [amount]\n", cgi->Cmd_Argv(0));
147  return;
148  }
149  base_t* base = B_GetBaseByIDX(atoi(cgi->Cmd_Argv(1)));
150  if (!base) {
151  cgi->Com_Printf("Invalid base idx\n");
152  return;
153  }
154 
155  int amount;
156  if (cgi->Cmd_Argc() >= 3)
157  amount = atoi(cgi->Cmd_Argv(1));
158  else
159  amount = 1;
160 
161  for (int type = 0; type < MAX_EMPL; type++) {
162  E_Foreach(type, employee) {
163  /* only those employees, that are in the current base */
164  if (!employee->isHiredInBase(base))
165  continue;
166  employee->chr.HP = std::max(0, employee->chr.HP - amount);
167  }
168  }
169 }
170 #endif
171 
177 void HOS_InitStartup (void)
178 {
179 #ifdef DEBUG
180  cgi->Cmd_AddCommand("debug_hosp_hurt_all", HOS_HurtAll_f, "Debug function to hurt all employees in the current base by one");
181  cgi->Cmd_AddCommand("debug_hosp_heal_all", HOS_HealAll_f, "Debug function to heal all employees in the current base completely");
182 #endif
183 }
184 
191 {
192  /* nothing to save here */
193  return true;
194 }
195 
202 {
203  return true;
204 }
205 
210 bool HOS_HospitalAllowed (const base_t* base)
211 {
212  return !B_IsUnderAttack(base) && B_GetBuildingStatus(base, B_HOSPITAL);
213 }
#define GET_HP_HEALING(ab)
Definition: cp_hospital.cpp:31
chrScoreGlobal_t score
Definition: chr_shared.h:387
QGL_EXTERN GLint GLenum type
Definition: r_gl.h:94
bool B_GetBuildingStatus(const base_t *const base, const buildingType_t buildingType)
Get the status associated to a building.
Definition: cp_base.cpp:477
#define E_Foreach(employeeType, var)
Definition: cp_employee.h:122
const teamDef_t * teamDef
Definition: chr_shared.h:394
float HOS_GetInjuryLevel(const character_t &chr)
Definition: cp_hospital.cpp:96
#define B_IsUnderAttack(base)
Definition: cp_base.h:53
bool HOS_LoadXML(xmlNode_t *p)
Saving function for hospital related data.
A base with all it's data.
Definition: cp_base.h:84
#define xmlNode_t
Definition: xml.h:24
void CHRSH_UpdateImplants(character_t &chr)
Updates the characters permanent implants. Called every day.
Definition: chr_shared.cpp:139
int treatmentLevel[BODYPART_MAXTYPE]
Definition: chr_shared.h:352
Header file for hospital related stuff.
bool HOS_HealCharacter(character_t *chr, bool hospital)
Heals character.
Definition: cp_hospital.cpp:51
const cgame_import_t * cgi
employeeType_t
The types of employees.
Definition: cp_employee.h:30
bool HOS_SaveXML(xmlNode_t *p)
Saving function for hospital related data.
ccs_t ccs
Definition: cp_campaign.cpp:62
base_t * B_GetBaseByIDX(int baseIdx)
Array bound check for the base index. Will also return unfounded bases as long as the index is in the...
Definition: cp_base.cpp:312
Info on a wound.
Definition: chr_shared.h:350
static void HOS_HealWounds(character_t *chr, int healing)
Definition: cp_hospital.cpp:33
void HOS_InitStartup(void)
Initial stuff for hospitals Bind some of the functions in this file to console-commands that you can ...
float woundThreshold(const short bodyPart) const
Definition: chr_shared.cpp:383
woundInfo_t wounds
Definition: chr_shared.h:383
QGL_EXTERN GLfloat f
Definition: r_gl.h:114
float healingRate
Definition: cp_campaign.h:198
short numBodyParts(void) const
Definition: chr_shared.cpp:388
QGL_EXTERN GLint i
Definition: r_gl.h:113
const BodyData * bodyTemplate
Definition: chr_shared.h:339
bool HOS_NeedsHealing(const character_t &chr)
Header file for single player campaign control.
#define INITIAL_HP
Definition: defines.h:102
void HOS_HospitalRun(void)
Checks health status of all employees in all bases.
Definition: cp_hospital.cpp:82
campaign_t * curCampaign
Definition: cp_campaign.h:377
int skills[SKILL_NUM_TYPES]
Definition: chr_shared.h:122
const char *IMPORT * Cmd_Argv(int n)
Describes a character with all its attributes.
Definition: chr_shared.h:369
bool HOS_HospitalAllowed(const base_t *base)
Returns true if you can enter in the hospital.