UFO: Alien Invasion
Loading...
Searching...
No Matches
cl_parse.cpp
Go to the documentation of this file.
1
5
6/*
7All original material Copyright (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 "../client.h"
27#include "cl_localentity.h"
28#include "cl_parse.h"
29#include "cl_hud.h"
30#include "../cgame/cl_game.h"
31#include "events/e_parse.h"
32
38static char const* const svc_strings[] =
39{
40 "svc_bad",
41
42 "svc_nop",
43 "svc_ping",
44 "svc_disconnect",
45 "svc_reconnect",
46 "svc_print",
47 "svc_stufftext",
48 "svc_serverdata",
49 "svc_configstring",
50 "svc_event"
51};
52
53/*
54=====================================================================
55SERVER CONNECTING MESSAGES
56=====================================================================
57*/
58
62static void CL_ParseServerData (dbuffer* msg)
63{
64 Com_DPrintf(DEBUG_CLIENT, "Serverdata packet received.\n");
65
67
68 /* parse protocol version number */
69 const int i = NET_ReadLong(msg);
70 /* compare versions */
71 if (i != PROTOCOL_VERSION)
72 Com_Error(ERR_DROP, "Server returned version %i, not %i", i, PROTOCOL_VERSION);
73
74 /* parse player entity number */
75 cl.pnum = NET_ReadShort(msg);
76
77 /* get the full level name */
78 char str[1024];
79 NET_ReadString(msg, str, sizeof(str));
80
81 Com_DPrintf(DEBUG_CLIENT, "serverdata: pnum %d, level %s\n", cl.pnum, str);
82
83 if (cl.pnum >= 0) {
84 /* need to prep refresh at next opportunity */
85 refdef.ready = false;
86 }
87}
88
94static void CL_ParseClientinfo (unsigned int player)
95{
96 clientinfo_t* ci = &cl.clientinfo[player];
97 const char* s = CL_GetConfigString(CS_PLAYERNAMES + player);
98
99 /* isolate the player's name */
100 Q_strncpyz(ci->name, s, sizeof(ci->name));
101}
102
104{
105 return cls.team;
106}
107
113const char* CL_PlayerGetName (unsigned int player)
114{
115 const clientinfo_t* ci = &cl.clientinfo[player];
116 return ci->name;
117}
118
123{
124 const int i = NET_ReadShort(msg);
125 const char* s = CL_SetConfigString(i, msg);
126
127 Com_DPrintf(DEBUG_CLIENT, "configstring %d: %s\n", i, s);
128
129 /* do something appropriate */
130 if (i >= CS_MODELS && i < CS_MODELS + MAX_MODELS) {
131 if (refdef.ready) {
132 const unsigned int index = i - CS_MODELS;
133 assert(index != 0);
134 cl.model_draw[index] = R_FindModel(s);
135 /* inline models are marked with * as first char followed by the number */
136 if (s[0] == '*')
137 cl.model_clip[index] = CM_InlineModel(cl.mapTiles, s);
138 else
139 cl.model_clip[index] = nullptr;
140 }
141 } else if (i >= CS_PLAYERNAMES && i < CS_PLAYERNAMES + MAX_CLIENTS) {
142 const unsigned int index = i - CS_PLAYERNAMES;
144 }
145}
146
147
148/*
149=====================================================================
150ACTION MESSAGES
151=====================================================================
152*/
153
161{
162 static svc_ops_t lastCmd;
163 static event_t eType;
164
165 /* parse the message */
166 if (cmd < svc_bad || cmd >= svc_oob)
167 return;
168
169 Com_DPrintf(DEBUG_CLIENT, "command: %s\n", svc_strings[cmd]);
170
171 /* commands */
172 switch (cmd) {
173 case svc_nop:
174/* Com_Printf("svc_nop\n"); */
175 break;
176
177 case svc_ping: {
178 dbuffer ack;
179 NET_WriteByte(&ack, clc_ack);
180 NET_WriteMsg(cls.netStream, ack);
181 break;
182 }
183
184 case svc_disconnect: {
185 char s[MAX_SVC_DISCONNECT];
186 NET_ReadString(msg, s, sizeof(s));
187 Com_Printf("%s\n", s);
188 CL_Drop(); /* ensure the right menu cvars are set */
189 break;
190 }
191
192 case svc_reconnect: {
193 char s[MAX_SVC_RECONNECT];
194 NET_ReadString(msg, s, sizeof(s));
195 Com_Printf("%s\n", s);
196 cls.reconnectTime = CL_Milliseconds() + 4000;
197 break;
198 }
199
200 case svc_print: {
201 const int i = NET_ReadByte(msg);
202 char s[MAX_SVC_PRINT];
203 NET_ReadString(msg, s, sizeof(s));
204 switch (i) {
205 case PRINT_HUD:
206 /* all game lib messages or server messages should be printed
207 * untranslated with BroadcastPrintf or PlayerPrintf */
208 /* see src/po/OTHER_STRINGS */
210 Com_Printf("%s\n", s);
211 break;
212 case PRINT_CHAT:
214 /* skip format strings */
215 if (s[0] == '^')
216 memmove(s, &s[2], sizeof(s) - 2);
217 /* also print to console */
218 break;
219 default:
220 Com_Printf("%s", s);
221 break;
222 }
223 Com_DPrintf(DEBUG_CLIENT, "svc_print(%d): %s", i, s);
224 break;
225 }
226
227 case svc_stufftext: {
228 char s[MAX_SVC_STUFFTEXT];
229 NET_ReadString(msg, s, sizeof(s));
230 Com_DPrintf(DEBUG_CLIENT, "stufftext: %s\n", s);
231 Cbuf_AddText("%s\n", s);
232 break;
233 }
234
235 case svc_serverdata:
236 Cbuf_Execute(); /* make sure any stuffed commands are done */
238 break;
239
240 case svc_configstring:
242 break;
243
244 case svc_event:
245 eType = CL_ParseEvent(msg);
246 break;
247
248 case svc_bad:
249 Com_Printf("CL_ParseServerMessage: bad server message %d\n", cmd);
250 break;
251
252 default:
253 Com_Error(ERR_DROP, "CL_ParseServerMessage: Illegal server message %d (last cmd was: %d, eType: %i)",
254 cmd, lastCmd, eType);
255 }
256
257 lastCmd = cmd;
258}
char * CL_GetConfigString(int index)
clientBattleScape_t cl
char * CL_SetConfigString(int index, dbuffer *msg)
void GAME_AddChatMessage(const char *format,...)
Definition cl_game.cpp:370
Shared game type headers.
void HUD_DisplayMessage(const char *text)
Displays a message on the hud.
Definition cl_hud.cpp:138
HUD related routines.
void CL_Drop(void)
Ensures the right menu cvars are set after error drop or map change.
Definition cl_main.cpp:167
void CL_SetClientState(connstate_t state)
Sets the client state.
Definition cl_main.cpp:1015
client_static_t cls
Definition cl_main.cpp:83
int CL_Milliseconds(void)
Definition cl_main.cpp:1207
const char * CL_PlayerGetName(unsigned int player)
Get the player name.
Definition cl_parse.cpp:113
void CL_ParseServerMessage(svc_ops_t cmd, dbuffer *msg)
Parses the server sent data from the given buffer.
Definition cl_parse.cpp:160
static char const *const svc_strings[]
see also svc_ops_e in common.h
Definition cl_parse.cpp:38
int CL_GetPlayerNum(void)
Definition cl_parse.cpp:103
static void CL_ParseClientinfo(unsigned int player)
Parses client names that are displayed on the targeting box for multiplayer games.
Definition cl_parse.cpp:94
static void CL_ParseServerData(dbuffer *msg)
Written by SV_New_f in sv_user.c.
Definition cl_parse.cpp:62
static void CL_ParseConfigString(dbuffer *msg)
Definition cl_parse.cpp:122
rendererData_t refdef
Definition r_main.cpp:45
#define _(String)
Definition cl_shared.h:44
@ ca_connected
Definition cl_shared.h:79
Primary header for client.
void Cbuf_Execute(void)
Pulls off terminated lines of text from the command buffer and sends them through Cmd_ExecuteString...
Definition cmd.cpp:214
void Cbuf_AddText(const char *format,...)
Adds command text at the end of the buffer.
Definition cmd.cpp:126
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
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
void Com_Error(int code, const char *fmt,...)
Definition common.cpp:459
void Com_Printf(const char *const fmt,...)
Definition common.cpp:428
#define MAX_SVC_PRINT
Definition common.h:160
@ clc_ack
Definition common.h:175
#define PROTOCOL_VERSION
Definition common.h:134
#define MAX_SVC_RECONNECT
Definition common.h:161
#define MAX_SVC_STUFFTEXT
Definition common.h:159
@ svc_event
Definition common.h:155
@ svc_configstring
Definition common.h:154
@ svc_stufftext
Definition common.h:152
@ svc_reconnect
Definition common.h:150
@ svc_ping
Definition common.h:148
@ svc_bad
Definition common.h:144
@ svc_serverdata
Definition common.h:153
@ svc_oob
Definition common.h:156
@ svc_print
Definition common.h:151
@ svc_disconnect
Definition common.h:149
@ svc_nop
Definition common.h:147
#define ERR_DROP
Definition common.h:211
int32_t svc_ops_t
Definition common.h:165
#define MAX_SVC_DISCONNECT
Definition common.h:162
#define PRINT_CHAT
Definition defines.h:106
#define PRINT_HUD
Definition defines.h:107
#define DEBUG_CLIENT
Definition defines.h:59
#define MAX_MODELS
Definition defines.h:100
event_t CL_ParseEvent(dbuffer *msg)
Called in case a svc_event was send via the network buffer.
Definition e_parse.cpp:261
int NET_ReadString(dbuffer *buf, char *string, size_t length)
Definition netpack.cpp:302
int NET_ReadLong(dbuffer *buf)
Definition netpack.cpp:282
void NET_WriteMsg(struct net_stream *s, dbuffer &buf)
Enqueue the buffer in the net stream for ONE client.
Definition netpack.cpp:569
int NET_ReadByte(dbuffer *buf)
Reads a byte from the netchannel.
Definition netpack.cpp:234
int NET_ReadShort(dbuffer *buf)
Definition netpack.cpp:242
void NET_WriteByte(dbuffer *buf, byte c)
Definition netpack.cpp:39
#define MAX_CLIENTS
Definition q_shared.h:299
event_t
Possible event values.
Definition q_shared.h:79
#define CS_PLAYERNAMES
Definition q_shared.h:328
#define CS_MODELS
Definition q_shared.h:327
QGL_EXTERN GLuint index
Definition r_gl.h:110
QGL_EXTERN GLint i
Definition r_gl.h:113
model_t * R_FindModel(const char *name)
Tries to load a model.
Definition r_model.cpp:203
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition shared.cpp:457
char name[MAX_VAR]