UFO: Alien Invasion
Doxygen documentation generating
cl_map_callbacks.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2023 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_map_callbacks.h"
26 #include "../client.h"
27 #include "cl_game.h"
28 
29 typedef enum ui_gamemode_s {
36 
37 static void UI_MapInfoGetNext (int step)
38 {
39  int ref = cls.currentSelectedMap;
40 
41  for (;;) {
42  cls.currentSelectedMap += step;
43  if (cls.currentSelectedMap < 0)
46 
48 
49  /* avoid infinite loop */
50  if (ref == cls.currentSelectedMap)
51  break;
52  /* special purpose maps are not startable without the specific context */
53  if (md->mapTheme[0] == '.')
54  continue;
55 
56  if (md->mapTheme[0] != '+' && FS_CheckFile("maps/%s.bsp", md->mapTheme) != -1)
57  break;
58  if (md->mapTheme[0] == '+' && FS_CheckFile("maps/%s.ump", md->mapTheme + 1) != -1)
59  break;
60  }
61 }
62 
66 static void UI_MapInfo (int step)
67 {
68  const cgame_export_t* list = cls.gametype;
69 
70  if (!list)
71  return;
72 
73  if (!csi.numMDs)
74  return;
75 
76  UI_MapInfoGetNext(step);
77 
78  const mapDef_t* md = list->MapInfo(step);
79  if (!md)
80  return;
81 
82  const char* mapname = md->mapTheme;
83  /* skip random map char. */
84  Cvar_Set("mn_svmapid", "%s", md->id);
85  if (mapname[0] == '+') {
86  Cvar_Set("mn_svmapname", "%s %s", md->mapTheme, md->params ? (const char*)LIST_GetRandom(md->params) : "");
87  mapname++;
88  } else {
89  Cvar_Set("mn_svmapname", "%s", md->mapTheme);
90  }
91 
92  if (R_ImageExists("pics/maps/shots/%s", mapname))
93  Cvar_Set("mn_mappic", "maps/shots/%s", mapname);
94  else
95  Cvar_Set("mn_mappic", "maps/shots/default");
96 
97  if (R_ImageExists("pics/maps/shots/%s_2", mapname))
98  Cvar_Set("mn_mappic2", "maps/shots/%s_2", mapname);
99  else
100  Cvar_Set("mn_mappic2", "maps/shots/default");
101 
102  if (R_ImageExists("pics/maps/shots/%s_3", mapname))
103  Cvar_Set("mn_mappic3", "maps/shots/%s_3", mapname);
104  else
105  Cvar_Set("mn_mappic3", "maps/shots/default");
106 }
107 
108 static void UI_GetMaps_f (void)
109 {
110  UI_MapInfo(0);
111 }
112 
116 static void UI_NextMap_f (void)
117 {
118  UI_MapInfo(1);
119 }
120 
124 static void UI_PreviousMap_f (void)
125 {
126  UI_MapInfo(-1);
127 }
128 
129 static void UI_SelectMap_f (void)
130 {
131  if (Cmd_Argc() != 2) {
132  Com_Printf("Usage: %s <mapname>\n", Cmd_Argv(0));
133  return;
134  }
135 
136  if (!csi.numMDs)
137  return;
138 
139  const char* mapname = Cmd_Argv(1);
140  int i = 0;
141  const mapDef_t* md;
142 
143  MapDef_Foreach(md) {
144  i++;
145  if (!Q_streq(md->mapTheme, mapname))
146  continue;
147  cls.currentSelectedMap = i - 1;
148  UI_MapInfo(0);
149  return;
150  }
151 
152  i = 0;
153  MapDef_Foreach(md) {
154  i++;
155  if (!Q_streq(md->id, mapname))
156  continue;
157  cls.currentSelectedMap = i - 1;
158  UI_MapInfo(0);
159  return;
160  }
161 
162  Com_Printf("Could not find map %s\n", mapname);
163 }
164 
172 static inline bool UI_MapGameModeCondition (ui_gamemode_t gameMode, const mapDef_t *mapDef)
173 {
174  if (mapDef == nullptr)
175  return false;
176  if (gameMode == GAMEMODE_SKIRMISH)
177  return mapDef->singleplayer;
178  else if (gameMode == GAMEMODE_MULTIPLAYER)
179  return mapDef->multiplayer;
180  else if (gameMode == GAMEMODE_CAMPAIGN)
181  return mapDef->campaign;
182  return false;
183 }
184 
188 static void UI_ListMaps_f (void)
189 {
190  if (!csi.numMDs) {
191  Com_Printf("%s No mapDefinitions found\n", Cmd_Argv(0));
192  return;
193  }
194 
195  if (Cmd_Argc() != 3) {
196  Com_Printf("Usage: %s <gamemode> <callback>\n", Cmd_Argv(0));
197  return;
198  }
199 
200  const char* gameMode = Cmd_Argv(1);
202  if (Q_streq("skirmish", gameMode))
204  else if (Q_streq("multiplayer", gameMode))
206  else if (Q_streq("campaign", gameMode))
208 
209  char callback[MAX_VAR];
210  Q_strncpyz(callback, Cmd_Argv(2), sizeof(callback));
211 
212  const mapDef_t* md;
214  /* special purpose maps are not startable without the specific context */
215  if (md->mapTheme[0] == '.')
216  continue;
217  /* do we have the map file? */
218  if (md->mapTheme[0] != '+' && FS_CheckFile("maps/%s.bsp", md->mapTheme) == -1)
219  continue;
220  if (md->mapTheme[0] == '+' && FS_CheckFile("maps/%s.ump", md->mapTheme + 1) == -1)
221  continue;
222 
223  const char* image = md->mapTheme;
224  if (image[0] == '+')
225  image++;
226  if (!R_ImageExists("pics/maps/shots/%s", image))
227  image = "default";
228 
229  UI_ExecuteConfunc("%s %s %s \"%s\" %s",
230  callback,
231  md->id,
232  md->mapTheme,
233  md->description,
234  image
235  );
236  }
237 }
238 
242 static const cmdList_t mapCallbacks[] = {
243 // {"ui_aircraft_changename", AIR_ChangeAircraftName_f, "Callback to change the name of the aircraft."},
244  {"mn_getmaps", UI_GetMaps_f, "The initial map to show"},
245 
246  {"mn_nextmap", UI_NextMap_f, "Switch to the next valid map for the selected gametype"},
247  {"mn_prevmap", UI_PreviousMap_f, "Switch to the previous valid map for the selected gametype"},
248  {"mn_selectmap", UI_SelectMap_f, "Switch to the map given by the parameter - may be invalid for the current gametype"},
249 
250  {"ui_listmaps", UI_ListMaps_f, "List available maps with some basic information for the UI"},
251 
252  {nullptr, nullptr, nullptr}
253 };
254 
258 void MAP_InitCallbacks (void)
259 {
261 }
262 
267 {
269 }
const char * Cmd_Argv(int arg)
Returns a given argument.
Definition: cmd.cpp:516
static void UI_MapInfo(int step)
Prints the map info for the server creation dialogue.
static void UI_SelectMap_f(void)
int FS_CheckFile(const char *fmt,...)
Just returns the filelength and -1 if the file wasn&#39;t found.
Definition: files.cpp:298
char * mapTheme
Definition: q_shared.h:464
void MAP_InitCallbacks(void)
Registers UI Callbacks for battlescape map selection.
static void UI_NextMap_f(void)
Select the next available map.
static void UI_GetMaps_f(void)
csi_t csi
Definition: common.cpp:39
const struct cgame_export_s * gametype
Definition: client.h:65
Shared game type headers.
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
const mapDef_t *EXPORT * MapInfo(int step)
int numMDs
Definition: q_shared.h:572
ui_gamemode_t
int currentSelectedMap
Definition: client.h:80
void Cmd_TableRemoveList(const cmdList_t *cmdList)
Definition: cmd.cpp:859
client_static_t cls
Definition: cl_main.cpp:83
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition: shared.cpp:457
#define MAX_VAR
Definition: shared.h:36
#define MapDef_ForeachCondition(var, condition)
Definition: q_shared.h:501
mapDef_t * Com_GetMapDefByIDX(int index)
bool multiplayer
Definition: q_shared.h:474
int Cmd_Argc(void)
Return the number of arguments of the current command. "command parameter" will result in a argc of 2...
Definition: cmd.cpp:505
static bool UI_MapGameModeCondition(ui_gamemode_t gameMode, const mapDef_t *mapDef)
Helper function to apply gameMode filter on map definitions.
static const cmdList_t mapCallbacks[]
List of UI Callbacks of battlescape map selection.
bool campaign
Definition: q_shared.h:480
QGL_EXTERN GLint i
Definition: r_gl.h:113
static void UI_ListMaps_f(void)
List available maps.
linkedList_t * params
Definition: q_shared.h:465
const char int mode
Definition: ioapi.h:41
#define MapDef_Foreach(var)
Definition: q_shared.h:505
static void UI_MapInfoGetNext(int step)
char * id
Definition: q_shared.h:463
cvar_t * Cvar_Set(const char *varName, const char *value,...)
Sets a cvar value.
Definition: cvar.cpp:615
#define Q_streq(a, b)
Definition: shared.h:136
void MAP_ShutdownCallbacks(void)
Unregisters UI Callbacks of battlescape map selection.
static void UI_PreviousMap_f(void)
Select the previous available map.
char * description
Definition: q_shared.h:466
void * LIST_GetRandom(linkedList_t *list)
Definition: list.cpp:381
bool singleplayer
Definition: q_shared.h:481
bool R_ImageExists(const char *pname,...)
Definition: r_image.cpp:681
void Cmd_TableAddList(const cmdList_t *cmdList)
Definition: cmd.cpp:853
void UI_ExecuteConfunc(const char *fmt,...)
Executes confunc - just to identify those confuncs in the code - in this frame.
Definition: ui_main.cpp:110
Definition: cmd.h:86