UFO: Alien Invasion
Loading...
Searching...
No Matches
cp_aliencont_callbacks.cpp
Go to the documentation of this file.
1
5
6/*
7Copyright (C) 2002-2025 UFO: Alien Invasion.
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18See the GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, 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
32#include "../../cl_shared.h"
33#include "cp_campaign.h"
34#include "aliencontainment.h"
35
41static void AC_Init_f (void)
42{
43 base_t* base;
44 if (cgi->Cmd_Argc() < 2)
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
73static void AC_KillAll_f (void)
74{
75 base_t* base;
76
77 if (cgi->Cmd_Argc() < 2) {
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
100static void AC_KillOne_f (void)
101{
102 base_t* base;
103 const int argc = cgi->Cmd_Argc();
104
105 if (argc < 3) {
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
138static 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
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};
180{
181 cgi->Cmd_TableAddList(alienContCallbacks);
182}
183
185{
186 cgi->Cmd_TableRemoveList(alienContCallbacks);
187}
Alien containment class header.
Share stuff between the different cgame implementations.
#define _(String)
Definition cl_shared.h:44
linkedList_t * list(void) const
Returns a copy of the cargo list.
virtual bool add(const teamDef_t *team, int alive, int dead)
Add aliens to the containment by teamDef.
static int getCapacityNeedForAlien(const teamDef_t *teamDef, const bool isDead)
Returns the number of capacity needed for an alien in the containment.
static void AC_KillExceeding_f(void)
Kill aliens over a certain capacity.
static void AC_KillOne_f(void)
Kill single alien of a given type.
void AC_InitCallbacks(void)
static void AC_Init_f(void)
Alien containment menu init function.
static void AC_KillAll_f(void)
Console command to kill all aliens on a base.
void AC_ShutdownCallbacks(void)
static const cmdList_t alienContCallbacks[]
Header file for menu callback functions used for alien containment menu.
base_t * B_GetFoundedBaseByIDX(int baseIdx)
Array bound check for the base index.
Definition cp_base.cpp:326
base_t * B_GetCurrentSelectedBase(void)
returns the currently selected base
Definition cp_base.cpp:1578
Header file for single player campaign control.
const cgame_import_t * cgi
#define CAP_GetCurrent(base, capacity)
Definition cp_capacity.h:52
@ CAP_ALIENS
Definition cp_capacity.h:28
#define CAP_GetMax(base, capacity)
Definition cp_capacity.h:51
technology_t * RS_GetTechForTeam(const teamDef_t *team)
Returns technology entry for a team.
bool RS_IsResearched_ptr(const technology_t *tech)
Checks whether an item is already researched.
#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
#define Q_streq(a, b)
Definition shared.h:136
alien cargo entry
Definition aliencargo.h:32
A base with all it's data.
Definition cp_base.h:84
class AlienContainment * alienContainment
Definition cp_base.h:108
This is the technology parsed from research.ufo.
float overallTime