UFO: Alien Invasion
Loading...
Searching...
No Matches
sv_init.cpp
Go to the documentation of this file.
1
5
6/*
7All original material Copyright (C) 2002-2025 UFO: Alien Invasion.
8
9Original file from Quake 2 v3.21: quake2-2.31/server/sv_init.c
10
11Copyright (C) 1997-2001 Id Software, Inc.
12
13This program is free software; you can redistribute it and/or
14modify it under the terms of the GNU General Public License
15as published by the Free Software Foundation; either version 2
16of the License, or (at your option) any later version.
17
18This program is distributed in the hope that it will be useful,
19but WITHOUT ANY WARRANTY; without even the implied warranty of
20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21
22See the GNU General Public License for more details.
23
24You should have received a copy of the GNU General Public License
25along with this program; if not, write to the Free Software
26Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
28*/
29
30#include "server.h"
31#include "sv_rma.h"
32#include "../shared/parse.h"
34
35serverInstanceStatic_t svs; /* persistant server info */
36serverInstanceGame_t* sv; /* local server */
37
43static const char* SV_GetMapTitle (const char* asmTitle, const char* const mapname)
44{
45 assert(mapname);
46
47 if (mapname[0] == '+') {
48 if (asmTitle && asmTitle[0]) {
49 /* return the assembly title itself - must be translated client side */
50 if (asmTitle[0] == '_')
51 return asmTitle + 1;
52 else {
53 Com_Printf("The assembly title '%s' is not marked as translatable\n", asmTitle);
54 return asmTitle;
55 }
56 }
57 }
58 return mapname;
59}
60
64static void SV_DiscoveryCallback (struct datagram_socket* s, const char* buf, int len, struct sockaddr* from)
65{
66 const char match[] = "discover";
67 if (len == sizeof(match) && memcmp(buf, match, len) == 0) {
68 const char msg[] = "discovered";
69 NET_DatagramSend(s, msg, sizeof(msg), from);
70 }
71}
72
76static void SV_InitGame (void)
77{
78 /* allow next change after map change or restart */
79 sv_maxclients->flags |= CVAR_LATCH;
80
81 /* get any latched variable changes (sv_maxclients, etc) */
83
84 if (svs.serverMutex)
85 Sys_Error("There is still a server running");
86
88 svs.serverMutex = SDL_CreateMutex();
89
90 /* init network stuff */
91 if (sv_maxclients->integer > 1) {
92 svs.initialized = SV_Start(nullptr, port->string, &SV_ReadPacket);
93 svs.netDatagramSocket = NET_DatagramSocketNew(nullptr, port->string, &SV_DiscoveryCallback);
94 } else {
95 svs.initialized = SV_Start(nullptr, nullptr, &SV_ReadPacket);
96 }
97
99
100 /* init game */
102
103 if (sv_maxclients->integer > 1 && (sv_dedicated->integer || sv_public->integer))
105}
106
113void SV_Map (bool day, const char* levelstring, const char* assembly, bool verbose)
114{
115 int i;
116 unsigned checksum = 0;
117 char* tileString = SV_GetConfigString(CS_TILES);
118 char* posString = SV_GetConfigString(CS_POSITIONS);
119 char* entityString = SV_GetConfigString(CS_ENTITYSTRING);
120 client_t* cl;
121
122 /* any partially connected client will be restarted */
124
125 /* the game is just starting */
126 SV_InitGame();
127
128 if (!svs.initialized) {
129 Com_Printf("Could not spawn the server\n");
130 return;
131 }
132
133 assert(Q_strvalid(levelstring));
134
135 Com_DPrintf(DEBUG_SERVER, "SpawnServer: %s\n", levelstring);
136
137 /* save name for levels that don't set message */
138 SV_SetConfigString(CS_NAME, levelstring);
141
142 Q_strncpyz(sv->name, levelstring, sizeof(sv->name));
143
144 /* set serverinfo variable */
145 sv_mapname = Cvar_FullSet("sv_mapname", sv->name, CVAR_SERVERINFO | CVAR_NOSET);
146
147 /* notify the client in case of a listening server */
149
150 if (assembly)
151 Q_strncpyz(sv->assembly, assembly, sizeof(sv->assembly));
152 else
153 sv->assembly[0] = '\0';
154
155 /* leave slots at start for clients only */
156 cl = nullptr;
157 while ((cl = SV_GetNextClient(cl)) != nullptr) {
158 /* needs to reconnect */
159 if (cl->state >= cs_spawning)
161 }
162
163 /* assemble and load the map */
164 char asmTitle[MAX_VAR];
165 int numPlaced = 0;
166 if (levelstring[0] == '+') {
167 if (verbose) {
168 Com_Printf("Map info - map type: RMA\n");
169 Com_Printf("Map info - map name: %s\n", levelstring);
170 Com_Printf("Map info - assembly name: %s\n", assembly);
171 }
172 numPlaced = SV_AssembleMapAndTitle(levelstring + 1, assembly, tileString, posString, entityString, 0, verbose, asmTitle);
173 if (!numPlaced) {
174 Com_Printf("Could not load assembly for map '%s'\n", levelstring);
175 return;
176 }
177 } else {
178 SV_SetConfigString(CS_TILES, levelstring);
179 SV_SetConfigString(CS_POSITIONS, assembly ? assembly : "");
181 if (verbose) {
182 Com_Printf("Map info - map type: Static\n");
183 Com_Printf("Map info - map name: %s\n", levelstring);
184 }
185 }
186
187 CM_LoadMap(tileString, day, posString, entityString, &sv->mapData, &sv->mapTiles);
188
189 Com_Printf("checksum for the map '%s': %u\n", levelstring, sv->mapData.mapChecksum);
190 SV_SetConfigString(CS_MAPCHECKSUM, sv->mapData.mapChecksum);
191
192 checksum = Com_GetScriptChecksum();
193
194 Com_Printf("ufo script checksum %u\n", checksum);
198 SV_SetConfigString(CS_MAPTITLE, SV_GetMapTitle(asmTitle, levelstring));
200 /* For base attack, CS_MAPTITLE contains too many chars */
201 SV_SetConfigString(CS_MAPTITLE, "Base attack");
202 SV_SetConfigString(CS_NAME, ".baseattack");
203 }
204
205 /* clear physics interaction links */
207
208 /* fix this! */
209 for (i = 1; i <= sv->mapData.numInline; i++)
210 sv->models[i] = CM_InlineModel(&sv->mapTiles, va("*%i", i));
211
212 /* precache and static commands can be issued during map initialization */
214
215 /* load and spawn all other entities */
216 {
217 const ScopedMutex scopedMutex(svs.serverMutex);
218 svs.ge->SpawnEntities(sv->name, SV_GetConfigStringInteger(CS_LIGHTMAP), sv->mapData.mapEntityString);
219 }
220
221 /* all precaches are complete */
223
224 Com_Printf("-------------------------------------\n");
225
227}
clientBattleScape_t cl
void SCR_BeginLoadingPlaque(void)
void Cbuf_CopyToDefer(void)
Defers any outstanding commands.
Definition cmd.cpp:191
void CM_LoadMap(const char *tiles, bool day, const char *pos, const char *entityString, mapData_t *mapData, mapTiles_t *mapTiles)
Loads in the map and all submodels.
Definition bsp.cpp:860
cBspModel_t * CM_InlineModel(const mapTiles_t *mapTiles, const char *name)
Searches all inline models and return the cBspModel_t pointer for the given modelnumber or -name.
Definition bsp.cpp:929
csi_t csi
Definition common.cpp:39
void Com_SetServerState(int state)
Definition common.cpp:587
cvar_t * port
Definition common.cpp:58
void Com_DPrintf(int level, const char *fmt,...)
A Com_Printf that only shows up if the "developer" cvar is set.
Definition common.cpp:440
cvar_t * sv_dedicated
Definition common.cpp:51
void Com_Printf(const char *const fmt,...)
Definition common.cpp:428
cvar_t * sv_maxclients
Definition g_main.cpp:43
#define UFO_VERSION
Definition common.h:36
cvar_t * Cvar_FullSet(const char *varName, const char *value, int flags)
Sets a cvar from console with the given flags.
Definition cvar.cpp:640
const char * Cvar_GetString(const char *varName)
Returns the value of cvar as string.
Definition cvar.cpp:210
void Cvar_UpdateLatchedVars(void)
Any variables with latched values will now be updated.
Definition cvar.cpp:683
#define CVAR_SERVERINFO
Definition cvar.h:42
#define CVAR_NOSET
Definition cvar.h:43
#define CVAR_LATCH
Definition cvar.h:44
#define DEBUG_SERVER
Definition defines.h:60
void Sys_Error(const char *error,...)
Definition g_main.cpp:421
voidpf void * buf
Definition ioapi.h:42
#define Mem_PoolAllocTypeN(type, n, pool)
Definition mem.h:42
struct datagram_socket * NET_DatagramSocketNew(const char *node, const char *service, datagram_callback_func *func)
Opens a datagram socket (UDP).
Definition net.cpp:1102
bool SV_Start(const char *node, const char *service, stream_callback_func *func)
Definition net.cpp:990
void NET_DatagramSend(struct datagram_socket *s, const char *buf, int len, struct sockaddr *to)
Definition net.cpp:1135
Shared parsing functions.
#define CS_ENTITYSTRING
Definition q_shared.h:324
#define CS_VERSION
Definition q_shared.h:318
#define CS_TILES
Definition q_shared.h:325
#define CS_LIGHTMAP
Definition q_shared.h:321
#define CS_POSITIONS
Definition q_shared.h:326
#define CS_NAME
Definition q_shared.h:309
#define CS_OBJECTAMOUNT
Definition q_shared.h:320
#define CS_MAPZONE
Definition q_shared.h:322
#define CS_MAPCHECKSUM
Definition q_shared.h:312
#define CS_UFOCHECKSUM
Definition q_shared.h:319
#define CS_MAPTITLE
Definition q_shared.h:310
QGL_EXTERN GLuint GLchar GLuint * len
Definition r_gl.h:99
QGL_EXTERN GLint i
Definition r_gl.h:113
int Com_GetScriptChecksum(void)
Definition scripts.cpp:3717
Main server include file.
cvar_t * sv_mapname
Definition sv_main.cpp:53
memPool_t * sv_genericPool
Definition sv_main.cpp:55
char * SV_GetConfigString(int index)
Definition sv_main.cpp:77
void SV_ClearWorld(void)
Clear physics interaction links.
Definition sv_world.cpp:81
void SV_Heartbeat_f(void)
Definition sv_ccmds.cpp:35
@ cs_connected
Definition server.h:141
@ cs_spawning
Definition server.h:142
void SV_ReadPacket(struct net_stream *s)
Definition sv_main.cpp:669
void SV_InitGameProgs(void)
Init the game subsystem for a new map.
Definition sv_game.cpp:747
int SV_GetConfigStringInteger(int index)
Definition sv_main.cpp:108
@ ss_loading
Definition server.h:98
@ ss_game
Definition server.h:99
@ ss_restart
Definition server.h:97
void SV_SetMaster_f(void)
Add the server to the master server list so that others can see the server in the server list.
Definition sv_ccmds.cpp:45
serverInstanceGame_t * sv
Definition sv_init.cpp:36
void SV_SetClientState(client_t *client, client_state_t state)
Set the client state.
Definition sv_user.cpp:36
#define SV_SetConfigString(index, value)
Definition server.h:188
client_t * SV_GetNextClient(client_t *lastClient)
Iterates through clients.
Definition sv_main.cpp:152
serverInstanceStatic_t svs
Definition sv_init.cpp:35
cvar_t * sv_public
Definition sv_main.cpp:52
#define Q_strvalid(string)
Definition shared.h:141
#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
char const * Q_strstart(char const *str, char const *start)
Matches the start of a string.
Definition shared.cpp:587
const char * va(const char *format,...)
does a varargs printf into a temp buffer, so I don't need to have varargs versions of all text functi...
Definition shared.cpp:410
Struct that is only valid for one map. It's deleted on every map load.
Definition server.h:106
static void SV_InitGame(void)
A brand new game has been started.
Definition sv_init.cpp:76
static void SV_DiscoveryCallback(struct datagram_socket *s, const char *buf, int len, struct sockaddr *from)
Definition sv_init.cpp:64
static const char * SV_GetMapTitle(const char *asmTitle, const char *const mapname)
Get the map title for a given map.
Definition sv_init.cpp:43
void SV_Map(bool day, const char *levelstring, const char *assembly, bool verbose)
Change the server to a new map, taking all connected clients along with it.
Definition sv_init.cpp:113
int SV_AssembleMapAndTitle(const char *mapTheme, const char *assembly, char *asmTiles, char *asmPos, char *entityString, const unsigned int seed, bool print, char *asmTitle)
Definition sv_rma.cpp:2220