Debian Codegraph @

similar/main/multibot.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Multiplayer robot code
23 *
24 */
25 #include <type_traits>
26 #include "multiinternal.h"
27
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdexcept>
31
32 #include "vecmat.h"
33 #include "object.h"
34 #include "multibot.h"
35 #include "game.h"
36 #include "laser.h"
37 #include "dxxerror.h"
38 #include "timer.h"
39 #include "player.h"
40 #include "ai.h"
41 #include "fireball.h"
42 #include "aistruct.h"
43 #include "gameseg.h"
44 #include "robot.h"
45 #include "powerup.h"
46 #include "gauges.h"
47 #include "fuelcen.h"
48 #include "morph.h"
49 #include "digi.h"
50 #include "sounds.h"
51 #include "effects.h"
52 #include "automap.h"
53 #include "physics.h"
54 #include "byteutil.h"
55 #include "escort.h"
56
57 #include "compiler-range_for.h"
58 #include "d_construct.h"
59 #include "d_levelstate.h"
60 #include "partial_range.h"
61
62 namespace dcx {
63 namespace {
64 std::array<multi_send_robot_position_priority, MAX_ROBOTS_CONTROLLED> robot_send_pending;
65 std::array<multi_command<multiplayer_command_t::MULTI_ROBOT_FIRE>, MAX_ROBOTS_CONTROLLED> robot_fire_buf;
66 }
67 std::array<objnum_t, MAX_ROBOTS_CONTROLLED> robot_controlled;
68 std::array<int, MAX_ROBOTS_CONTROLLED> robot_agitation;
69 std::bitset<MAX_ROBOTS_CONTROLLED> robot_fired;
70 }
71
72 namespace dsx {
73 namespace {
74 static int multi_add_controlled_robot(vmobjptridx_t objnum, int agitation);
75 void multi_drop_robot_powerups(object &objnum);
76 static void multi_send_robot_position_sub(const vmobjptridx_t objnum, multiplayer_data_priority now);
77 static void multi_send_release_robot(vmobjptridx_t objnum);
78 static void multi_delete_controlled_robot(const vmobjptridx_t objnum);
79 }
80 }
81
82 //
83 // Code for controlling robots in multiplayer games
84 //
85
86 #define STANDARD_EXPL_DELAY (F1_0/4)
87 #if DXX_BUILD_DESCENT == 1
88 #define MIN_CONTROL_TIME F1_0*2
89 #define ROBOT_TIMEOUT F1_0*3
90
91 #define MAX_TO_DELETE 67
92 #elif DXX_BUILD_DESCENT == 2
93 #define MIN_CONTROL_TIME F1_0*1
94 #define ROBOT_TIMEOUT F1_0*2
95 #endif
96
97 #define MIN_TO_ADD 60
98
99 std::array<fix64, MAX_ROBOTS_CONTROLLED> robot_controlled_time,
100 robot_last_send_time,
101 robot_last_message_time;
102
103 #define MULTI_ROBOT_PRIORITY(objnum, pnum) (((objnum % 4) + pnum) % N_players)
104
105 //#define MULTI_ROBOT_PRIORITY(objnum, pnum) multi_robot_priority(objnum, pnum)
106 //int multi_robot_priority(int objnum, int pnum)
107 //{
108 // return( ((objnum % 4) + pnum) % N_players);
109 //}
110
111 int multi_can_move_robot(const vmobjptridx_t objnum, int agitation)
112 {
113 auto &BossUniqueState = LevelUniqueObjectState.BossState;
114 // Determine whether or not I am allowed to move this robot.
115 // Claim robot if necessary.
116
117 if (Player_dead_state == player_dead_state::exploded)
118 return 0;
119
120 auto &objrobot = *objnum;
121 if (objrobot.type != OBJ_ROBOT)
122 {
123 #ifndef NDEBUG
124 Int3();
125 #endif
126 return 0;
127 }
128
129 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
130 if (Robot_info[get_robot_id(objrobot)].boss_flag != boss_robot_id::None && BossUniqueState.Boss_dying)
131 return 0;
132 else if (objrobot.ctype.ai_info.REMOTE_OWNER == Player_num) // Already my robot!
133 {
134 const auto slot_num = objrobot.ctype.ai_info.REMOTE_SLOT_NUM;
135
136 if ((slot_num < 0) || (slot_num >= MAX_ROBOTS_CONTROLLED))
137 {
138 return 0;
139 }
140
141 if (robot_fired[slot_num]) {
142 return 0;
143 }
144 else {
145 robot_agitation[slot_num] = agitation;
146 robot_last_message_time[slot_num] = {GameTime64};
147 return 1;
148 }
149 }
150 else if (objrobot.ctype.ai_info.REMOTE_OWNER != -1 || agitation < MIN_TO_ADD)
151 {
152 if (agitation == ROBOT_FIRE_AGITATION) // Special case for firing at non-player
153 {
154 return 1; // Try to fire at player even tho we're not in control!
155 }
156 else
157 return 0;
158 }
159 else
160 return multi_add_controlled_robot(objnum, agitation);
161 }
162
163 void multi_check_robot_timeout()
164 {
165 auto &Objects = LevelUniqueObjectState.Objects;
166 auto &vmobjptridx = Objects.vmptridx;
167 static fix64 lastcheck = 0;
168 int i;
169
170 if (GameTime64 > lastcheck + F1_0 || lastcheck > GameTime64)
171 {
172 lastcheck = {GameTime64};
173 for (i = 0; i < MAX_ROBOTS_CONTROLLED; i++)
174 {
175 if (robot_controlled[i] != object_none && robot_last_send_time[i] + ROBOT_TIMEOUT < GameTime64)
176 {
177 const auto &&robot_objp = vmobjptridx(robot_controlled[i]);
178 if (robot_objp->ctype.ai_info.REMOTE_OWNER != Player_num)
179 {
180 robot_controlled[i] = object_none;
181 Int3(); // Non-terminal but Rob is interesting, step over please...
182 return;
183 }
184 if (robot_send_pending[i] != multi_send_robot_position_priority::_0)
185 multi_send_robot_position(robot_objp, multi_send_robot_position_priority::_2);
186 multi_send_release_robot(robot_objp);
187 // multi_delete_controlled_robot(robot_controlled[i]);
188 // robot_controlled[i] = -1;
189 }
190 }
191 }
192 }
193
194 void multi_strip_robots(const int playernum)
195 {
196 auto &Objects = LevelUniqueObjectState.Objects;
197 auto &vmobjptr = Objects.vmptr;
198 auto &vmobjptridx = Objects.vmptridx;
199 // Grab all robots away from a player
200 // (player died or exited the game)
201 if (Game_mode & GM_MULTI_ROBOTS) {
202
203 if (playernum == Player_num)
204 {
205 range_for (const auto r, robot_controlled)
206 if (r != object_none)
207 multi_delete_controlled_robot(vmobjptridx(r));
208 }
209
210 /* clang-3.7 crashes with a segmentation fault if asked to
211 * implicitly convert 1u to std::size_t in partial_range inline
212 * chain. Add a conversion up front, which seems to avoid the
213 * bug. The value must not be cast on non-clang targets because
214 * some non-clang targets issue a `-Wuseless-cast` diagnostic
215 * for the cast. Fortunately, clang does not understand
216 * `-Wuseless-cast`, so the cast can be used on all clang
217 * targets, even ones where it is useless.
218 *
219 * This crash was first seen in x86_64-pc-linux-gnu-clang-3.7,
220 * then later reported by kreator in `Apple LLVM version 7.3.0`
221 * on `x86_64-apple-darwin15.6.0`.
222 *
223 * Comment from kreator regarding the clang crash bug:
224 * This has been reported with Apple, bug report 28247284
225 */
226 #ifdef __clang__
227 #define DXX_partial_range_vobjptr_skip_distance static_cast<std::size_t>
228 #else
229 #define DXX_partial_range_vobjptr_skip_distance
230 #endif
231 for (auto &obj : partial_range(vmobjptr, DXX_partial_range_vobjptr_skip_distance(1u), vmobjptr.count()))
232 #undef DXX_partial_range_vobjptr_skip_distance
233 {
234 if (obj.type == OBJ_ROBOT && obj.ctype.ai_info.REMOTE_OWNER == playernum)
235 {
236 assert(obj.control_source == object::control_type::ai || obj.control_source == object::control_type::None || obj.control_source == object::control_type::morph);
237 obj.ctype.ai_info.REMOTE_OWNER = -1;
238 if (playernum == Player_num)
239 obj.ctype.ai_info.REMOTE_SLOT_NUM = HANDS_OFF_PERIOD;
240 else
241 obj.ctype.ai_info.REMOTE_SLOT_NUM = 0;
242 }
243 }
244 }
245 // Note -- only call this with playernum == Player_num if all other players
246 // already know that we are clearing house. This does not send a release
247 // message for each controlled robot!!
248
249 }
250
251 namespace dsx {
252 namespace {
253 int multi_add_controlled_robot(const vmobjptridx_t objnum, int agitation)
254 {
255 auto &Objects = LevelUniqueObjectState.Objects;
256 auto &vcobjptr = Objects.vcptr;
257 auto &vmobjptridx = Objects.vmptridx;
258 int i;
259 int lowest_agitation = INT32_MAX; // MAX POSITIVE INT
260 int lowest_agitated_bot = -1;
261 int first_free_robot = -1;
262
263 // Try to add a new robot to the controlled list, return 1 if added, 0 if not.
264
265 auto &objrobot = *objnum;
266 #if DXX_BUILD_DESCENT == 2
267 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
268 if (Robot_info[get_robot_id(objrobot)].boss_flag != boss_robot_id::None) // this is a boss, so make sure he gets a slot
269 agitation=(agitation*3)+Player_num;
270 #endif
271 if (objrobot.ctype.ai_info.REMOTE_SLOT_NUM > 0)
272 {
273 objrobot.ctype.ai_info.REMOTE_SLOT_NUM -= 1;
274 return 0;
275 }
276
277 for (i = 0; i < MAX_ROBOTS_CONTROLLED; i++)
278 {
279 if (robot_controlled[i] == object_none || vcobjptr(robot_controlled[i])->type != OBJ_ROBOT) {
280 first_free_robot = i;
281 break;
282 }
283
284 if (robot_last_message_time[i] + ROBOT_TIMEOUT < GameTime64) {
285 const auto &&robot_objp = vmobjptridx(robot_controlled[i]);
286 if (robot_send_pending[i] != multi_send_robot_position_priority::_0)
287 multi_send_robot_position(robot_objp, multi_send_robot_position_priority::_2);
288 multi_send_release_robot(robot_objp);
289 first_free_robot = i;
290 break;
291 }
292
293 if (robot_agitation[i] < lowest_agitation && robot_controlled_time[i] + MIN_CONTROL_TIME < GameTime64)
294 {
295 lowest_agitation = robot_agitation[i];
296 lowest_agitated_bot = i;
297 }
298 }
299
300 if (first_free_robot != -1) // Room left for new robots
301 i = first_free_robot;
302
303 else if ((agitation > lowest_agitation)
304 #if DXX_BUILD_DESCENT == 1
305 && (lowest_agitation <= MAX_TO_DELETE)
306 #endif
307 ) // Replace some old robot with a more agitated one
308 {
309 const auto &&robot_objp = vmobjptridx(robot_controlled[lowest_agitated_bot]);
310 if (robot_send_pending[lowest_agitated_bot] != multi_send_robot_position_priority::_0)
311 multi_send_robot_position(robot_objp, multi_send_robot_position_priority::_2);
312 multi_send_release_robot(robot_objp);
313
314 i = lowest_agitated_bot;
315 }
316 else {
317 return(0); // Sorry, can't squeeze him in!
318 }
319
320 multi_send_claim_robot(objnum);
321 robot_controlled[i] = objnum;
322 robot_agitation[i] = agitation;
323 objrobot.ctype.ai_info.REMOTE_OWNER = Player_num;
324 objrobot.ctype.ai_info.REMOTE_SLOT_NUM = i;
325 robot_controlled_time[i] = {GameTime64};
326 robot_last_send_time[i] = robot_last_message_time[i] = {GameTime64};
327 return(1);
328 }
329
330 void multi_delete_controlled_robot(const vmobjptridx_t objnum)
331 {
332 int i;
333
334 // Delete robot object number objnum from list of controlled robots because it is dead
335
336 for (i = 0; i < MAX_ROBOTS_CONTROLLED; i++)
337 if (robot_controlled[i] == objnum)
338 break;
339
340 if (i == MAX_ROBOTS_CONTROLLED)
341 return;
342
343 auto &objrobot = *objnum;
344 if (objrobot.ctype.ai_info.REMOTE_SLOT_NUM != i)
345 {
346 Int3(); // can't release this bot!
347 return;
348 }
349
350 objrobot.ctype.ai_info.REMOTE_OWNER = -1;
351 objrobot.ctype.ai_info.REMOTE_SLOT_NUM = 0;
352 robot_controlled[i] = object_none;
353 robot_send_pending[i] = multi_send_robot_position_priority::_0;
354 robot_fired[i] = 0;
355 }
356 }
357 }
358
359 namespace {
360 struct multi_claim_robot
361 {
362 uint8_t pnum;
363 int8_t owner;
364 uint16_t robjnum;
365 };
366 DEFINE_MULTIPLAYER_SERIAL_MESSAGE(multiplayer_command_t::MULTI_ROBOT_CLAIM, multi_claim_robot, b, (b.pnum, b.owner, b.robjnum));
367 }
368
369 void multi_send_claim_robot(const vmobjptridx_t objnum)
370 {
371 if (objnum->type != OBJ_ROBOT)
372 throw std::runtime_error("claiming non-robot"); // See rob
373 // The AI tells us we should take control of this robot.
374 auto r = objnum_local_to_remote(objnum);
375 multi_serialize_write(multiplayer_data_priority::_2, multi_claim_robot{static_cast<uint8_t>(Player_num), r.owner, r.objnum});
376 }
377
378 namespace dsx {
379 namespace {
380
381 void multi_send_release_robot(const vmobjptridx_t objnum)
382 {
383 multi_command<multiplayer_command_t::MULTI_ROBOT_RELEASE> multibuf;
384 if (objnum->type != OBJ_ROBOT)
385 {
386 Int3(); // See rob
387 return;
388 }
389
390 multi_delete_controlled_robot(objnum);
391
392 multibuf[1] = Player_num;
393 const auto &&[obj_owner, remote_objnum] = objnum_local_to_remote(objnum);
394 multibuf[4] = obj_owner;
395 PUT_INTEL_SHORT(&multibuf[2], remote_objnum);
396
397 multi_send_data(multibuf, multiplayer_data_priority::_2);
398 }
399
400 }
401 }
402
403 void multi_send_robot_frame()
404 {
405 auto &Objects = LevelUniqueObjectState.Objects;
406 auto &vmobjptridx = Objects.vmptridx;
407 static int last_sent = 0;
408
409 int i;
410
411 const unsigned sent = 0;
412 for (i = 0; i < MAX_ROBOTS_CONTROLLED; i++)
413 {
414 int sending = (last_sent+1+i)%MAX_ROBOTS_CONTROLLED;
415 if (robot_controlled[sending] != object_none && (underlying_value(robot_send_pending[sending]) > sent || robot_fired[sending] > sent))
416 {
417 if (auto &pending = robot_send_pending[sending]; pending != multi_send_robot_position_priority::_0)
418 {
419 const auto p = std::exchange(pending, multi_send_robot_position_priority::_0);
420 multi_send_robot_position_sub(vmobjptridx(robot_controlled[sending]), underlying_value(p) > 1 ? multiplayer_data_priority::_1 : multiplayer_data_priority::_0);
421 }
422
423 if (auto &&b = robot_fired[sending])
424 {
425 b = 0;
426 multi_send_data(robot_fire_buf[sending], multiplayer_data_priority::_1);
427 }
428 last_sent = sending;
429 }
430 }
431 Assert((last_sent >= 0) && (last_sent <= MAX_ROBOTS_CONTROLLED));
432 }
433
434 namespace dsx {
435 #if DXX_BUILD_DESCENT == 2
436 /*
437 * The thief bot moves around even when not controlled by a player. Due to its erratic and random behaviour, it's movement will diverge heavily between players and cause it to teleport when a player takes over.
438 * To counter this, let host update positions when no one controls it OR the client which does.
439 * Seperated this function to allow the positions being updated more frequently then multi_send_robot_frame (see dispatch_table::do_protocol_frame()).
440 */
441 void multi_send_thief_frame()
442 {
443 auto &Objects = LevelUniqueObjectState.Objects;
444 auto &vmobjptridx = Objects.vmptridx;
445 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
446 if (!(Game_mode & GM_MULTI_ROBOTS))
447 return;
448
449 range_for (const auto &&objp, vmobjptridx)
450 {
451 if (objp->type == OBJ_ROBOT)
452 {
453 if (robot_is_thief(Robot_info[get_robot_id(objp)]))
454 {
455 if ((multi_i_am_master() && objp->ctype.ai_info.REMOTE_OWNER == -1) || objp->ctype.ai_info.REMOTE_OWNER == Player_num)
456 multi_send_robot_position_sub(objp, multiplayer_data_priority::_1);
457 return;
458 }
459 }
460 }
461 }
462
463 #endif
464
465 namespace {
466 void multi_send_robot_position_sub(const vmobjptridx_t objnum, const multiplayer_data_priority priority)
467 {
468 multi_command<multiplayer_command_t::MULTI_ROBOT_POSITION> multibuf;
469 int loc{0};
470
471 loc += 1;
472 multibuf[loc] = Player_num; loc += 1;
473 const auto &&[obj_owner, remote_objnum] = objnum_local_to_remote(objnum);
474 multibuf[loc+2] = obj_owner;
475 PUT_INTEL_SHORT(&multibuf[loc], remote_objnum); loc += 3; // 5
476
477 const auto qpp{build_quaternionpos(objnum)};
478 PUT_INTEL_SHORT(&multibuf[loc], qpp.orient.w); loc += 2;
479 PUT_INTEL_SHORT(&multibuf[loc], qpp.orient.x); loc += 2;
480 PUT_INTEL_SHORT(&multibuf[loc], qpp.orient.y); loc += 2;
481 PUT_INTEL_SHORT(&multibuf[loc], qpp.orient.z); loc += 2;
482 multi_put_vector(&multibuf[loc], qpp.pos);
483 loc += 12;
484 PUT_INTEL_SEGNUM(&multibuf[loc], qpp.segment); loc += 2;
485 multi_put_vector(&multibuf[loc], qpp.vel);
486 loc += 12;
487 multi_put_vector(&multibuf[loc], qpp.rotvel);
488 // 46 + 5 = 51
489
490 multi_send_data(multibuf, priority);
491 }
492
493 }
494 }
495
496 void multi_send_robot_position(object &obj, const multi_send_robot_position_priority force)
497 {
498 // Send robot position to other player(s). Includes a byte
499 // value describing whether or not they fired a weapon
500
501 if (!(Game_mode & GM_MULTI))
502 return;
503
504 if (obj.type != OBJ_ROBOT)
505 {
506 Int3(); // See rob
507 return;
508 }
509
510 if (obj.ctype.ai_info.REMOTE_OWNER != Player_num)
511 return;
512
513 // Objects[objnum].phys_info.drag = Robot_info[Objects[objnum].id].drag; // Set drag to normal
514
515 const auto slot = obj.ctype.ai_info.REMOTE_SLOT_NUM;
516 robot_last_send_time[slot] = {GameTime64};
517 robot_send_pending[slot] = force;
518 return;
519 }
520
521 void multi_send_robot_fire(const vmobjptridx_t obj, const robot_gun_number gun_num, const vms_vector &fire)
522 {
523 multi_command<multiplayer_command_t::MULTI_ROBOT_FIRE> multibuf;
524 // Send robot fire event
525 int loc{0};
526
527 loc += 1;
528 multibuf[loc] = Player_num; loc += 1;
529 const auto &&[obj_owner, remote_objnum] = objnum_local_to_remote(obj);
530 multibuf[loc+2] = obj_owner;
531 PUT_INTEL_SHORT(&multibuf[loc], remote_objnum);
532 loc += 3;
533 multibuf[loc] = underlying_value(gun_num); loc += 1;
534 multi_put_vector(&multibuf[loc], fire);
535 loc += sizeof(vms_vector); // 12
536 // --------------------------
537 // Total = 18
538
539 if (obj->ctype.ai_info.REMOTE_OWNER == Player_num)
540 {
541 const auto slot = obj->ctype.ai_info.REMOTE_SLOT_NUM;
542 if (slot<0 || slot>=MAX_ROBOTS_CONTROLLED)
543 {
544 return;
545 }
546 if (auto &&b = robot_fired[slot])
547 {
548 // Int3(); // ROB!
549 return;
550 }
551 else
552 {
553 b = 1;
554 }
555 robot_fire_buf[slot] = multibuf;
556 }
557 else
558 multi_send_data(multibuf, multiplayer_data_priority::_1); // Not our robot, send ASAP
559 }
560
561 namespace {
562 struct multi_explode_robot
563 {
564 int16_t robj_killer, robj_killed;
565 int8_t owner_killer, owner_killed;
566 };
567 DEFINE_MULTIPLAYER_SERIAL_MESSAGE(multiplayer_command_t::MULTI_ROBOT_EXPLODE, multi_explode_robot, b, (b.robj_killer, b.robj_killed, b.owner_killer, b.owner_killed));
568 }
569
570 void multi_send_robot_explode(const imobjptridx_t objnum, objnum_t killer)
571 {
572 // Send robot explosion event to the other players
573 const auto k = objnum_local_to_remote(killer);
574 multi_explode_robot e;
575 e.robj_killer = k.objnum;
576 e.owner_killer = k.owner;
577 const auto d = objnum_local_to_remote(objnum);
578 e.robj_killed = d.objnum;
579 e.owner_killed = d.owner;
580 multi_serialize_write(multiplayer_data_priority::_2, e);
581
582 multi_delete_controlled_robot(objnum);
583 }
584
585 void multi_send_create_robot(const station_number station, const objnum_t objnum, const robot_id type)
586 {
587 multi_command<multiplayer_command_t::MULTI_CREATE_ROBOT> multibuf;
588 // Send create robot information
589
590 int loc{0};
591
592 loc += 1;
593 multibuf[loc] = Player_num; loc += 1;
594 static_assert(sizeof(underlying_value(station)) == 1);
595 multibuf[loc] = underlying_value(station); loc += 1;
596 PUT_INTEL_SHORT(&multibuf[loc], objnum); loc += 2;
597 multibuf[loc] = underlying_value(type); loc += 1;
598
599 map_objnum_local_to_local(objnum);
600
601 multi_send_data(multibuf, multiplayer_data_priority::_2);
602 }
603
604 namespace {
605
606 struct boss_teleport
607 {
608 objnum_t objnum;
609 segnum_t where;
610 };
611 DEFINE_MULTIPLAYER_SERIAL_MESSAGE(multiplayer_command_t::MULTI_BOSS_TELEPORT, boss_teleport, b, (b.objnum, b.where));
612
613 struct boss_cloak
614 {
615 objnum_t objnum;
616 };
617 DEFINE_MULTIPLAYER_SERIAL_MESSAGE(multiplayer_command_t::MULTI_BOSS_CLOAK, boss_cloak, b, (b.objnum));
618
619 struct boss_start_gate
620 {
621 objnum_t objnum;
622 };
623 DEFINE_MULTIPLAYER_SERIAL_MESSAGE(multiplayer_command_t::MULTI_BOSS_START_GATE, boss_start_gate, b, (b.objnum));
624
625 struct boss_stop_gate
626 {
627 objnum_t objnum;
628 };
629 DEFINE_MULTIPLAYER_SERIAL_MESSAGE(multiplayer_command_t::MULTI_BOSS_STOP_GATE, boss_stop_gate, b, (b.objnum));
630
631 struct boss_create_robot
632 {
633 objnum_t objnum;
634 objnum_t objrobot;
635 segnum_t where;
636 uint8_t robot_type;
637 };
638 DEFINE_MULTIPLAYER_SERIAL_MESSAGE(multiplayer_command_t::MULTI_BOSS_CREATE_ROBOT, boss_create_robot, b, (b.objnum, b.objrobot, b.where, b.robot_type));
639
640 #if DXX_BUILD_DESCENT == 2
641 struct update_buddy_state
642 {
643 uint8_t Looking_for_marker;
644 escort_goal_t Escort_special_goal;
645 int Last_buddy_key;
646 };
647 DEFINE_MULTIPLAYER_SERIAL_MESSAGE(multiplayer_command_t::MULTI_UPDATE_BUDDY_STATE, update_buddy_state, b, (b.Looking_for_marker, b.Escort_special_goal, b.Last_buddy_key));
648 #endif
649
650 }
651
652 namespace {
653
654 template <typename T, typename... Args>
655 static inline void multi_send_boss_action(objnum_t bossobjnum, Args&&... args)
656 {
657 multi_serialize_write(multiplayer_data_priority::_2, T{bossobjnum, std::forward<Args>(args)...});
658 }
659
660 }
661
662 namespace dsx {
663 void multi_send_boss_teleport(const vmobjptridx_t bossobj, const vcsegidx_t where)
664 {
665 // Boss is up for grabs after teleporting
666 Assert((bossobj->ctype.ai_info.REMOTE_SLOT_NUM >= 0) && (bossobj->ctype.ai_info.REMOTE_SLOT_NUM < MAX_ROBOTS_CONTROLLED));
667 multi_delete_controlled_robot(bossobj);
668 #if DXX_BUILD_DESCENT == 1
669 bossobj->ctype.ai_info.REMOTE_SLOT_NUM = HANDS_OFF_PERIOD; // Hands-off period!
670 #endif
671 multi_send_boss_action<boss_teleport>(bossobj, where);
672 }
673 }
674
675 void multi_send_boss_cloak(objnum_t bossobjnum)
676 {
677 multi_send_boss_action<boss_cloak>(bossobjnum);
678 }
679
680 void multi_send_boss_start_gate(objnum_t bossobjnum)
681 {
682 multi_send_boss_action<boss_start_gate>(bossobjnum);
683 }
684
685 void multi_send_boss_stop_gate(objnum_t bossobjnum)
686 {
687 multi_send_boss_action<boss_stop_gate>(bossobjnum);
688 }
689
690 void multi_send_boss_create_robot(vmobjidx_t bossobjnum, const vmobjptridx_t objrobot)
691 {
692 map_objnum_local_to_local(objrobot);
693 multi_send_boss_action<boss_create_robot>(bossobjnum, objrobot, objrobot->segnum, underlying_value(get_robot_id(objrobot)));
694 }
695
696 #define MAX_ROBOT_POWERUPS 4
697
698 namespace dsx {
699 namespace {
700
701 static void multi_send_create_robot_powerups(const object_base &del_obj)
702 {
703 multi_command<multiplayer_command_t::MULTI_CREATE_ROBOT_POWERUPS> multibuf;
704 // Send create robot information
705
706 int loc{0};
707
708 loc += 1;
709 multibuf[loc] = Player_num; loc += 1;
710 multibuf[loc] = del_obj.contains.count; loc += 1;
711 multibuf[loc] = underlying_value(del_obj.contains.type); loc += 1;
712 multibuf[loc] = underlying_value(del_obj.contains.id.robot); loc += 1;
713 PUT_INTEL_SEGNUM(&multibuf[loc], del_obj.segnum); loc += 2;
714 multi_put_vector(&multibuf[loc], del_obj.pos);
715 loc += 12;
716
717 memset(&multibuf[loc], -1, MAX_ROBOT_POWERUPS*sizeof(short));
718 #if DXX_BUILD_DESCENT == 2
719 if (del_obj.contains.count != Net_create_loc)
720 Int3(); //Get Jason, a bad thing happened
721 #endif
722
723 if ((Net_create_loc > MAX_ROBOT_POWERUPS) || (Net_create_loc < 1))
724 {
725 Int3(); // See Rob
726 }
727 range_for (const auto i, partial_const_range(Net_create_objnums, Net_create_loc))
728 {
729 PUT_INTEL_SHORT(&multibuf[loc], i);
730 loc += 2;
731 map_objnum_local_to_local(i);
732 }
733
734 Net_create_loc = 0;
735
736 multi_send_data(multibuf, multiplayer_data_priority::_2);
737 }
738 }
739
740 void multi_do_claim_robot(const playernum_t pnum, const multiplayer_rspan<multiplayer_command_t::MULTI_ROBOT_CLAIM> buf)
741 {
742 auto &Objects = LevelUniqueObjectState.Objects;
743 auto &vmobjptridx = Objects.vmptridx;
744 multi_claim_robot b;
745 multi_serialize_read(buf, b);
746 auto botnum = objnum_remote_to_local(b.robjnum, b.owner);
747 if (botnum > Highest_object_index)
748 {
749 return;
750 }
751
752 const auto &&botp = vmobjptridx(botnum);
753 if (botp->type != OBJ_ROBOT)
754 {
755 return;
756 }
757
758 if (botp->ctype.ai_info.REMOTE_OWNER != -1)
759 {
760 if (MULTI_ROBOT_PRIORITY(b.robjnum, pnum) <= MULTI_ROBOT_PRIORITY(b.robjnum, botp->ctype.ai_info.REMOTE_OWNER))
761 return;
762 }
763
764 // Perform the requested change
765
766 if (botp->ctype.ai_info.REMOTE_OWNER == Player_num)
767 {
768 multi_delete_controlled_robot(botp);
769 }
770
771 botp->ctype.ai_info.REMOTE_OWNER = pnum;
772 botp->ctype.ai_info.REMOTE_SLOT_NUM = 0;
773 }
774
775 void multi_do_release_robot(const playernum_t pnum, const multiplayer_rspan<multiplayer_command_t::MULTI_ROBOT_RELEASE> buf)
776 {
777 auto &Objects = LevelUniqueObjectState.Objects;
778 auto &vmobjptr = Objects.vmptr;
779
780 const auto remote_botnum{GET_INTEL_SHORT<int16_t>(&buf[2])};
781 auto botnum = objnum_remote_to_local(remote_botnum, buf[4]);
782
783 if (botnum > Highest_object_index)
784 {
785 return;
786 }
787
788 const auto &&botp = vmobjptr(botnum);
789 if (botp->type != OBJ_ROBOT)
790 {
791 return;
792 }
793
794 if (botp->ctype.ai_info.REMOTE_OWNER != pnum)
795 {
796 return;
797 }
798
799 // Perform the requested change
800
801 botp->ctype.ai_info.REMOTE_OWNER = -1;
802 botp->ctype.ai_info.REMOTE_SLOT_NUM = 0;
803 }
804
805 void multi_do_robot_position(const playernum_t pnum, const multiplayer_rspan<multiplayer_command_t::MULTI_ROBOT_POSITION> buf)
806 {
807 auto &Objects = LevelUniqueObjectState.Objects;
808 auto &vmobjptridx = Objects.vmptridx;
809 // Process robot movement sent by another player
810
811 int loc{1};
812
813 ; loc += 1;
814
815 const auto remote_botnum{GET_INTEL_SHORT<int16_t>(&buf[loc])};
816 auto botnum = objnum_remote_to_local(remote_botnum, buf[loc+2]); loc += 3;
817
818 if (botnum > Highest_object_index)
819 {
820 return;
821 }
822
823 const auto robot = vmobjptridx(botnum);
824
825 if ((robot->type != OBJ_ROBOT) || (robot->flags & OF_EXPLODING)) {
826 return;
827 }
828
829 if (robot->ctype.ai_info.REMOTE_OWNER != pnum)
830 {
831 if (robot->ctype.ai_info.REMOTE_OWNER == -1)
832 {
833 // Robot claim packet must have gotten lost, let this player claim it.
834 if (robot->ctype.ai_info.REMOTE_SLOT_NUM >= MAX_ROBOTS_CONTROLLED) { // == HANDS_OFF_PERIOD should do the same trick
835 robot->ctype.ai_info.REMOTE_OWNER = pnum;
836 robot->ctype.ai_info.REMOTE_SLOT_NUM = 0;
837 }
838 else
839 robot->ctype.ai_info.REMOTE_SLOT_NUM++;
840 }
841 else
842 {
843 return;
844 }
845 }
846
847 set_thrust_from_velocity(robot); // Try to smooth out movement
848 // Objects[botnum].phys_info.drag = Robot_info[Objects[botnum].id].drag >> 4; // Set drag to low
849
850 quaternionpos qpp{};
851 qpp.orient.w = GET_INTEL_SHORT(&buf[loc]); loc += 2;
852 qpp.orient.x = GET_INTEL_SHORT(&buf[loc]); loc += 2;
853 qpp.orient.y = GET_INTEL_SHORT(&buf[loc]); loc += 2;
854 qpp.orient.z = GET_INTEL_SHORT(&buf[loc]); loc += 2;
855 qpp.pos = multi_get_vector(buf.subspan<5 + 8, 12>());
856 loc += 12;
857 if (const auto s{vmsegidx_t::check_nothrow_index(GET_INTEL_SHORT(&buf[loc]))})
858 {
859 qpp.segment = *s;
860 loc += 2;
861 }
862 else
863 return;
864 qpp.vel = multi_get_vector(buf.subspan<5 + 8 + 12 + 2, 12>());
865 loc += 12;
866 qpp.rotvel = multi_get_vector(buf.subspan<5 + 8 + 12 + 2 + 12, 12>());
867 extract_quaternionpos(Objects.vmptr, vmsegptr, robot, qpp);
868 }
869
870 }
871
872 static inline vms_vector calc_gun_point(const robot_info &robptr, const object_base &obj, const robot_gun_number gun_num)
873 {
874 vms_vector v;
875 return calc_gun_point(robptr, v, obj, gun_num), v;
876 }
877
878 namespace dsx {
879 void multi_do_robot_fire(const multiplayer_rspan<multiplayer_command_t::MULTI_ROBOT_FIRE> buf)
880 {
881 auto &Objects = LevelUniqueObjectState.Objects;
882 auto &vmobjptridx = Objects.vmptridx;
883 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
884 // Send robot fire event
885 int loc{1};
886 loc += 1; // pnum
887 const auto remote_botnum{GET_INTEL_SHORT<int16_t>(&buf[loc])};
888 auto botnum = objnum_remote_to_local(remote_botnum, buf[loc+2]); loc += 3;
889 const auto gun_num = buf[loc]; loc += 1;
890 const auto fire = multi_get_vector(buf.subspan<6, 12>());
891
892 if (botnum > Highest_object_index)
893 return;
894
895 auto botp = vmobjptridx(botnum);
896 if (botp->type != OBJ_ROBOT || botp->flags & OF_EXPLODING)
897 return;
898
899 using pt_weapon = std::pair<vms_vector, weapon_id_type>;
900 auto &robptr = Robot_info[get_robot_id(botp)];
901 switch (gun_num)
902 {
903 case underlying_value(robot_gun_number::proximity):
904 #if DXX_BUILD_DESCENT == 2
905 case underlying_value(robot_gun_number::smart_mine):
906 #endif
907 break;
908 default:
909 if (gun_num >= robptr.n_guns)
910 return;
911 break;
912 }
913 const pt_weapon pw =
914 // Do the firing
915 (gun_num == underlying_value(robot_gun_number::proximity)
916 #if DXX_BUILD_DESCENT == 2
917 || gun_num == underlying_value(robot_gun_number::smart_mine)
918 #endif
919 )
920 ? pt_weapon(vm_vec_build_add(botp->pos, fire),
921 #if DXX_BUILD_DESCENT == 2
922 gun_num != underlying_value(robot_gun_number::proximity) ? weapon_id_type::SUPERPROX_ID :
923 #endif
924 weapon_id_type::PROXIMITY_ID)
925 : pt_weapon(calc_gun_point(robptr, botp, robot_gun_number{gun_num}), get_robot_weapon(robptr, robot_gun_number::_1));
926 Laser_create_new_easy(Robot_info, fire, pw.first, botp, pw.second, weapon_sound_flag::audible);
927 }
928
929 int multi_explode_robot_sub(const d_robot_info_array &Robot_info, const vmobjptridx_t robot)
930 {
931 auto &BossUniqueState = LevelUniqueObjectState.BossState;
932 auto &objrobot = *robot;
933 if (objrobot.type != OBJ_ROBOT) { // Object is robot?
934 return 0;
935 }
936
937 if (objrobot.flags & OF_EXPLODING) { // Object not already exploding
938 return 0;
939 }
940
941 // Data seems valid, explode the sucker
942
943 if (Network_send_objects && multi::dispatch->objnum_is_past(robot))
944 {
945 Network_send_objnum = -1;
946 }
947
948 // Drop non-random KEY powerups locally only!
949 if (objrobot.contains.count > 0 && objrobot.contains.type == contained_object_type::powerup && (Game_mode & GM_MULTI_COOP) && objrobot.contains.id.powerup >= powerup_type_t::POW_KEY_BLUE && objrobot.contains.id.powerup <= powerup_type_t::POW_KEY_GOLD)
950 {
951 object_create_robot_egg(Robot_info, objrobot);
952 }
953 else if (objrobot.ctype.ai_info.REMOTE_OWNER == Player_num)
954 {
955 multi_drop_robot_powerups(robot);
956 multi_delete_controlled_robot(robot);
957 }
958 else if (objrobot.ctype.ai_info.REMOTE_OWNER == -1 && multi_i_am_master())
959 {
960 multi_drop_robot_powerups(robot);
961 }
962 const auto robot_id = get_robot_id(objrobot);
963 #if DXX_BUILD_DESCENT == 2
964 if (robot_is_thief(Robot_info[robot_id]))
965 drop_stolen_items_local(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, Vclip, vmsegptridx(objrobot.segnum), objrobot.mtype.phys_info.velocity, objrobot.pos, LevelUniqueObjectState.ThiefState.Stolen_items);
966 #endif
967 if (Robot_info[robot_id].boss_flag != boss_robot_id::None)
968 {
969 if (!BossUniqueState.Boss_dying)
970 start_boss_death_sequence(LevelUniqueObjectState.BossState, Robot_info, robot);
971 else
972 return (0);
973 }
974 #if DXX_BUILD_DESCENT == 2
975 else if (Robot_info[robot_id].death_roll) {
976 start_robot_death_sequence(robot);
977 }
978 #endif
979 else
980 {
981 #if DXX_BUILD_DESCENT == 2
982 if (robot_id == robot_id::special_reactor)
983 special_reactor_stuff();
984 #endif
985 // Kamikaze, explode right away, IN YOUR FACE!
986 explode_object(LevelUniqueObjectState, Robot_info, LevelSharedSegmentState, LevelUniqueSegmentState, robot,
987 #if DXX_BUILD_DESCENT == 2
988 Robot_info[robot_id].kamikaze ? 1 :
989 #endif
990 STANDARD_EXPL_DELAY);
991 }
992
993 return 1;
994 }
995
996 void multi_do_robot_explode(const d_robot_info_array &Robot_info, const multiplayer_rspan<multiplayer_command_t::MULTI_ROBOT_EXPLODE> buf)
997 {
998 auto &Objects = LevelUniqueObjectState.Objects;
999 auto &vmobjptridx = Objects.vmptridx;
1000 multi_explode_robot b;
1001 multi_serialize_read(buf, b);
1002 auto killer = objnum_remote_to_local(b.robj_killer, b.owner_killer);
1003 auto botnum = objnum_remote_to_local(b.robj_killed, b.owner_killed);
1004 // Explode robot controlled by other player
1005 if (botnum > Highest_object_index)
1006 {
1007 return;
1008 }
1009
1010 const auto robot = vmobjptridx(botnum);
1011 const auto rval = multi_explode_robot_sub(Robot_info, robot);
1012 if (!rval)
1013 return;
1014
1015 ++ Players[0u].num_kills_level;
1016 ++ Players[0u].num_kills_total;
1017 if (killer == get_local_player().objnum)
1018 add_points_to_score(ConsoleObject->ctype.player_info, Robot_info[get_robot_id(robot)].score_value, Game_mode);
1019 }
1020
1021 void multi_do_create_robot(const d_robot_info_array &Robot_info, const d_vclip_array &Vclip, const playernum_t pnum, const multiplayer_rspan<multiplayer_command_t::MULTI_CREATE_ROBOT> buf)
1022 {
1023 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
1024 auto &LevelUniqueMorphObjectState = LevelUniqueObjectState.MorphObjectState;
1025 auto &Station = LevelUniqueFuelcenterState.Station;
1026 auto &Vertices = LevelSharedVertexState.get_vertices();
1027 const auto untrusted_fuelcen_num = buf[2];
1028 const auto untrusted_robot_type = buf[5];
1029
1030 const objnum_t objnum{GET_INTEL_SHORT(&buf[3])};
1031
1032 const auto trusted_fuelcen_num = ({
1033 const auto o = LevelUniqueFuelcenterState.Station.valid_index(untrusted_fuelcen_num);
1034 if (!o)
1035 return;
1036 if (untrusted_fuelcen_num >= LevelUniqueFuelcenterState.Num_fuelcenters || pnum >= N_players)
1037 {
1038 Int3(); // Bogus data
1039 return;
1040 }
1041 *o;
1042 });
1043 const auto trusted_robot_type = ({
1044 const auto o = LevelSharedRobotInfoState.Robot_info.valid_index(untrusted_robot_type);
1045 if (!o)
1046 return;
1047 *o;
1048 });
1049 auto &robotcen = Station[trusted_fuelcen_num];
1050
1051 // Play effect and sound
1052
1053 // Set robot center flags, in case we become the master for the next one
1054
1055 robotcen.Flag = 0;
1056 robotcen.Capacity -= EnergyToCreateOneRobot;
1057 robotcen.Timer = 0;
1058
1059 const auto &&robotcen_segp = vmsegptridx(robotcen.segnum);
1060 auto &vcvertptr = Vertices.vcptr;
1061 const auto cur_object_loc{compute_segment_center(vcvertptr, robotcen_segp)};
1062 if (const auto &&obj = object_create_explosion_without_damage(Vclip, robotcen_segp, cur_object_loc, i2f(10), vclip_index::morphing_robot))
1063 reconstruct_at(obj->orient, extract_orient_from_segment, vcvertptr, robotcen_segp);
1064 digi_link_sound_to_pos(Vclip[vclip_index::morphing_robot].sound_num, robotcen_segp, sidenum_t::WLEFT, cur_object_loc, 0, F1_0);
1065
1066 const auto &&obj = create_morph_robot(Robot_info, robotcen_segp, cur_object_loc, trusted_robot_type);
1067 if (obj == object_none)
1068 return; // Cannot create object!
1069
1070 obj->matcen_creator = untrusted_fuelcen_num | 0x80;
1071 const auto direction{vm_vec_build_sub(ConsoleObject->pos, obj->pos)};
1072 {
1073 /* Copy `objp.orient.uvec` into a temporary and pass the temporary,
1074 * since it is undefined behavior to access `objp.orient` after the old
1075 * object is destroyed until the new value is constructed.
1076 */
1077 auto old_uvec{obj->orient.uvec};
1078 reconstruct_at(obj->orient, vm_vector_to_matrix_u, direction, std::move(old_uvec));
1079 }
1080 morph_start(LevelUniqueMorphObjectState, LevelSharedPolygonModelState, obj);
1081
1082 map_objnum_local_to_remote(obj, objnum, pnum);
1083
1084 Assert(obj->ctype.ai_info.REMOTE_OWNER == -1);
1085 }
1086
1087 #if DXX_BUILD_DESCENT == 2
1088 void multi_send_escort_goal(const d_unique_buddy_state &BuddyState)
1089 {
1090 update_buddy_state b;
1091 b.Looking_for_marker = static_cast<uint8_t>(BuddyState.Looking_for_marker);
1092 b.Escort_special_goal = BuddyState.Escort_special_goal;
1093 b.Last_buddy_key = BuddyState.Last_buddy_key;
1094 multi_serialize_write(multiplayer_data_priority::_2, b);
1095 }
1096
1097 void multi_recv_escort_goal(d_unique_buddy_state &BuddyState, const multiplayer_rspan<multiplayer_command_t::MULTI_UPDATE_BUDDY_STATE> buf)
1098 {
1099 update_buddy_state b;
1100 multi_serialize_read(buf, b);
1101 const auto Looking_for_marker = b.Looking_for_marker;
1102 BuddyState.Looking_for_marker = ({
1103 const auto o = MarkerState.imobjidx.valid_index(Looking_for_marker);
1104 o ? *o : game_marker_index::None;
1105 });
1106 BuddyState.Escort_special_goal = b.Escort_special_goal;
1107 BuddyState.Last_buddy_key = b.Last_buddy_key;
1108 BuddyState.Buddy_messages_suppressed = 0;
1109 BuddyState.Last_buddy_message_time = GameTime64 - 2 * F1_0;
1110 BuddyState.Escort_goal_object = ESCORT_GOAL_UNSPECIFIED;
1111 }
1112 #endif
1113
1114 void multi_do_boss_teleport(const d_robot_info_array &Robot_info, const d_vclip_array &Vclip, const playernum_t pnum, const multiplayer_rspan<multiplayer_command_t::MULTI_BOSS_TELEPORT> buf)
1115 {
1116 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
1117 auto &BossUniqueState = LevelUniqueObjectState.BossState;
1118 auto &Objects = LevelUniqueObjectState.Objects;
1119 auto &Vertices = LevelSharedVertexState.get_vertices();
1120 auto &vcobjptr = Objects.vcptr;
1121 auto &vmobjptr = Objects.vmptr;
1122 boss_teleport b;
1123 multi_serialize_read(buf, b);
1124 const auto &&guarded_boss_obj = Objects.vmptridx.check_untrusted(b.objnum);
1125 if (!guarded_boss_obj)
1126 return;
1127 const auto &&boss_obj = *guarded_boss_obj;
1128 if (boss_obj->type != OBJ_ROBOT || Robot_info[get_robot_id(boss_obj)].boss_flag == boss_robot_id::None)
1129 {
1130 Int3(); // Got boss actions for a robot who's not a boss?
1131 return;
1132 }
1133 const auto &&guarded_teleport_segnum = vmsegptridx.check_untrusted(b.where);
1134 if (!guarded_teleport_segnum)
1135 return;
1136 const auto &&teleport_segnum = *guarded_teleport_segnum;
1137 auto &vcvertptr = Vertices.vcptr;
1138 boss_obj->pos = compute_segment_center(vcvertptr, teleport_segnum);
1139 obj_relink(vmobjptr, vmsegptr, boss_obj, teleport_segnum);
1140 BossUniqueState.Last_teleport_time = {GameTime64};
1141
1142 const auto boss_dir{vm_vec_build_sub(vcobjptr(vcplayerptr(pnum)->objnum)->pos, boss_obj->pos)};
1143 reconstruct_at(boss_obj->orient, vm_vector_to_matrix, boss_dir);
1144
1145 digi_link_sound_to_pos(Vclip[vclip_index::morphing_robot].sound_num, teleport_segnum, sidenum_t::WLEFT, boss_obj->pos, 0, F1_0);
1146 digi_kill_sound_linked_to_object( boss_obj);
1147 boss_link_see_sound(Robot_info, boss_obj);
1148 ai_local *ailp = &boss_obj->ctype.ai_info.ail;
1149 ailp->next_fire = 0;
1150
1151 if (boss_obj->ctype.ai_info.REMOTE_OWNER == Player_num)
1152 {
1153 multi_delete_controlled_robot(boss_obj);
1154 }
1155
1156 boss_obj->ctype.ai_info.REMOTE_OWNER = -1; // Boss is up for grabs again!
1157 boss_obj->ctype.ai_info.REMOTE_SLOT_NUM = 0; // Available immediately!
1158 }
1159
1160 void multi_do_boss_cloak(const multiplayer_rspan<multiplayer_command_t::MULTI_BOSS_CLOAK> buf)
1161 {
1162 auto &BossUniqueState = LevelUniqueObjectState.BossState;
1163 auto &Objects = LevelUniqueObjectState.Objects;
1164 auto &vmobjptridx = Objects.vmptridx;
1165 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
1166 boss_cloak b;
1167 multi_serialize_read(buf, b);
1168 const auto &&guarded_boss_obj = vmobjptridx.check_untrusted(b.objnum);
1169 if (!guarded_boss_obj)
1170 return;
1171 const auto &&boss_obj = *guarded_boss_obj;
1172 if (boss_obj->type != OBJ_ROBOT || Robot_info[get_robot_id(boss_obj)].boss_flag == boss_robot_id::None)
1173 {
1174 Int3(); // Got boss actions for a robot who's not a boss?
1175 return;
1176 }
1177 BossUniqueState.Boss_hit_this_frame = 0;
1178 #if DXX_BUILD_DESCENT == 2
1179 BossUniqueState.Boss_hit_time = -F1_0*10;
1180 #endif
1181 BossUniqueState.Boss_cloak_start_time = {GameTime64};
1182 boss_obj->ctype.ai_info.CLOAKED = 1;
1183 }
1184
1185 void multi_do_boss_start_gate(const multiplayer_rspan<multiplayer_command_t::MULTI_BOSS_START_GATE> buf)
1186 {
1187 auto &Objects = LevelUniqueObjectState.Objects;
1188 auto &vmobjptridx = Objects.vmptridx;
1189 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
1190 boss_start_gate b;
1191 multi_serialize_read(buf, b);
1192 const auto &&guarded_boss_obj = vmobjptridx.check_untrusted(b.objnum);
1193 if (!guarded_boss_obj)
1194 return;
1195 const object_base &boss_obj = *guarded_boss_obj;
1196 if (boss_obj.type != OBJ_ROBOT || Robot_info[get_robot_id(boss_obj)].boss_flag == boss_robot_id::None)
1197 {
1198 Int3(); // Got boss actions for a robot who's not a boss?
1199 return;
1200 }
1201 restart_effect(ECLIP_NUM_BOSS);
1202 }
1203
1204 void multi_do_boss_stop_gate(const multiplayer_rspan<multiplayer_command_t::MULTI_BOSS_STOP_GATE> buf)
1205 {
1206 auto &Objects = LevelUniqueObjectState.Objects;
1207 auto &vmobjptridx = Objects.vmptridx;
1208 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
1209 boss_start_gate b;
1210 multi_serialize_read(buf, b);
1211 const auto &&guarded_boss_obj = vmobjptridx.check_untrusted(b.objnum);
1212 if (!guarded_boss_obj)
1213 return;
1214 const object_base &boss_obj = *guarded_boss_obj;
1215 if (boss_obj.type != OBJ_ROBOT || Robot_info[get_robot_id(boss_obj)].boss_flag == boss_robot_id::None)
1216 {
1217 Int3(); // Got boss actions for a robot who's not a boss?
1218 return;
1219 }
1220 stop_effect(ECLIP_NUM_BOSS);
1221 }
1222
1223 void multi_do_boss_create_robot(const playernum_t pnum, const multiplayer_rspan<multiplayer_command_t::MULTI_BOSS_CREATE_ROBOT> buf)
1224 {
1225 auto &Objects = LevelUniqueObjectState.Objects;
1226 auto &vmobjptridx = Objects.vmptridx;
1227 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
1228 boss_create_robot b;
1229 multi_serialize_read(buf, b);
1230 const auto &&guarded_boss_obj = vmobjptridx.check_untrusted(b.objnum);
1231 if (!guarded_boss_obj)
1232 return;
1233 const object_base &boss_obj = *guarded_boss_obj;
1234 if (boss_obj.type != OBJ_ROBOT || Robot_info[get_robot_id(boss_obj)].boss_flag == boss_robot_id::None)
1235 {
1236 Int3(); // Got boss actions for a robot who's not a boss?
1237 return;
1238 }
1239 // Do some validity checking
1240 if (b.objrobot >= MAX_OBJECTS)
1241 {
1242 Int3(); // See Rob, bad data in boss gate action message
1243 return;
1244 }
1245 if (const auto trusted_robot_type = LevelSharedRobotInfoState.Robot_info.valid_index(b.robot_type); !trusted_robot_type)
1246 return;
1247 // Gate one in!
1248 else if (const auto &&robot = gate_in_robot(Robot_info, *trusted_robot_type, vmsegptridx(b.where)); robot != object_none)
1249 map_objnum_local_to_remote(robot, b.objrobot, pnum);
1250 }
1251
1252 void multi_do_create_robot_powerups(const playernum_t pnum, const multiplayer_rspan<multiplayer_command_t::MULTI_CREATE_ROBOT_POWERUPS> buf)
1253 {
1254 auto &Objects = LevelUniqueObjectState.Objects;
1255 auto &vmobjptr = Objects.vmptr;
1256 // Code to drop remote-controlled robot powerups
1257
1258 int loc{1};
1259 ; loc += 1;
1260 const uint8_t untrusted_contains_count{buf[loc]}; loc += 1;
1261 if (untrusted_contains_count == 0)
1262 return;
1263 const uint8_t untrusted_contains_type = buf[loc]; loc += 1;
1264 const uint8_t untrusted_contains_id = buf[loc]; loc += 1;
1265 const auto segnum{vmsegidx_t::check_nothrow_index(GET_INTEL_SHORT(&buf[loc]))}; loc += 2;
1266 if (!segnum)
1267 return;
1268 const auto pos = multi_get_vector(buf.subspan<7, 12>());
1269 loc += 12;
1270 vms_vector velocity{};
1271
1272 Assert(pnum < N_players);
1273 Assert (pnum!=Player_num); // What? How'd we send ourselves this?
1274
1275 Net_create_loc = 0;
1276 d_srand(1245L);
1277
1278 if (const auto contains_parameters{build_contained_object_parameters_from_untrusted(untrusted_contains_type, untrusted_contains_id, untrusted_contains_count)}; !object_create_robot_egg(LevelSharedRobotInfoState.Robot_info, contains_parameters.type, contains_parameters.id, contains_parameters.count, velocity, pos, vmsegptridx(*segnum)))
1279 return; // Object buffer full
1280
1281 Assert((Net_create_loc > 0) && (Net_create_loc <= MAX_ROBOT_POWERUPS));
1282
1283 range_for (const auto i, partial_const_range(Net_create_objnums, Net_create_loc))
1284 {
1285 const auto s{GET_INTEL_SHORT<int16_t>(&buf[loc])};
1286 if ( s != -1)
1287 map_objnum_local_to_remote(i, s, pnum);
1288 else
1289 vmobjptr(i)->flags |= OF_SHOULD_BE_DEAD; // Delete objects other guy didn't create one of
1290 loc += 2;
1291 }
1292 }
1293
1294 namespace {
1295
1296 void multi_drop_robot_powerups(object &del_obj)
1297 {
1298 // Code to handle dropped robot powerups in network mode ONLY!
1299 if (del_obj.type != OBJ_ROBOT)
1300 {
1301 Int3(); // dropping powerups for non-robot, Rob's fault
1302 return;
1303 }
1304 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
1305 auto &robptr = Robot_info[get_robot_id(del_obj)];
1306
1307 Net_create_loc = 0;
1308
1309 bool created = false;
1310 if (del_obj.contains.count > 0)
1311 {
1312 // If dropping a weapon that the player has, drop energy instead, unless it's vulcan, in which case drop vulcan ammo.
1313 if (del_obj.contains.type == contained_object_type::powerup) {
1314 maybe_replace_powerup_with_energy(del_obj);
1315 if (auto &powerup = del_obj.contains.id.powerup; multi_powerup_is_allowed(powerup, Netgame.AllowedItems) == netflag_flag::None)
1316 powerup = powerup_type_t::POW_SHIELD_BOOST;
1317
1318 // No key drops in non-coop games!
1319 if (!(Game_mode & GM_MULTI_COOP)) {
1320 if (del_obj.contains.id.powerup >= powerup_type_t::POW_KEY_BLUE && del_obj.contains.id.powerup <= powerup_type_t::POW_KEY_GOLD)
1321 return;
1322 }
1323 }
1324 d_srand(1245L);
1325 if (del_obj.contains.count > 0)
1326 created = object_create_robot_egg(LevelSharedRobotInfoState.Robot_info, del_obj);
1327 }
1328
1329 else if (del_obj.ctype.ai_info.REMOTE_OWNER == -1) // No random goodies for robots we weren't in control of
1330 return;
1331
1332 else if (robptr.contains.count)
1333 {
1334 d_srand(static_cast<fix>(timer_query()));
1335 if (((d_rand() * 16) >> 15) < robptr.contains_prob) {
1336 del_obj.contains = robptr.contains;
1337 del_obj.contains.count = ((d_rand() * robptr.contains.count) >> 15) + 1;
1338 if (del_obj.contains.type == contained_object_type::powerup)
1339 {
1340 maybe_replace_powerup_with_energy(del_obj);
1341 if (multi_powerup_is_allowed(del_obj.contains.id.powerup, Netgame.AllowedItems) == netflag_flag::None)
1342 del_obj.contains.id.powerup = powerup_type_t::POW_SHIELD_BOOST;
1343 }
1344
1345 d_srand(1245L);
1346 if (del_obj.contains.count > 0)
1347 created = object_create_robot_egg(LevelSharedRobotInfoState.Robot_info, del_obj);
1348 }
1349 }
1350 if (created)
1351 // Transmit the object creation to the other players
1352 multi_send_create_robot_powerups(del_obj);
1353 }
1354
1355 }
1356
1357 // -----------------------------------------------------------------------------
1358 // Robot *robot got whacked by player player_num and requests permission to do something about it.
1359 // Note: This function will be called regardless of whether Game_mode is a multiplayer mode, so it
1360 // should quick-out if not in a multiplayer mode. On the other hand, it only gets called when a
1361 // player or player weapon whacks a robot, so it happens rarely.
1362 void multi_robot_request_change(const vmobjptridx_t robot, int player_num)
1363 {
1364 if (!(Game_mode & GM_MULTI_ROBOTS))
1365 return;
1366 auto &objrobot = *robot;
1367 #if DXX_BUILD_DESCENT == 1
1368 if (objrobot.ctype.ai_info.REMOTE_OWNER != Player_num)
1369 return;
1370 #endif
1371
1372 const auto slot = objrobot.ctype.ai_info.REMOTE_SLOT_NUM;
1373
1374 if (slot == HANDS_OFF_PERIOD)
1375 {
1376 con_printf(CON_DEBUG, "Suppressing debugger trap for hands off robot %hu with player %i", robot.get_unchecked_index(), player_num);
1377 return;
1378 }
1379 if ((slot < 0) || (slot >= MAX_ROBOTS_CONTROLLED)) {
1380 Int3();
1381 return;
1382 }
1383 if (robot_controlled[slot] == object_none)
1384 return;
1385 const auto &&rcrobot = robot.absolute_sibling(robot_controlled[slot]);
1386 const auto remote_objnum = objnum_local_to_remote(robot).objnum;
1387 if (remote_objnum >= MAX_OBJECTS)
1388 return;
1389
1390 if ( (robot_agitation[slot] < 70) || (MULTI_ROBOT_PRIORITY(remote_objnum, player_num) > MULTI_ROBOT_PRIORITY(remote_objnum, Player_num)) || (d_rand() > 0x4400))
1391 {
1392 if (robot_send_pending[slot] != multi_send_robot_position_priority::_0)
1393 multi_send_robot_position(rcrobot, multi_send_robot_position_priority::_0);
1394 multi_send_release_robot(rcrobot);
1395 objrobot.ctype.ai_info.REMOTE_SLOT_NUM = HANDS_OFF_PERIOD; // Hands-off period
1396 }
1397 }
1398
1399 }