]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/command/getreplies.qc
81121cfe89f0891c6ea871192da948f8eb50ca73
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / getreplies.qc
1 #include "getreplies.qh"
2
3 #include <common/command/_mod.qh>
4 #include <common/constants.qh>
5 #include <common/gamemodes/_mod.qh>
6 #include <common/monsters/_mod.qh>
7 #include <common/net_linked.qh>
8 #include <common/notifications/all.qh>
9 #include <common/playerstats.qh>
10 #include <common/stats.qh>
11 #include <common/util.qh>
12 #include <common/weapons/_all.qh>
13 #include <common/wepent.qh>
14 #include <server/command/getreplies.qh>
15 #include <server/intermission.qh>
16 #include <server/main.qh>
17 #include <server/mutators/_mod.qh>
18 #include <server/race.qh>
19 #include <server/weapons/selection.qh>
20 #include <server/world.qh>
21
22 // =========================================================
23 //  Reply messages for common commands, re-worked by Samual
24 //  Last updated: December 30th, 2011
25 // =========================================================
26
27 // These strings are set usually during init in world.qc,
28 // or also by some game modes or other functions manually,
29 // and their purpose is to output information to clients
30 // without using any extra processing time.
31
32 // See common.qc for their proper commands
33
34 string getrecords(int page)  // 50 records per page
35 {
36         string s = "";
37
38         MUTATOR_CALLHOOK(GetRecords, page, s);
39         s = M_ARGV(1, string);
40
41         MapInfo_ClearTemps();
42
43         if (s == "" && page == 0)
44                 return "No records are available on this server for the current game mode.\n";
45         return s;
46 }
47
48 string getrankings()
49 {
50         float t, i;
51         string n, s, p, map;
52
53         map = GetMapname();
54
55         s = "";
56         for (i = 1; i <= RANKINGS_CNT; ++i)
57         {
58                 t = race_readTime(map, i);
59
60                 if (t == 0) continue;
61
62                 n = race_readName(map, i);
63                 p = count_ordinal(i);
64                 s = strcat(s, strpad(8, p), " ", strpad(-8, TIME_ENCODED_TOSTRING(t)), " ", n, "\n");
65         }
66
67         MapInfo_ClearTemps();
68
69         if (s == "") return strcat("No records are available for the map: ", map, "\n");
70         else return strcat("Records for ", map, ":\n", s);
71 }
72
73 string getladder()
74 {
75         int i, j, k, uidcnt = 0, thiscnt;
76         string s, temp_s, rr, myuid, thisuid;
77
78         rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
79
80         for (k = 0; k < MapInfo_count; ++k)
81         {
82                 if (MapInfo_Get_ByID(k))
83                 {
84                         for (i = 0; i <= LADDER_CNT; ++i) // i = 0 because it is the speed award
85                         {
86                                 if (i == 0)                   // speed award
87                                 {
88                                         if (stof(db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, rr, "speed/speed"))) == 0) continue;
89
90                                         myuid = db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, rr, "speed/crypto_idfp"));
91                                 }
92                                 else  // normal record, if it exists (else break)
93                                 {
94                                         if (race_readTime(MapInfo_Map_bspname, i) == 0) continue;
95
96                                         myuid = race_readUID(MapInfo_Map_bspname, i);
97                                 }
98
99                                 // string s contains:
100                                 // arg 0 = # of speed recs
101                                 // arg 1 = # of 1st place recs
102                                 // arg 2 = # of 2nd place recs
103                                 // ... etc
104                                 // LADDER_CNT+1 = total points
105
106                                 temp_s = db_get(TemporaryDB, strcat("ladder", myuid));
107
108                                 if (temp_s == "")
109                                 {
110                                         db_put(TemporaryDB, strcat("uid", ftos(uidcnt)), myuid);
111                                         ++uidcnt;
112
113                                         for (j = 0; j <= LADDER_CNT + 1; ++j)
114                                         {
115                                                 if (j != LADDER_CNT + 1) temp_s = strcat(temp_s, "0 ");
116                                                 else temp_s = strcat(temp_s, "0");
117                                         }
118                                 }
119
120                                 tokenize_console(temp_s);
121                                 s = "";
122
123                                 if (i == 0)                                         // speed award
124                                 {
125                                         for (j = 0; j <= LADDER_CNT; ++j)               // loop over each arg in the string
126                                         {
127                                                 if (j == 0)                                 // speed award
128                                                         s = strcat(s, ftos(stof(argv(j)) + 1)); // add 1 to speed rec count and write
129                                                 else s = strcat(s, " ", argv(j));           // just copy over everything else
130                                         }
131                                 }
132                                 else  // record
133                                 {
134                                         for (j = 0; j <= LADDER_CNT; ++j)                    // loop over each arg in the string
135                                         {
136                                                 if (j == 0) s = strcat(s, argv(j));              // speed award, dont prefix with " "
137                                                 else if (j == i)                                 // wanted rec!
138                                                         s = strcat(s, " ", ftos(stof(argv(j)) + 1)); // update argv(j)
139                                                 else s = strcat(s, " ", argv(j));                // just copy over everything else
140                                         }
141                                 }
142
143                                 // total points are (by default) calculated like this:
144                                 // speedrec = floor(100 / 10) = 10 points
145                                 // 1st place = floor(100 / 1) = 100 points
146                                 // 2nd place = floor(100 / 2) = 50 points
147                                 // 3rd place = floor(100 / 3) = 33 points
148                                 // 4th place = floor(100 / 4) = 25 points
149                                 // 5th place = floor(100 / 5) = 20 points
150                                 // ... etc
151
152                                 if (i == 0) s = strcat(s, " ", ftos(stof(argv(LADDER_CNT + 1)) + LADDER_FIRSTPOINT / 10)); // speed award, add LADDER_FIRSTPOINT / 10 points
153                                 else s = strcat(s, " ", ftos(stof(argv(LADDER_CNT + 1)) + floor(LADDER_FIRSTPOINT / i)));  // record, add LADDER_FIRSTPOINT / i points
154
155                                 db_put(TemporaryDB, strcat("ladder", myuid), s);
156                         }
157                 }
158         }
159
160         for (i = 0; i <= uidcnt; ++i)  // for each known uid
161         {
162                 thisuid = db_get(TemporaryDB, strcat("uid", ftos(i)));
163                 temp_s = db_get(TemporaryDB, strcat("ladder", thisuid));
164                 tokenize_console(temp_s);
165                 thiscnt = stof(argv(LADDER_CNT + 1));
166
167                 if (thiscnt > top_scores[LADDER_SIZE - 1])
168                 {
169                         for (j = 0; j < LADDER_SIZE; ++j)  // for each place in ladder
170                         {
171                                 if (thiscnt > top_scores[j])
172                                 {
173                                         for (k = LADDER_SIZE - 1; k >= j; --k)
174                                         {
175                                                 top_uids[k] = top_uids[k - 1];
176                                                 top_scores[k] = top_scores[k - 1];
177                                         }
178
179                                         top_uids[j] = thisuid;
180                                         top_scores[j] = thiscnt;
181                                         break;
182                                 }
183                         }
184                 }
185         }
186
187         s = "^3-----------------------\n\n";
188
189         s = strcat(s, "Pos ^3|");
190         s = strcat(s, " ^7Total  ^3|");
191
192         for (i = 1; i <= LADDER_CNT; ++i)
193                 s = strcat(s, " ^7", count_ordinal(i), " ^3|");
194         s = strcat(s, " ^7Speed awards ^3| ^7Name");
195         s = strcat(s, "\n^3----+--------");
196
197         for (i = 1; i <= min(9, LADDER_CNT); ++i)
198                 s = strcat(s, "+-----");
199 #if LADDER_CNT > 9
200                 for (i = 1; i <= LADDER_CNT - 9; ++i)
201                         s = strcat(s, "+------");
202 #endif
203
204         s = strcat(s, "+--------------+--------------------\n");
205
206         for (i = 0; i < LADDER_SIZE; ++i)
207         {
208                 temp_s = db_get(TemporaryDB, strcat("ladder", top_uids[i]));
209                 tokenize_console(temp_s);
210
211                 if (argv(LADDER_CNT + 1) == "")                           // total is 0, skip
212                         continue;
213
214                 s = strcat(s, strpad(4, count_ordinal(i + 1)), "^3| ^7"); // pos
215                 s = strcat(s, strpad(7, argv(LADDER_CNT + 1)), "^3| ^7"); // total
216
217                 for (j = 1; j <= min(9, LADDER_CNT); ++j)
218                         s = strcat(s, strpad(4, argv(j)), "^3| ^7");          // 1st, 2nd, 3rd etc cnt
219
220 #if LADDER_CNT > 9
221                         for (j = 10; j <= LADDER_CNT; ++j)
222                                 s = strcat(s, strpad(4, argv(j)), " ^3| ^7"); // 1st, 2nd, 3rd etc cnt
223 #endif
224
225                 s = strcat(s, strpad(13, argv(0)), "^3| ^7");         // speed award cnt
226                 s = strcat(s, uid2name(top_uids[i]), "\n");           // name
227         }
228
229         MapInfo_ClearTemps();
230
231         if (s == "") return "No ladder on this server!\n";
232         else return strcat("Top ", ftos(LADDER_SIZE), " ladder rankings:\n", s);
233 }
234
235 string getmaplist()
236 {
237         string maplist = "", col;
238         int i, argc;
239
240         argc = tokenize_console(autocvar_g_maplist);
241         for (i = 0; i < argc; ++i)
242         {
243                 if (MapInfo_CheckMap(argv(i)))
244                 {
245                         if (i % 2) col = "^2"; else col = "^3";
246                         maplist = sprintf("%s%s%s ", maplist, col, argv(i));
247                 }
248         }
249
250         MapInfo_ClearTemps();
251         return sprintf("^7Maps in list: %s\n", maplist);
252 }
253
254 const int LSMAPS_MAX = 250;
255 string getlsmaps()
256 {
257         string lsmaps = "", col;
258         bool newmaps = false;
259         int added = 0;
260
261         for (int i = 0; i < MapInfo_count; ++i)
262         {
263                 if ((MapInfo_Get_ByID(i)) && !(MapInfo_Map_flags & MapInfo_ForbiddenFlags()))
264                 {
265                         ++added;
266
267                         if(added > LSMAPS_MAX)
268                                 continue; // we still get the added count, but skip the actual processing
269
270                         // todo: Check by play count of maps for other game types?
271                         if((g_race || g_cts) && !race_readTime(MapInfo_Map_bspname, 1))
272                         {
273                                 newmaps = true;
274                                 if (i % 2) col = "^4*"; else col = "^5*";
275                         }
276                         else
277                         {
278                                 if (i % 2) col = "^2"; else col = "^3";
279                         }
280
281                         lsmaps = sprintf("%s%s%s ", lsmaps, col, MapInfo_Map_bspname);
282                 }
283         }
284
285         if(added > LSMAPS_MAX)
286                 lsmaps = sprintf("%s^7(%d not listed)", lsmaps, added - LSMAPS_MAX);
287
288         MapInfo_ClearTemps();
289         return sprintf("^7Maps available (%d)%s: %s\n", added, (newmaps ? " (New maps have asterisks marked in blue)" : ""), lsmaps);
290 }
291
292 string getmonsterlist()
293 {
294         string monsterlist = "";
295
296         FOREACH(Monsters, it != MON_Null && !(it.spawnflags & MON_FLAG_HIDDEN),
297         {
298                 string col = ((i % 2) ? "^2" : "^3");
299                 monsterlist = sprintf("%s%s%s ", monsterlist, col, it.netname);
300         });
301
302         return sprintf("^7Monsters available: %s\n", monsterlist);
303 }
304
305 /*
306 =============
307 GetCvars
308 =============
309 Called with:
310   0:  sends the request
311   >0: receives a cvar from name=argv(f) value=argv(f+1)
312 */
313 void GetCvars_handleString(entity this, entity store, string thisname, float f, .string field, string name)
314 {
315         if (f < 0)
316         {
317                 strfree(store.(field));
318         }
319         else if (f > 0)
320         {
321                 if (thisname == name)
322                 {
323                         strcpy(store.(field), argv(f + 1));
324                 }
325         }
326         else
327                 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
328 }
329 void GetCvars_handleString_Fixup(entity this, entity store, string thisname, float f, .string field, string name, string(entity, string) func)
330 {
331         GetCvars_handleString(this, store, thisname, f, field, name);
332         if (f >= 0) // also initialize to the fitting value for "" when sending cvars out
333                 if (thisname == name)
334                 {
335                         string s = func(this, strcat1(store.(field)));
336                         if (s != store.(field))
337                         {
338                                 strcpy(store.(field), s);
339                         }
340                 }
341 }
342 void GetCvars_handleFloat(entity this, entity store, string thisname, float f, .float field, string name)
343 {
344         if (f < 0)
345         {
346         }
347         else if (f > 0)
348         {
349                 if (thisname == name)
350                         store.(field) = stof(argv(f + 1));
351         }
352         else
353                 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
354 }
355 void GetCvars_handleFloatOnce(entity this, entity store, string thisname, float f, .float field, string name)
356 {
357         if (f < 0)
358         {
359         }
360         else if (f > 0)
361         {
362                 if (thisname == name)
363                 {
364                         if (!store.(field))
365                         {
366                                 store.(field) = stof(argv(f + 1));
367                                 if (!store.(field))
368                                         store.(field) = -1;
369                         }
370                 }
371         }
372         else
373         {
374                 if (!store.(field))
375                         stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
376         }
377 }
378 string W_FixWeaponOrder_ForceComplete_AndBuildImpulseList(entity this, string wo)
379 {
380         string o = W_FixWeaponOrder_ForceComplete(wo);
381         strcpy(CS(this).weaponorder_byimpulse, W_FixWeaponOrder_BuildImpulseList(o));
382         return o;
383 }
384
385 /**
386  * @param f -1: cleanup, 0: request, 1: receive
387  */
388 void GetCvars(entity this, entity store, int f)
389 {
390         string s = string_null;
391
392         if (f == 0)
393                 LOG_INFO("Warning: requesting cvar values is deprecated. Client should send them automatically using REPLICATE.\n");
394
395         if (f > 0)
396                 s = strcat1(argv(f));
397
398         get_cvars_f = f;
399         get_cvars_s = s;
400         MUTATOR_CALLHOOK(GetCvars);
401
402         Notification_GetCvars(this);
403
404         ReplicateVars(this, store, s, f);
405
406         GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriority, "cl_weaponpriority", W_FixWeaponOrder_ForceComplete_AndBuildImpulseList);
407         GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[0], "cl_weaponpriority0", W_FixWeaponOrder_AllowIncomplete);
408         GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[1], "cl_weaponpriority1", W_FixWeaponOrder_AllowIncomplete);
409         GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[2], "cl_weaponpriority2", W_FixWeaponOrder_AllowIncomplete);
410         GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[3], "cl_weaponpriority3", W_FixWeaponOrder_AllowIncomplete);
411         GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[4], "cl_weaponpriority4", W_FixWeaponOrder_AllowIncomplete);
412         GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[5], "cl_weaponpriority5", W_FixWeaponOrder_AllowIncomplete);
413         GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[6], "cl_weaponpriority6", W_FixWeaponOrder_AllowIncomplete);
414         GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[7], "cl_weaponpriority7", W_FixWeaponOrder_AllowIncomplete);
415         GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[8], "cl_weaponpriority8", W_FixWeaponOrder_AllowIncomplete);
416         GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[9], "cl_weaponpriority9", W_FixWeaponOrder_AllowIncomplete);
417
418         GetCvars_handleFloat(this, store, s, f, cvar_cl_allow_uidtracking, "cl_allow_uidtracking");
419
420         // fixup of switchweapon (needed for LMS or when spectating is disabled, as PutClientInServer comes too early)
421         if (f > 0)
422         {
423                 if (s == "cl_weaponpriority")
424                 {
425                         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
426                         {
427                                 .entity weaponentity = weaponentities[slot];
428                                 if (this.(weaponentity) && (this.(weaponentity).m_weapon != WEP_Null || slot == 0))
429                                         this.(weaponentity).m_switchweapon = w_getbestweapon(this, weaponentity);
430                         }
431                 }
432                 if (s == "cl_allow_uidtracking")
433                         PlayerStats_GameReport_AddPlayer(this);
434                 //if (s == "cl_gunalign")
435                         //W_ResetGunAlign(this, store.cvar_cl_gunalign);
436         }
437 }