Debian Codegraph @

Descent3/multi_server.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/multi_server.cpp $
21 * $Revision: 276 $
22 * $Date: 9/19/01 7:26p $
23 * $Author: Matt $
24 *
25 * Multiplayer server code
26 *
27 * $Log: /DescentIII/Main/multi_server.cpp $
28 *
29 * 276 9/19/01 7:26p Matt
30 * Fix for clients not hearing when the server player took omega damage.
31 *
32 * 275 9/10/01 3:39p Matt
33 * Always send the server position, because we always say that the server
34 * is visible. This should fix some or all ghost ship problems.
35 *
36 * 274 2/19/00 6:45p Jason
37 * fixed dumb -usesmoothing bug...server was turning it on when it
38 * shouldn't have
39 *
40 * 273 9/28/99 7:08a Kevin
41 * Fixed bug with MultiAddVector calls getting passes the wrong pointers
42 *
43 * 272 9/16/99 2:37p Kevin
44 * don't send server or observer position updates
45 *
46 * 271 9/02/99 3:35p Jason
47 * send secondary fire reliable in a C/S game
48 *
49 * 270 9/01/99 4:12p Kevin
50 * Byte ordering fixes for the macintosh
51 *
52 * 269 8/20/99 9:45a Kevin
53 * Rankings changes!
54 *
55 * 268 8/17/99 1:27p Kevin
56 * fixed wacky rank bug
57 *
58 * 267 8/02/99 12:25p Jason
59 * fixed bug in ranking system
60 *
61 * 266 7/20/99 12:57p Jason
62 * added MOTD to server
63 *
64 * 265 7/08/99 5:45p Jason
65 * don't track rank if flag isn't set
66 *
67 * 264 5/24/99 11:58p Matt
68 * Removed Jason's previous fix because he and I decided it was too risky
69 * to change the night before the (hopefully) final build.
70 *
71 * 263 5/24/99 10:19p Jason
72 * fixed objects disappearing over terrain
73 *
74 * 262 5/23/99 7:37p Jason
75 * changed rankins so it is a little more fair to players
76 *
77 * 261 5/23/99 3:05a Jason
78 * fixed bug with player rankings not being updated correctly
79 *
80 * 260 5/22/99 3:10a Jason
81 * fixed weird invisible powerup bug
82 *
83 * 259 5/22/99 12:33a Jason
84 * added sending of physics flags upon joining
85 *
86 * 258 5/21/99 7:08p Jason
87 * fixed some weird multiplayer rejoin anomalies
88 *
89 * 257 5/20/99 4:55p Jason
90 * added heartbeats to server
91 *
92 * 256 5/20/99 2:53p Jason
93 * made autowaypoints work in coop
94 *
95 * 255 5/20/99 2:48a Matt
96 * Auto-waypoints now stored & used per player. Manual waypoints will all
97 * players, once Jason makes it work.
98 *
99 * 254 5/19/99 4:58p Jason
100 * changes for ranking system
101 *
102 * 253 5/19/99 12:11p Jason
103 * fixed some issues with multiplayer
104 *
105 * 252 5/17/99 6:27p Jason
106 * added reliable overrun message
107 *
108 * 251 5/13/99 7:18p Jason
109 * upped the limit of players disconnecting while joining
110 *
111 * 250 5/12/99 7:45p Jason
112 * fixed boneheaded powerup repositioning bug...I should be shot.
113 *
114 * 249 5/12/99 4:19p Jason
115 * pick a random start position for server
116 *
117 * 248 5/12/99 2:42p Jason
118 * changed default netgame difficult to hotshot (1.0 scalar)
119 *
120 * 247 5/12/99 1:57p Jason
121 * fixed yet more buggy/ugly code
122 *
123 * 246 5/11/99 9:42p Jeff
124 * send tracker id of players to other players
125 *
126 * 245 5/11/99 10:59a Kevin
127 * Ship allow/dissalow works now!
128 *
129 * 244 5/10/99 10:23p Ardussi
130 * changes to compile on Mac
131 *
132 * 243 5/10/99 8:28p Jason
133 * adjusted blackshark popping bug
134 *
135 * 242 5/10/99 5:35p Kevin
136 * New command line options for heat and scoring API enhancements
137 *
138 * 241 5/10/99 3:13a Jeff
139 * put in missing prototype
140 *
141 * 240 5/10/99 2:54a Jason
142 * fixed player spawning in opposite team waypoints
143 *
144 * 239 5/10/99 1:45a Jason
145 * took out robot and turret position counters
146 *
147 * 238 5/09/99 1:34p Kevin
148 * Added diffuculty level system to multiplayer
149 *
150 * 237 5/08/99 12:10p Kevin
151 *
152 * 236 5/08/99 12:09p Kevin
153 * fixed dedicated server num team selection
154 *
155 * 235 5/07/99 6:59p Jason
156 * fixed marker/guided nonvis issues
157 *
158 * 234 5/07/99 2:51p Jason
159 * fixed a bunch of endlevel multiplayer issues
160 *
161 * 233 5/06/99 3:26a Kevin
162 * heat scoring stuff
163 *
164 * 232 5/05/99 5:42p Jason
165 * fixed various multiplayer issues, including sequencing bugs and cheat
166 * prevention
167 *
168 * 231 5/03/99 4:22p Jeff
169 * changed byte var to uint8_t type for Linux
170 *
171 * 230 5/03/99 2:36p Jason
172 * changes for multiplayer games
173 *
174 * 229 5/03/99 8:38a Jeff
175 * handle world states for room's with goal/special flag change
176 *
177 * 228 5/02/99 2:32p Kevin
178 * fixed various dedicated server problems.
179 *
180 * 227 5/01/99 4:06p Jeff
181 * fixed #incldue for Linux
182 *
183 * 226 4/30/99 7:50p Jason
184 * setups urgent/non urgent reliable stuff better
185 *
186 * 225 4/30/99 5:06p Kevin
187 * misc dedicated server, networking and low memory enhancements
188 *
189 * 224 4/29/99 11:02p Jeff
190 * added the ability for the server to set audio taunt delay time via
191 * command line option and/or dedicated server console
192 *
193 * 223 4/29/99 12:23p Jason
194 * fixed blackshark related problem with powerups
195 *
196 * 222 4/29/99 2:00a Chris
197 * Added the portal blockage support
198 *
199 * 221 4/28/99 2:27a Jeff
200 * added buddybot world states
201 *
202 * 220 4/27/99 12:08p Kevin
203 * fixed team count dialog
204 *
205 * 219 4/25/99 5:20p Jason
206 * fixed some more coop bugs
207 *
208 * 218 4/23/99 5:32p Kevin
209 * Fixed a few mission bugs.
210 *
211 * 217 4/23/99 3:33p Kevin
212 * mission file/multiplayer mod keyword system
213 *
214 * 216 4/21/99 11:05a Kevin
215 * new ps_rand and ps_srand to replace rand & srand
216 *
217 * 215 4/20/99 3:32p Kevin
218 * made it possible for an edit box to not have a cancel button... used it
219 * for choosing team count for server
220 *
221 * 214 4/17/99 4:34p Jason
222 * changes for robot/turret tracking
223 *
224 * 212 4/08/99 11:08a Jason
225 * cleaned up a variable naming problem
226 *
227 * 211 4/06/99 6:24p Jason
228 * various fixes for multiplayer
229 *
230 * 210 4/05/99 10:54a Matt
231 * Added auto-waypoint system
232 *
233 * 209 4/03/99 6:34p Jeff
234 * fixed bug with calling BOA_IsVisible, in which it was passing in an
235 * object number instead of a room number
236 *
237 * 208 4/02/99 6:31p Jason
238 * Fixed multiplayer powerup/player start bugs
239 *
240 * 207 3/29/99 7:31p Jason
241 * added better handling of non-vis generic objects
242 *
243 * 206 3/27/99 2:20p Jason
244 * transmit rankings when a player joins
245 *
246 * 205 3/25/99 3:29p Jason
247 * added option to randomize powerup respawn points
248 *
249 * 204 3/25/99 1:41p Jason
250 * took out mprintfs
251 *
252 * 203 3/25/99 11:20a Jason
253 * fixed fog state bug
254 *
255 * 202 3/24/99 1:41p Jeff
256 * some dedicated server fixups...ability to set number of teams
257 *
258 * 201 3/23/99 6:09p Kevin
259 * Added team count prompt for Jeff
260 *
261 * 200 3/22/99 6:22p Jeff
262 * added 2 more audio taunts. a mulitplayer event when someone plays an
263 * audio taunt. option to disable audio taunts.
264 *
265 * 199 3/16/99 12:06p Kevin
266 * Enabled rankings, but took out the hud message in the OEM build
267 *
268 * 198 3/11/99 9:10p Jeff
269 * fixed remaining known bugs in demo system (DemoWriteObjCreate being
270 * called twice and </<= problems with
271 * Highest_room_index/Highest_object_index).
272 *
273 * 197 3/11/99 6:31p Jeff
274 * numerous fixes to demo system in multiplayer games (when
275 * recording/playback a demo in a multiplayer game)
276 *
277 * 196 3/11/99 3:36p Jason
278 * made k factor for ranks always be the same regardless of skill level
279 *
280 * 195 3/11/99 11:03a Jason
281 * turned off rankings for oem and demo builds
282 *
283 * 194 3/04/99 3:46p Jason
284 * fixed some multisafe stuff
285 *
286 * 193 3/02/99 11:05p 3dsmax
287 * took out dot product throttling
288 *
289 * 192 3/02/99 5:50p Kevin
290 * Ouch. Duplicate structures existed and were conflicting.
291 *
292 * 191 2/25/99 8:55p Jeff
293 * Inventory supports level change persistant items. Inventory supports
294 * time-out objects. Inventory Reset changed (takes a level of reset
295 * now). Quad lasers stay across level change (single player). Guidebot
296 * bug fixed (now back in ship on level start). Quads time out when
297 * spewed. Invulnerability and cloak powerups no longer use game
298 * event/callbacks, so they can be saved in game saves (moved to
299 * MakePlayerInvulnerable and MakeObjectInvisible)
300 *
301 * 190 2/25/99 12:44p Jason
302 * fixed some major multiplayer bugs
303 *
304 * 189 2/25/99 11:00a Matt
305 * Added new explosion system.
306 *
307 * 188 2/25/99 10:30a Jason
308 * added nonvis generic/robot system
309 *
310 * 187 2/24/99 1:02p Jason
311 * various fixes for multiplayer
312 *
313 * 186 2/24/99 11:21a Jason
314 * Fixed bugs for OEM build
315 *
316 * 185 2/17/99 2:12p Jason
317 * fixed up some coop vis issues
318 *
319 * 184 2/16/99 5:56p Jason
320 * added guided missiles to positional updates
321 *
322 * 183 2/16/99 3:47p Jason
323 * added marker updates to multiplayer server stream
324 *
325 * 182 2/15/99 7:50p Jeff
326 * new pilot file class and read/write system checked in...should be more
327 * robust than old
328 *
329 * 181 2/10/99 5:43p Jason
330 * adjusted ranking formula
331 *
332 * 180 2/09/99 6:52p Jeff
333 * implemented 'typing inidcator' in multiplayer...players that are typing
334 * messages have an icon on them
335 *
336 * 179 2/09/99 9:58a Chris
337 * Massive BOA update :) Terrain happy now. Vis happy now. Sound happy
338 * now.
339 *
340 * 178 2/08/99 5:25p Jeff
341 * removed all calls to MultiSendRemoveObject, incorportated into
342 * SetObjectDeadFlag. Fixes sequencing issues in multiplayer
343 *
344 * 177 2/03/99 6:32p Jason
345 * more changes for world states
346 *
347 * 176 2/03/99 4:26p Jason
348 * made multiplayer coop actually work!
349 *
350 * 175 2/03/99 12:15p Jason
351 * added multiplayer vis optimizations
352 *
353 * 174 2/02/99 7:06p Jason
354 * added ranking system
355 *
356 * 173 2/01/99 3:47p Jason
357 * added more multisafe functionality
358 *
359 * 172 1/31/99 7:26p Matt
360 * Renamed a bunch of functions to have HUD capitalized
361 *
362 * 171 1/31/99 1:03a Jason
363 * made multiplayer code leaner
364 *
365 * 170 1/28/99 6:17p Jason
366 * added markers
367 *
368 * 169 1/27/99 5:47p Jeff
369 * audio taunts implemented!
370 *
371 * 168 1/26/99 11:38a Jason
372 * small changes for level loading
373 *
374 * 167 1/21/99 11:34p Jason
375 * integrated some vis changes
376 *
377 * 166 1/21/99 11:15p Jeff
378 * pulled out some structs and defines from header files and moved them
379 * into seperate header files so that multiplayer dlls don't require major
380 * game headers, just those new headers. Side effect is a shorter build
381 * time. Also cleaned up some header file #includes that weren't needed.
382 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
383 * manage.h and multi.h
384 *
385 * 165 1/20/99 8:06p Jeff
386 * added members into DLLinfo struct for game change segment events, pass
387 * them over on execute dll packets
388 *
389 * 164 1/18/99 2:46p Matt
390 * Combined flags & flags2 fields in object struct
391 *
392 * 163 1/17/99 11:59p Jeff
393 * removed call to the change seg event for player, since it's done in
394 * ObjSetPos(). Also send over collide data on collide event
395 * (EVT_CLIENT_GAMECOLLIDE)
396 *
397 * 162 1/15/99 3:52a Jeff
398 * added the point and normal fields of DLLInfo to be passed for client
399 * events
400 *
401 * 161 1/13/99 3:13p Jason
402 * more work on world states
403 *
404 * 160 1/08/99 2:56p Samir
405 * Ripped out OSIRIS1.
406 *
407 * 159 1/07/99 11:51a Kevin
408 * Added support for joining servers on alternate ports and hosting behind
409 * a proxy/firewall
410 *
411 * 158 1/06/99 12:34p Jason
412 * misc changes for multiplayer
413 *
414 * 157 1/05/99 12:45p Jason
415 * fixed PPS thing to be more tolerant of low bandwidth players
416 *
417 * 156 1/04/99 8:38p Jason
418 * one more time!
419 *
420 * 155 1/04/99 8:37p Jason
421 * another test
422 *
423 * 154 1/04/99 7:58p Jason
424 * fixed packet throttling...it didn't work with the new architecture very
425 * well!
426 *
427 * 153 1/04/99 7:35p Jason
428 * fixed popping problem
429 *
430 * 152 1/04/99 7:16p Jason
431 * testing a theory about popping problems
432 *
433 * 151 1/04/99 12:54p Jason
434 * added some world state packets
435 *
436 * 150 12/30/98 10:54a Jason
437 * added positional priorities to server updates
438 *
439 * 149 12/21/98 10:22a Jason
440 * added auto pps
441 *
442 * 148 12/16/98 12:05p Kevin
443 * GameSpy!
444 *
445 * 147 12/16/98 11:29a Jason
446 * added smoothing command line option
447 *
448 * 146 12/16/98 11:26a Jason
449 * more changes for 1.1 multiplayer
450 *
451 * 145 12/15/98 12:59p Jason
452 * added debug mprintf
453 *
454 * 144 12/15/98 10:53a Jason
455 * yet more changes for 1.1
456 *
457 * 143 12/14/98 5:32p Jason
458 * added multisafe functions
459 *
460 * 142 12/08/98 4:52p Jeff
461 * changed the pilot pics packing to use Ushort instead of short just for
462 * my conscience...removed some annoying mprintf's too
463 *
464 * 141 12/04/98 7:05p Jason
465 * fixed problems with peer 2 peer
466 *
467 * 140 12/02/98 10:30a Jason
468 * added additional damage types for client-side multiplayer
469 *
470 * 139 12/01/98 5:48p Jeff
471 * added pilot picture id to netplayer struct
472 *
473 * 138 12/01/98 5:36p Jason
474 * took out additional damage sound for now
475 *
476 * 137 12/01/98 3:42p Jason
477 * made additional damage play a sound
478 *
479 * 136 12/01/98 12:47p Jason
480 * got rid of NF_DROPMISORDERED and added NF_USE_SMOOTHING
481 *
482 * 135 11/28/98 3:11p Jason
483 * made observers not send their positions
484 *
485 * 134 11/11/98 7:18p Jeff
486 * changes made so that a dedicated server's team is always -1 (team game
487 * or not)
488 *
489 * 133 11/11/98 11:41a Jason
490 * added sunlight damage
491 *
492 * 132 11/10/98 11:16a Jason
493 * fixed some multiplayer bugs with powerups disappearing
494 *
495 *
496 * 131 11/06/98 5:43p Jason
497 * made pings sent not reliably
498 *
499 * 130 10/30/98 4:24p Jason
500 * changes for multiplayer
501 *
502 * 129 10/29/98 7:09p Jason
503 * fixed server vis problem
504 *
505 * 128 10/28/98 5:07p Jason
506 * more changes for cleaner multiplayer
507 *
508 * 127 10/28/98 11:51a Jason
509 * fixed some multiplayer problems, plus redid coop a bit to make it
510 * cleaner
511 *
512 * 126 10/27/98 10:17a Jason
513 * changes for new architecture
514 *
515 * 125 10/22/98 12:28p Kevin
516 * Fixed pings in peer-peer
517 *
518 * 124 10/22/98 10:39a Kevin
519 * Fixed DirectPlay
520 *
521 * 123 10/20/98 2:53a Kevin
522 * gunboy crap
523 *
524 * 122 10/19/98 11:46p Jason
525 * changes for multiplayer debug layer
526 *
527 * 121 10/19/98 7:18p Matt
528 * Added system to support different types of damage to the player and
529 * have these different types make different sounds.
530 *
531 * 120 10/19/98 5:07p Jason
532 * put vis stuff back in
533 *
534 * 119 10/17/98 12:46p Kevin
535 * Beta 4 fixes
536 *
537 * 118 10/16/98 2:52p Jason
538 * trying to debug world states
539 *
540 * 117 10/14/98 11:46a Jason
541 * made camera scripts work correctly
542 *
543 * 116 10/13/98 9:26p Jason
544 * trying to track down wind problems
545 *
546 * 115 10/07/98 1:07p Jason
547 * added more safety precautions
548 *
549 * 114 10/05/98 7:23p Jason
550 * added protective layer onto multiplayer
551 *
552 * 113 10/05/98 11:08a Jason
553 * fixed some potential gap problems in the multiplayer sequencing code
554 *
555 * 112 10/02/98 5:51p Jason
556 * made default max players 16 on the dedicated server
557 *
558 * 111 10/01/98 12:41p Jason
559 * more world state stuff
560 *
561 * 110 10/01/98 11:29a Jason
562 * changes for world states in multiplayer games
563 *
564 * 109 10/01/98 10:22a Jason
565 * added mprintf
566 *
567 * 108 9/29/98 2:23p Kevin
568 * More UI tweaks
569 *
570 * 107 9/29/98 11:18a Jeff
571 * fixed OBJ_DUMMY problems on client join objects
572 *
573 * 106 9/28/98 6:36p Kevin
574 * added dummy objects to get sent over in join
575 *
576 * 105 9/28/98 6:20p Jason
577 * added persistant data from scripts
578 *
579 * 104 9/25/98 9:25p Jason
580 * turned vis stuff back on
581 *
582 * 103 9/25/98 4:21p Jason
583 * more multiplayer bug checking
584 *
585 * 102 9/25/98 12:06a Jason
586 * fixed some multiplayer scripting issues
587 *
588 * 101 9/16/98 7:42p Kevin
589 * Called first frame for dll in non mastertracker games.
590 *
591 * 100 9/15/98 5:44p Jason
592 * more work on dedicated server
593 *
594 * 99 9/15/98 4:31p Jason
595 * added more functionality for the dedicated server
596 *
597 * 98 9/15/98 12:42p Jason
598 * got dedicated server actually working
599 *
600 * 97 9/04/98 3:46p Kevin
601 * Added ping_time to Netplayers struct. It's updated in peer-peer and
602 * client server
603 *
604 * 96 9/02/98 6:54p Kevin
605 * Fixed general directplay support up, and got modem-modem working
606 *
607 * 95 8/19/98 11:49a Kevin
608 * Got DirectPlay IPX working, and localized connection DLLs
609 *
610 * 94 8/14/98 4:54p Kevin
611 * More directplay stuff
612 *
613 * 93 8/13/98 6:32p Kevin
614 * Initial implementation of directplay API
615 *
616 * 92 8/10/98 4:58p Kevin
617 * Send robot orientation as 3 angles
618 *
619 * 91 8/07/98 12:27p Kevin
620 * Added network stuff for energy/shield conversion
621 *
622 * 90 8/05/98 10:46a Kevin
623 * sound & bmp exchange fixes
624 *
625 */
626
627 #include "args.h"
628 #include "multi.h"
629 #include "multi_server.h"
630 #include "player.h"
631 #include "game.h"
632 #include "mono.h"
633 #include "ddio.h"
634 #include "hud.h"
635 #include "pilot.h"
636 #include "BOA.h"
637 #include "LoadLevel.h"
638 #include "Mission.h"
639 #include "game2dll.h"
640 #include "stringtable.h"
641
642 // #define USE_DIRECTPLAY
643
644 #ifdef USE_DIRECTPLAY
645 #include "directplay.h"
646 #endif
647
648 #include "dedicated_server.h"
649 #include "damage.h"
650 // #include "gamespy.h"
651 #include "multi_world_state.h"
652 #include "ObjScript.h"
653 #include "marker.h"
654 #include "findintersection.h"
655 #include "weapon.h"
656 #include "weather.h"
657 #include "doorway.h"
658 #include "object_lighting.h"
659 #include "ship.h"
660 #include "pstring.h"
661 #include "audiotaunts.h"
662
663 #include "ui.h"
664 #include "newui.h"
665 #include "multi_dll_mgr.h"
666 #include "spew.h"
667 #include "psrand.h"
668 #include "polymodel.h"
669 #include "init.h"
670 #include "../md5/md5.h"
671
672 #include <algorithm>
673
674 void MultiProcessShipChecksum(MD5 *md5, int ship_index);
675
676 int GameDLLGetConnectingPlayersTeam(int slot);
677
678 int Moved_robots[MAX_NET_PLAYERS][MAX_CHANGED_OBJECTS];
679 uint16_t Num_moved_robots[MAX_NET_PLAYERS];
680
681 int Changed_anim[MAX_CHANGED_OBJECTS][MAX_NET_PLAYERS];
682 uint16_t Num_changed_anim[MAX_NET_PLAYERS];
683
684 int Changed_wb_anim[MAX_CHANGED_OBJECTS][MAX_NET_PLAYERS];
685 uint16_t Num_changed_wb_anim[MAX_NET_PLAYERS];
686
687 int Changed_turret[MAX_CHANGED_OBJECTS][MAX_NET_PLAYERS];
688 uint16_t Num_changed_turret[MAX_NET_PLAYERS];
689
690 float last_sent_bytes[MAX_NET_PLAYERS];
691 float Multi_last_send_visible[MAX_NET_PLAYERS];
692 uint32_t Multi_visible_players[MAX_NET_PLAYERS];
693 extern int Buddy_handle[MAX_PLAYERS];
694 extern char Multi_message_of_the_day[];
695
696 int Join_response_strings[] = {TXI_MLTOK, TXI_MLTNOTSERV, TXI_MLTDORK, TXI_MLTNOTENOUGHSTR, TXI_MLTGAMEFULL};
697
698 // d3_net_game_data D3_tracker_info;
699 float LastTrackerDataUpdate;
700
701 int MTReadingPilot = -1;
702 int MTWritingPilot = -1;
703
704 int Player_count = 0; // The total number of players playing the game
705
706 extern int Use_file_xfer;
707
708 uint32_t Secret_net_id = 0;
709 // Stars a multiplayer server. If playing is non-zero, then the server is a player in the
710 // game
711 // Server only
712 void MultiStartServer(int playing, char *scriptname, int dedicated_server_num_teams) {
713 // Clear out some stuff
714 for (int i = 0; i < MAX_NET_PLAYERS; i++) {
715 memset(&NetPlayers[i], 0, sizeof(netplayer));
716 Multi_send_size[i] = 0;
717
718 for (int t = 0; t < MAX_NET_PLAYERS; t++)
719 Multi_last_sent_time[i][t] = 0;
720
721 Player_fire_packet[i].fired_on_this_frame = PFP_NO_FIRED;
722
723 Multi_reliable_send_size[i] = 0;
724 Multi_reliable_last_send_time[i] = 0;
725
726 Multi_reliable_urgent[i] = 0;
727 Multi_last_send_visible[i] = 0;
728 Multi_visible_players[i] = 0xFFFFFFFF;
729
730 Num_moved_robots[i] = 0;
731 Num_changed_anim[i] = 0;
732 Num_changed_turret[i] = 0;
733 Num_changed_wb_anim[i] = 0;
734 last_sent_bytes[i] = timer_GetTime();
735 NetPlayers[i].file_xfer_flags = NETFILE_NONE;
736 NetPlayers[i].ship_logo[0] = 0;
737 NetPlayers[i].voice_taunt1[0] = 0;
738 NetPlayers[i].voice_taunt2[0] = 0;
739 NetPlayers[i].voice_taunt3[0] = 0;
740 NetPlayers[i].voice_taunt4[0] = 0;
741 NetPlayers[i].pilot_pic_id = PPIC_INVALID_ID;
742 NetPlayers[i].custom_file_seq = NETFILE_ID_NOFILE;
743 PlayerSetCustomTexture(i, NULL);
744
745 NetPlayers[i].ping_time = 0;
746 NetPlayers[i].last_ping_time = 0;
747 }
748 NetPlayers[Player_num].custom_file_seq = 0xff;
749 Current_pilot.get_multiplayer_data(NetPlayers[Player_num].ship_logo, NetPlayers[Player_num].voice_taunt1,
750 NetPlayers[Player_num].voice_taunt2, NULL, NetPlayers[Player_num].voice_taunt3,
751 NetPlayers[Player_num].voice_taunt4);
752
753 // ship_logo
754 if (NetPlayers[Player_num].ship_logo[0])
755 PlayerSetCustomTexture(Player_num, NetPlayers[Player_num].ship_logo);
756
757 Player_num = 0;
758 NetPlayers[Player_num].flags = NPF_SERVER | NPF_CONNECTED;
759
760 // Strip trailing .d3m if there is one
761 int sl = strlen(scriptname);
762 if (scriptname[sl - 4] == '.' && scriptname[sl - 3] == 'd' && scriptname[sl - 2] == '3' && scriptname[sl - 1] == 'm')
763 scriptname[sl - 4] = 0;
764
765 NetPlayers[Player_num].sequence = NETSEQ_PREGAME;
766
767 Netgame.local_role = LR_SERVER;
768 Netgame.server_sequence = 0;
769 Netgame.server_version = MULTI_VERSION;
770
771 Game_mode = GM_NETWORK;
772
773 // Setup audio taunt delay time
774 int audiotauntdelayarg = FindArg("-audiotauntdelay");
775 if (audiotauntdelayarg > 0) {
776 float t;
777 char adt_str[64], *p;
778 strcpy(adt_str, GameArgs[audiotauntdelayarg + 1]);
779 p = adt_str;
780 while (*p && (*p == ' ' || *p == '\t'))
781 p++;
782 t = atof(p);
783 if (t == 0)
784 t = 5.0f;
785
786 mprintf(0, "MULTI: Setting audio taunt delay time to %.2f seconds\n", t);
787 taunt_SetDelayTime(t);
788 } else {
789 taunt_SetDelayTime(5.0f);
790 }
791
792 // Temporary name fix
793 Current_pilot.get_name(Players[Player_num].callsign);
794
795 // setup the server's pilot id
796 Current_pilot.get_multiplayer_data(NULL, NULL, NULL, &NetPlayers[Player_num].pilot_pic_id);
797
798 // Choose ship
799 char pshipmodel[PAGENAME_LEN];
800 Current_pilot.get_ship(pshipmodel);
801
802 Players[Player_num].ship_index = FindShipName(pshipmodel);
803 if (Players[Player_num].ship_index < 0)
804 Players[Player_num].ship_index = 0;
805
806 SetGamemodeScript(scriptname, dedicated_server_num_teams);
807
808 MultiFlushAllIncomingBuffers();
809 #ifdef USE_DIRECTPLAY
810 dp_StartGame(Netgame.name);
811 #endif
812
813 // gspy_StartGame(Netgame.name);
814 // We use this to identify clients....
815 ps_srand(timer_GetTime());
816 Secret_net_id = ps_rand() * ps_rand();
817 LastPacketReceived = timer_GetTime();
818
819 // At this point we have a level checksum, so add the ships into the mix
820 for (int i = 0; i < MAX_SHIPS; i++)
821 if (Ships[i].used)
822 MultiGetShipChecksum(i);
823 }
824
825 // Checks if the selected mission and script are compatible
826 // Return values:
827 //-1 Not compatible!
828 //>=0 Number of teams supported for this mod & level
829
830 #define SCRIPTBADFORMISSION -1
831
832 int CheckMissionForScript(char *mission, char *script, int dedicated_server_num_teams) {
833 char mod_keys[MAX_KEYWORDLEN];
834 if (!GetDLLRequirements(script, mod_keys, MAX_KEYWORDLEN)) {
835 return SCRIPTBADFORMISSION;
836 }
837 int teams = MissionGetKeywords(mission, mod_keys);
838 // Bad match!
839 if (teams == -1) {
840 if (!Dedicated_server) {
841 DoMessageBox(script, TXT_MSNNOTCOMPATIBLE, MSGBOX_OK);
842 return SCRIPTBADFORMISSION;
843 } else {
844 PrintDedicatedMessage(TXT_MSNNOTCOMPATIBLE);
845 PrintDedicatedMessage("\n");
846 }
847 }
848
849 // See how many teams this dll will allow.
850 int desired_teams = 1;
851 int max_teams, min_teams;
852
853 GetDLLNumTeamInfo(script, &min_teams, &max_teams);
854
855 if (min_teams > teams) {
856 mprintf(0, "This multiplayer game requires more teams than the mission supports!\n");
857 return SCRIPTBADFORMISSION;
858 }
859 // Use whatever is smaller, the dll's max teams, or what the missions supports
860 if (max_teams > teams)
861 max_teams = teams;
862
863 if (Dedicated_server) {
864 teams = min_teams;
865
866 if (min_teams != max_teams) {
867 // they can set the number of teams for this game
868 // make sure it's valid
869 if (dedicated_server_num_teams >= min_teams && dedicated_server_num_teams <= max_teams) {
870 teams = dedicated_server_num_teams;
871 } else {
872 // invalid num teams
873 PrintDedicatedMessage(TXT_INVALIDNUMTEAMS, teams);
874 PrintDedicatedMessage("\n");
875 }
876 }
877
878 } else {
879 if (min_teams != max_teams) {
880 char a_num_teams[5];
881 char team_count_msg[300];
882 snprintf(a_num_teams, sizeof(a_num_teams), "%d", min_teams);
883 snprintf(team_count_msg, sizeof(team_count_msg), "%s %s (%d - %d)", TXT_TEAMCOUNTPROMPT, script, min_teams,
884 max_teams);
885 retry_team_count:
886 int res = DoEditDialog(team_count_msg, a_num_teams, 4);
887 if (res) {
888 desired_teams = atoi(a_num_teams);
889 if ((desired_teams > max_teams) || (desired_teams < min_teams)) {
890 DoMessageBox(script, TXT_INVALIDTEAMCOUNT, MSGBOX_OK);
891 goto retry_team_count;
892 } else {
893 teams = desired_teams;
894 }
895 } else {
896 return SCRIPTBADFORMISSION;
897 }
898 }
899 }
900
901 return teams;
902 }
903
904 // multi_check_listen() calls low level routines to see if we have a connection from a client we
905 // should accept.
906 void MultiCheckListen() {
907 int i;
908 network_address addr;
909 SOCKET sock = INVALID_SOCKET;
910
911 // call routine which calls select to see if we need to check for a connect from a client
912 // by passing addr, we are telling check_for_listen to do the accept and return who it was from in
913 // addr. The
914 sock = nw_CheckListenSocket(&addr);
915 if (sock != INVALID_SOCKET) {
916 // the connection was accepted. Find a free slot for this new player
917 for (i = 0; i < MAX_NET_PLAYERS; i++) {
918 if (i == Player_num)
919 continue;
920
921 if (!(NetPlayers[i].flags & NPF_CONNECTED)) {
922 // This slot is free!
923 MultiSendConnectionAccepted(i, sock, &addr);
924 break;
925 }
926 }
927
928 // if we didn't find a player, close the socket
929 if (i == MAX_NET_PLAYERS) {
930 mprintf(0, "Got accept on my listen socket, but no free slots. Closing socket.\n");
931 nw_CloseSocket(&sock);
932 }
933 }
934 }
935
936 // Tells the client that we're done sending players
937 void MultiSendDonePlayers(int slot) {
938 int size;
939 uint8_t data[MAX_GAME_DATA_SIZE];
940 int count = 0;
941
942 mprintf(0, "Sending done players\n");
943
944 size = START_DATA(MP_DONE_PLAYERS, data, &count);
945 int num = MultiCountPlayers();
946 MultiAddByte(num, data, &count);
947 END_DATA(count, data, size);
948
949 nw_SendReliable(NetPlayers[slot].reliable_socket, data, count);
950 }
951
952 // Tells the client that we're done sending buildings
953 void MultiSendDoneBuildings(int slot) {
954 int size;
955 uint8_t data[MAX_GAME_DATA_SIZE];
956 int count = 0;
957
958 mprintf(0, "Sending done buildings\n");
959
960 size = START_DATA(MP_DONE_BUILDINGS, data, &count);
961 END_DATA(count, data, size);
962
963 nw_SendReliable(NetPlayers[slot].reliable_socket, data, count);
964 }
965
966 // Tells the client that we're done sending objects
967 void MultiSendDoneObjects(int slot) {
968 int size;
969 uint8_t data[MAX_GAME_DATA_SIZE];
970 int count = 0;
971
972 mprintf(0, "Sending done objects\n");
973
974 // Take the level checksum and clone it.
975 MD5 playermd5(*Level_md5);
976 // Generate a random salt value and send it to the client.
977 int salt = ps_rand();
978 // process the salt through the md5
979 playermd5.update(salt);
980 // process the ships through the md5
981 for (int i = 0; i < MAX_SHIPS; i++)
982 if (Ships[i].used)
983 MultiProcessShipChecksum(&playermd5, i);
984 // save the digest value in the netplayer slot
985 playermd5.digest(NetPlayers[slot].digest);
986
987 size = START_DATA(MP_DONE_OBJECTS, data, &count);
988 MultiAddInt(salt, data, &count);
989 END_DATA(count, data, size);
990
991 nw_SendReliable(NetPlayers[slot].reliable_socket, data, count);
992 }
993
994 // Tells the client that we're done sending the world states
995 void MultiSendDoneWorldStates(int slot) {
996 int size;
997 uint8_t data[MAX_GAME_DATA_SIZE];
998 int count = 0;
999
1000 mprintf(0, "Sending done world states\n");
1001
1002 size = START_DATA(MP_DONE_WORLD_STATES, data, &count);
1003 END_DATA(count, data, size);
1004
1005 nw_SendReliable(NetPlayers[slot].reliable_socket, data, count);
1006 }
1007
1008 // Clears all the player markers belonging to a particular slot
1009 void MultiClearPlayerMarkers(int slot) {
1010 int m1 = slot * 2;
1011 int m2 = (slot * 2) + 1;
1012
1013 for (int i = 0; i <= Highest_object_index; i++) {
1014 if (Objects[i].type == OBJ_MARKER && (Objects[i].id == m1 || Objects[i].id == m2)) {
1015 SetObjectDeadFlag(&Objects[i], true, false);
1016 }
1017 }
1018 }
1019
1020 // Tells our clients that a player disconnected
1021 void MultiSendPlayerDisconnect(int slot) {
1022 int size, count = 0;
1023 uint8_t data[MAX_GAME_DATA_SIZE];
1024
1025 size = START_DATA(MP_DISCONNECT, data, &count);
1026
1027 // Send player whose disconnected
1028 MultiAddByte(slot, data, &count);
1029
1030 END_DATA(count, data, size);
1031
1032 if (Game_is_master_tracker_game) {
1033 NetPlayers[slot].flags |= NPF_MT_WRITING_PILOT;
1034 }
1035 for (int i = 0; i < MAX_NET_PLAYERS; i++) {
1036 if (i == Player_num || i == slot)
1037 continue;
1038
1039 if (NetPlayers[i].flags & NPF_CONNECTED)
1040 nw_SendReliable(NetPlayers[i].reliable_socket, data, count);
1041 }
1042 }
1043
1044 #define DISCONNECT_TIME 10
1045 // Disconnects all players that haven't been heard from in a while
1046 // Server only
1047 void MultiDisconnectDeadPlayers() {
1048 for (int i = 0; i < MAX_NET_PLAYERS; i++) {
1049 if (i == Player_num)
1050 continue;
1051
1052 if (NetPlayers[i].flags & NPF_CONNECTED) {
1053 float cur_time = timer_GetTime();
1054
1055 if (!nw_CheckReliableSocket(NetPlayers[i].reliable_socket)) {
1056 mprintf(0, "Disconnecting player %d because the reliable socket closed.\n", i);
1057 AddHUDMessage("%s (%s)", TXT_RELIABLE_OVERRUN, Players[i].callsign);
1058 MultiDisconnectPlayer(i);
1059 } else if (NetPlayers[i].sequence > NETSEQ_LEVEL_START && NetPlayers[i].sequence != NETSEQ_LEVEL_END) {
1060 if (cur_time - NetPlayers[i].last_packet_time > DISCONNECT_TIME) {
1061 mprintf(0, "8sec disconnecting player %d. Last packet time=%f Sequence=%d\n",
1062 i,
1063 cur_time - NetPlayers[i].last_packet_time,
1064 NetPlayers[i].sequence);
1065 MultiDisconnectPlayer(i);
1066 }
1067 } else // If not playing (ie joining, give them longer)
1068 {
1069 if (cur_time - NetPlayers[i].last_packet_time > (DISCONNECT_TIME * 15)) {
1070 mprintf(0, "Too long...disconnecting player %d. Last packet time=%f Sequence=%d\n",
1071 i,
1072 cur_time - NetPlayers[i].last_packet_time,
1073 NetPlayers[i].sequence);
1074 MultiDisconnectPlayer(i);
1075 }
1076 }
1077 }
1078 }
1079 }
1080
1081 // Disconnect a player for whatever reason
1082 // Server only
1083 void MultiDisconnectPlayer(int slot) {
1084 ASSERT(Netgame.local_role == LR_SERVER);
1085
1086 DLLInfo.me_handle = DLLInfo.it_handle = Objects[Players[slot].objnum].handle;
1087 CallGameDLL(EVT_GAMEPLAYERDISCONNECT, &DLLInfo);
1088
1089 if (NetPlayers[slot].flags & NPF_CONNECTED) {
1090 mprintf(0, "Disconnecting player %d (%s)...\n", slot, Players[slot].callsign);
1091 if (NetPlayers[slot].file_xfer_flags != NETFILE_NONE) {
1092 MultiCancelFile(slot, NetPlayers[slot].custom_file_seq, NetPlayers[slot].file_xfer_who);
1093 }
1094 nw_CloseSocket(&NetPlayers[slot].reliable_socket);
1095
1096 if (NetPlayers[slot].sequence == NETSEQ_PLAYING) {
1097 PlayerSpewInventory(&Objects[Players[slot].objnum], true, true);
1098 MultiMakePlayerGhost(slot);
1099 AddHUDMessage(TXT_MLTDISCONNECTED, Players[slot].callsign);
1100 }
1101
1102 NetPlayers[slot].flags &= ~NPF_CONNECTED;
1103 NetPlayers[slot].secret_net_id = 0;
1104 MultiClearGuidebot(slot);
1105 MultiClearPlayerMarkers(slot);
1106 MultiSendPlayerDisconnect(slot);
1107 Players[slot].flags = 0;
1108
1109 } else
1110 mprintf(0, "Trying to disconnect a non-existant player!\n");
1111 }
1112
1113 // Sends existing players to a joining player
1114 // Sends to "slot" and describes player "which"
1115 // Server only
1116 void MultiSendPlayer(int slot, int which) {
1117 ASSERT(Netgame.local_role == LR_SERVER);
1118
1119 uint8_t data[MAX_GAME_DATA_SIZE];
1120 int count = 0;
1121 int size_offset;
1122
1123 mprintf(0, "Sending MP_PLAYER packet to player %d!\n", slot);
1124
1125 size_offset = START_DATA(MP_PLAYER, data, &count);
1126
1127 MultiAddByte(which, data, &count);
1128
1129 // Add name
1130 int len = strlen(Players[which].callsign) + 1;
1131 MultiAddByte(len, data, &count);
1132 memcpy(&data[count], Players[which].callsign, len);
1133 count += len;
1134
1135 // Add name
1136 len = strlen(Ships[Players[which].ship_index].name) + 1;
1137 MultiAddByte(len, data, &count);
1138 memcpy(&data[count], Ships[Players[which].ship_index].name, len);
1139 count += len;
1140
1141 // Add flags, shields, inventory
1142 MultiAddInt(Players[which].flags, data, &count);
1143 MultiAddInt(Players[which].weapon_flags, data, &count);
1144 MultiAddInt(NetPlayers[which].flags, data, &count);
1145 MultiAddFloat(Objects[Players[which].objnum].shields, data, &count);
1146
1147 // Add position, roomnum, and terrain flag
1148
1149 // Just for safety sake add the object number
1150 MultiAddShort(Players[which].objnum, data, &count);
1151
1152 // If this is a team game, get the first start position
1153 if (slot == which) {
1154 int myteam = GameDLLGetConnectingPlayersTeam(slot);
1155
1156 uint8_t temp_team = (myteam == -1) ? 255 : myteam;
1157 MultiAddByte(temp_team, data, &count);
1158
1159 if (myteam >= 0 && myteam <= 4)
1160 Players[slot].team = myteam;
1161
1162 if (temp_team == 255)
1163 Players[slot].start_index = slot;
1164 else
1165 Players[slot].start_index = PlayerGetRandomStartPosition(slot);
1166
1167 MultiAddShort(Players[slot].start_index, data, &count);
1168 } else {
1169 uint8_t temp_team = (Players[which].team == -1) ? 255 : Players[which].team;
1170 MultiAddByte(temp_team, data, &count);
1171 }
1172
1173 // Tell them if this player is currently observing
1174 if (Objects[Players[which].objnum].type == OBJ_OBSERVER)
1175 MultiAddByte(1, data, &count);
1176 else
1177 MultiAddByte(0, data, &count);
1178
1179 // send the player's address
1180 memcpy(data + count, &NetPlayers[which].addr, sizeof(network_address));
1181 count += sizeof(network_address);
1182
1183 // PPS
1184 MultiAddByte(NetPlayers[which].pps, data, &count);
1185
1186 // pilot picture id
1187 MultiAddUshort(NetPlayers[which].pilot_pic_id, data, &count);
1188
1189 // Ranking
1190 MultiAddFloat(Players[which].rank, data, &count);
1191
1192 // Mastertracker id
1193 if (Game_is_master_tracker_game) {
1194 int len = strlen(Players[which].tracker_id) + 1;
1195 MultiAddByte(len, data, &count);
1196 memcpy(&data[count], Players[which].tracker_id, len);
1197 count += len;
1198 }
1199
1200 END_DATA(count, data, size_offset);
1201
1202 // Send it out
1203 nw_SendReliable(NetPlayers[slot].reliable_socket, data, count);
1204 }
1205
1206 // Sends blownup buildings to a joining player
1207 // Server only
1208 void MultiSendBuildings(int slot) {
1209 ASSERT(Netgame.local_role == LR_SERVER);
1210
1211 uint8_t data[MAX_GAME_DATA_SIZE];
1212 int count = 0;
1213 int size_offset;
1214
1215 mprintf(0, "Sending MP_BUILDING packet to player %d!\n", slot);
1216
1217 size_offset = START_DATA(MP_BUILDING, data, &count);
1218
1219 MultiAddByte(Multi_num_buildings_changed, data, &count);
1220
1221 for (int i = 0; i < MAX_OBJECTS; i++) {
1222 if (Multi_building_states[i] != 0) {
1223 MultiAddShort((uint16_t)i, data, &count);
1224 }
1225 }
1226
1227 ASSERT(count < MAX_GAME_DATA_SIZE);
1228
1229 END_DATA(count, data, size_offset);
1230
1231 // Send it out
1232 nw_SendReliable(NetPlayers[slot].reliable_socket, data, count);
1233 }
1234
1235 // Returns true if we should send an update about this object
1236 bool MultiIsValidMovedObject(object *obj) {
1237 bool good = false;
1238
1239 if (obj->type == OBJ_ROBOT || obj->type == OBJ_CLUTTER || obj->type == OBJ_BUILDING)
1240 good = true;
1241
1242 if (good == true) {
1243 if (obj->flags & (OF_DEAD))
1244 good = false;
1245 if (!(obj->flags & OF_CLIENT_KNOWS))
1246 good = false;
1247
1248 // Still going? Good!
1249 if (good == true) {
1250 if (obj->movement_type == MT_WALKING || obj->movement_type == MT_PHYSICS)
1251 return true;
1252 }
1253 }
1254
1255 // Didn't pass all the tests else it wouldn't make it this fare
1256 return false;
1257 }
1258
1259 void MultiSendJoinDemoObjects(int slot) {
1260 uint8_t data[MAX_GAME_DATA_SIZE];
1261 int count = 0;
1262 int size_offset, i;
1263
1264 int num_demo_objects = 0;
1265 for (i = 0; i < MAX_OBJECTS; i++) {
1266 if (Objects[i].type != OBJ_NONE && Objects[i].flags & OF_CLIENTDEMOOBJECT) {
1267 num_demo_objects++;
1268 }
1269 }
1270
1271 ASSERT((num_demo_objects * sizeof(uint16_t) + sizeof(uint16_t)) < MAX_GAME_DATA_SIZE);
1272
1273 mprintf(0, "Sending DemoJoinObjects (%d)\n", num_demo_objects);
1274
1275 count = 0;
1276 size_offset = START_DATA(MP_SEND_DEMO_OBJECT_FLAGS, data, &count);
1277
1278 MultiAddUshort(num_demo_objects, data, &count);
1279
1280 for (i = 0; i < MAX_OBJECTS; i++) {
1281 if (Objects[i].type != OBJ_NONE && Objects[i].flags & OF_CLIENTDEMOOBJECT) {
1282 MultiAddUshort(i, data, &count);
1283 }
1284 }
1285
1286 END_DATA(count, data, size_offset);
1287 nw_SendReliable(NetPlayers[slot].reliable_socket, data, count);
1288 }
1289
1290 int StuffObjectIntoPacket(object *obj, uint8_t *data) {
1291 uint32_t index;
1292 int count = 0;
1293 bool obj_is_dummy = false;
1294
1295 MultiAddUshort(obj - Objects, data, &count);
1296 MultiAddByte(obj->type, data, &count);
1297
1298 // Send old object type if it's a dummy
1299 if (obj->type == OBJ_DUMMY) {
1300 obj_is_dummy = true;
1301 MultiAddByte(obj->dummy_type, data, &count);
1302 ObjUnGhostObject(obj - Objects);
1303 }
1304
1305 if (obj->type != OBJ_CAMERA && obj->type != OBJ_DOOR) {
1306 // Stuff the checksum
1307 index = MultiGetMatchChecksum(obj->type, obj->id);
1308 MultiAddUint(index, data, &count);
1309 }
1310
1311 if (obj->type != OBJ_POWERUP && obj->type != OBJ_DOOR) {
1312 multi_orientation mat;
1313 MultiMakeMatrix(&mat, &obj->orient);
1314 MultiMatrixMakeEndianFriendly(&mat);
1315 memcpy(&data[count], &mat, sizeof(multi_orientation));
1316 count += sizeof(multi_orientation);
1317 }
1318
1319 // Send position
1320 if (obj->type != OBJ_DOOR) {
1321 // memcpy (&data[count],&obj->pos,sizeof(vector));
1322 // count+=sizeof(vector);
1323 MultiAddVector(obj->pos, data, &count);
1324
1325 // Send room number and terrain flag
1326 MultiAddUshort(CELLNUM(obj->roomnum), data, &count);
1327
1328 if (OBJECT_OUTSIDE(obj))
1329 MultiAddByte(1, data, &count);
1330 else
1331 MultiAddByte(0, data, &count);
1332
1333 if (obj->flags & OF_USES_LIFELEFT) {
1334 ASSERT(obj->lifeleft < 255);
1335 MultiAddUbyte(obj->lifeleft, data, &count);
1336 } else
1337 MultiAddUbyte(255, data, &count);
1338 }
1339
1340 if (obj->type == OBJ_MARKER) {
1341 // Add marker message to the end of this
1342 uint8_t len = strlen(MarkerMessages[obj->id]) + 1;
1343 MultiAddByte(len, data, &count);
1344 memcpy(data + count, MarkerMessages[obj->id], len);
1345 count += len;
1346 }
1347
1348 ASSERT(count < MAX_GAME_DATA_SIZE);
1349
1350 // we need to reghost the object if what originally a ghost
1351 if (obj_is_dummy) {
1352 ObjGhostObject(obj - Objects);
1353 }
1354
1355 return count;
1356 }
1357
1358 // Sends objects to a joining player
1359 // Server only
1360 #define MAX_OBJECTS_PER_PACKET 50
1361 void MultiSendJoinObjects(int slot) {
1362 ASSERT(Netgame.local_role == LR_SERVER);
1363
1364 uint8_t data[MAX_GAME_DATA_SIZE];
1365 uint16_t outgoing_objects[MAX_OBJECTS];
1366 int count = 0;
1367 int size_offset;
1368 int i;
1369
1370 mprintf(0, "Sending MP_JOIN_OBJECTS packet to player %d!\n", slot);
1371
1372 uint16_t total_objects = 0;
1373 last_sent_bytes[slot] = timer_GetTime();
1374
1375 Num_moved_robots[slot] = 0;
1376 Num_changed_anim[slot] = 0;
1377 Num_changed_turret[slot] = 0;
1378 Num_changed_wb_anim[slot] = 0;
1379
1380 // Count how many powerups/robots we have to send and make a list
1381 for (i = 0; i < MAX_OBJECTS; i++) {
1382 if (Objects[i].type == OBJ_ROBOT || Objects[i].type == OBJ_POWERUP || Objects[i].type == OBJ_MARKER ||
1383 Objects[i].type == OBJ_CAMERA || Objects[i].type == OBJ_CLUTTER || Objects[i].type == OBJ_BUILDING ||
1384 Objects[i].type == OBJ_DUMMY || Objects[i].type == OBJ_DOOR) {
1385 outgoing_objects[total_objects] = i;
1386 total_objects++;
1387
1388 if (MultiIsValidMovedObject(&Objects[i])) {
1389 if (Num_changed_anim[slot] < MAX_CHANGED_OBJECTS) {
1390 Changed_anim[Num_changed_anim[slot]][slot] = Objects[i].handle;
1391 Num_changed_anim[slot]++;
1392 }
1393
1394 object *obj = &Objects[i];
1395 polyobj_info *p_info = &obj->rtype.pobj_info;
1396 if (p_info->multi_turret_info.num_turrets) {
1397 if (Num_changed_turret[slot] < MAX_CHANGED_OBJECTS) {
1398 Changed_turret[Num_changed_turret[slot]][slot] = Objects[i].handle;
1399 Num_changed_turret[slot]++;
1400 }
1401 }
1402 }
1403 }
1404 }
1405
1406 // Only send up to n objects in a packet lest we overflow outgoing packet size
1407
1408 int cur_object = 0;
1409 int objects_to_go = total_objects;
1410
1411 while (objects_to_go > 0) {
1412 int num_objects_this_packet = std::min(MAX_OBJECTS_PER_PACKET, objects_to_go);
1413 int overflow = 0;
1414
1415 count = 0;
1416 size_offset = START_DATA(MP_JOIN_OBJECTS, data, &count);
1417 int num_objs_offset = count;
1418 MultiAddByte(0, data, &count);
1419
1420 // Actually add the objects to our outgoing buffer
1421 for (i = 0; i < num_objects_this_packet && !overflow; i++) {
1422 // Add objnum and id
1423 object *obj = &Objects[outgoing_objects[i + cur_object]];
1424 uint8_t obj_data[MAX_GAME_DATA_SIZE];
1425
1426 int num_bytes = StuffObjectIntoPacket(obj, obj_data);
1427
1428 // If their is an overflow, send it out!
1429 if ((num_bytes + count) >= MAX_GAME_DATA_SIZE) {
1430 num_objects_this_packet = i;
1431 data[num_objs_offset] = num_objects_this_packet;
1432 END_DATA(count, data, size_offset);
1433 nw_SendReliable(NetPlayers[slot].reliable_socket, data, count, false);
1434 overflow = 1;
1435 } else {
1436 memcpy(&data[count], obj_data, num_bytes);
1437 count += num_bytes;
1438 }
1439 }
1440
1441 if (!overflow) {
1442 data[num_objs_offset] = num_objects_this_packet;
1443 END_DATA(count, data, size_offset);
1444 nw_SendReliable(NetPlayers[slot].reliable_socket, data, count, false);
1445 }
1446
1447 cur_object += num_objects_this_packet;
1448 objects_to_go -= num_objects_this_packet;
1449 }
1450
1451 MultiSendJoinDemoObjects(slot);
1452 }
1453
1454 void MultiStoreWorldPacket(int slot, uint8_t *big_data, int *big_count, uint8_t *cur_data, int *cur_count,
1455 int *size_offset) {
1456 if (*big_count + *cur_count >= (MAX_GAME_DATA_SIZE - 3)) {
1457 mprintf(0, "Starting another world packet!\n");
1458 MultiAddByte(WS_END, big_data, big_count);
1459 END_DATA(*big_count, big_data, *size_offset);
1460
1461 // Send it out
1462 nw_SendReliable(NetPlayers[slot].reliable_socket, big_data, *big_count);
1463
1464 // Restart another packet
1465 *big_count = 0;
1466 *size_offset = START_DATA(MP_WORLD_STATES, big_data, big_count);
1467 }
1468
1469 memcpy(&big_data[*big_count], cur_data, *cur_count);
1470 *big_count += *cur_count;
1471 *cur_count = 0;
1472 ASSERT(*big_count < (MAX_GAME_DATA_SIZE - 3));
1473 }
1474
1475 doorway *GetDoorwayFromObject(int door_obj_handle);
1476 // Function that sends all the changed world states to an incoming player
1477 void MultiSendWorldStates(int slot) {
1478 int i;
1479 ASSERT(Netgame.local_role == LR_SERVER);
1480
1481 uint8_t data[MAX_GAME_DATA_SIZE];
1482 uint8_t cur_data[MAX_GAME_DATA_SIZE];
1483 int cur_count;
1484 int count = 0;
1485 int size_offset;
1486
1487 mprintf(0, "Sending MP_WORLD_STATES packet to player %d!\n", slot);
1488
1489 size_offset = START_DATA(MP_WORLD_STATES, data, &count);
1490
1491 // Send MOTD
1492 if (Dedicated_server && Multi_message_of_the_day[0] != 0) {
1493 cur_count = 0;
1494 MultiAddByte(WS_MOTD, cur_data, &cur_count);
1495 MultiAddString(Multi_message_of_the_day, cur_data, &cur_count);
1496 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1497 }
1498
1499 // Send weather
1500 cur_count = 0;
1501 MultiAddByte(WS_WEATHER, cur_data, &cur_count);
1502 MultiAddByte(Weather.flags, cur_data, &cur_count);
1503 MultiAddFloat(Weather.rain_intensity_scalar, cur_data, &cur_count);
1504 MultiAddFloat(Weather.snow_intensity_scalar, cur_data, &cur_count);
1505 MultiAddFloat(Weather.lightning_interval_time, cur_data, &cur_count);
1506 MultiAddShort(Weather.lightning_rand_value, cur_data, &cur_count);
1507 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1508
1509 // Send broke glass
1510 if (Num_broke_glass > 0) {
1511 for (i = 0; i < Num_broke_glass; i++) {
1512 cur_count = 0;
1513 MultiAddByte(WS_ROOM_GLASS, cur_data, &cur_count);
1514 MultiAddUshort(Broke_glass_rooms[i], cur_data, &cur_count);
1515 MultiAddUshort(Broke_glass_faces[i], cur_data, &cur_count);
1516 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1517 }
1518 }
1519
1520 // Send waypoint
1521 cur_count = 0;
1522 MultiAddByte(WS_WAYPOINT, cur_data, &cur_count);
1523 MultiAddByte(Current_waypoint, cur_data, &cur_count);
1524 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1525
1526 // Send player bit-fields
1527 cur_count = 0;
1528 MultiAddByte(WS_PLAYERBF, cur_data, &cur_count);
1529 MultiAddUint(Players_typing, cur_data, &cur_count);
1530 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1531
1532 // Do room stuff
1533 for (i = 0; i <= Highest_room_index; i++) {
1534 if (!Rooms[i].used)
1535 continue;
1536
1537 if (Rooms[i].room_change_flags & RCF_WIND) {
1538 cur_count = 0;
1539
1540 MultiAddByte(WS_ROOM_WIND, cur_data, &cur_count);
1541 MultiAddShort(i, cur_data, &cur_count);
1542
1543 MultiAddFloat(Rooms[i].wind.x, cur_data, &cur_count);
1544 MultiAddFloat(Rooms[i].wind.y, cur_data, &cur_count);
1545 MultiAddFloat(Rooms[i].wind.z, cur_data, &cur_count);
1546
1547 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1548 }
1549 if (Rooms[i].room_change_flags & RCF_FOG) {
1550 cur_count = 0;
1551
1552 MultiAddByte(WS_ROOM_FOG, cur_data, &cur_count);
1553 MultiAddShort(i, cur_data, &cur_count);
1554
1555 MultiAddFloat(Rooms[i].fog_depth, cur_data, &cur_count);
1556 MultiAddUbyte(Rooms[i].fog_r * 255.0, cur_data, &cur_count);
1557 MultiAddUbyte(Rooms[i].fog_g * 255.0, cur_data, &cur_count);
1558 MultiAddUbyte(Rooms[i].fog_b * 255.0, cur_data, &cur_count);
1559
1560 if (Rooms[i].flags & RF_FOG)
1561 MultiAddUbyte(1, cur_data, &cur_count);
1562 else
1563 MultiAddUbyte(0, cur_data, &cur_count);
1564
1565 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1566 }
1567
1568 if (Rooms[i].room_change_flags & RCF_LIGHTING) {
1569 cur_count = 0;
1570
1571 MultiAddByte(WS_ROOM_LIGHTING, cur_data, &cur_count);
1572 MultiAddShort(i, cur_data, &cur_count);
1573 MultiAddUbyte(Rooms[i].pulse_time, cur_data, &cur_count);
1574 MultiAddUbyte(Rooms[i].pulse_offset, cur_data, &cur_count);
1575
1576 if (Rooms[i].flags & RF_FLICKER)
1577 MultiAddUbyte(1, cur_data, &cur_count);
1578 else
1579 MultiAddUbyte(0, cur_data, &cur_count);
1580
1581 if (Rooms[i].flags & RF_STROBE)
1582 MultiAddUbyte(1, cur_data, &cur_count);
1583 else
1584 MultiAddUbyte(0, cur_data, &cur_count);
1585
1586 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1587 }
1588
1589 if (Rooms[i].room_change_flags & RCF_TEXTURE) {
1590 for (int t = 0; t < Rooms[i].num_faces; t++) {
1591 if (Rooms[i].faces[t].flags & FF_TEXTURE_CHANGED) {
1592 cur_count = 0;
1593 MultiAddByte(WS_ROOM_TEXTURE, cur_data, &cur_count);
1594 MultiAddShort(i, cur_data, &cur_count);
1595 MultiAddShort(t, cur_data, &cur_count);
1596 MultiAddString(GameTextures[Rooms[i].faces[t].tmap].name, cur_data, &cur_count);
1597 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1598 }
1599 }
1600 }
1601
1602 if (Rooms[i].room_change_flags & RCF_GOALSPECIAL_FLAGS) {
1603 int mask = (RF_SPECIAL1 | RF_SPECIAL2 | RF_SPECIAL3 | RF_SPECIAL4 | RF_SPECIAL5 | RF_SPECIAL6 | RF_GOAL1 |
1604 RF_GOAL2 | RF_GOAL3 | RF_GOAL4);
1605 int change_mask = (Rooms[i].flags & mask);
1606 MultiAddByte(WS_ROOM_GOALSPECFLAG, cur_data, &cur_count);
1607 MultiAddShort(i, cur_data, &cur_count);
1608 MultiAddInt(change_mask, cur_data, &cur_count);
1609 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1610 }
1611
1612 if (Rooms[i].room_change_flags & RCF_DAMAGE) {
1613 cur_count = 0;
1614 MultiAddByte(WS_ROOM_DAMAGE, cur_data, &cur_count);
1615 MultiAddShort(i, cur_data, &cur_count);
1616
1617 MultiAddFloat(Rooms[i].damage, cur_data, &cur_count);
1618 MultiAddUbyte(Rooms[i].damage_type, cur_data, &cur_count);
1619 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1620 }
1621 if (Rooms[i].room_change_flags & RCF_REFUEL) {
1622 cur_count = 0;
1623 MultiAddByte(WS_ROOM_REFUEL, cur_data, &cur_count);
1624 MultiAddShort(i, cur_data, &cur_count);
1625
1626 if (Rooms[i].flags & RF_FUELCEN)
1627 MultiAddUbyte(1, cur_data, &cur_count);
1628 else
1629 MultiAddUbyte(0, cur_data, &cur_count);
1630
1631 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1632 }
1633
1634 if (Rooms[i].room_change_flags & RCF_PORTAL_RENDER) {
1635 for (int t = 0; t < Rooms[i].num_portals; t++) {
1636 if (Rooms[i].portals[t].flags & PF_CHANGED) {
1637 cur_count = 0;
1638 MultiAddByte(WS_ROOM_PORTAL_RENDER, cur_data, &cur_count);
1639 MultiAddShort(i, cur_data, &cur_count);
1640 MultiAddShort(t, cur_data, &cur_count);
1641 MultiAddByte(Rooms[i].portals[t].flags, cur_data, &cur_count);
1642 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1643 }
1644 }
1645 }
1646
1647 if (Rooms[i].room_change_flags & RCF_PORTAL_BLOCK) {
1648 for (int t = 0; t < Rooms[i].num_portals; t++) {
1649 if (Rooms[i].portals[t].flags & PF_CHANGED) {
1650 cur_count = 0;
1651 MultiAddByte(WS_ROOM_PORTAL_BLOCK, cur_data, &cur_count);
1652 MultiAddShort(i, cur_data, &cur_count);
1653 MultiAddShort(t, cur_data, &cur_count);
1654 MultiAddByte(Rooms[i].portals[t].flags, cur_data, &cur_count);
1655 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1656 }
1657 }
1658 }
1659 }
1660
1661 // Do object stuff
1662 for (i = 0; i <= Highest_object_index; i++) {
1663 object *obj = &Objects[i];
1664
1665 if ((obj->type == OBJ_NONE) || (obj->flags & OF_DEAD))
1666 continue;
1667
1668 if (obj->type == OBJ_DOOR) {
1669 doorway *dp;
1670 dp = GetDoorwayFromObject(Objects[i].handle);
1671 cur_count = 0;
1672 MultiAddByte(WS_DOOR, cur_data, &cur_count);
1673 MultiAddShort(i, cur_data, &cur_count);
1674 MultiAddByte(dp->state, cur_data, &cur_count);
1675 MultiAddByte((dp->flags & DF_LOCKED) != 0, cur_data, &cur_count);
1676 MultiAddFloat(dp->position, cur_data, &cur_count);
1677 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1678 }
1679
1680 if (obj->change_flags & OCF_LIGHT_DISTANCE) {
1681 cur_count = 0;
1682 MultiAddByte(WS_LIGHT_DISTANCE, cur_data, &cur_count);
1683 MultiAddShort(i, cur_data, &cur_count);
1684
1685 light_info *li = ObjGetLightInfo(obj);
1686 if (li)
1687 MultiAddFloat(li->light_distance, cur_data, &cur_count);
1688 else
1689 MultiAddFloat(0, cur_data, &cur_count);
1690
1691 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1692 }
1693
1694 if (obj->change_flags & OCF_LIGHT_COLOR) {
1695 cur_count = 0;
1696 MultiAddByte(WS_LIGHT_COLOR, cur_data, &cur_count);
1697 MultiAddShort(i, cur_data, &cur_count);
1698
1699 light_info *li = ObjGetLightInfo(obj);
1700 if (li) {
1701 MultiAddFloat(li->red_light1, cur_data, &cur_count);
1702 MultiAddFloat(li->green_light1, cur_data, &cur_count);
1703 MultiAddFloat(li->blue_light1, cur_data, &cur_count);
1704 } else {
1705 MultiAddFloat(0, cur_data, &cur_count);
1706 MultiAddFloat(0, cur_data, &cur_count);
1707 MultiAddFloat(0, cur_data, &cur_count);
1708 }
1709
1710 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1711 }
1712
1713 if (obj->change_flags & OCF_NO_RENDER) {
1714 cur_count = 0;
1715 MultiAddByte(WS_NO_RENDER, cur_data, &cur_count);
1716 MultiAddShort(i, cur_data, &cur_count);
1717
1718 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1719 }
1720
1721 if (obj->change_flags & OCF_PHYS_FLAGS) {
1722 cur_count = 0;
1723 MultiAddByte(WS_OBJECT_PHYS_FLAGS, cur_data, &cur_count);
1724 MultiAddShort(i, cur_data, &cur_count);
1725 MultiAddInt(Objects[i].mtype.phys_info.flags, cur_data, &cur_count);
1726
1727 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1728 }
1729
1730 // Check to see if we have an attached object
1731 if (obj->attach_children != NULL) {
1732 cur_count = 0;
1733
1734 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
1735 MultiAddByte(WS_OBJECT_ATTACH, cur_data, &cur_count);
1736 MultiAddUshort(i, cur_data, &cur_count);
1737
1738 MultiAddByte(pm->n_attach, cur_data, &cur_count);
1739
1740 for (int t = 0; t < pm->n_attach; t++) {
1741 object *child = ObjGet(obj->attach_children[t]);
1742 if (!child)
1743 MultiAddUshort(65535, cur_data, &cur_count);
1744 else {
1745 ASSERT(child->flags & OF_ATTACHED);
1746 MultiAddUshort(OBJNUM(child), cur_data, &cur_count);
1747 MultiAddByte(child->attach_index, cur_data, &cur_count);
1748 MultiAddByte(child->attach_type, cur_data, &cur_count);
1749
1750 if (child->attach_type == AT_RAD)
1751 MultiAddFloat(child->attach_dist / child->size, cur_data, &cur_count);
1752 }
1753 }
1754
1755 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1756 }
1757 }
1758
1759 // Do spew stuff
1760 int spewcount = 0;
1761 for (i = 0; i < MAX_SPEW_EFFECTS; i++) {
1762
1763 if (SpewEffects[i].inuse) {
1764 spewcount++;
1765 cur_count = 0;
1766 spewinfo *sp = &SpewEffects[i];
1767
1768 MultiAddByte(WS_SPEW, cur_data, &cur_count);
1769 MultiAddShort(i, cur_data, &cur_count);
1770
1771 if (sp->use_gunpoint) {
1772 MultiAddByte(1, cur_data, &cur_count);
1773 MultiAddUshort(sp->gp.obj_handle & HANDLE_OBJNUM_MASK, cur_data, &cur_count);
1774 MultiAddByte(sp->gp.gunpoint, cur_data, &cur_count);
1775 } else {
1776 MultiAddByte(0, cur_data, &cur_count);
1777 // memcpy (&cur_data[cur_count],&sp->pt.normal,sizeof(vector));
1778 // cur_count+=sizeof(vector);
1779 MultiAddVector(sp->pt.normal, cur_data, &cur_count);
1780 // memcpy (&cur_data[cur_count],&sp->pt.origin,sizeof(vector));
1781 // cur_count+=sizeof(vector);
1782 MultiAddVector(sp->pt.origin, cur_data, &cur_count);
1783
1784 MultiAddInt(sp->pt.room_num, cur_data, &cur_count);
1785 }
1786
1787 MultiAddByte(sp->random, cur_data, &cur_count);
1788 MultiAddByte(sp->real_obj, cur_data, &cur_count);
1789 MultiAddByte(sp->effect_type, cur_data, &cur_count);
1790 MultiAddByte(sp->phys_info, cur_data, &cur_count);
1791 MultiAddFloat(sp->drag, cur_data, &cur_count);
1792 MultiAddFloat(sp->mass, cur_data, &cur_count);
1793 MultiAddFloat(sp->time_int, cur_data, &cur_count);
1794 MultiAddFloat(sp->longevity, cur_data, &cur_count);
1795 MultiAddFloat(sp->lifetime, cur_data, &cur_count);
1796 MultiAddFloat(sp->size, cur_data, &cur_count);
1797 MultiAddFloat(sp->speed, cur_data, &cur_count);
1798
1799 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1800 }
1801 }
1802 mprintf(0, "Send %d spew events\n", spewcount);
1803
1804 // Send buddybot handles if needed
1805 if (Netgame.flags & NF_ALLOWGUIDEBOT) {
1806 for (int i = 0; i < MAX_PLAYERS; i++) {
1807 if (Buddy_handle[i] != OBJECT_HANDLE_NONE) {
1808 MultiAddByte(WS_BUDDYBOTS, cur_data, &cur_count);
1809 MultiAddByte(i, cur_data, &cur_count);
1810 MultiAddUshort(Buddy_handle[i] & HANDLE_OBJNUM_MASK, cur_data, &cur_count);
1811 MultiStoreWorldPacket(slot, data, &count, cur_data, &cur_count, &size_offset);
1812 }
1813 }
1814 }
1815
1816 MultiAddByte(WS_END, data, &count);
1817
1818 ASSERT(count < MAX_GAME_DATA_SIZE);
1819
1820 END_DATA(count, data, size_offset);
1821
1822 // Send it out
1823 nw_SendReliable(NetPlayers[slot].reliable_socket, data, count, false);
1824 }
1825
1826 // Sends this reliable packet to everyone except the server and the named slot
1827 void MultiSendReliablyToAllExcept(int except, uint8_t *data, int size, int seq_threshold, bool urgent) {
1828 for (int i = 0; i < MAX_NET_PLAYERS; i++) {
1829 if (!(NetPlayers[i].flags & NPF_CONNECTED))
1830 continue;
1831
1832 // Don't send to these clowns
1833 if (i == except || i == Player_num)
1834 continue;
1835
1836 if (NetPlayers[i].sequence >= seq_threshold && NetPlayers[i].sequence != NETSEQ_LEVEL_END)
1837 nw_SendReliable(NetPlayers[i].reliable_socket, data, size, urgent);
1838 }
1839 }
1840
1841 // Sends this nonreliable packet to everyone except the server and the named slot
1842 void MultiSendToAllExcept(int except, uint8_t *data, int size, int seq_threshold) {
1843
1844 ASSERT(Netgame.local_role == LR_SERVER);
1845 for (int i = 0; i < MAX_NET_PLAYERS; i++) {
1846 if (!(NetPlayers[i].flags & NPF_CONNECTED))
1847 continue;
1848
1849 // Don't send to these clowns
1850 if (i == except || i == Player_num)
1851 continue;
1852
1853 if (seq_threshold == -1 || (NetPlayers[i].sequence >= seq_threshold && NetPlayers[i].sequence != NETSEQ_LEVEL_END))
1854 nw_Send(&NetPlayers[i].addr, data, size, 0);
1855 }
1856 }
1857
1858 // Flushes all receive sockets so that there is no data coming from them
1859 void MultiFlushAllIncomingBuffers() {
1860 uint8_t *data;
1861 network_address from_addr;
1862 int size;
1863
1864 data = &(Multi_receive_buffer[0]);
1865
1866 // get all incoming data and throw it away
1867 while ((size = nw_Receive(data, &from_addr)) > 0)
1868 ;
1869
1870 if (Netgame.local_role == LR_SERVER) {
1871 // Flush reliable sockets
1872 for (int i = 0; i < MAX_NET_PLAYERS; i++) {
1873 if (Player_num == i)
1874 continue;
1875
1876 if ((NetPlayers[i].flags & NPF_CONNECTED) && (NetPlayers[i].reliable_socket != INVALID_SOCKET) &&
1877 (NetPlayers[i].reliable_socket != 0)) {
1878 while ((size = nw_ReceiveReliable(NetPlayers[i].reliable_socket, data, MAX_RECEIVE_SIZE)) > 0)
1879 ;
1880 }
1881 }
1882 }
1883 }
1884
1885 // Checks to see if its any powerups need repositioning on the client machine
1886 void MultiCheckToRepositionPowerups() {
1887 int i;
1888 static int invis_id = -2;
1889
1890 if (invis_id == -2) {
1891 invis_id = FindObjectIDName("InvisiblePowerup");
1892 }
1893
1894 int changed = 0;
1895
1896 for (i = 0; i <= Highest_object_index; i++) {
1897 object *obj = &Objects[i];
1898 if (obj->type != OBJ_POWERUP)
1899 continue;
1900
1901 if (obj->id == invis_id)
1902 continue;
1903
1904 if (obj->flags & OF_DEAD)
1905 continue;
1906
1907 if (obj->flags & OF_STOPPED_THIS_FRAME) {
1908 // Send out our positional change
1909 int count = 0;
1910 uint8_t data[MAX_GAME_DATA_SIZE];
1911 int size = START_DATA(MP_POWERUP_REPOSITION, data, &count);
1912 changed++;
1913
1914 MultiAddUshort(i, data, &count);
1915 // memcpy (&data[count],&obj->pos,sizeof(vector));
1916 // count+=sizeof(vector);
1917 MultiAddVector(obj->pos, data, &count);
1918
1919 // Send room number and terrain flag
1920 MultiAddUshort(CELLNUM(obj->roomnum), data, &count);
1921
1922 if (OBJECT_OUTSIDE(obj))
1923 MultiAddByte(1, data, &count);
1924 else
1925 MultiAddByte(0, data, &count);
1926
1927 END_DATA(count, data, size);
1928 MultiSendReliablyToAllExcept(Player_num, data, count, NETSEQ_OBJECTS, false);
1929 }
1930 }
1931
1932 if (changed > 0) {
1933 mprintf(0, "Stopped=%d\n", changed);
1934 }
1935 }
1936
1937 // Checks to see if its time to respawn any powerups
1938 void MultiCheckToRespawnPowerups() {
1939 int i, t;
1940
1941 for (i = 0; i < Num_powerup_timer; i++) {
1942 if (Powerup_timer[i].respawn_time < Gametime) {
1943 // This powerup needs to be respawned, so find a vacant spot
1944 int num_cand = 0;
1945 int candidates[MAX_RESPAWNS];
1946
1947 for (t = 0; t < Num_powerup_respawn; t++) {
1948 if (Powerup_respawn[t].used == 0) {
1949 if (Netgame.flags & NF_RANDOMIZE_RESPAWN) {
1950 // We found a spot!
1951 candidates[num_cand++] = t;
1952 } else {
1953 if (Powerup_timer[i].id == Powerup_respawn[t].original_id)
1954 candidates[num_cand++] = t;
1955 }
1956 }
1957 }
1958
1959 if (num_cand == 0) {
1960 mprintf(0, "Couldn't find a good spot to respawn!!!\n");
1961 return;
1962 }
1963
1964 t = candidates[ps_rand() % num_cand];
1965
1966 mprintf(0, "Respawning powerup with id of %d.\n", Powerup_timer[i].id);
1967 Powerup_respawn[t].used = 1;
1968 int objnum =
1969 ObjCreate(OBJ_POWERUP, Powerup_timer[i].id, Powerup_respawn[t].roomnum, &Powerup_respawn[t].pos, NULL);
1970 if (objnum > -1)
1971 MultiSendObject(&Objects[objnum], 1);
1972
1973 InitObjectScripts(&Objects[objnum]);
1974 Powerup_respawn[t].objnum = objnum;
1975
1976 // Now erase this powerup from our timer info
1977 for (t = i; t < Num_powerup_timer - 1; t++)
1978 Powerup_timer[t] = Powerup_timer[t + 1];
1979
1980 Num_powerup_timer--;
1981 i--;
1982 }
1983 }
1984 }
1985
1986 extern int Multi_occluded;
1987 // Sends out positional updates based on clients pps
1988 void MultiSendPositionalUpdates(int to_slot) {
1989 uint8_t data[MAX_GAME_DATA_SIZE];
1990
1991 // Figure out if we should send positional updates to players
1992 for (int i = 0; i < MAX_PLAYERS; i++) {
1993 uint8_t send_position = 0, timer_popped = 0;
1994
1995 if ((Netgame.flags & NF_PEER_PEER) && i != Player_num)
1996 continue; // Don't send out other positions if in peer mode
1997
1998 // Don't send out position of the server in dedicated mode.
1999 if (Dedicated_server && (i == Player_num))
2000 continue;
2001
2002 // Don't send out position of dead players (ghosts) or observers
2003 if (Objects[Players[i].objnum].type != OBJ_PLAYER)
2004 continue;
2005
2006 if (i != Player_num)
2007 Multi_last_sent_time[to_slot][i] += Frametime;
2008
2009 if (i == to_slot)
2010 continue;
2011
2012 if (!(NetPlayers[i].flags & NPF_CONNECTED)) {
2013 Multi_visible_players[i] &= ~(1 << to_slot);
2014 Multi_visible_players[to_slot] &= ~(1 << i);
2015 continue;
2016 }
2017
2018 if (NetPlayers[i].sequence != NETSEQ_PLAYING) {
2019 Multi_visible_players[i] &= ~(1 << to_slot);
2020 Multi_visible_players[to_slot] &= ~(1 << i);
2021 continue;
2022 }
2023
2024 // Find out the player priority of this player in relation to the to_slot player
2025 float pps_check_time = NetPlayers[to_slot].pps;
2026
2027 /*if (pps_check_time>=8)
2028 {
2029 vector subvec=Objects[Players[i].objnum].pos-Objects[Players[to_slot].objnum].pos;
2030 float mag=vm_GetMagnitudeFast (&subvec);
2031 subvec/=mag;
2032
2033 float dp=vm_DotProduct (&subvec,&Objects[Players[to_slot].objnum].orient.fvec);
2034 dp=fabs(dp);
2035
2036 pps_check_time=((float)pps_check_time/2.0)+((dp*((float)pps_check_time/2.0))+(dp*.9));
2037 pps_check_time=std::clamp(pps_check_time,6.0,20.0);
2038
2039 int pps=pps_check_time;
2040 pps_check_time=pps;
2041 }*/
2042
2043 // Set the timer as popped if it actually did
2044 if (Multi_last_sent_time[to_slot][i] > (1.0 / pps_check_time))
2045 timer_popped = 1;
2046
2047 // if (i==Player_num) // Always send server position
2048 // send_position=1;
2049
2050 int num_src_to_check = 1;
2051 int srcs[10];
2052 srcs[0] = Players[to_slot].objnum;
2053
2054 // Check for guideds
2055 if (Players[to_slot].guided_obj != NULL)
2056 srcs[num_src_to_check++] = Players[to_slot].guided_obj - Objects;
2057
2058 if (Players[to_slot].small_dll_obj != -1) {
2059 int objnum = Players[to_slot].small_dll_obj;
2060 if (Objects[objnum].flags & OF_DEAD || Objects[objnum].type == OBJ_NONE) {
2061 Players[to_slot].small_dll_obj = -1;
2062 } else {
2063 srcs[num_src_to_check] = objnum;
2064 num_src_to_check++;
2065 }
2066 }
2067
2068 // See if there are any small views we need to send for
2069 // Do left view
2070 if (Players[to_slot].small_left_obj != -1) {
2071 int objnum = Players[to_slot].small_left_obj;
2072 if (Objects[objnum].flags & OF_DEAD || Objects[objnum].type == OBJ_NONE || Objects[objnum].type == OBJ_WEAPON) {
2073 Players[to_slot].small_left_obj = -1;
2074 } else {
2075 srcs[num_src_to_check] = objnum;
2076 num_src_to_check++;
2077 }
2078 }
2079
2080 // Do right view
2081 if (Players[to_slot].small_right_obj != -1) {
2082 int objnum = Players[to_slot].small_right_obj;
2083 if ((Objects[objnum].flags & OF_DEAD) || (Objects[objnum].type == OBJ_NONE) ||
2084 (Objects[objnum].type == OBJ_WEAPON)) {
2085 Players[to_slot].small_right_obj = -1;
2086 } else {
2087 srcs[num_src_to_check] = objnum;
2088 num_src_to_check++;
2089 }
2090 }
2091
2092 for (int t = 0; t < num_src_to_check; t++) {
2093 int room_a = Objects[srcs[t]].roomnum;
2094 int room_b = Objects[Players[i].objnum].roomnum;
2095
2096 if (BOA_IsVisible(room_a, room_b))
2097 send_position = 1;
2098
2099 if (Player_fire_packet[i].fired_on_this_frame != PFP_NO_FIRED) {
2100 timer_popped = 1;
2101
2102 if (Player_fire_packet[i].wb_index >= SECONDARY_INDEX) {
2103 send_position = 1;
2104 } else {
2105 if (BOA_IsVisible(Player_fire_packet[i].dest_roomnum, Objects[srcs[t]].roomnum))
2106 send_position = 1;
2107 }
2108 }
2109 }
2110
2111 // Always send the server position, because we always say that the server is visible. This
2112 // should fix some or all ghost ship problems.
2113 if (i == Player_num) {
2114 send_position = 1;
2115 }
2116
2117 if (timer_popped) {
2118 if (i != Player_num)
2119 Multi_last_sent_time[to_slot][i] = 0;
2120
2121 if (send_position) {
2122 Multi_visible_players[to_slot] |= (1 << i);
2123
2124 int count = MultiStuffPosition(i, data);
2125 NetPlayers[to_slot].total_bytes_sent += count;
2126
2127 int add_count = 0;
2128 // Send firing if needed
2129 if (Player_fire_packet[i].fired_on_this_frame == PFP_FIRED)
2130 add_count = MultiStuffPlayerFire(i, &data[count]);
2131 count += add_count;
2132 // Add in guided stuff
2133 if (Players[i].guided_obj != NULL) {
2134 add_count = MultiStuffGuidedInfo(i, &data[count]);
2135 count += add_count;
2136 }
2137
2138 ASSERT(count < MAX_GAME_DATA_SIZE);
2139
2140 nw_Send(&NetPlayers[to_slot].addr, data, count, 0);
2141
2142 // TODO: SEND RELIABLE WEAPON FIRE HERE
2143 if (Player_fire_packet[i].fired_on_this_frame == PFP_FIRED_RELIABLE) {
2144 // mprintf(0,"NEED TO SEND RELIABLE FIRE FOR %d\n",i);
2145 count = MultiStuffPlayerFire(i, data);
2146 nw_SendReliable(NetPlayers[to_slot].reliable_socket, data, count, true);
2147 }
2148 } else {
2149 Multi_visible_players[to_slot] &= ~(1 << i);
2150 Multi_occluded++;
2151 }
2152 }
2153 }
2154 }
2155
2156 // Figures out which robots have moved since the last time this player slot was updated
2157 void MultiUpdateRobotMovedList(int slot) {
2158 bool skip_this_obj = false;
2159 // check for moved robots
2160 for (int a = 0; a <= Highest_object_index; a++) {
2161 object *obj = &Objects[a];
2162 if (obj->type == OBJ_NONE)
2163 continue;
2164
2165 if (MultiIsValidMovedObject(obj)) {
2166 if ((obj->flags & OF_MOVED_THIS_FRAME)) {
2167 skip_this_obj = false;
2168 // mprintf(0,"Object %d (type=%d) moved this frame!\n",a,obj->type);
2169 // Check to see if this robot is on the list already
2170 for (int b = 0; b < Num_moved_robots[slot] && !skip_this_obj; b++) {
2171 if (Moved_robots[slot][b] == obj->handle) {
2172 skip_this_obj = true;
2173 }
2174 }
2175
2176 if (!skip_this_obj) {
2177 Moved_robots[slot][Num_moved_robots[slot]] = obj->handle;
2178 ASSERT(Objects[a].flags & OF_CLIENT_KNOWS);
2179 Num_moved_robots[slot]++;
2180 }
2181 }
2182 }
2183 }
2184 }
2185
2186 // Sets up our data structures so that the nonvisible robots will be sent when they are needed
2187 void MultiSetupNonVisRobots(int slot, object *obj) { obj->generic_nonvis_flags |= (1 << slot); }
2188
2189 // Sends out a list of generics that this client can't see
2190 void MultiSendGenericNonVis(int slot, uint16_t *objarray, int num) {
2191 uint8_t data[MAX_GAME_DATA_SIZE];
2192 int count = 0;
2193 int size_offset;
2194
2195 size_offset = START_DATA(MP_GENERIC_NONVIS, data, &count);
2196
2197 MultiAddShort(num, data, &count);
2198
2199 for (int i = 0; i < num; i++) {
2200 MultiAddUshort(objarray[i], data, &count);
2201 }
2202
2203 ASSERT(count < MAX_GAME_DATA_SIZE);
2204
2205 END_DATA(count, data, size_offset);
2206
2207 // Send it out
2208 nw_SendReliable(NetPlayers[slot].reliable_socket, data, count);
2209 }
2210
2211 // Does the bookkeeping that needs to be done for non visible generic objects and a player
2212 void MultiDoNonVisGenericForSlot(int slot) {
2213 // Keep track of the ones we need to send about
2214 uint16_t send_out[MAX_OBJECTS];
2215 int num_to_send = 0;
2216 int i;
2217
2218 // Deal with non-vis objects
2219 for (i = 0; i <= Highest_object_index; i++) {
2220 int objnum = i;
2221 object *obj = &Objects[i];
2222 if (obj->type == OBJ_NONE)
2223 continue;
2224
2225 if (obj->generic_nonvis_flags & (1 << slot)) {
2226 if (MultiIsValidMovedObject(obj)) {
2227 if (!(obj->generic_sent_nonvis & (1 << slot))) {
2228 ASSERT(obj->flags & OF_CLIENT_KNOWS);
2229 send_out[num_to_send++] = objnum;
2230 obj->generic_sent_nonvis |= (1 << slot);
2231 }
2232
2233 bool skip_this_obj = false;
2234 int b;
2235
2236 // For movement, Check to see if this robot is on the list already
2237 for (b = 0; b < Num_moved_robots[slot] && !skip_this_obj; b++) {
2238 if (Moved_robots[slot][b] == obj->handle) {
2239 skip_this_obj = true;
2240 }
2241 }
2242
2243 if (!skip_this_obj) {
2244 if (Num_moved_robots[slot] < MAX_CHANGED_OBJECTS) {
2245 Moved_robots[slot][Num_moved_robots[slot]] = obj->handle;
2246 ASSERT(Objects[i].flags & OF_CLIENT_KNOWS);
2247 Num_moved_robots[slot]++;
2248 }
2249 }
2250
2251 // Now do changed anim
2252 skip_this_obj = false;
2253 // Check to see if this robot is on the list already
2254 for (b = 0; b < Num_changed_anim[slot]; b++) {
2255 if (Changed_anim[b][slot] == Objects[objnum].handle) {
2256 skip_this_obj = true;
2257 break;
2258 }
2259 }
2260 if (skip_this_obj == false) {
2261 if (Num_changed_anim[slot] < MAX_CHANGED_OBJECTS) {
2262 Changed_anim[Num_changed_anim[slot]][slot] = Objects[objnum].handle;
2263 ASSERT(Objects[i].flags & OF_CLIENT_KNOWS);
2264 Num_changed_anim[slot]++;
2265 }
2266 }
2267
2268 // Now do changed turrets
2269 skip_this_obj = false;
2270 // Check to see if this robot is on the list already
2271 for (b = 0; b < Num_changed_turret[slot]; b++) {
2272 if (Changed_turret[b][slot] == Objects[objnum].handle) {
2273 skip_this_obj = true;
2274 break;
2275 }
2276 }
2277 if (skip_this_obj == false) {
2278 if (Num_changed_turret[slot] < MAX_CHANGED_OBJECTS) {
2279 Changed_turret[Num_changed_turret[slot]][slot] = Objects[objnum].handle;
2280 ASSERT(Objects[i].flags & OF_CLIENT_KNOWS);
2281 Num_changed_turret[slot]++;
2282 }
2283 }
2284
2285 // Finally, do WB anim
2286 skip_this_obj = false;
2287 // Check to see if this robot is on the list already
2288 for (b = 0; b < Num_changed_wb_anim[slot]; b++) {
2289 if (Changed_wb_anim[b][slot] == Objects[objnum].handle) {
2290 skip_this_obj = true;
2291 break;
2292 }
2293 }
2294 if (skip_this_obj == false) {
2295 if (Num_changed_wb_anim[slot] < MAX_CHANGED_OBJECTS) {
2296 Changed_wb_anim[Num_changed_wb_anim[slot]][slot] = Objects[objnum].handle;
2297 ASSERT(Objects[i].flags & OF_CLIENT_KNOWS);
2298 Num_changed_wb_anim[slot]++;
2299 }
2300 }
2301 }
2302
2303 // Clear our flag
2304 obj->generic_nonvis_flags &= ~(1 << slot);
2305 }
2306 }
2307
2308 // Now send out what we need to
2309 if (num_to_send > 0) {
2310 int num_packets = num_to_send / 100;
2311 int num_left = num_to_send % 100;
2312
2313 for (i = 0; i < num_packets; i++)
2314 MultiSendGenericNonVis(slot, &send_out[i * 100], 100);
2315
2316 // Send out leftovers
2317 if (num_left > 0)
2318 MultiSendGenericNonVis(slot, &send_out[num_packets * 100], num_left);
2319 }
2320 }
2321
2322 // Returns true or false based on whether or not the player can see this generic object in a multiplayer game
2323 // This takes into account markers, dll objects, and other stuff
2324 bool MultiIsGenericVisibleToPlayer(int test_objnum, int to_slot) {
2325 int srcs[10], num_src_to_check = 1;
2326
2327 srcs[0] = Players[to_slot].objnum;
2328
2329 if (Players[to_slot].guided_obj != NULL)
2330 srcs[num_src_to_check++] = Players[to_slot].guided_obj - Objects;
2331
2332 if (Players[to_slot].small_dll_obj != -1) {
2333 int objnum = Players[to_slot].small_dll_obj;
2334 if (Objects[objnum].flags & OF_DEAD || Objects[objnum].type == OBJ_NONE) {
2335 Players[to_slot].small_dll_obj = -1;
2336 } else {
2337 srcs[num_src_to_check] = objnum;
2338 num_src_to_check++;
2339 }
2340 }
2341 // See if there are any small views we need to send for
2342 // Do left view
2343 if (Players[to_slot].small_left_obj != -1) {
2344 int objnum = Players[to_slot].small_left_obj;
2345 if (Objects[objnum].flags & OF_DEAD || Objects[objnum].type == OBJ_NONE || Objects[objnum].type == OBJ_WEAPON) {
2346 Players[to_slot].small_left_obj = -1;
2347 } else {
2348 srcs[num_src_to_check] = objnum;
2349 num_src_to_check++;
2350 }
2351 }
2352
2353 // Do right view
2354 if (Players[to_slot].small_right_obj != -1) {
2355 int objnum = Players[to_slot].small_right_obj;
2356 if (Objects[objnum].flags & OF_DEAD || Objects[objnum].type == OBJ_NONE || Objects[objnum].type == OBJ_WEAPON) {
2357 Players[to_slot].small_right_obj = -1;
2358 } else {
2359 srcs[num_src_to_check] = objnum;
2360 num_src_to_check++;
2361 }
2362 }
2363
2364 for (int t = 0; t < num_src_to_check; t++) {
2365 if (BOA_IsVisible(Objects[test_objnum].roomnum, Objects[srcs[t]].roomnum))
2366 return true;
2367 }
2368
2369 return false;
2370 }
2371
2372 // Does robot stuff for a particular client
2373 void MultiDoServerRobotFrame(int slot) {
2374 int rcount = 0;
2375 int m = 0;
2376 uint8_t rdata[MAX_GAME_DATA_SIZE];
2377
2378 // send robot information for any robots that have moved.
2379 for (m = 0; m < Num_moved_robots[slot]; m++) {
2380 int send_position = 0;
2381 int objnum = Moved_robots[slot][m] & HANDLE_OBJNUM_MASK;
2382
2383 if (Moved_robots[slot][m] != Objects[objnum].handle) {
2384 mprintf(0, "Caught handle objnum problem!\n");
2385 continue;
2386 }
2387
2388 if (MultiIsGenericVisibleToPlayer(objnum, slot))
2389 send_position = 1;
2390
2391 if (send_position) {
2392 rcount = MultiStuffRobotPosition(objnum, rdata);
2393 if (rcount > 0) {
2394 if (Multi_send_size[slot] + rcount >= MAX_GAME_DATA_SIZE)
2395 MultiSendFullPacket(slot, 0);
2396 memcpy(&Multi_send_buffer[slot][Multi_send_size[slot]], rdata, rcount);
2397 Multi_send_size[slot] += rcount;
2398 }
2399
2400 Objects[objnum].generic_sent_nonvis &= ~(1 << slot);
2401 } else
2402 MultiSetupNonVisRobots(slot, &Objects[objnum]);
2403 }
2404
2405 // clear the robot movement flag for this player
2406 Num_moved_robots[slot] = 0;
2407
2408 // Now do anim changed stuff
2409 for (m = 0; m < Num_changed_anim[slot]; m++) {
2410 int send_position = 0;
2411 int objnum = Changed_anim[m][slot] & HANDLE_OBJNUM_MASK;
2412
2413 object *obj = &Objects[objnum];
2414
2415 if (Changed_anim[m][slot] != obj->handle) {
2416 mprintf(0, "Caught anim handle objnum problem!\n");
2417 continue;
2418 }
2419
2420 if (MultiIsGenericVisibleToPlayer(objnum, slot))
2421 send_position = 1;
2422
2423 if (send_position) {
2424 rcount = MultiStuffObjAnimUpdate(objnum, rdata);
2425 if (Multi_send_size[slot] + rcount >= MAX_GAME_DATA_SIZE)
2426 MultiSendFullPacket(slot, 0);
2427 memcpy(&Multi_send_buffer[slot][Multi_send_size[slot]], rdata, rcount);
2428 Multi_send_size[slot] += rcount;
2429 Objects[objnum].generic_sent_nonvis &= ~(1 << slot);
2430 } else
2431 MultiSetupNonVisRobots(slot, &Objects[objnum]);
2432 }
2433 // Clear changed animation for this player
2434 Num_changed_anim[slot] = 0;
2435
2436 // Now send any changed turrets
2437 for (m = 0; m < Num_changed_turret[slot]; m++) {
2438 int send_position = 0;
2439 int objnum = Changed_turret[m][slot] & HANDLE_OBJNUM_MASK;
2440
2441 object *obj = &Objects[objnum];
2442
2443 if (Changed_turret[m][slot] != obj->handle) {
2444 mprintf(0, "Caught turret handle objnum problem!\n");
2445 continue;
2446 }
2447
2448 if (MultiIsGenericVisibleToPlayer(objnum, slot))
2449 send_position = 1;
2450
2451 if (send_position) {
2452 rcount = MultiStuffTurretUpdate(objnum, rdata);
2453 if (Multi_send_size[slot] + rcount >= MAX_GAME_DATA_SIZE)
2454 MultiSendFullPacket(slot, 0);
2455 memcpy(&Multi_send_buffer[slot][Multi_send_size[slot]], rdata, rcount);
2456 Multi_send_size[slot] += rcount;
2457 Objects[objnum].generic_sent_nonvis &= ~(1 << slot);
2458 } else
2459 MultiSetupNonVisRobots(slot, &Objects[objnum]);
2460 }
2461 // Clear changed turret flag for this player
2462 Num_changed_turret[slot] = 0;
2463
2464 // Next is weapon battery animation states
2465 for (m = 0; m < Num_changed_wb_anim[slot]; m++) {
2466 int send_position = 0;
2467 int objnum = Changed_wb_anim[m][slot] & HANDLE_OBJNUM_MASK;
2468
2469 object *obj = &Objects[objnum];
2470
2471 if (Changed_wb_anim[m][slot] != obj->handle) {
2472 mprintf(0, "Caught wb anim handle objnum problem!\n");
2473 continue;
2474 }
2475
2476 if (MultiIsGenericVisibleToPlayer(objnum, slot))
2477 send_position = 1;
2478
2479 if (send_position) {
2480 rcount = MultiStuffObjWBAnimUpdate(objnum, rdata);
2481 if (Multi_send_size[slot] + rcount >= MAX_GAME_DATA_SIZE)
2482 MultiSendFullPacket(slot, 0);
2483 memcpy(&Multi_send_buffer[slot][Multi_send_size[slot]], rdata, rcount);
2484 Multi_send_size[slot] += rcount;
2485 Objects[objnum].generic_sent_nonvis &= ~(1 << slot);
2486 } else
2487 MultiSetupNonVisRobots(slot, &Objects[objnum]);
2488 }
2489
2490 Num_changed_wb_anim[slot] = 0;
2491
2492 MultiSendFullPacket(slot, 0);
2493
2494 MultiDoNonVisGenericForSlot(slot);
2495 }
2496
2497 // Shoots a straight vector from all the firing players and finds out what room
2498 // they hit. This is useful for vis stuff
2499
2500 void MultiGetDestFireRooms() {
2501 for (int i = 0; i < MAX_PLAYERS; i++) {
2502 if (!(NetPlayers[i].flags & NPF_CONNECTED))
2503 continue;
2504 if (NetPlayers[i].flags != NETSEQ_PLAYING)
2505 continue;
2506
2507 if (Player_fire_packet[i].fired_on_this_frame == PFP_NO_FIRED)
2508 continue;
2509
2510 // Ok, so now we know the player has fired, so just cast a ray and see where it hits
2511 fvi_query fq;
2512 fvi_info hit_data;
2513 int fate;
2514
2515 vector dest_vector = Objects[Players[i].objnum].pos + (Objects[Players[i].objnum].orient.fvec * 50000);
2516
2517 fq.p0 = &Objects[Players[i].objnum].pos;
2518 fq.startroom = Objects[Players[i].objnum].roomnum;
2519 fq.p1 = &dest_vector;
2520 fq.rad = .05f;
2521 fq.thisobjnum = -1;
2522 fq.ignore_obj_list = NULL;
2523 fq.flags = FQ_IGNORE_NON_LIGHTMAP_OBJECTS | FQ_CHECK_OBJS;
2524
2525 fate = fvi_FindIntersection(&fq, &hit_data);
2526
2527 if (hit_data.hit_room < 0)
2528 Player_fire_packet[i].dest_roomnum = hit_data.hit_room;
2529 else {
2530 if (hit_data.hit_room > Highest_room_index)
2531 Player_fire_packet[i].dest_roomnum = Highest_room_index + 1;
2532 else {
2533 if (Rooms[hit_data.hit_room].flags & RF_EXTERNAL) {
2534 int roomnum = GetTerrainRoomFromPos(&hit_data.hit_pnt);
2535 Player_fire_packet[i].dest_roomnum = roomnum;
2536 } else
2537 Player_fire_packet[i].dest_roomnum = hit_data.hit_room;
2538 }
2539 }
2540 }
2541 }
2542
2543 int Multi_occluded = 0;
2544 #define MT_DATA_UPDATE_INTERVAL 300 // 300 seconds
2545 // Does whatever the server needs to do for this frame
2546 void MultiDoServerFrame() {
2547 int i;
2548
2549 if (Netgame.flags & NF_EXIT_NOW) {
2550 // DLL says we should bail!
2551 Netgame.flags &= ~NF_EXIT_NOW;
2552 MultiLeaveGame();
2553 SetFunctionMode(MENU_MODE);
2554 return;
2555 }
2556
2557 if (NetPlayers[Player_num].sequence == NETSEQ_PREGAME) {
2558 MultiFlushAllIncomingBuffers();
2559 NetPlayers[Player_num].sequence = NETSEQ_PLAYING;
2560
2561 Players[Player_num].time_in_game = timer_GetTime();
2562
2563 // Pick a start slot for me
2564 Players[Player_num].start_index = PlayerGetRandomStartPosition(Player_num);
2565 PlayerMoveToStartPos(Player_num, Players[Player_num].start_index);
2566
2567 // Update the tracker if it's a master tracker game
2568 CallMultiDLL(MT_EVT_FIRST_FRAME);
2569 if (Game_is_master_tracker_game) {
2570 NetPlayers[Player_num].flags |= NPF_MT_READING_PILOT;
2571 strcpy(MTPilotinfo[Player_num].tracker_id, Players[Player_num].tracker_id);
2572 strcpy(MTPilotinfo[Player_num].pilot_name, Players[Player_num].callsign);
2573 NetPlayers[Player_num].flags &= ~NPF_WROTE_RANK;
2574 }
2575
2576 if (Dedicated_server) // Make this player an observer
2577 PlayerSwitchToObserver(Player_num, OBSERVER_MODE_ROAM);
2578
2579 for (int i = 0; i < MAX_PLAYERS; i++)
2580 Multi_visible_players[i] = 0xFFFFFFFF;
2581 }
2582
2583 if (Game_is_master_tracker_game) {
2584 CallMultiDLL(MT_EVT_FRAME);
2585 }
2586
2587 // Check for new connections
2588 MultiCheckListen();
2589 MultiDisconnectDeadPlayers();
2590 MultiCheckToRespawnPowerups();
2591 MultiCheckToRepositionPowerups();
2592
2593 // get the other net players data
2594 MultiProcessIncoming();
2595
2596 // Find destination room if they fired
2597 MultiGetDestFireRooms();
2598
2599 Player_count = 1;
2600
2601 // Send out data
2602 for (i = 0; i < MAX_NET_PLAYERS; i++) {
2603
2604 // Check to see if we should send rank to everyone
2605 if (Game_is_master_tracker_game) {
2606 if (NetPlayers[i].flags & NPF_CONNECTED) {
2607 if (Players[i].rank != -1) {
2608 if (!(NetPlayers[i].flags & NPF_WROTE_RANK)) {
2609 MultiSendInitialRank(i);
2610 NetPlayers[i].flags |= NPF_WROTE_RANK;
2611 }
2612 }
2613 }
2614 }
2615
2616 if (i == Player_num) {
2617
2618 // START: code to fix clients not hearing server omega damage
2619 // Note the code here to keep us from sending the damage every frame. Since NetPlayers[].pps doesn't
2620 // seem to be defined for the server/player, I've hard-coded it to 12 fps.
2621 Multi_last_sent_time[i][Player_num] += Frametime;
2622 float last_client_update = Multi_last_sent_time[i][Player_num];
2623
2624 // Send other info if the timer has popped
2625 if (last_client_update > (1.0 / 12.0)) // was: (float)NetPlayers[i].pps
2626 {
2627 // Check to see if there is additional damage or shields to be added to this guy
2628 if (Multi_additional_damage[i] != 0) {
2629 if (!(Players[i].flags & (PLAYER_FLAGS_DYING | PLAYER_FLAGS_DEAD)))
2630 MultiSendAdditionalDamage(i, Multi_additional_damage_type[i], Multi_additional_damage[i]);
2631 Multi_additional_damage[i] = 0;
2632 Multi_additional_damage_type[i] = PD_NONE;
2633 }
2634
2635 Multi_last_sent_time[i][Player_num] = 0;
2636 }
2637 // END: code to fix clients not hearing server omega damage
2638
2639 continue;
2640 }
2641
2642 if (NetPlayers[i].flags & NPF_CONNECTED) {
2643 // Check to see if this guy as any special requests
2644 if (NetPlayers[i].sequence == NETSEQ_REQUEST_PLAYERS) {
2645 for (int t = 0; t < MAX_NET_PLAYERS; t++) {
2646 if (NetPlayers[t].flags & NPF_CONNECTED) {
2647 MultiSendPlayer(i, t);
2648 }
2649 }
2650
2651 NetPlayers[i].sequence = NETSEQ_PLAYERS;
2652 MultiSendDonePlayers(i);
2653 }
2654
2655 if (NetPlayers[i].sequence == NETSEQ_REQUEST_BUILDINGS) {
2656 MultiSendBuildings(i);
2657 NetPlayers[i].sequence = NETSEQ_BUILDINGS;
2658 MultiSendDoneBuildings(i);
2659 }
2660 if (NetPlayers[i].sequence == NETSEQ_REQUEST_OBJECTS) {
2661 MultiSendJoinObjects(i);
2662 NetPlayers[i].sequence = NETSEQ_OBJECTS;
2663 MultiSendDoneObjects(i);
2664 }
2665 if (NetPlayers[i].sequence == NETSEQ_REQUEST_WORLD) {
2666 MultiSendWorldStates(i);
2667 NetPlayers[i].sequence = NETSEQ_WORLD;
2668 MultiSendDoneWorldStates(i);
2669 } else if (NetPlayers[i].sequence == NETSEQ_PLAYING) {
2670 Player_count++;
2671
2672 if ((!(Netgame.flags & NF_PEER_PEER)) &&
2673 ((timer_GetTime() - NetPlayers[i].last_ping_time) > MULTI_PING_INTERVAL)) {
2674 MultiSendPing(i);
2675 }
2676 // If we actually want to send custom data out..
2677 if (Use_file_xfer) {
2678 // Check to see if we need to request any files from this bozo
2679 if ((NetPlayers[i].custom_file_seq != NETFILE_ID_NOFILE) &&
2680 (NetPlayers[i].custom_file_seq != NETFILE_ID_DONE)) {
2681 // See if we are already sending or receiving a file from this player
2682 if (NetPlayers[i].file_xfer_flags == NETFILE_NONE) {
2683 // Check to see if we have this file in our cache
2684 // Time to ask for the file specified in the sequence
2685 MultiAskForFile(NetPlayers[i].custom_file_seq, i, i);
2686 }
2687 }
2688 }
2689
2690 // Send the client how much data we have sent him so far.
2691 if ((timer_GetTime() - last_sent_bytes[i]) > 5) {
2692 MultiSendBytesSent(i);
2693 last_sent_bytes[i] = timer_GetTime();
2694 }
2695
2696 if ((timer_GetTime() - Multi_last_send_visible[i]) > .5) {
2697 Multi_last_send_visible[i] = timer_GetTime();
2698 MultiSendVisiblePlayers(i);
2699 }
2700
2701 // Figure out which robots moved this frame
2702 MultiUpdateRobotMovedList(i);
2703
2704 Multi_last_sent_time[i][Player_num] += Frametime;
2705 float last_client_update = Multi_last_sent_time[i][Player_num];
2706
2707 // Send out all players movement
2708 MultiSendPositionalUpdates(i);
2709
2710 // Send other info if the timer has popped
2711 if (last_client_update > (1.0 / (float)NetPlayers[i].pps)) {
2712 // Send out robot updates if needed
2713 MultiDoServerRobotFrame(i);
2714
2715 // Check to see if there is additional damage or shields to be added to this guy
2716 if (Multi_additional_damage[i] != 0) {
2717 if (!(Players[i].flags & (PLAYER_FLAGS_DYING | PLAYER_FLAGS_DEAD)))
2718 MultiSendAdditionalDamage(i, Multi_additional_damage_type[i], Multi_additional_damage[i]);
2719 Multi_additional_damage[i] = 0;
2720 Multi_additional_damage_type[i] = PD_NONE;
2721 }
2722
2723 Multi_last_sent_time[i][Player_num] = 0;
2724 }
2725 }
2726 }
2727 }
2728
2729 for (i = 0; i < MAX_NET_PLAYERS; i++)
2730 Player_fire_packet[i].fired_on_this_frame = PFP_NO_FIRED;
2731
2732 mprintf_at(2, 5, 0, "Occ=%d ", Multi_occluded);
2733
2734 // If this is a dedicated server then we should get possible input
2735 if (Dedicated_server)
2736 DoDedicatedServerFrame();
2737 }
2738
2739 // Tells clients to execute dlls on their machines
2740 void MultiSendClientExecuteDLL(int eventnum, int me_objnum, int it_objnum, int to, dllinfo *info) {
2741 uint8_t data[MAX_GAME_DATA_SIZE];
2742 int count = 0;
2743 int size_offset;
2744
2745 ASSERT(to >= -1 && to < MAX_PLAYERS);
2746
2747 if (to == Player_num)
2748 return;
2749 // mprintf(0,"Sending MP_EXECUTE_DLL packet to slot %d! type=%d
2750 // id=%d\n",to,Objects[me_objnum].type,Objects[me_objnum].id);
2751
2752 size_offset = START_DATA(MP_EXECUTE_DLL, data, &count, 1);
2753
2754 MultiAddShort(eventnum, data, &count);
2755 MultiAddShort(me_objnum, data, &count);
2756 MultiAddShort(it_objnum, data, &count);
2757
2758 if (info) {
2759 MultiAddByte(1, data, &count);
2760 MultiAddFloat(info->fParam, data, &count);
2761 MultiAddInt(info->iParam, data, &count);
2762
2763 switch (eventnum) {
2764 case EVT_CLIENT_GAMEWALLCOLLIDE:
2765 case EVT_GAMEWALLCOLLIDE:
2766 MultiAddFloat(info->collide_info.point.x, data, &count);
2767 MultiAddFloat(info->collide_info.point.y, data, &count);
2768 MultiAddFloat(info->collide_info.point.z, data, &count);
2769 MultiAddFloat(info->collide_info.normal.x, data, &count);
2770 MultiAddFloat(info->collide_info.normal.y, data, &count);
2771 MultiAddFloat(info->collide_info.normal.z, data, &count);
2772 MultiAddFloat(info->collide_info.hitspeed, data, &count);
2773 MultiAddFloat(info->collide_info.hit_dot, data, &count);
2774 MultiAddInt(info->collide_info.hitseg, data, &count);
2775 MultiAddInt(info->collide_info.hitwall, data, &count);
2776 break;
2777 case EVT_CLIENT_GAMECOLLIDE:
2778 case EVT_GAMECOLLIDE:
2779 MultiAddFloat(info->collide_info.point.x, data, &count);
2780 MultiAddFloat(info->collide_info.point.y, data, &count);
2781 MultiAddFloat(info->collide_info.point.z, data, &count);
2782 MultiAddFloat(info->collide_info.normal.x, data, &count);
2783 MultiAddFloat(info->collide_info.normal.y, data, &count);
2784 MultiAddFloat(info->collide_info.normal.z, data, &count);
2785 break;
2786 case EVT_GAMEOBJCHANGESEG:
2787 case EVT_CLIENT_GAMEOBJCHANGESEG:
2788 case EVT_GAMEPLAYERCHANGESEG:
2789 case EVT_CLIENT_GAMEPLAYERCHANGESEG:
2790 MultiAddInt(info->newseg, data, &count);
2791 MultiAddInt(info->oldseg, data, &count);
2792 break;
2793 }
2794 } else {
2795 MultiAddByte(0, data, &count);
2796 }
2797
2798 END_DATA(count, data, size_offset);
2799
2800 // Send it out
2801 if (to == -1)
2802 MultiSendReliablyToAllExcept(Player_num, data, count, NETSEQ_OBJECTS, false);
2803 else
2804 nw_SendReliable(NetPlayers[to].reliable_socket, data, count);
2805 }
2806
2807 // Resets the settings that a server uses
2808 void MultiResetSettings() {
2809 strcpy(Netgame.scriptname, "Anarchy.d3m");
2810 strcpy(Netgame.connection_name, "Direct TCP~IP");
2811 strcpy(Netgame.mission, "fury.mn3");
2812 strcpy(Netgame.name, "Generic D3 Game");
2813
2814 Netgame.packets_per_second = 10;
2815 Netgame.respawn_time = 60;
2816 Netgame.flags = NF_RANDOMIZE_RESPAWN;
2817 Netgame.max_players = 8;
2818 Netgame.difficulty = 2;
2819
2820 // Clear MOTD
2821 Multi_message_of_the_day[0] = 0;
2822 }
2823
2824 #define LEVEL_SPAN 10.0f
2825
2826 #define KILL_BASE 0.52f
2827 #define DEATH_BASE 0.50f
2828
2829 #define KILL_SCALAR (KILL_BASE / LEVEL_SPAN)
2830 #define DEATH_SCALAR (DEATH_BASE / LEVEL_SPAN)
2831
2832 int GetRankLevel(int rank) {
2833 if (rank < 600)
2834 return 1;
2835 if (rank < 900)
2836 return 2;
2837 if (rank < 1200)
2838 return 3;
2839 if (rank < 1500)
2840 return 4;
2841 if (rank < 1800)
2842 return 5;
2843 if (rank < 2100)
2844 return 6;
2845 if (rank < 2400)
2846 return 7;
2847 if (rank < 2600)
2848 return 8;
2849 if (rank < 3000)
2850 return 9;
2851 return 10;
2852 }
2853
2854 // Given two objects, calculates rank for them after a kill
2855 float CalculateNewRanking(object *obj_a, object *obj_b, int won) {
2856 ASSERT(obj_a->type == OBJ_PLAYER && obj_b->type == OBJ_PLAYER);
2857
2858 float rank_a = Players[obj_a->id].rank;
2859 float rank_b = Players[obj_b->id].rank;
2860
2861 int level_a = GetRankLevel((int)rank_a);
2862 int level_b = GetRankLevel((int)rank_b);
2863
2864 if ((obj_a == obj_b) && won)
2865 return rank_a;
2866
2867 if (won) {
2868 rank_a += (KILL_SCALAR * float(level_b - level_a) + KILL_BASE);
2869 } else {
2870 rank_a -= (DEATH_SCALAR * float(level_a - level_b) + DEATH_BASE);
2871 }
2872 float new_rank = rank_a;
2873
2874 if (new_rank < 0)
2875 new_rank = 0;
2876
2877 return new_rank;
2878 }
2879
2880 /*
2881 // Given two objects, calculates rank for them after a kill
2882 float CalculateNewRanking (object *obj_a,object *obj_b,int won)
2883 {
2884 ASSERT (obj_a->type==OBJ_PLAYER && obj_b->type==OBJ_PLAYER);
2885
2886 float rank_a=Players[obj_a->id].rank;
2887 float rank_b=Players[obj_b->id].rank;
2888
2889 float k_factor;
2890 float g_factor=400;
2891 float scalar;
2892 int num_players=Player_count;
2893
2894 if (num_players>8)
2895 num_players=8;
2896 if (num_players<2)
2897 num_players=2;
2898
2899 scalar=1.0-(((num_players-2)/6.0)*.5);
2900 g_factor=400*(1.0+(((num_players-2)/6.0)*.4));
2901
2902 k_factor=16;
2903 k_factor*=scalar;
2904
2905 float delta_r=rank_b-rank_a;
2906 float expected_wins = 1.0/((pow(10.0,delta_r / g_factor) + 1));
2907 float new_rank = (k_factor * (won - (expected_wins)));
2908
2909 if (won && new_rank<.005f)
2910 new_rank=.005f;
2911
2912
2913 // This prevents players of vastly different rank from hurting each the better rank
2914 if ((rank_a-rank_b)>400 && !won)
2915 new_rank=0;
2916
2917 new_rank=Players[obj_a->id].rank+new_rank;
2918
2919 if (new_rank<0)
2920 new_rank=0;
2921
2922 mprintf(0,"Player %s rating is now %f\n",Players[obj_a->id].callsign,new_rank);
2923
2924 return new_rank;
2925 }
2926 */
2927
2928 // Returns a ranking index based on the player rating
2929 // If rankbuf is non-null, fills in the string corresponding to that rank
2930 // Returns -1 if not a pxo game (ie no rankings in this game)
2931 int GetRankIndex(int pnum, char *rankbuf) {
2932 if (!Game_is_master_tracker_game)
2933 return -1;
2934
2935 float ranking = Players[pnum].rank;
2936 int val = 0;
2937
2938 if (ranking >= 0 && ranking < 600)
2939 val = 0;
2940 else if (ranking >= 600 && ranking < 900)
2941 val = 1;
2942 else if (ranking >= 900 && ranking < 1200)
2943 val = 2;
2944 else if (ranking >= 1200 && ranking < 1500)
2945 val = 3;
2946 else if (ranking >= 1500 && ranking < 1800)
2947 val = 4;
2948 else if (ranking >= 1800 && ranking < 2100)
2949 val = 5;
2950 else if (ranking >= 2100 && ranking < 2400)
2951 val = 6;
2952 else if (ranking >= 2400 && ranking < 2600)
2953 val = 7;
2954 else if (ranking >= 2600 && ranking < 3000)
2955 val = 8;
2956 else if (ranking >= 3000)
2957 val = 9;
2958
2959 if (rankbuf)
2960 sprintf(rankbuf, "%s", TXT(TXT_MULTI_RANKS + val));
2961
2962 return val;
2963 }
2964
2965 // Tells the clients to change rank for a player
2966 void MultiSendChangeRank(int pnum, char *str, uint8_t goodnews) {
2967 int size;
2968 uint8_t data[MAX_GAME_DATA_SIZE];
2969 int count = 0;
2970
2971 mprintf(0, "Sending change rank!\n");
2972
2973 size = START_DATA(MP_CHANGE_RANK, data, &count);
2974 MultiAddByte(pnum, data, &count);
2975 MultiAddString(str, data, &count);
2976 MultiAddByte(goodnews, data, &count);
2977 MultiAddFloat(Players[pnum].rank, data, &count);
2978 END_DATA(count, data, size);
2979
2980 MultiSendReliablyToAllExcept(Player_num, data, count, NETSEQ_PLAYING, false);
2981 MultiDoChangeRank(data);
2982 }
2983
2984 // Does PXO stuff and sends out messages demoting/promoting the player
2985 void ChangeRankIndex(int old_index, int pnum) {
2986 char str[255];
2987 int new_index = GetRankIndex(pnum);
2988 uint8_t goodnews = 1;
2989
2990 if (old_index < new_index) // promoted
2991 {
2992 snprintf(str, sizeof(str), "%s %s %s %s!", Players[pnum].callsign, TXT_HAS_BEEN, TXT_PROMOTED,
2993 TXT(TXT_MULTI_RANKS + new_index));
2994 goodnews = 1;
2995 } else {
2996 snprintf(str, sizeof(str), "%s %s %s %s!", Players[pnum].callsign, TXT_HAS_BEEN, TXT_DEMOTED,
2997 TXT(TXT_MULTI_RANKS + new_index));
2998 goodnews = 0;
2999 }
3000
3001 MultiSendChangeRank(pnum, str, goodnews);
3002 }
3003
3004 // Given a killer and killed player, calculates their new rankings
3005 void GetNewRankings(object *killed, object *killer) {
3006 float killed_rank;
3007 float killer_rank;
3008
3009 // #ifdef OEM
3010 // return;
3011 // #endif
3012
3013 #ifdef DEMO
3014 return;
3015 #endif
3016
3017 if (!killer || !killed)
3018 return;
3019
3020 if (!(Netgame.flags & NF_TRACK_RANK))
3021 return;
3022
3023 if (killed->type != OBJ_PLAYER || killer->type != OBJ_PLAYER)
3024 return;
3025
3026 int old_killed_index = GetRankIndex(killed->id);
3027 int old_killer_index = GetRankIndex(killer->id);
3028
3029 if (killed == killer) // Suicide, always subract .5 points
3030 {
3031 Players[killer->id].rank -= .5;
3032 if (Players[killer->id].rank < 0) {
3033 Players[killer->id].rank = 0;
3034 }
3035
3036 if (GetRankIndex(killer->id) != old_killer_index)
3037 ChangeRankIndex(old_killer_index, killer->id);
3038 } else {
3039 killed_rank = CalculateNewRanking(killed, killer, 0);
3040 killer_rank = CalculateNewRanking(killer, killed, 1);
3041
3042 Players[killer->id].rank = killer_rank;
3043 Players[killed->id].rank = killed_rank;
3044
3045 if (GetRankIndex(killer->id) != old_killer_index)
3046 ChangeRankIndex(old_killer_index, killer->id);
3047
3048 if (GetRankIndex(killed->id) != old_killed_index)
3049 ChangeRankIndex(old_killed_index, killed->id);
3050 }
3051 }