UFO: Alien Invasion
Loading...
Searching...
No Matches
cl_map_callbacks.cpp
Go to the documentation of this file.
1
4
5/*
6Copyright (C) 2002-2025 UFO: Alien Invasion.
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17See the GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, 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
36
37static void UI_MapInfoGetNext (int step)
38{
39 int ref = cls.currentSelectedMap;
40
41 for (;;) {
42 cls.currentSelectedMap += step;
43 if (cls.currentSelectedMap < 0)
44 cls.currentSelectedMap = csi.numMDs - 1;
45 cls.currentSelectedMap %= csi.numMDs;
46
47 const mapDef_t* md = Com_GetMapDefByIDX(cls.currentSelectedMap);
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
66static 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
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
108static void UI_GetMaps_f (void)
109{
110 UI_MapInfo(0);
111}
112
116static void UI_NextMap_f (void)
117{
118 UI_MapInfo(1);
119}
120
124static void UI_PreviousMap_f (void)
125{
126 UI_MapInfo(-1);
127}
128
129static 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
172static 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
188static 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
242static 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
262
Shared game type headers.
mapDef_t * Com_GetMapDefByIDX(int index)
client_static_t cls
Definition cl_main.cpp:83
static void UI_MapInfo(int step)
Prints the map info for the server creation dialogue.
static void UI_SelectMap_f(void)
ui_gamemode_t
@ GAMEMODE_CAMPAIGN
@ MAX_GAMEMODE
@ GAMEMODE_MULTIPLAYER
@ GAMEMODE_NONE
@ GAMEMODE_SKIRMISH
static void UI_MapInfoGetNext(int step)
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.
static void UI_NextMap_f(void)
Select the next available map.
static void UI_PreviousMap_f(void)
Select the previous available map.
static void UI_ListMaps_f(void)
List available maps.
void MAP_ShutdownCallbacks(void)
Unregisters UI Callbacks of battlescape map selection.
static void UI_GetMaps_f(void)
void MAP_InitCallbacks(void)
Registers UI Callbacks for battlescape map selection.
Primary header for client.
const char * Cmd_Argv(int arg)
Returns a given argument.
Definition cmd.cpp:516
void Cmd_TableAddList(const cmdList_t *cmdList)
Definition cmd.cpp:853
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
void Cmd_TableRemoveList(const cmdList_t *cmdList)
Definition cmd.cpp:859
csi_t csi
Definition common.cpp:39
void Com_Printf(const char *const fmt,...)
Definition common.cpp:428
cvar_t * Cvar_Set(const char *varName, const char *value,...)
Sets a cvar value.
Definition cvar.cpp:615
int FS_CheckFile(const char *fmt,...)
Just returns the filelength and -1 if the file wasn't found.
Definition files.cpp:298
const char int mode
Definition ioapi.h:41
void * LIST_GetRandom(linkedList_t *list)
Definition list.cpp:381
#define MapDef_ForeachCondition(var, condition)
Definition q_shared.h:501
#define MapDef_Foreach(var)
Definition q_shared.h:505
QGL_EXTERN GLint i
Definition r_gl.h:113
bool R_ImageExists(const char *pname,...)
Definition r_image.cpp:681
#define Q_streq(a, b)
Definition shared.h:136
#define MAX_VAR
Definition shared.h:36
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition shared.cpp:457
const mapDef_t *EXPORT * MapInfo(int step)
char * mapTheme
Definition q_shared.h:464
char * id
Definition q_shared.h:463
bool campaign
Definition q_shared.h:480
char * description
Definition q_shared.h:466
linkedList_t * params
Definition q_shared.h:465
bool multiplayer
Definition q_shared.h:474
bool singleplayer
Definition q_shared.h:481
void UI_ExecuteConfunc(const char *fmt,...)
Executes confunc - just to identify those confuncs in the code - in this frame.
Definition ui_main.cpp:110