Debian Codegraph @

netgames/dmfc/dmfcpinfo.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * 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, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/Dmfc/dmfcpinfo.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003/08/26 03:57:21 $
23 * $Author: kevinb $
24 *
25 * DMFC Playerinfo class
26 *
27 * $Log: dmfcpinfo.cpp,v $
28 * Revision 1.1.1.1 2003/08/26 03:57:21 kevinb
29 * initial 1.5 import
30 *
31 *
32 * 21 7/09/99 2:54p Jeff
33 * handle gametime better (pause it when needed) if the server is 'waiting
34 * for players'
35 *
36 * 20 7/07/99 6:10p Jeff
37 * don't count the time that a player is in observer mode for death/kill
38 * messages
39 *
40 * 19 5/13/99 4:55p Ardussi
41 * changes for compiling on the Mac
42 *
43 * 18 5/12/99 11:05p Jeff
44 * dmfc and multiplayer games now have endian friendly packets (*whew*)
45 *
46 * 17 4/25/99 7:19p Jeff
47 * added code to handle suicides in pinfo
48 *
49 * 16 3/17/99 12:25p Jeff
50 * converted DMFC to be COM interface
51 *
52 * 15 2/11/99 12:51a Jeff
53 * changed names of exported variables
54 *
55 * 13 9/30/98 3:50p Jeff
56 * general improvements (many)
57 *
58 * 12 9/29/98 3:04p Jeff
59 * added time in game and start_time support
60 *
61 * 11 9/25/98 8:02p Jason
62 * added checks for jeff
63 *
64 * 10 9/25/98 7:56p Jeff
65 * fixed the hosing of the data in UnpackData
66 *
67 * 9 9/25/98 7:25p Jeff
68 *
69 * 8 9/25/98 4:50p Jeff
70 *
71 * 7 9/24/98 5:52p Jeff
72 * starting adding statistical death messages, checked in for testing
73 *
74 * 6 9/21/98 7:11p Jeff
75 * made InputCommand interface API and moved existing input commands to
76 * the interface. Changed mprintf/ASSERT so they are valid in DMFC
77 *
78 * 5 8/04/98 3:29p Jeff
79 * completion of player records. Moved Pinfo to player records...anyone
80 * can save detailed stats of the game now
81 *
82 * 4 7/28/98 2:04p Jeff
83 *
84 * 3 7/22/98 12:13p Jeff
85 * keep track of who you kill now...plus save memory with a name table
86 *
87 * 2 7/19/98 7:23p Jeff
88 * added support to keep track of who kills who
89 *
90 * $NoKeywords: $
91 */
92
93 #include "gamedll_header.h"
94 #include "DMFC.h"
95 #include "dmfcinternal.h"
96 #include <string.h>
97
98 extern DMFCBase *basethis;
99
100 PInfo::PInfo(int slot) {
101 m_slot = slot;
102
103 extra_info.last_kill_time = 0;
104 extra_info.last_death_time = 0;
105 extra_info.kill_time = 0;
106 extra_info.death_time = 0;
107 extra_info.observer_time = 0;
108 extra_info.kills_in_a_row = 0;
109 extra_info.deaths_in_a_row = 0;
110 extra_info.last_kill_num = -1;
111 extra_info.last_death_num = -1;
112 extra_info.got_revenge = 0;
113
114 killer_list = NULL;
115 curr = NULL;
116
117 curr = CreateKiller(slot); // Setup Suicide
118 if (curr)
119 curr->kills = 0;
120 }
121
122 PInfo::~PInfo(void) {
123 tPKillerInfo *curr, *next;
124 curr = next = killer_list;
125
126 while (curr) {
127 next = curr->next;
128 free(curr);
129 curr = next;
130 }
131 }
132
133 tPKillerInfo *PInfo::GetKillerInfo(int slot) {
134 tPKillerInfo *info;
135 info = FindKiller(slot);
136 return info;
137 }
138
139 void PInfo::Update(int slot, tPKillerInfo *amount) {
140 tPKillerInfo *info;
141
142 info = FindKiller(slot);
143 if (!info) {
144 // new one
145 info = CreateKiller(slot);
146 if (!info)
147 return;
148 }
149 info->kills += amount->kills;
150 }
151
152 void PInfo::HandleKill(int victim_num) {
153 extra_info.last_kill_time = extra_info.kill_time;
154 extra_info.kill_time = basethis->GetRealGametime();
155 extra_info.kills_in_a_row++;
156 extra_info.deaths_in_a_row = 0;
157 extra_info.last_kill_num = victim_num;
158 }
159
160 void PInfo::HandleDeath(int killer_num) {
161 extra_info.got_revenge = 0;
162 extra_info.last_death_time = extra_info.death_time;
163 extra_info.death_time = basethis->GetRealGametime();
164 extra_info.deaths_in_a_row++;
165 extra_info.kills_in_a_row = 0;
166 extra_info.last_death_num = killer_num;
167 }
168
169 void PInfo::HandleSuicide(void) {
170 extra_info.got_revenge = 1; // mark it so they don't get a revenge message
171 extra_info.last_death_time = extra_info.death_time;
172 extra_info.death_time = basethis->GetRealGametime();
173 extra_info.deaths_in_a_row++;
174 extra_info.kills_in_a_row = 0;
175 }
176
177 tPExtraInfo *PInfo::GetExtraInfo(void) { return &extra_info; }
178
179 tPKillerInfo *PInfo::GetFirstKiller(void) {
180 curr = killer_list;
181 return killer_list;
182 }
183
184 tPKillerInfo *PInfo::GetNextKiller(void) {
185 if (!curr)
186 return NULL;
187 if (!curr->next)
188 return NULL;
189 curr = curr->next;
190 return curr;
191 }
192
193 tPKillerInfo *PInfo::FindKiller(int slot, tPKillerInfo **wprev) {
194 tPKillerInfo *curr;
195 tPKillerInfo *prev;
196 curr = killer_list;
197 prev = NULL;
198
199 while (curr) {
200
201 if (slot == curr->slot) {
202 // we found a match
203 break;
204 }
205
206 prev = curr;
207 curr = curr->next;
208 }
209
210 if (wprev)
211 *wprev = prev;
212 return curr;
213 }
214
215 tPKillerInfo *PInfo::CreateKiller(int slot) {
216 tPKillerInfo *curr, *prev;
217 curr = FindKiller(slot, &prev);
218
219 if (!curr) {
220 if (prev) {
221 // the name wasn't in the list so we can tack it on here at the end
222 curr = (tPKillerInfo *)malloc(sizeof(tPKillerInfo));
223 if (!curr)
224 return NULL;
225 prev->next = curr;
226 } else {
227 // no names in the list
228 curr = killer_list = (tPKillerInfo *)malloc(sizeof(tPKillerInfo));
229 if (!killer_list)
230 return NULL;
231 }
232
233 curr->next = NULL;
234 curr->kills = 0;
235 curr->slot = slot;
236 }
237
238 return curr;
239 }
240
241 void PInfo::RemoveKiller(int slot) {
242 tPKillerInfo *curr, *prev;
243 curr = FindKiller(slot, &prev);
244
245 if (curr) {
246 if (prev) {
247 prev->next = curr->next;
248 free(curr);
249 } else {
250 // we're removing root (odd...this is yourself)
251 ASSERT(!prev); // Get Jeff
252 killer_list = curr->next;
253 free(curr);
254 }
255 }
256 }
257
258 void PInfo::ResetAll(void) {
259 tPKillerInfo *c, *next;
260 c = next = killer_list;
261
262 while (c) {
263 next = c->next;
264 free(c);
265 c = next;
266 }
267 curr = killer_list = NULL;
268
269 extra_info.last_kill_time = 0;
270 extra_info.last_death_time = 0;
271 extra_info.kill_time = 0;
272 extra_info.death_time = 0;
273 extra_info.observer_time = 0;
274 extra_info.kills_in_a_row = 0;
275 extra_info.deaths_in_a_row = 0;
276 extra_info.last_kill_num = -1;
277 extra_info.last_death_num = -1;
278 extra_info.got_revenge = 0;
279 }
280
281 int PInfo::GetExtraInfoSize(void) {
282 int size = 0;
283
284 size += sizeof(float); // extrainfo.last_kill_time
285 size += sizeof(float); // extrainfo.last_death_time
286 size += sizeof(float); // extra_info.observer_time
287 size += sizeof(float); // extrainfo.kill_time
288 size += sizeof(float); // extrainfo.death_time
289 size += sizeof(uint16_t); // extrainfo.kills_in_a_row
290 size += sizeof(uint16_t); // extrainfo.deaths_in_a_row
291 size += sizeof(int8_t); // extrainfo.last_kill_num
292 size += sizeof(int8_t); // extrainfo.last_death_num
293 size += sizeof(char); // extrainfo.got_revenge
294
295 return size;
296 }
297
298 int PInfo::GetSizeOfData(void) {
299 tPKillerInfo *c;
300 c = killer_list;
301 int size = 0;
302
303 size = 0;
304 while (c) {
305 size += sizeof(int); // kills
306 size++; // slot
307 c = c->next;
308 }
309
310 size += GetExtraInfoSize();
311
312 return size;
313 }
314
315 void PInfo::PackData(uint8_t *buffer) {
316 if (!buffer)
317 return;
318 tPKillerInfo *c;
319 c = killer_list;
320 int count = 0;
321
322 MultiAddFloat(extra_info.last_kill_time, buffer, &count);
323 MultiAddFloat(extra_info.last_death_time, buffer, &count);
324 MultiAddFloat(extra_info.kill_time, buffer, &count);
325 MultiAddFloat(extra_info.death_time, buffer, &count);
326 MultiAddFloat(extra_info.observer_time, buffer, &count);
327 MultiAddUshort(extra_info.kills_in_a_row, buffer, &count);
328 MultiAddUshort(extra_info.deaths_in_a_row, buffer, &count);
329 MultiAddByte(extra_info.last_kill_num, buffer, &count);
330 MultiAddByte(extra_info.last_death_num, buffer, &count);
331 MultiAddByte(extra_info.got_revenge, buffer, &count);
332
333 while (c) {
334 ASSERT(c->slot >= 0 && c->slot < 64);
335 MultiAddByte(c->slot, buffer, &count); // slot
336 MultiAddInt(c->kills, buffer, &count);
337 c = c->next;
338 }
339 }
340
341 bool PInfo::UnpackData(uint8_t *buffer, int buffsize) {
342 if (!buffer)
343 return false;
344 int count = 0;
345 int slot, kills;
346 bool done = false;
347 tPKillerInfo *k;
348 ResetAll();
349
350 // buffsize is taking into account extra info, which is guaranteed to be in there
351 extra_info.last_kill_time = MultiGetFloat(buffer, &count);
352 extra_info.last_death_time = MultiGetFloat(buffer, &count);
353 extra_info.kill_time = MultiGetFloat(buffer, &count);
354 extra_info.death_time = MultiGetFloat(buffer, &count);
355 extra_info.observer_time = MultiGetFloat(buffer, &count);
356 extra_info.kills_in_a_row = MultiGetUshort(buffer, &count);
357 extra_info.deaths_in_a_row = MultiGetUshort(buffer, &count);
358 extra_info.last_kill_num = MultiGetByte(buffer, &count);
359 extra_info.last_death_num = MultiGetByte(buffer, &count);
360 extra_info.got_revenge = MultiGetByte(buffer, &count);
361
362 int t_size = sizeof(int) + GetExtraInfoSize() + 1;
363 if (buffsize < t_size) // not even 1 slot in here
364 done = true;
365
366 while (!done) {
367 slot = MultiGetByte(buffer, &count); // slot
368 kills = MultiGetInt(buffer, &count); // kills
369
370 if (count >= buffsize)
371 done = true;
372
373 ASSERT(slot >= 0 && slot < MAX_PLAYER_RECORDS);
374 k = CreateKiller(slot);
375 if (k) {
376 k->kills = kills;
377 }
378 }
379
380 return true;
381 }
382
383 void PInfo::EnterObserverMode(void) { extra_info.observer_time = basethis->GetRealGametime(); }
384
385 void PInfo::ExitObserverMode(void) {
386 ASSERT(extra_info.observer_time > 0);
387 float observe_time = (basethis->GetRealGametime()) - extra_info.observer_time;
388 extra_info.observer_time = 0;
389
390 if (extra_info.last_kill_time > 0)
391 extra_info.last_kill_time += observe_time;
392
393 if (extra_info.last_death_time > 0)
394 extra_info.last_death_time += observe_time;
395
396 if (extra_info.kill_time > 0)
397 extra_info.kill_time += observe_time;
398
399 if (extra_info.death_time > 0)
400 extra_info.death_time += observe_time;
401 }