UFO: Alien Invasion
cl_map_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_map_callbacks.h"
26 #include "../client.h"
27 #include "cl_game.h"
28 
29 static void UI_MapInfoGetNext (int step)
30 {
31  int ref = cls.currentSelectedMap;
32 
33  for (;;) {
34  cls.currentSelectedMap += step;
35  if (cls.currentSelectedMap < 0)
38 
40 
41  /* avoid infinite loop */
42  if (ref == cls.currentSelectedMap)
43  break;
44  /* special purpose maps are not startable without the specific context */
45  if (md->mapTheme[0] == '.')
46  continue;
47 
48  if (md->mapTheme[0] != '+' && FS_CheckFile("maps/%s.bsp", md->mapTheme) != -1)
49  break;
50  if (md->mapTheme[0] == '+' && FS_CheckFile("maps/%s.ump", md->mapTheme + 1) != -1)
51  break;
52  }
53 }
54 
58 static void UI_MapInfo (int step)
59 {
60  const cgame_export_t* list = cls.gametype;
61 
62  if (!list)
63  return;
64 
65  if (!csi.numMDs)
66  return;
67 
68  UI_MapInfoGetNext(step);
69 
70  const mapDef_t* md = list->MapInfo(step);
71  if (!md)
72  return;
73 
74  const char* mapname = md->mapTheme;
75  /* skip random map char. */
76  Cvar_Set("mn_svmapid", "%s", md->id);
77  if (mapname[0] == '+') {
78  Cvar_Set("mn_svmapname", "%s %s", md->mapTheme, md->params ? (const char*)LIST_GetRandom(md->params) : "");
79  mapname++;
80  } else {
81  Cvar_Set("mn_svmapname", "%s", md->mapTheme);
82  }
83 
84  if (R_ImageExists("pics/maps/shots/%s", mapname))
85  Cvar_Set("mn_mappic", "maps/shots/%s", mapname);
86  else
87  Cvar_Set("mn_mappic", "maps/shots/default");
88 
89  if (R_ImageExists("pics/maps/shots/%s_2", mapname))
90  Cvar_Set("mn_mappic2", "maps/shots/%s_2", mapname);
91  else
92  Cvar_Set("mn_mappic2", "maps/shots/default");
93 
94  if (R_ImageExists("pics/maps/shots/%s_3", mapname))
95  Cvar_Set("mn_mappic3", "maps/shots/%s_3", mapname);
96  else
97  Cvar_Set("mn_mappic3", "maps/shots/default");
98 }
99 
100 static void UI_GetMaps_f (void)
101 {
102  UI_MapInfo(0);
103 }
104 
108 static void UI_NextMap_f (void)
109 {
110  UI_MapInfo(1);
111 }
112 
116 static void UI_PreviousMap_f (void)
117 {
118  UI_MapInfo(-1);
119 }
120 
121 static void UI_SelectMap_f (void)
122 {
123  if (Cmd_Argc() != 2) {
124  Com_Printf("Usage: %s <mapname>\n", Cmd_Argv(0));
125  return;
126  }
127 
128  if (!csi.numMDs)
129  return;
130 
131  const char* mapname = Cmd_Argv(1);
132  int i = 0;
133  const mapDef_t* md;
134 
135  MapDef_Foreach(md) {
136  i++;
137  if (!Q_streq(md->mapTheme, mapname))
138  continue;
139  cls.currentSelectedMap = i - 1;
140  UI_MapInfo(0);
141  return;
142  }
143 
144  i = 0;
145  MapDef_Foreach(md) {
146  i++;
147  if (!Q_streq(md->id, mapname))
148  continue;
149  cls.currentSelectedMap = i - 1;
150  UI_MapInfo(0);
151  return;
152  }
153 
154  Com_Printf("Could not find map %s\n", mapname);
155 }
156 
157 static void UI_RequestMapList_f (void)
158 {
159  if (Cmd_Argc() != 2) {
160  Com_Printf("Usage: %s <callback>\n", Cmd_Argv(0));
161  return;
162  }
163 
164  if (!csi.numMDs)
165  return;
166 
167  const char* callbackCmd = Cmd_Argv(1);
168 
169  Cbuf_AddText("%s begin\n", callbackCmd);
170 
171  const mapDef_t* md;
172  const bool multiplayer = GAME_IsMultiplayer();
173  MapDef_ForeachCondition(md, multiplayer ? md->multiplayer : md->singleplayer) {
174  const char* preview;
175 
176  /* special purpose maps are not startable without the specific context */
177  if (md->mapTheme[0] == '.')
178  continue;
179 
180  /* do we have the map file? */
181  if (md->mapTheme[0] != '+' && FS_CheckFile("maps/%s.bsp", md->mapTheme) == -1)
182  continue;
183  if (md->mapTheme[0] == '+' && FS_CheckFile("maps/%s.ump", md->mapTheme + 1) == -1)
184  continue;
185 
186  preview = md->mapTheme;
187  if (preview[0] == '+')
188  preview++;
189  if (!R_ImageExists("pics/maps/shots/%s", preview))
190  preview = "default";
191 
192  Cbuf_AddText("%s add \"%s\" \"%s\"\n", callbackCmd, md->id, preview);
193  }
194  Cbuf_AddText("%s end\n", callbackCmd);
195 }
196 
200 static const cmdList_t mapCallbacks[] = {
201 // {"ui_aircraft_changename", AIR_ChangeAircraftName_f, "Callback to change the name of the aircraft."},
202  {"mn_getmaps", UI_GetMaps_f, "The initial map to show"},
203  {"mn_nextmap", UI_NextMap_f, "Switch to the next valid map for the selected gametype"},
204  {"mn_prevmap", UI_PreviousMap_f, "Switch to the previous valid map for the selected gametype"},
205  {"mn_selectmap", UI_SelectMap_f, "Switch to the map given by the parameter - may be invalid for the current gametype"},
206  {"mn_requestmaplist", UI_RequestMapList_f, "Request to send the list of available maps for the current gametype to a command."},
207 
208  {nullptr, nullptr, nullptr}
209 };
210 
214 void MAP_InitCallbacks (void)
215 {
217 }
218 
223 {
225 }
char * mapTheme
Definition: q_shared.h:464
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
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
void Cbuf_AddText(const char *format,...)
Adds command text at the end of the buffer.
Definition: cmd.cpp:126
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
bool singleplayer
Definition: q_shared.h:481
int currentSelectedMap
Definition: client.h:80
void Cmd_TableRemoveList(const cmdList_t *cmdList)
Definition: cmd.cpp:859
linkedList_t * params
Definition: q_shared.h:465
bool multiplayer
Definition: q_shared.h:474
client_static_t cls
Definition: cl_main.cpp:83
#define MapDef_ForeachCondition(var, condition)
Definition: q_shared.h:501
mapDef_t * Com_GetMapDefByIDX(int index)
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
bool GAME_IsMultiplayer(void)
Definition: cl_game.cpp:300
char * id
Definition: q_shared.h:463
static const cmdList_t mapCallbacks[]
List of UI Callbacks of battlescape map selection.
static void UI_RequestMapList_f(void)
QGL_EXTERN GLint i
Definition: r_gl.h:113
Definition: cmd.h:86
#define MapDef_Foreach(var)
Definition: q_shared.h:505
static void UI_MapInfoGetNext(int step)
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.
void * LIST_GetRandom(linkedList_t *list)
Definition: list.cpp:381
bool R_ImageExists(const char *pname,...)
Definition: r_image.cpp:681
void Cmd_TableAddList(const cmdList_t *cmdList)
Definition: cmd.cpp:853