UFO: Alien Invasion
cp_hospital_callbacks.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2022 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23 */
24 
25 #include "../../cl_shared.h"
26 #include "../../cl_team.h" /* CL_ActorGetSkillString */
27 #include "../../ui/ui_dataids.h"
28 #include "cp_campaign.h"
29 #include "cp_hospital_callbacks.h"
30 #include "cp_hospital.h"
31 #include "cp_team.h"
32 
33 static void HOS_EntryWoundData (const character_t& chr)
34 {
35  const BodyData* bodyData = chr.teamDef->bodyTemplate;
36  const woundInfo_t& wounds = chr.wounds;
37 
38  for (int bodyPart = 0; bodyPart < bodyData->numBodyParts(); ++bodyPart) {
39  if (wounds.treatmentLevel[bodyPart] == 0)
40  continue;
41  const float severity = static_cast<float>(wounds.treatmentLevel[bodyPart]) / chr.maxHP;
42  char text[MAX_VAR];
43  Com_sprintf(text, lengthof(text), CHRSH_IsTeamDefRobot(chr.teamDef) ? _("Damaged %s (damage: %i)") :
44  _("Wounded %s (damage: %i)"), _(bodyData->name(bodyPart)), wounds.treatmentLevel[bodyPart]);
45  cgi->UI_ExecuteConfunc("hospital_wounds %i %s %f \"%s\"", chr.ucn, bodyData->id(bodyPart), severity, text);
46  }
47 }
48 
49 static const char* HOS_GetRank (const Employee& employee)
50 {
51  /* Print rank for soldiers or type for other employees. */
52  if (employee.isSoldier()) {
53  const rank_t* rankPtr = CL_GetRankByIdx(employee.chr.score.rank);
54  return _(rankPtr->name);
55  }
56  return E_GetEmployeeString(employee.getType(), 1);
57 }
58 
59 static const char* HOS_GetInjuryLevelString (const Employee& employee, float injuryLevel)
60 {
61  /* If the employee is seriously wounded (HP <= 50% maxHP), make him red. */
62  if (employee.chr.HP <= (int) (employee.chr.maxHP * 0.5) || injuryLevel > 0.5f)
63  return "serious";
64 
65  /* If the employee is semi-seriously wounded (HP <= 85% maxHP), make him yellow. */
66  else if (employee.chr.HP <= (int) (employee.chr.maxHP * 0.85) || injuryLevel > 0.15f)
67  return "medium";
68 
69  /* no wounds and full hp */
70  else if (employee.chr.HP >= employee.chr.maxHP && injuryLevel < 0.0001f)
71  return "healthy";
72 
73  return "light";
74 }
75 
76 static inline void HOS_Entry (const Employee& employee)
77 {
78  const character_t& chr = employee.chr;
79  const float injuryLevel = HOS_GetInjuryLevel(chr);
80  if (!employee.isSoldier() && chr.HP >= chr.maxHP && injuryLevel <= 0.0f)
81  return;
82 
83  const char* rank = HOS_GetRank(employee);
84  const char* level = HOS_GetInjuryLevelString(employee, injuryLevel);
85  const aircraft_t* craft = AIR_IsEmployeeInAircraft(&employee, nullptr);
86 
87  cgi->UI_ExecuteConfunc("hospitaladd %i \"%s\" %i %i \"%s\" \"%s\" \"%s\"",
88  chr.ucn, level, chr.HP, chr.maxHP, chr.name, rank, craft ? craft->name : "");
89  HOS_EntryWoundData(chr);
90 }
91 
92 static void HOS_ImplantDetails_f (void)
93 {
94  const int odIdx = atoi(cgi->Cmd_Argv(1));
95  const objDef_t* od = INVSH_GetItemByIDX(odIdx);
96  if (od == nullptr)
97  return;
98  const implantDef_t* def = INVSH_GetImplantForObjDef(od);
99  if (def == nullptr)
100  return;
101  cgi->Com_Printf("implant: %s\n", def->id);
102 }
103 
108 static void HOS_Init_f (void)
109 {
110  if (cgi->Cmd_Argc() < 2) {
111  cgi->Com_Printf("Usage: %s <baseIDX>\n", cgi->Cmd_Argv(0));
112  return;
113  }
114  base_t* base = B_GetFoundedBaseByIDX(atoi(cgi->Cmd_Argv(1)));
115  if (base == nullptr) {
116  cgi->Com_Printf("%s: Invalid base IDX: %s\n", cgi->Cmd_Argv(0), cgi->Cmd_Argv(1));
117  return;
118  }
119 
120  if (!B_GetBuildingStatus(base, B_HOSPITAL)) {
121  cgi->UI_PopWindow(false);
122  return;
123  }
124 
125  bool containerSet = false;
126  for (int type = 0; type < MAX_EMPL; type++) {
127  E_Foreach(type, employee) {
128  /* Don't show soldiers who are not in this base or gone in mission */
129  if (!employee->isHiredInBase(base) || employee->isAwayFromBase())
130  continue;
131  HOS_Entry(*employee);
132  if (containerSet)
133  continue;
134  CP_SetEquipContainer(&employee->chr);
135  containerSet = true;
136  }
137  }
138 }
139 
140 static const cmdList_t hospitalCmds[] = {
141  {"hosp_init", HOS_Init_f, "Init function for hospital menu"},
142  {"hosp_implant_details", HOS_ImplantDetails_f, "Print details for an implant"},
143  {nullptr, nullptr, nullptr}
144 };
145 
146 void HOS_InitCallbacks (void)
147 {
148  cgi->Cmd_TableAddList(hospitalCmds);
149 }
150 
152 {
153  cgi->Cmd_TableRemoveList(hospitalCmds);
154 }
QGL_EXTERN GLint GLenum type
Definition: r_gl.h:94
Defines all attributes of objects used in the inventory.
Definition: inv_shared.h:264
bool B_GetBuildingStatus(const base_t *const base, const buildingType_t buildingType)
Get the status associated to a building.
Definition: cp_base.cpp:478
Header file for menu related console command callbacks.
A base with all it&#39;s data.
Definition: cp_base.h:84
#define E_Foreach(employeeType, var)
Definition: cp_employee.h:122
const aircraft_t * AIR_IsEmployeeInAircraft(const Employee *employee, const aircraft_t *aircraft)
Tells you if an employee is assigned to an aircraft.
#define _(String)
Definition: cl_shared.h:44
short numBodyParts(void) const
Definition: chr_shared.cpp:389
static const char * HOS_GetInjuryLevelString(const Employee &employee, float injuryLevel)
float HOS_GetInjuryLevel(const character_t &chr)
Definition: cp_hospital.cpp:96
const BodyData * bodyTemplate
Definition: chr_shared.h:350
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition: shared.cpp:494
static void HOS_Entry(const Employee &employee)
Describes a character with all its attributes.
Definition: chr_shared.h:388
character_t chr
Definition: cp_employee.h:119
const char *IMPORT * Cmd_Argv(int n)
const teamDef_t * teamDef
Definition: chr_shared.h:413
char name[MAX_VAR]
Definition: cp_aircraft.h:121
rank_t * CL_GetRankByIdx(const int index)
Returns a rank at an index.
Definition: cp_rank.cpp:50
static const char * HOS_GetRank(const Employee &employee)
static void HOS_EntryWoundData(const character_t &chr)
base_t * B_GetFoundedBaseByIDX(int baseIdx)
Array bound check for the base index.
Definition: cp_base.cpp:326
Info on a wound.
Definition: chr_shared.h:361
static void HOS_Init_f(void)
Script command to initialize the hospital menu.
employeeType_t getType() const
Definition: cp_employee.h:99
#define MAX_VAR
Definition: shared.h:36
Header file for hospital related stuff.
const cgame_import_t * cgi
int treatmentLevel[BODYPART_MAXTYPE]
Definition: chr_shared.h:363
void HOS_ShutdownCallbacks(void)
bool isSoldier() const
Definition: cp_employee.h:82
woundInfo_t wounds
Definition: chr_shared.h:402
bool CHRSH_IsTeamDefRobot(const teamDef_t *const td)
Check if a team definition is a robot.
Definition: chr_shared.cpp:103
const char * name
Definition: cp_rank.h:31
const implantDef_t * INVSH_GetImplantForObjDef(const objDef_t *od)
Definition: inv_shared.cpp:291
const char * id(void) const
Definition: chr_shared.cpp:359
An aircraft with all it&#39;s data.
Definition: cp_aircraft.h:115
chrScoreGlobal_t score
Definition: chr_shared.h:406
const char * id
Definition: inv_shared.h:102
Definition: cmd.h:86
const char * E_GetEmployeeString(employeeType_t type, int n)
Convert employeeType_t to translated string.
void HOS_InitCallbacks(void)
Header file for single player campaign control.
const objDef_t * INVSH_GetItemByIDX(int index)
Returns the item that belongs to the given index or nullptr if the index is invalid.
Definition: inv_shared.cpp:266
char name[MAX_VAR]
Definition: chr_shared.h:390
#define lengthof(x)
Definition: shared.h:105
static const cmdList_t hospitalCmds[]
Team management for the campaign gametype headers.
Describes a rank that a recruit can gain.
Definition: cp_rank.h:29
const char * name(const short bodyPart) const
Definition: chr_shared.cpp:369
static void HOS_ImplantDetails_f(void)
level_locals_t level
Definition: g_main.cpp:38
void CP_SetEquipContainer(character_t *chr)
Set up equip (floor) container for soldiers.
Definition: cp_team.cpp:137