UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cp_aliencont_callbacks.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2020 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24 
25 /*
26 #include "../../cl_shared.h"
27 #include "../../ui/ui_dataids.h"
28 #include "cp_aliencont.h"
29 */
30 
31 #include "cp_aliencont_callbacks.h"
32 #include "../../cl_shared.h"
33 #include "cp_campaign.h"
34 #include "aliencontainment.h"
35 
41 static void AC_Init_f (void)
42 {
43  base_t* base;
44  if (cgi->Cmd_Argc() < 2)
45  base = B_GetCurrentSelectedBase();
46  else
47  base = B_GetFoundedBaseByIDX(atoi(cgi->Cmd_Argv(1)));
48  if (!base) {
49  cgi->Com_Printf("No base selected\n");
50  return;
51  }
52 
53  cgi->UI_ExecuteConfunc("ui_aliencont_cap %d %d", CAP_GetCurrent(base, CAP_ALIENS), CAP_GetMax(base, CAP_ALIENS));
54  cgi->UI_ExecuteConfunc("ui_aliencont_clear");
55  if (!base->alienContainment)
56  return;
57  linkedList_t* list = base->alienContainment->list();
58  LIST_Foreach(list, alienCargo_t, item) {
59  const technology_t* tech = RS_GetTechForTeam(item->teamDef);
60  cgi->UI_ExecuteConfunc("ui_aliencont_add \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" %f %d %d",
61  item->teamDef->id, _(item->teamDef->name), tech->id, tech->image,
62  (RS_IsResearched_ptr(tech)) ? _("Researched") : _("Awaiting autopsy"),
63  (1.0f - tech->time / tech->overallTime) * 100, item->alive, item->dead);
64  }
65  cgi->LIST_Delete(&list);
66 }
67 
73 static void AC_KillAll_f (void)
74 {
75  base_t* base;
76 
77  if (cgi->Cmd_Argc() < 2) {
78  base = B_GetCurrentSelectedBase();
79  } else {
80  base = B_GetFoundedBaseByIDX(atoi(cgi->Cmd_Argv(1)));
81  }
82 
83  if (!base)
84  return;
85  if (!base->alienContainment)
86  return;
87 
88  linkedList_t* list = base->alienContainment->list();
89  LIST_Foreach(list, alienCargo_t, item) {
90  base->alienContainment->add(item->teamDef, -item->alive, item->alive);
91  }
92  cgi->LIST_Delete(&list);
93 
94  cgi->Cmd_ExecuteString("ui_aliencont_init");
95 }
96 
100 static void AC_KillOne_f (void)
101 {
102  base_t* base;
103  const int argc = cgi->Cmd_Argc();
104 
105  if (argc < 3) {
106  base = B_GetCurrentSelectedBase();
107  } else {
108  base = B_GetFoundedBaseByIDX(atoi(cgi->Cmd_Argv(1)));
109  }
110  if (!base)
111  return;
112  if (!base->alienContainment)
113  return;
114 
115  if (argc < 2) {
116  cgi->Com_Printf("Usage: %s [baseIdx] <techId>\n", cgi->Cmd_Argv(0));
117  return;
118  }
119 
120  /* this function should work by teamDef ID (or raceID), but currently multple teams defined per race
121  * that makes the thing more complicated */
122  const char* techId = cgi->Cmd_Argv(argc - 1);
123  linkedList_t* list = base->alienContainment->list();
124  LIST_Foreach(list, alienCargo_t, item) {
125  const technology_t* tech = RS_GetTechForTeam(item->teamDef);
126  if (!Q_streq(tech->id, techId))
127  continue;
128  base->alienContainment->add(item->teamDef, -1, 1);
129  }
130  cgi->LIST_Delete(&list);
131 
132  cgi->Cmd_ExecuteString("ui_aliencont_init");
133 }
134 
138 static void AC_KillExceeding_f (void)
139 {
140  base_t* base;
141  const int argc = cgi->Cmd_Argc();
142 
143  if (argc < 2) {
144  cgi->Com_Printf("Usage: %s <baseID>\n", cgi->Cmd_Argv(0));
145  return;
146  }
147  base = B_GetFoundedBaseByIDX(atoi(cgi->Cmd_Argv(1)));
148  if (!base)
149  return;
150  if (!base->alienContainment)
151  return;
152 
153  const int limit = CAP_GetMax(base, CAP_ALIENS);
154  int aliens = CAP_GetCurrent(base, CAP_ALIENS);
155  if (aliens <= limit)
156  return;
157 
158  /* Kill aliens */
159  linkedList_t* list = base->alienContainment->list();
160  LIST_Foreach(list, alienCargo_t, item) {
161  const int alienSize = base->alienContainment->getCapacityNeedForAlien(item->teamDef, false);
162  int substract = std::min(int(ceil(1.0f * (aliens - limit) / alienSize)), int(item->alive * alienSize));
163 
164  base->alienContainment->add(item->teamDef, -1 * substract, substract);
165  aliens = CAP_GetCurrent(base, CAP_ALIENS);
166  if (aliens <= limit)
167  break;
168  }
169  cgi->LIST_Delete(&list);
170 }
171 
172 static const cmdList_t alienContCallbacks[] = {
173  {"ui_aliencont_init", AC_Init_f, "Init function for alien containment menu"},
174  {"ui_aliencont_killall", AC_KillAll_f, "Kills all aliens in current base"},
175  {"ui_aliencont_killone", AC_KillOne_f, "Kills one alien of a given type"},
176  {"aliencont_killexceeding", AC_KillExceeding_f, "Kills aliens that exceed a certain base capacity"},
177  {nullptr, nullptr, nullptr}
178 };
179 void AC_InitCallbacks (void)
180 {
181  cgi->Cmd_TableAddList(alienContCallbacks);
182 }
183 
185 {
186  cgi->Cmd_TableRemoveList(alienContCallbacks);
187 }
static const cmdList_t alienContCallbacks[]
Header file for menu callback functions used for alien containment menu.
bool RS_IsResearched_ptr(const technology_t *tech)
Checks whether an item is already researched.
#define _(String)
Definition: cl_shared.h:43
static void AC_KillAll_f(void)
Console command to kill all aliens on a base.
static void AC_KillOne_f(void)
Kill single alien of a given type.
linkedList_t * list(void) const
Returns a copy of the cargo list.
Definition: aliencargo.cpp:150
base_t * B_GetCurrentSelectedBase(void)
returns the currently selected base
Definition: cp_base.cpp:1578
class AlienContainment * alienContainment
Definition: cp_base.h:108
#define CAP_GetCurrent(base, capacity)
Definition: cp_capacity.h:52
A base with all it's data.
Definition: cp_base.h:84
base_t * B_GetFoundedBaseByIDX(int baseIdx)
Array bound check for the base index.
Definition: cp_base.cpp:325
static void AC_KillExceeding_f(void)
Kill aliens over a certain capacity.
technology_t * RS_GetTechForTeam(const teamDef_t *team)
Returns technology entry for a team.
static int getCapacityNeedForAlien(const teamDef_t *teamDef, const bool isDead)
Returns the number of capacity needed for an alien in the containment.
alien cargo entry
Definition: aliencargo.h:32
const cgame_import_t * cgi
This is the technology parsed from research.ufo.
Definition: cp_research.h:137
Definition: cmd.h:86
static void AC_Init_f(void)
Alien containment menu init function.
Alien containment class header.
QGL_EXTERN GLfloat f
Definition: r_gl.h:114
#define CAP_GetMax(base, capacity)
Definition: cp_capacity.h:51
char * image
Definition: cp_research.h:170
virtual bool add(const teamDef_t *team, int alive, int dead)
Add aliens to the containment by teamDef.
void AC_ShutdownCallbacks(void)
#define LIST_Foreach(list, type, var)
Iterates over a linked list, it's safe to delete the returned entry from the list while looping over ...
Definition: list.h:41
Header file for single player campaign control.
void AC_InitCallbacks(void)
#define Q_streq(a, b)
Definition: shared.h:136
float overallTime
Definition: cp_research.h:156
const char *IMPORT * Cmd_Argv(int n)