]> git.xonotic.org Git - xonotic/darkplaces.git/blob - host_cmd.c
(Round 6) Break up host_cmd.c
[xonotic/darkplaces.git] / host_cmd.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "quakedef.h"
22 #include "sv_demo.h"
23 #include "image.h"
24
25 #include "prvm_cmds.h"
26 #include "utf8lib.h"
27
28 extern cvar_t sv_adminnick;
29 extern cvar_t sv_status_privacy;
30 extern cvar_t sv_status_show_qcstatus;
31 extern cvar_t sv_namechangetimer;
32 cvar_t cl_team = {CVAR_CLIENT | CVAR_USERINFO | CVAR_SAVE, "team", "none", "QW team (4 character limit, example: blue)"};
33 cvar_t cl_skin = {CVAR_CLIENT | CVAR_USERINFO | CVAR_SAVE, "skin", "", "QW player skin name (example: base)"};
34 cvar_t cl_playermodel = {CVAR_CLIENT | CVAR_SERVER | CVAR_USERINFO | CVAR_SAVE, "playermodel", "", "current player model in Nexuiz/Xonotic"};
35 cvar_t cl_playerskin = {CVAR_CLIENT | CVAR_SERVER | CVAR_USERINFO | CVAR_SAVE, "playerskin", "", "current player skin in Nexuiz/Xonotic"};
36 cvar_t cl_noaim = {CVAR_CLIENT | CVAR_USERINFO | CVAR_SAVE, "noaim", "1", "QW option to disable vertical autoaim"};
37 cvar_t cl_pmodel = {CVAR_CLIENT | CVAR_USERINFO | CVAR_SAVE, "pmodel", "0", "current player model number in nehahra"};
38 cvar_t r_fixtrans_auto = {CVAR_CLIENT, "r_fixtrans_auto", "0", "automatically fixtrans textures (when set to 2, it also saves the fixed versions to a fixtrans directory)"};
39
40 //============================================================================
41
42 /*
43 ======================
44 CL_Playermodel_f
45 ======================
46 */
47 // the old cl_playermodel in cl_main has been renamed to __cl_playermodel
48 static void CL_Playermodel_f(cmd_state_t *cmd)
49 {
50         prvm_prog_t *prog = SVVM_prog;
51         int i, j;
52         char newPath[sizeof(host_client->playermodel)];
53
54         if (Cmd_Argc (cmd) == 1)
55         {
56                 if (cmd->source == src_command)
57                 {
58                         Con_Printf("\"playermodel\" is \"%s\"\n", cl_playermodel.string);
59                 }
60                 return;
61         }
62
63         if (Cmd_Argc (cmd) == 2)
64                 strlcpy (newPath, Cmd_Argv(cmd, 1), sizeof (newPath));
65         else
66                 strlcpy (newPath, Cmd_Args(cmd), sizeof (newPath));
67
68         for (i = 0, j = 0;newPath[i];i++)
69                 if (newPath[i] != '\r' && newPath[i] != '\n')
70                         newPath[j++] = newPath[i];
71         newPath[j] = 0;
72
73         if (cmd->source == src_command)
74         {
75                 Cvar_Set (&cvars_all, "_cl_playermodel", newPath);
76                 return;
77         }
78
79         /*
80         if (host.realtime < host_client->nametime)
81         {
82                 SV_ClientPrintf("You can't change playermodel more than once every 5 seconds!\n");
83                 return;
84         }
85
86         host_client->nametime = host.realtime + 5;
87         */
88
89         // point the string back at updateclient->name to keep it safe
90         strlcpy (host_client->playermodel, newPath, sizeof (host_client->playermodel));
91         PRVM_serveredictstring(host_client->edict, playermodel) = PRVM_SetEngineString(prog, host_client->playermodel);
92         if (strcmp(host_client->old_model, host_client->playermodel))
93         {
94                 strlcpy(host_client->old_model, host_client->playermodel, sizeof(host_client->old_model));
95                 /*// send notification to all clients
96                 MSG_WriteByte (&sv.reliable_datagram, svc_updatepmodel);
97                 MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
98                 MSG_WriteString (&sv.reliable_datagram, host_client->playermodel);*/
99         }
100 }
101
102 /*
103 ======================
104 CL_Playerskin_f
105 ======================
106 */
107 static void CL_Playerskin_f(cmd_state_t *cmd)
108 {
109         prvm_prog_t *prog = SVVM_prog;
110         int i, j;
111         char newPath[sizeof(host_client->playerskin)];
112
113         if (Cmd_Argc (cmd) == 1)
114         {
115                 if (cmd->source == src_command)
116                 {
117                         Con_Printf("\"playerskin\" is \"%s\"\n", cl_playerskin.string);
118                 }
119                 return;
120         }
121
122         if (Cmd_Argc (cmd) == 2)
123                 strlcpy (newPath, Cmd_Argv(cmd, 1), sizeof (newPath));
124         else
125                 strlcpy (newPath, Cmd_Args(cmd), sizeof (newPath));
126
127         for (i = 0, j = 0;newPath[i];i++)
128                 if (newPath[i] != '\r' && newPath[i] != '\n')
129                         newPath[j++] = newPath[i];
130         newPath[j] = 0;
131
132         if (cmd->source == src_command)
133         {
134                 Cvar_Set (&cvars_all, "_cl_playerskin", newPath);
135                 return;
136         }
137
138         /*
139         if (host.realtime < host_client->nametime)
140         {
141                 SV_ClientPrintf("You can't change playermodel more than once every 5 seconds!\n");
142                 return;
143         }
144
145         host_client->nametime = host.realtime + 5;
146         */
147
148         // point the string back at updateclient->name to keep it safe
149         strlcpy (host_client->playerskin, newPath, sizeof (host_client->playerskin));
150         PRVM_serveredictstring(host_client->edict, playerskin) = PRVM_SetEngineString(prog, host_client->playerskin);
151         if (strcmp(host_client->old_skin, host_client->playerskin))
152         {
153                 //if (host_client->begun)
154                 //      SV_BroadcastPrintf("%s changed skin to %s\n", host_client->name, host_client->playerskin);
155                 strlcpy(host_client->old_skin, host_client->playerskin, sizeof(host_client->old_skin));
156                 /*// send notification to all clients
157                 MSG_WriteByte (&sv.reliable_datagram, svc_updatepskin);
158                 MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
159                 MSG_WriteString (&sv.reliable_datagram, host_client->playerskin);*/
160         }
161 }
162
163 cvar_t cl_rate = {CVAR_CLIENT | CVAR_SAVE | CVAR_USERINFO, "rate", "20000", "change your connection speed"};
164 cvar_t cl_rate_burstsize = {CVAR_CLIENT | CVAR_SAVE | CVAR_USERINFO, "rate_burstsize", "1024", "internal storage cvar for current rate control burst size (changed by rate_burstsize command)"};
165
166 /*
167 ======================
168 CL_PModel_f
169 LadyHavoc: only supported for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
170 LadyHavoc: correction, Mindcrime will be removing pmodel in the future, but it's still stuck here for compatibility.
171 ======================
172 */
173 static void CL_PModel_f(cmd_state_t *cmd)
174 {
175         prvm_prog_t *prog = SVVM_prog;
176         int i;
177
178         if (Cmd_Argc (cmd) == 1)
179         {
180                 if (cmd->source == src_command)
181                 {
182                         Con_Printf("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
183                 }
184                 return;
185         }
186         i = atoi(Cmd_Argv(cmd, 1));
187
188         if (cmd->source == src_command)
189         {
190                 if (cl_pmodel.integer == i)
191                         return;
192                 Cvar_SetValue (&cvars_all, "_cl_pmodel", i);
193                 if (cls.state == ca_connected)
194                         CL_ForwardToServer_f(cmd);
195                 return;
196         }
197
198         PRVM_serveredictfloat(host_client->edict, pmodel) = i;
199 }
200
201 //===========================================================================
202
203 //===========================================================================
204
205 static void CL_SendCvar_f(cmd_state_t *cmd)
206 {
207         int             i;
208         cvar_t  *c;
209         const char *cvarname;
210         client_t *old;
211         char vabuf[1024];
212
213         if(Cmd_Argc(cmd) != 2)
214                 return;
215         cvarname = Cmd_Argv(cmd, 1);
216         if (cls.state == ca_connected)
217         {
218                 c = Cvar_FindVar(&cvars_all, cvarname, CVAR_CLIENT | CVAR_SERVER);
219                 // LadyHavoc: if there is no such cvar or if it is private, send a
220                 // reply indicating that it has no value
221                 if(!c || (c->flags & CVAR_PRIVATE))
222                         CL_ForwardToServer(va(vabuf, sizeof(vabuf), "sentcvar %s", cvarname));
223                 else
224                         CL_ForwardToServer(va(vabuf, sizeof(vabuf), "sentcvar %s \"%s\"", c->name, c->string));
225                 return;
226         }
227         if(!sv.active)// || !PRVM_serverfunction(SV_ParseClientCommand))
228                 return;
229
230         old = host_client;
231         if (cls.state != ca_dedicated)
232                 i = 1;
233         else
234                 i = 0;
235         for(;i<svs.maxclients;i++)
236                 if(svs.clients[i].active && svs.clients[i].netconnection)
237                 {
238                         host_client = &svs.clients[i];
239                         SV_ClientCommands("sendcvar %s\n", cvarname);
240                 }
241         host_client = old;
242 }
243
244 //=============================================================================
245
246 // QuakeWorld commands
247
248 /*
249 ==================
250 CL_FullServerinfo_f
251
252 Sent by server when serverinfo changes
253 ==================
254 */
255 // TODO: shouldn't this be a cvar instead?
256 static void CL_FullServerinfo_f(cmd_state_t *cmd) // credit: taken from QuakeWorld
257 {
258         char temp[512];
259         if (Cmd_Argc(cmd) != 2)
260         {
261                 Con_Printf ("usage: fullserverinfo <complete info string>\n");
262                 return;
263         }
264
265         strlcpy (cl.qw_serverinfo, Cmd_Argv(cmd, 1), sizeof(cl.qw_serverinfo));
266         InfoString_GetValue(cl.qw_serverinfo, "teamplay", temp, sizeof(temp));
267         cl.qw_teamplay = atoi(temp);
268 }
269
270 /*
271 ==================
272 CL_FullInfo_f
273
274 Allow clients to change userinfo
275 ==================
276 Casey was here :)
277 */
278 static void CL_FullInfo_f(cmd_state_t *cmd) // credit: taken from QuakeWorld
279 {
280         char key[512];
281         char value[512];
282         const char *s;
283
284         if (Cmd_Argc(cmd) != 2)
285         {
286                 Con_Printf ("fullinfo <complete info string>\n");
287                 return;
288         }
289
290         s = Cmd_Argv(cmd, 1);
291         if (*s == '\\')
292                 s++;
293         while (*s)
294         {
295                 size_t len = strcspn(s, "\\");
296                 if (len >= sizeof(key)) {
297                         len = sizeof(key) - 1;
298                 }
299                 strlcpy(key, s, len + 1);
300                 s += len;
301                 if (!*s)
302                 {
303                         Con_Printf ("MISSING VALUE\n");
304                         return;
305                 }
306                 ++s; // Skip over backslash.
307
308                 len = strcspn(s, "\\");
309                 if (len >= sizeof(value)) {
310                         len = sizeof(value) - 1;
311                 }
312                 strlcpy(value, s, len + 1);
313
314                 CL_SetInfo(key, value, false, false, false, false);
315
316                 s += len;
317                 if (!*s)
318                 {
319                         break;
320                 }
321                 ++s; // Skip over backslash.
322         }
323 }
324
325 /*
326 ==================
327 CL_SetInfo_f
328
329 Allow clients to change userinfo
330 ==================
331 */
332 static void CL_SetInfo_f(cmd_state_t *cmd) // credit: taken from QuakeWorld
333 {
334         if (Cmd_Argc(cmd) == 1)
335         {
336                 InfoString_Print(cls.userinfo);
337                 return;
338         }
339         if (Cmd_Argc(cmd) != 3)
340         {
341                 Con_Printf ("usage: setinfo [ <key> <value> ]\n");
342                 return;
343         }
344         CL_SetInfo(Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), true, false, false, false);
345 }
346
347 static void CL_PingPLReport_f(cmd_state_t *cmd)
348 {
349         char *errbyte;
350         int i;
351         int l = Cmd_Argc(cmd);
352         if (l > cl.maxclients)
353                 l = cl.maxclients;
354         for (i = 0;i < l;i++)
355         {
356                 cl.scores[i].qw_ping = atoi(Cmd_Argv(cmd, 1+i*2));
357                 cl.scores[i].qw_packetloss = strtol(Cmd_Argv(cmd, 1+i*2+1), &errbyte, 0);
358                 if(errbyte && *errbyte == ',')
359                         cl.scores[i].qw_movementloss = atoi(errbyte + 1);
360                 else
361                         cl.scores[i].qw_movementloss = 0;
362         }
363 }
364
365 //=============================================================================
366
367 /*
368 ==================
369 Host_InitCommands
370 ==================
371 */
372 void Host_InitCommands (void)
373 {
374         dpsnprintf(cls.userinfo, sizeof(cls.userinfo), "\\name\\player\\team\\none\\topcolor\\0\\bottomcolor\\0\\rate\\10000\\msg\\1\\noaim\\1\\*ver\\dp");
375
376         Cvar_RegisterVariable(&cl_name);
377         Cvar_RegisterAlias(&cl_name, "_cl_name");
378         Cvar_RegisterVariable(&cl_rate);
379         Cvar_RegisterAlias(&cl_rate, "_cl_rate");
380         Cvar_RegisterVariable(&cl_rate_burstsize);
381         Cvar_RegisterAlias(&cl_rate_burstsize, "_cl_rate_burstsize");
382         Cvar_RegisterVariable(&cl_pmodel);
383         Cvar_RegisterAlias(&cl_pmodel, "_cl_pmodel");
384         Cvar_RegisterVariable(&cl_playermodel);
385         Cvar_RegisterAlias(&cl_playermodel, "_cl_playermodel");
386         Cvar_RegisterVariable(&cl_playerskin);
387         Cvar_RegisterAlias(&cl_playerskin, "_cl_playerskin");
388         Cvar_RegisterVariable(&rcon_password);
389         Cvar_RegisterVariable(&r_fixtrans_auto);
390         Cvar_RegisterVariable(&cl_team);
391         Cvar_RegisterVariable(&cl_skin);
392         Cvar_RegisterVariable(&cl_noaim);
393
394         Cmd_AddCommand(CMD_USERINFO, "pmodel", CL_PModel_f, "(Nehahra-only) change your player model choice");
395         Cmd_AddCommand(CMD_USERINFO, "playermodel", CL_Playermodel_f, "change your player model");
396         Cmd_AddCommand(CMD_USERINFO, "playerskin", CL_Playerskin_f, "change your player skin number");
397
398         Cmd_AddCommand(CMD_CLIENT, "sendcvar", CL_SendCvar_f, "sends the value of a cvar to the server as a sentcvar command, for use by QuakeC");
399         Cmd_AddCommand(CMD_CLIENT, "fullinfo", CL_FullInfo_f, "allows client to modify their userinfo");
400         Cmd_AddCommand(CMD_CLIENT, "setinfo", CL_SetInfo_f, "modifies your userinfo");
401         Cmd_AddCommand(CMD_CLIENT, "fixtrans", Image_FixTransparentPixels_f, "change alpha-zero pixels in an image file to sensible values, and write out a new TGA (warning: SLOW)");
402
403         // commands that are only sent by server to client for execution
404         Cmd_AddCommand(CMD_CLIENT_FROM_SERVER, "pingplreport", CL_PingPLReport_f, "command sent by server containing client ping and packet loss values for scoreboard, triggered by pings command from client (not used by QW servers)");
405         Cmd_AddCommand(CMD_CLIENT_FROM_SERVER, "fullserverinfo", CL_FullServerinfo_f, "internal use only, sent by server to client to update client's local copy of serverinfo string");
406 }
407
408 void Host_NoOperation_f(cmd_state_t *cmd)
409 {
410 }