Debian Codegraph @

similar/main/collide.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 * Routines to handle collisions
23 *
24 */
25
26 #include <algorithm>
27 #include <cstdlib>
28 #include <stdexcept>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <type_traits>
32
33 #include "rle.h"
34 #include "inferno.h"
35 #include "game.h"
36 #include "gr.h"
37 #include "bm.h"
38 #include "3d.h"
39 #include "segment.h"
40 #include "laser.h"
41 #include "key.h"
42 #include "gameseg.h"
43 #include "object.h"
44 #include "physics.h"
45 #include "wall.h"
46 #include "vclip.h"
47 #include "polyobj.h"
48 #include "fireball.h"
49 #include "hudmsg.h"
50 #include "laser.h"
51 #include "dxxerror.h"
52 #include "ai.h"
53 #include "hostage.h"
54 #include "sounds.h"
55 #include "robot.h"
56 #include "weapon.h"
57 #include "player.h"
58 #include "gauges.h"
59 #include "powerup.h"
60 #include "scores.h"
61 #include "effects.h"
62 #include "textures.h"
63 #include "multi.h"
64 #include "cntrlcen.h"
65 #include "newdemo.h"
66 #include "endlevel.h"
67 #include "multibot.h"
68 #include "playsave.h"
69 #include "piggy.h"
70 #include "text.h"
71 #include "automap.h"
72 #include "switch.h"
73 #include "palette.h"
74 #include "gameseq.h"
75 #include "collide.h"
76 #include "escort.h"
77
78 #include "d_levelstate.h"
79 #include "partial_range.h"
80 #include <utility>
81
82 using std::min;
83
84 #if DXX_BUILD_DESCENT == 1
85 constexpr auto HAS_VULCAN_AND_GAUSS_FLAGS = HAS_VULCAN_FLAG;
86 #elif DXX_BUILD_DESCENT == 2
87 constexpr auto HAS_VULCAN_AND_GAUSS_FLAGS = HAS_VULCAN_FLAG | HAS_GAUSS_FLAG;
88 #endif
89
90 #if DXX_BUILD_DESCENT == 2
91 namespace dsx {
92 namespace {
93 constexpr std::array<uint8_t, MAX_WEAPON_TYPES> Weapon_is_energy{{
94 1, 1, 1, 1, 1,
95 1, 1, 1, 0, 1,
96 1, 0, 1, 1, 1,
97 0, 1, 0, 0, 1,
98 1, 0, 0, 1, 1,
99 1, 1, 1, 0, 1,
100 1, 1, 0, 1, 1,
101 1
102 }};
103 }
104 }
105 #endif
106
107 #define WALL_DAMAGE_SCALE (128) // Was 32 before 8:55 am on Thursday, September 15, changed by MK, walls were hurting me more than robots!
108 #define WALL_DAMAGE_THRESHOLD (F1_0/3)
109 #define WALL_LOUDNESS_SCALE (20)
110 #define FORCE_DAMAGE_THRESHOLD (F1_0/3)
111 #define STANDARD_EXPL_DELAY (F1_0/4)
112
113 namespace dcx {
114 void bump_one_object(object_base &obj0, const vms_vector &hit_dir, const fix damage)
115 {
116 phys_apply_force(obj0, vm_vec_copy_scale(hit_dir, damage));
117 }
118
119 namespace {
120
121 static int check_collision_delayfunc_exec()
122 {
123 static fix64 last_play_time=0;
124 if (last_play_time + (F1_0/3) < GameTime64 || last_play_time > GameTime64)
125 {
126 last_play_time = {GameTime64};
127 last_play_time -= (d_rand()/2); // add some randomness
128 return 1;
129 }
130 return 0;
131 }
132
133 static fix64 Last_volatile_scrape_time;
134 static fix64 Last_volatile_scrape_sound_time;
135
136 }
137
138 }
139
140 // -------------------------------------------------------------------------------------------------------------
141 // The only reason this routine is called (as of 10/12/94) is so Brain guys can open doors.
142 namespace dsx {
143 namespace {
144 static void collide_robot_and_wall(fvcwallptr &vcwallptr, object &robot, const vmsegptridx_t hitseg, const sidenum_t hitwall, const vms_vector &)
145 {
146 const auto robot_id{get_robot_id(robot)};
147 #if DXX_BUILD_DESCENT == 1
148 if (robot_id == robot_id::brain || robot.ctype.ai_info.behavior == ai_behavior::AIB_RUN_FROM)
149 #elif DXX_BUILD_DESCENT == 2
150 auto &BuddyState = LevelUniqueObjectState.BuddyState;
151 auto &Objects = LevelUniqueObjectState.Objects;
152 auto &vmobjptr = Objects.vmptr;
153 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
154 auto &robptr = Robot_info[robot_id];
155 if (robot_id == robot_id::brain || robot.ctype.ai_info.behavior == ai_behavior::AIB_RUN_FROM || robot_is_companion(robptr) == 1 || robot.ctype.ai_info.behavior == ai_behavior::AIB_SNIPE)
156 #endif
157 {
158 const auto wall_num = hitseg->shared_segment::sides[hitwall].wall_num;
159 if (wall_num != wall_none) {
160 auto &w = *vcwallptr(wall_num);
161 if (w.type == WALL_DOOR && w.keys == wall_key::none && w.state == wall_state::closed && !(w.flags & wall_flag::door_locked))
162 {
163 wall_open_door(hitseg, hitwall);
164 // -- Changed from this, 10/19/95, MK: Don't want buddy getting stranded from player
165 //-- } else if ((Robot_info[robot->id].companion == 1) && (Walls[wall_num].type == WALL_DOOR) && (Walls[wall_num].keys != KEY_NONE) && (Walls[wall_num].state == WALL_DOOR_CLOSED) && !(Walls[wall_num].flags & WALL_DOOR_LOCKED)) {
166 }
167 #if DXX_BUILD_DESCENT == 2
168 else if (robot_is_companion(robptr) && w.type == WALL_DOOR)
169 {
170 ai_local *const ailp = &robot.ctype.ai_info.ail;
171 if (ailp->mode == ai_mode::AIM_GOTO_PLAYER || BuddyState.Escort_special_goal == ESCORT_GOAL_SCRAM) {
172 if (w.keys != wall_key::none)
173 {
174 auto &player_info = get_local_plrobj().ctype.player_info;
175 if (player_info.powerup_flags & static_cast<PLAYER_FLAG>(w.keys))
176 wall_open_door(hitseg, hitwall);
177 }
178 else if (!(w.flags & wall_flag::door_locked))
179 wall_open_door(hitseg, hitwall);
180 }
181 } else if (Robot_info[get_robot_id(robot)].thief) { // Thief allowed to go through doors to which player has key.
182 if (w.keys != wall_key::none)
183 {
184 auto &player_info = get_local_plrobj().ctype.player_info;
185 if (player_info.powerup_flags & static_cast<PLAYER_FLAG>(w.keys))
186 wall_open_door(hitseg, hitwall);
187 }
188 }
189 #endif
190 }
191 }
192
193 return;
194 }
195
196 // -------------------------------------------------------------------------------------------------------------
197
198 static int apply_damage_to_clutter(const d_robot_info_array &Robot_info, const vmobjptridx_t clutter, const fix damage)
199 {
200 if ( clutter->flags&OF_EXPLODING) return 0;
201
202 if (clutter->shields < 0 ) return 0; //clutter already dead...
203
204 clutter->shields -= damage;
205
206 if (clutter->shields < 0) {
207 explode_object(LevelUniqueObjectState, Robot_info, LevelSharedSegmentState, LevelUniqueSegmentState, clutter, 0);
208 return 1;
209 } else
210 return 0;
211 }
212
213 //given the specified force, apply damage from that force to an object
214 static void apply_force_damage(const d_robot_info_array &Robot_info, const vmobjptridx_t obj, const vm_magnitude force, const vmobjptridx_t other_obj)
215 {
216 auto &Objects = LevelUniqueObjectState.Objects;
217 auto &vcobjptr = Objects.vcptr;
218 fix damage;
219
220 if (obj->flags & (OF_EXPLODING|OF_SHOULD_BE_DEAD))
221 return; //already exploding or dead
222
223 damage = fixdiv(force,obj->mtype.phys_info.mass) / 8;
224
225 #if DXX_BUILD_DESCENT == 2
226 if ((other_obj->type == OBJ_PLAYER) && cheats.monsterdamage)
227 damage = INT32_MAX;
228 #endif
229
230 if (damage < FORCE_DAMAGE_THRESHOLD)
231 return;
232
233 switch (obj->type) {
234
235 case OBJ_ROBOT:
236 {
237 auto &robptr = Robot_info[get_robot_id(obj)];
238 const auto other_type = other_obj->type;
239 const auto result = apply_damage_to_robot(Robot_info, obj, (robptr.attack_type == 1)
240 ? damage / 4
241 : damage / 2,
242 (other_type == OBJ_WEAPON)
243 ? other_obj->ctype.laser_info.parent_num
244 : static_cast<objnum_t>(other_obj));
245 if (!result)
246 break;
247
248 const auto console{ConsoleObject};
249 if ((other_type == OBJ_PLAYER && other_obj == console) ||
250 (other_type == OBJ_WEAPON && laser_parent_is_player(vcobjptr, other_obj->ctype.laser_info, *console)))
251 add_points_to_score(console->ctype.player_info, robptr.score_value, Game_mode);
252 break;
253 }
254
255 case OBJ_PLAYER:
256
257 // If colliding with a claw type robot, do damage proportional to FrameTime because you can collide with those
258 // bots every frame since they don't move.
259 if ( (other_obj->type == OBJ_ROBOT) && (Robot_info[get_robot_id(other_obj)].attack_type) )
260 damage = fixmul(damage, FrameTime*2);
261
262 #if DXX_BUILD_DESCENT == 2
263 // Make trainee easier.
264 if (GameUniqueState.Difficulty_level == Difficulty_level_type::_0)
265 damage /= 2;
266 #endif
267
268 apply_damage_to_player(obj,other_obj,damage, apply_damage_player::always);
269 break;
270
271 case OBJ_CLUTTER:
272
273 apply_damage_to_clutter(Robot_info, obj,damage);
274 break;
275
276 case OBJ_CNTRLCEN: // Never hits! Reactor does not have object::movement_type::physics - it's stationary! So no force damage here.
277 apply_damage_to_controlcen(Robot_info, obj, damage, other_obj);
278 break;
279
280 case OBJ_WEAPON:
281
282 break; //weapons don't take damage
283
284 default:
285
286 Int3();
287
288 }
289 }
290
291 // -----------------------------------------------------------------------------
292 static void bump_this_object(const d_robot_info_array &Robot_info, const vmobjptridx_t objp, const vmobjptridx_t other_objp, const vms_vector &force, int damage_flag)
293 {
294 if (! (objp->mtype.phys_info.flags & PF_PERSISTENT))
295 {
296 if (objp->type == OBJ_PLAYER) {
297 vms_vector force2;
298 force2.x = force.x/4;
299 force2.y = force.y/4;
300 force2.z = force.z/4;
301 phys_apply_force(objp,force2);
302 if (damage_flag && (other_objp->type != OBJ_ROBOT || !robot_is_companion(Robot_info[get_robot_id(other_objp)])))
303 {
304 auto force_mag = vm_vec_mag_quick(force2);
305 apply_force_damage(Robot_info, objp, force_mag, other_objp);
306 }
307 } else if ((objp->type == OBJ_ROBOT) || (objp->type == OBJ_CLUTTER) || (objp->type == OBJ_CNTRLCEN)) {
308 if (Robot_info[get_robot_id(objp)].boss_flag == boss_robot_id::None)
309 {
310 vms_vector force2;
311 const auto Difficulty_level = underlying_value(GameUniqueState.Difficulty_level);
312 force2.x = force.x/(4 + Difficulty_level);
313 force2.y = force.y/(4 + Difficulty_level);
314 force2.z = force.z/(4 + Difficulty_level);
315
316 phys_apply_force(objp, force);
317 phys_apply_rot(objp, force2);
318 if (damage_flag) {
319 auto force_mag = vm_vec_mag_quick(force);
320 apply_force_damage(Robot_info, objp, force_mag, other_objp);
321 }
322 }
323 }
324 }
325 }
326
327 // -----------------------------------------------------------------------------
328 //deal with two objects bumping into each other. Apply force from collision
329 //to each robot. The flags tells whether the objects should take damage from
330 //the collision.
331 static void bump_two_objects(const d_robot_info_array &Robot_info, const vmobjptridx_t obj0,const vmobjptridx_t obj1,int damage_flag)
332 {
333 const vmobjptridx_t *pt;
334 if ((obj0->movement_source != object::movement_type::physics && (pt = &obj1, true)) ||
335 (obj1->movement_source != object::movement_type::physics && (pt = &obj0, true)))
336 {
337 object_base &t = *pt;
338 Assert(t.movement_source == object::movement_type::physics);
339 phys_apply_force(t, vm_vec_copy_scale(t.mtype.phys_info.velocity, -t.mtype.phys_info.mass));
340 return;
341 }
342
343 auto force{vm_vec_build_sub(obj0->mtype.phys_info.velocity, obj1->mtype.phys_info.velocity)};
344 vm_vec_scale2(force,2*fixmul(obj0->mtype.phys_info.mass,obj1->mtype.phys_info.mass),(obj0->mtype.phys_info.mass+obj1->mtype.phys_info.mass));
345
346 bump_this_object(Robot_info, obj1, obj0, force, damage_flag);
347 bump_this_object(Robot_info, obj0, obj1, vm_vec_build_negated(force), damage_flag);
348 }
349
350 static void collide_player_and_wall(const vmobjptridx_t playerobj, const fix hitspeed, const vmsegptridx_t hitseg, const sidenum_t hitwall, const vms_vector &hitpt)
351 {
352 auto &Objects = LevelUniqueObjectState.Objects;
353 auto &vmobjptr = Objects.vmptr;
354 auto &TmapInfo = LevelUniqueTmapInfoState.TmapInfo;
355 fix damage;
356
357 if (get_player_id(playerobj) != Player_num) // Execute only for local player
358 return;
359
360 auto &tmi1 = TmapInfo[get_texture_index(hitseg->unique_segment::sides[hitwall].tmap_num)];
361
362 // If this wall does damage, don't make *BONK* sound, we'll be making another sound.
363 if (tmi1.damage > 0)
364 return;
365
366 #if DXX_BUILD_DESCENT == 1
367 const int ForceFieldHit = 0;
368 #elif DXX_BUILD_DESCENT == 2
369 int ForceFieldHit{0};
370 if (tmi1.flags & tmapinfo_flag::force_field)
371 {
372 vms_vector force;
373
374 PALETTE_FLASH_ADD(0, 0, 60); //flash blue
375
376 //knock player around
377 force.x = 40*(d_rand() - 16384);
378 force.y = 40*(d_rand() - 16384);
379 force.z = 40*(d_rand() - 16384);
380 phys_apply_rot(playerobj, force);
381
382 //make sound
383 multi_digi_link_sound_to_pos(sound_effect::SOUND_FORCEFIELD_BOUNCE_PLAYER, hitseg, sidenum_t::WLEFT, hitpt, 0, f1_0);
384 ForceFieldHit=1;
385 }
386 else
387 #endif
388 {
389 auto &player_info = get_local_plrobj().ctype.player_info;
390 wall_hit_process(player_info.powerup_flags, hitseg, hitwall, 20, get_player_id(playerobj), playerobj);
391 }
392
393 // ** Damage from hitting wall **
394 // If the player has less than 10% shields, don't take damage from bump
395 #if DXX_BUILD_DESCENT == 1
396 damage = hitspeed / WALL_DAMAGE_SCALE;
397 #elif DXX_BUILD_DESCENT == 2
398 // Note: Does quad damage if hit a force field - JL
399 damage = (hitspeed / WALL_DAMAGE_SCALE) * (ForceFieldHit*8 + 1);
400
401 const auto tmap_num2 = hitseg->unique_segment::sides[hitwall].tmap_num2;
402
403 //don't do wall damage and sound if hit lava or water
404 constexpr auto tmi_no_damage = (tmapinfo_flag::water | tmapinfo_flag::lava);
405 if ((tmi1.flags & tmi_no_damage) || (tmap_num2 != texture2_value::None && (TmapInfo[get_texture_index(tmap_num2)].flags & tmi_no_damage)))
406 damage = 0;
407 #endif
408
409 if (damage >= WALL_DAMAGE_THRESHOLD) {
410 int volume;
411 volume = (hitspeed-(WALL_DAMAGE_SCALE*WALL_DAMAGE_THRESHOLD)) / WALL_LOUDNESS_SCALE ;
412
413 create_awareness_event(playerobj, player_awareness_type_t::PA_WEAPON_WALL_COLLISION, LevelUniqueRobotAwarenessState);
414
415 if ( volume > F1_0 )
416 volume = F1_0;
417 if (volume > 0 && !ForceFieldHit) { // uhhhgly hack
418 multi_digi_link_sound_to_pos(sound_effect::SOUND_PLAYER_HIT_WALL, hitseg, sidenum_t::WLEFT, hitpt, 0, volume);
419 }
420
421 auto &player_info = playerobj->ctype.player_info;
422 if (!(player_info.powerup_flags & PLAYER_FLAGS_INVULNERABLE))
423 if (playerobj->shields > f1_0*10 || ForceFieldHit)
424 apply_damage_to_player(playerobj, playerobj, damage, apply_damage_player::always);
425
426 // -- No point in doing this unless we compute a reasonable hitpt. Currently it is just the player's position. --MK, 01/18/96
427 // -- if (!(TmapInfo[Segments[hitseg].unique_segment::sides[hitwall].tmap_num].flags & TMI_FORCE_FIELD)) {
428 // -- vms_vector hitpt1;
429 // -- int hitseg1;
430 // --
431 // -- vm_vec_avg(&hitpt1, hitpt, &Objects[Players[Player_num].objnum].pos);
432 // -- hitseg1 = find_point_seg(&hitpt1, Objects[Players[Player_num].objnum].segnum);
433 // -- if (hitseg1 != -1)
434 // -- object_create_explosion( hitseg, hitpt, Weapon_info[0].impact_size, Weapon_info[0].wall_hit_vclip );
435 // -- }
436
437 }
438
439 return;
440 }
441 }
442
443 #if DXX_BUILD_DESCENT == 1
444 static
445 #endif
446 //see if wall is volatile or water
447 //if volatile, cause damage to player
448 //returns 1=lava, 2=water
449 volatile_wall_result check_volatile_wall(const vmobjptridx_t obj, const unique_side &side)
450 {
451 Assert(obj->type==OBJ_PLAYER);
452
453 auto &TmapInfo = LevelUniqueTmapInfoState.TmapInfo;
454 const auto &tmi1 = TmapInfo[get_texture_index(side.tmap_num)];
455 const fix d = tmi1.damage;
456 if (d > 0
457 #if DXX_BUILD_DESCENT == 2
458 || (tmi1.flags & tmapinfo_flag::water)
459 #endif
460 )
461 {
462 #if DXX_BUILD_DESCENT == 2
463 if (get_player_id(obj) == Player_num)
464 #endif
465 {
466 if (!((GameTime64 > Last_volatile_scrape_time + DESIGNATED_GAME_FRAMETIME) || (GameTime64 < Last_volatile_scrape_time)))
467 return volatile_wall_result::none;
468 Last_volatile_scrape_time = {GameTime64};
469
470 #if DXX_BUILD_DESCENT == 2
471 if (d > 0)
472 #endif
473 {
474 fix damage = fixmul(d,((FrameTime>DESIGNATED_GAME_FRAMETIME)?FrameTime:DESIGNATED_GAME_FRAMETIME));
475
476 #if DXX_BUILD_DESCENT == 2
477 if (GameUniqueState.Difficulty_level == Difficulty_level_type::_0)
478 damage /= 2;
479 #endif
480
481 apply_damage_to_player(obj, obj, damage, apply_damage_player::always);
482 PALETTE_FLASH_ADD(f2i(damage*4), 0, 0); //flash red
483 }
484
485 obj->mtype.phys_info.rotvel.x = (d_rand() - 16384)/2;
486 obj->mtype.phys_info.rotvel.z = (d_rand() - 16384)/2;
487 }
488
489 return
490 #if DXX_BUILD_DESCENT == 2
491 (d <= 0)
492 ? volatile_wall_result::water
493 :
494 #endif
495 volatile_wall_result::lava;
496 }
497 else
498 {
499 return volatile_wall_result::none;
500 }
501 }
502
503 //this gets called when an object is scraping along the wall
504 bool scrape_player_on_wall(const vmobjptridx_t obj, const vmsegptridx_t hitseg, const sidenum_t hitside, const vms_vector &hitpt)
505 {
506 if (obj->type != OBJ_PLAYER || get_player_id(obj) != Player_num)
507 return false;
508
509 const auto type = check_volatile_wall(obj, hitseg->unique_segment::sides[hitside]);
510 if (type != volatile_wall_result::none)
511 {
512 if ((GameTime64 > Last_volatile_scrape_sound_time + F1_0/4) || (GameTime64 < Last_volatile_scrape_sound_time)) {
513 Last_volatile_scrape_sound_time = {GameTime64};
514 const auto sound =
515 #if DXX_BUILD_DESCENT == 2
516 (type != volatile_wall_result::lava)
517 ? sound_effect::SOUND_SHIP_IN_WATER
518 :
519 #endif
520 sound_effect::SOUND_VOLATILE_WALL_HISS;
521 multi_digi_link_sound_to_pos(sound, hitseg, sidenum_t::WLEFT, hitpt, 0, F1_0);
522 }
523
524 auto hit_dir = hitseg->shared_segment::sides[hitside].normals[0];
525
526 vm_vec_scale_add2(hit_dir, make_random_vector(), F1_0/8);
527 vm_vec_normalize_quick(hit_dir);
528 bump_one_object(obj, hit_dir, F1_0*8);
529
530 return true;
531 }
532 return false;
533 }
534
535 #if DXX_BUILD_DESCENT == 2
536 namespace {
537 static int effect_parent_is_guidebot(fvcobjptr &vcobjptr, const laser_parent &laser)
538 {
539 if (laser.parent_type != OBJ_ROBOT)
540 return 0;
541 const auto &&robot = vcobjptr(laser.parent_num);
542 if (robot->type != OBJ_ROBOT)
543 return 0;
544 if (robot->signature != laser.parent_signature)
545 /* parent replaced, no idea what it once was */
546 return 0;
547 const auto robot_id = get_robot_id(robot);
548 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
549 auto &robptr = Robot_info[robot_id];
550 return robot_is_companion(robptr);
551 }
552 }
553 #endif
554
555 //if an effect is hit, and it can blow up, then blow it up
556 //returns true if it blew up
557 int check_effect_blowup(const d_level_shared_destructible_light_state &LevelSharedDestructibleLightState, const d_vclip_array &Vclip, const vmsegptridx_t seg, const sidenum_t side, const vms_vector &pnt, const laser_parent &blower, int force_blowup_flag, int remote)
558 {
559 auto &Effects = LevelUniqueEffectsClipState.Effects;
560 auto &TmapInfo = LevelUniqueTmapInfoState.TmapInfo;
561 #if DXX_BUILD_DESCENT == 1
562 static constexpr std::integral_constant<int, 0> force_blowup_flag{};
563 #elif DXX_BUILD_DESCENT == 2
564 auto &Objects = LevelUniqueObjectState.Objects;
565 auto &vcobjptr = Objects.vcptr;
566 const auto wall_num = seg->shared_segment::sides[side].wall_num;
567
568 auto &Walls = LevelUniqueWallSubsystemState.Walls;
569 auto &vcwallptr = Walls.vcptr;
570 const auto is_trigger = wall_num != wall_none && vcwallptr(wall_num)->trigger != trigger_none;
571 if (is_trigger)
572 {
573 if (force_blowup_flag || (
574 (Game_mode & GM_MULTI)
575 // If this wall has a trigger and the blower-upper is not the player or the buddy, abort!
576 // For Multiplayer perform an additional check to see if it's a local-player hit. If a remote player hits, a packet is expected (remote 1) which would be followed by MULTI_TRIGGER to ensure sync with the switch and the actual trigger.
577 ? (!(blower.parent_type == OBJ_PLAYER && (blower.parent_num == get_local_player().objnum || remote)))
578 : !(blower.parent_type == OBJ_PLAYER || effect_parent_is_guidebot(vcobjptr, blower)))
579 )
580 return(0);
581 }
582 #endif
583
584 if (const auto tmap2 = seg->unique_segment::sides[side].tmap_num2; tmap2 != texture2_value::None)
585 {
586 const auto tm = get_texture_index(tmap2); //tm without flags
587 auto &tmi2 = TmapInfo[tm];
588 const auto ec = tmi2.eclip_num;
589 unsigned db{0};
590 if (
591 #if DXX_BUILD_DESCENT == 1
592 ec != eclip_none &&
593 #elif DXX_BUILD_DESCENT == 2
594 //check if it's an animation (monitor) or casts light
595 ec == eclip_none
596 ? tmi2.destroyed != -1
597 :
598 #endif
599 (db = Effects[ec].dest_bm_num) != ~0u && !(Effects[ec].flags & EF_ONE_SHOT)
600 )
601 {
602 const auto tmf = get_texture_rotation_high(tmap2); //tm flags
603 auto &texture2 = Textures[tm];
604 const grs_bitmap *bm = &GameBitmaps[texture2];
605 int x{0},y=0,t;
606
607 PIGGY_PAGE_IN(texture2);
608
609 //this can be blown up...did we hit it?
610
611 if (!force_blowup_flag) {
612 const auto hitpoint = find_hitpoint_uv(pnt,seg,side,0); //evil: always say face zero
613 auto &u = hitpoint.u;
614 auto &v = hitpoint.v;
615
616 x = static_cast<unsigned>(f2i(u*bm->bm_w)) % bm->bm_w;
617 y = static_cast<unsigned>(f2i(v*bm->bm_h)) % bm->bm_h;
618
619 switch (tmf) { //adjust for orientation of paste-on
620 case texture2_rotation_high::Normal:
621 break;
622 case texture2_rotation_high::_1:
623 t = y;
624 y = x;
625 x = bm->bm_w - t - 1;
626 break;
627 case texture2_rotation_high::_2:
628 y = bm->bm_h - y - 1;
629 x = bm->bm_w - x - 1;
630 break;
631 case texture2_rotation_high::_3:
632 t = x;
633 x = y;
634 y = bm->bm_h - t - 1;
635 break;
636 }
637 bm = rle_expand_texture(*bm);
638 }
639
640 #if DXX_BUILD_DESCENT == 1
641 if (gr_gpixel (*bm, x, y) != TRANSPARENCY_COLOR)
642 #elif DXX_BUILD_DESCENT == 2
643 if (force_blowup_flag || (bm->bm_data[y*bm->bm_w+x] != TRANSPARENCY_COLOR))
644 #endif
645 { //not trans, thus on effect
646 #if DXX_BUILD_DESCENT == 2
647 if ((Game_mode & GM_MULTI) && Netgame.AlwaysLighting)
648 if (!(ec != eclip_none && db != -1 && !(Effects[ec].flags & EF_ONE_SHOT)))
649 return 0;
650
651 //note: this must get called before the texture changes,
652 //because we use the light value of the texture to change
653 //the static light in the segment
654 subtract_light(LevelSharedDestructibleLightState, seg, side);
655
656 // we blew up something connected to a trigger. Send it to others!
657 if ((Game_mode & GM_MULTI) && is_trigger && !remote && !force_blowup_flag)
658 multi_send_effect_blowup(seg, side, pnt);
659 #endif
660 if (Newdemo_state == ND_STATE_RECORDING)
661 newdemo_record_effect_blowup( seg, side, pnt);
662
663 fix dest_size;
664 vclip_index vc;
665 if (ec != eclip_none)
666 {
667 dest_size = Effects[ec].dest_size;
668 vc = Effects[ec].dest_vclip;
669 }
670 else
671 {
672 dest_size = i2f(20);
673 vc = vclip_index{3};
674 }
675
676 object_create_explosion_without_damage(Vclip, seg, pnt, dest_size, vc);
677
678 #if DXX_BUILD_DESCENT == 2
679 if (ec != eclip_none && db != -1 && !(Effects[ec].flags & EF_ONE_SHOT))
680 #endif
681 {
682
683 if (const auto sound_num{Vclip[vc].sound_num}; sound_num != sound_effect::None)
684 digi_link_sound_to_pos( sound_num, seg, sidenum_t::WLEFT, pnt, 0, F1_0 );
685
686 if (const auto sound_num{Effects[ec].sound_num}; sound_num != sound_effect::None) //kill sound
687 digi_kill_sound_linked_to_segment(seg,side,sound_num);
688
689 if (Effects[ec].dest_eclip!=-1 && Effects[Effects[ec].dest_eclip].segnum == segment_none)
690 {
691 int bm_num;
692 eclip *new_ec;
693
694 new_ec = &Effects[Effects[ec].dest_eclip];
695 bm_num = new_ec->changing_wall_texture;
696
697 new_ec->time_left = new_ec->vc.frame_time;
698 new_ec->frame_count = 0;
699 new_ec->segnum = seg;
700 new_ec->sidenum = side;
701 new_ec->flags |= EF_ONE_SHOT;
702 new_ec->dest_bm_num = Effects[ec].dest_bm_num;
703
704 assert(bm_num != 0);
705 auto &tmap_num2 = seg->unique_segment::sides[side].tmap_num2;
706 tmap_num2 = build_texture2_value(bm_num, tmf); //replace with destroyed
707
708 }
709 else {
710 assert(db != 0);
711 auto &tmap_num2 = seg->unique_segment::sides[side].tmap_num2;
712 tmap_num2 = build_texture2_value(db, tmf); //replace with destroyed
713 }
714 }
715 #if DXX_BUILD_DESCENT == 2
716 else {
717 seg->unique_segment::sides[side].tmap_num2 = build_texture2_value(tmi2.destroyed, tmf);
718
719 //assume this is a light, and play light sound
720 digi_link_sound_to_pos( sound_effect::SOUND_LIGHT_BLOWNUP, seg, sidenum_t::WLEFT, pnt, 0, F1_0 );
721 }
722 #endif
723
724 return 1; //blew up!
725 }
726 }
727 }
728
729 return 0; //didn't blow up
730 }
731
732 //these gets added to the weapon's values when the weapon hits a volitle wall
733 #define VOLATILE_WALL_EXPL_STRENGTH i2f(10)
734 #define VOLATILE_WALL_IMPACT_SIZE i2f(3)
735 #define VOLATILE_WALL_DAMAGE_FORCE i2f(5)
736 #define VOLATILE_WALL_DAMAGE_RADIUS i2f(30)
737
738 namespace {
739
740 static window_event_result collide_weapon_and_wall(
741 #if DXX_BUILD_DESCENT == 2
742 const d_level_shared_destructible_light_state &LevelSharedDestructibleLightState,
743 #endif
744 const d_robot_info_array &Robot_info, object_array &Objects, fvmsegptridx &vmsegptridx, const vmobjptridx_t weapon, const vmsegptridx_t hitseg, const sidenum_t hitwall, const vms_vector &hitpt)
745 {
746 auto &TmapInfo = LevelUniqueTmapInfoState.TmapInfo;
747 auto &imobjptridx = Objects.imptridx;
748 auto &vcobjptr = Objects.vcptr;
749 auto &vmobjptr = Objects.vmptr;
750 int blew_up;
751 int playernum;
752 auto result = window_event_result::handled;
753
754 #if DXX_BUILD_DESCENT == 1
755 if (weapon->mtype.phys_info.flags & PF_BOUNCE)
756 return window_event_result::ignored;
757 auto &uhitside = hitseg->unique_segment::sides[hitwall];
758 #elif DXX_BUILD_DESCENT == 2
759 auto &vcobjptridx = Objects.vcptridx;
760 if (get_weapon_id(weapon) == weapon_id_type::OMEGA_ID)
761 if (!ok_to_do_omega_damage(weapon)) // see comment in laser.c
762 return window_event_result::ignored;
763
764 // If this is a guided missile and it strikes fairly directly, clear bounce flag.
765 if (get_weapon_id(weapon) == weapon_id_type::GUIDEDMISS_ID) {
766 fix dot;
767
768 dot = vm_vec_build_dot(weapon->orient.fvec, hitseg->shared_segment::sides[hitwall].normals[0]);
769 if (dot < -F1_0/6) {
770 weapon->mtype.phys_info.flags &= ~PF_BOUNCE;
771 }
772 }
773
774 auto &uhitside = hitseg->unique_segment::sides[hitwall];
775 auto &tmi1 = TmapInfo[get_texture_index(uhitside.tmap_num)];
776 //if an energy weapon hits a forcefield, let it bounce
777 if ((tmi1.flags & tmapinfo_flag::force_field) &&
778 !(weapon->type == OBJ_WEAPON && Weapon_info[get_weapon_id(weapon)].energy_usage==0)) {
779
780 //make sound
781 multi_digi_link_sound_to_pos(sound_effect::SOUND_FORCEFIELD_BOUNCE_WEAPON, hitseg, sidenum_t::WLEFT, hitpt, 0, f1_0);
782 return window_event_result::ignored; //bail here. physics code will bounce this object
783 }
784 #endif
785
786 #ifndef NDEBUG
787 if (keyd_pressed[KEY_LAPOSTRO])
788 if (weapon->ctype.laser_info.parent_num == get_local_player().objnum) {
789 // MK: Real pain when you need to know a seg:side and you've got quad lasers.
790 HUD_init_message(HM_DEFAULT, "Hit at segment = %hu, side = %i", static_cast<vmsegptridx_t::integral_type>(hitseg), underlying_value(hitwall));
791 #if DXX_BUILD_DESCENT == 2
792 if (get_weapon_id(weapon) < 4)
793 subtract_light(LevelSharedDestructibleLightState, hitseg, hitwall);
794 else if (get_weapon_id(weapon) == weapon_id_type::FLARE_ID)
795 add_light(LevelSharedDestructibleLightState, hitseg, hitwall);
796 #endif
797 }
798 #endif
799
800 if (weapon->mtype.phys_info.velocity == vms_vector{})
801 {
802 Int3(); // Contact Matt: This is impossible. A weapon with 0 velocity hit a wall, which doesn't move.
803 return window_event_result::ignored;
804 }
805
806 blew_up = check_effect_blowup(LevelSharedDestructibleLightState, Vclip, hitseg, hitwall, hitpt, weapon->ctype.laser_info, 0, 0);
807
808 #if DXX_BUILD_DESCENT == 1
809 const unsigned robot_escort = 0;
810 #elif DXX_BUILD_DESCENT == 2
811 const unsigned robot_escort = effect_parent_is_guidebot(vcobjptr, weapon->ctype.laser_info);
812 if (robot_escort) {
813 /* Guidebot is always associated with the host */
814 playernum = 0;
815 }
816 else
817 #endif
818 {
819 auto &objp = *vcobjptr(weapon->ctype.laser_info.parent_num);
820 if (objp.type == OBJ_PLAYER)
821 playernum = get_player_id(objp);
822 else
823 playernum = -1; //not a player (thus a robot)
824 }
825
826 auto &plrobj = get_local_plrobj();
827 auto &player_info = plrobj.ctype.player_info;
828 #if DXX_BUILD_DESCENT == 2
829 if (blew_up) { //could be a wall switch
830 //for wall triggers, always say that the player shot it out. This is
831 //because robots can shoot out wall triggers, and so the trigger better
832 //take effect
833 // NO -- Changed by MK, 10/18/95. We don't want robots blowing puzzles. Only player or buddy can open!
834 result = check_trigger(hitseg, hitwall, plrobj, vcobjptridx(weapon->ctype.laser_info.parent_num), 1);
835 }
836
837 if (get_weapon_id(weapon) == weapon_id_type::EARTHSHAKER_ID)
838 smega_rock_stuff();
839 #endif
840
841 const auto wall_type = wall_hit_process(player_info.powerup_flags, hitseg, hitwall, weapon->shields, playernum, weapon);
842
843 const auto Difficulty_level = GameUniqueState.Difficulty_level;
844 #if DXX_BUILD_DESCENT == 1
845 auto &tmi1 = TmapInfo[get_texture_index(uhitside.tmap_num)];
846 #endif
847 // Wall is volatile if either tmap 1 or 2 is volatile
848 if ((tmi1.flags & tmapinfo_flag::lava) ||
849 (uhitside.tmap_num2 != texture2_value::None && (TmapInfo[get_texture_index(uhitside.tmap_num2)].flags & tmapinfo_flag::lava))
850 )
851 {
852 weapon_info *wi = &Weapon_info[get_weapon_id(weapon)];
853
854 //we've hit a volatile wall
855
856 digi_link_sound_to_pos( sound_effect::SOUND_VOLATILE_WALL_HIT,hitseg, sidenum_t::WLEFT, hitpt, 0, F1_0 );
857
858 // New by MK: If powerful badass, explode as badass, not due to lava, fixes megas being wimpy in lava.
859 if (wi->damage_radius >= VOLATILE_WALL_DAMAGE_RADIUS/2) {
860 explode_badass_weapon(Robot_info, weapon, hitpt);
861 }
862 else
863 {
864 object_create_badass_explosion(Robot_info, weapon, hitseg, hitpt,
865 wi->impact_size + VOLATILE_WALL_IMPACT_SIZE,
866 //for most weapons, use volatile wall hit. For mega, use its special vclip
867 (get_weapon_id(weapon) == weapon_id_type::MEGA_ID) ? wi->robot_hit_vclip : vclip_index::volatile_wall_hit,
868 wi->strength[Difficulty_level]/4+VOLATILE_WALL_EXPL_STRENGTH, // diminished by mk on 12/08/94, i was doing 70 damage hitting lava on lvl 1.
869 wi->damage_radius+VOLATILE_WALL_DAMAGE_RADIUS,
870 wi->strength[Difficulty_level]/2+VOLATILE_WALL_DAMAGE_FORCE,
871 imobjptridx(weapon->ctype.laser_info.parent_num));
872 }
873
874 weapon->flags |= OF_SHOULD_BE_DEAD; //make flares die in lava
875
876 }
877 #if DXX_BUILD_DESCENT == 2
878 else if ((tmi1.flags & tmapinfo_flag::water) ||
879 (uhitside.tmap_num2 != texture2_value::None && (TmapInfo[get_texture_index(uhitside.tmap_num2)].flags & tmapinfo_flag::water))
880 )
881 {
882 weapon_info *wi = &Weapon_info[get_weapon_id(weapon)];
883
884 //we've hit water
885
886 // MK: 09/13/95: Badass in water is 1/2 normal intensity.
887 if (Weapon_info[get_weapon_id(weapon)].matter != weapon_info::matter_flag::energy)
888 {
889
890 digi_link_sound_to_pos( sound_effect::SOUND_MISSILE_HIT_WATER,hitseg, sidenum_t::WLEFT, hitpt, 0, F1_0 );
891
892 if ( Weapon_info[get_weapon_id(weapon)].damage_radius ) {
893
894 digi_link_sound_to_object(sound_effect::SOUND_BADASS_EXPLOSION, weapon, 0, F1_0, sound_stack::allow_stacking);
895
896 // MK: 09/13/95: Badass in water is 1/2 normal intensity.
897 object_create_badass_explosion(Robot_info, weapon, hitseg, hitpt,
898 wi->impact_size/2,
899 wi->robot_hit_vclip,
900 wi->strength[Difficulty_level]/4,
901 wi->damage_radius,
902 wi->strength[Difficulty_level]/2,
903 imobjptridx(weapon->ctype.laser_info.parent_num));
904 }
905 else
906 object_create_explosion_without_damage(Vclip, vmsegptridx(weapon->segnum), weapon->pos, wi->impact_size, wi->wall_hit_vclip);
907
908 } else {
909 digi_link_sound_to_pos( sound_effect::SOUND_LASER_HIT_WATER,hitseg, sidenum_t::WLEFT, hitpt, 0, F1_0 );
910 object_create_explosion_without_damage(Vclip, vmsegptridx(weapon->segnum), weapon->pos, wi->impact_size, vclip_index::water_hit);
911 }
912
913 weapon->flags |= OF_SHOULD_BE_DEAD; //make flares die in water
914
915 }
916 #endif
917 else {
918
919 #if DXX_BUILD_DESCENT == 2
920 if (weapon->mtype.phys_info.flags & PF_BOUNCE) {
921
922 //do special bound sound & effect
923
924 }
925 else
926 #endif
927 {
928
929 //if it's not the player's weapon, or it is the player's and there
930 //is no wall, and no blowing up monitor, then play sound
931 if ((weapon->ctype.laser_info.parent_type != OBJ_PLAYER) || ((hitseg->shared_segment::sides[hitwall].wall_num == wall_none || wall_type == wall_hit_process_t::WHP_NOT_SPECIAL) && !blew_up))
932 if (const auto wall_hit_sound{Weapon_info[get_weapon_id(weapon)].wall_hit_sound}; wall_hit_sound != sound_effect::None && !(weapon->flags & OF_SILENT))
933 digi_link_sound_to_pos(wall_hit_sound, vmsegptridx(weapon->segnum), sidenum_t::WLEFT, weapon->pos, 0, F1_0);
934
935 if (const auto wall_hit_vclip = Weapon_info[get_weapon_id(weapon)].wall_hit_vclip; Vclip.valid_index(wall_hit_vclip))
936 {
937 if ( Weapon_info[get_weapon_id(weapon)].damage_radius )
938 #if DXX_BUILD_DESCENT == 1
939 explode_badass_weapon(Robot_info, weapon, weapon->pos);
940 #elif DXX_BUILD_DESCENT == 2
941 explode_badass_weapon(Robot_info, weapon, hitpt);
942 #endif
943 else
944 object_create_explosion_without_damage(Vclip, vmsegptridx(weapon->segnum), weapon->pos, Weapon_info[get_weapon_id(weapon)].impact_size, wall_hit_vclip);
945 }
946 }
947 }
948
949 // If weapon fired by player or companion...
950 if (( weapon->ctype.laser_info.parent_type== OBJ_PLAYER ) || robot_escort) {
951
952 if (!(weapon->flags & OF_SILENT) && (weapon->ctype.laser_info.parent_num == get_local_player().objnum))
953 create_awareness_event(weapon, player_awareness_type_t::PA_WEAPON_WALL_COLLISION, LevelUniqueRobotAwarenessState); // object "weapon" can attract attention to player
954
955 // We now allow flares to open doors.
956 {
957 #if DXX_BUILD_DESCENT == 1
958 if (get_weapon_id(weapon) != weapon_id_type::FLARE_ID)
959 weapon->flags |= OF_SHOULD_BE_DEAD;
960 #elif DXX_BUILD_DESCENT == 2
961 if (((get_weapon_id(weapon) != weapon_id_type::FLARE_ID) || (weapon->ctype.laser_info.parent_type != OBJ_PLAYER)) && !(weapon->mtype.phys_info.flags & PF_BOUNCE))
962 weapon->flags |= OF_SHOULD_BE_DEAD;
963
964 //don't let flares stick in force fields
965 if ((get_weapon_id(weapon) == weapon_id_type::FLARE_ID) && (tmi1.flags & tmapinfo_flag::force_field))
966 weapon->flags |= OF_SHOULD_BE_DEAD;
967 #endif
968
969 if (!(weapon->flags & OF_SILENT)) {
970 switch (wall_type) {
971
972 case wall_hit_process_t::WHP_NOT_SPECIAL:
973 //should be handled above
974 //digi_link_sound_to_pos( Weapon_info[weapon->id].wall_hit_sound, weapon->segnum, 0, &weapon->pos, 0, F1_0 );
975 break;
976
977 case wall_hit_process_t::WHP_NO_KEY:
978 //play special hit door sound (if/when we get it)
979 multi_digi_link_sound_to_pos(sound_effect::SOUND_WEAPON_HIT_DOOR, vmsegptridx(weapon->segnum), sidenum_t::WLEFT, weapon->pos, 0, F1_0);
980
981 break;
982
983 case wall_hit_process_t::WHP_BLASTABLE:
984 //play special blastable wall sound (if/when we get it)
985 #if DXX_BUILD_DESCENT == 2
986 if (Weapon_info[get_weapon_id(weapon)].wall_hit_sound != sound_effect::None && !(weapon->flags & OF_SILENT))
987 #endif
988 digi_link_sound_to_pos(sound_effect::SOUND_WEAPON_HIT_BLASTABLE, vmsegptridx(weapon->segnum), sidenum_t::WLEFT, weapon->pos, 0, F1_0);
989 break;
990
991 case wall_hit_process_t::WHP_DOOR:
992 //don't play anything, since door open sound will play
993 break;
994 }
995 } // else
996 } // else {
997 // if (weapon->lifeleft <= 0)
998 // weapon->flags |= OF_SHOULD_BE_DEAD;
999 // }
1000
1001 } else {
1002 // This is a robot's laser
1003 #if DXX_BUILD_DESCENT == 2
1004 if (!(weapon->mtype.phys_info.flags & PF_BOUNCE))
1005 #endif
1006 weapon->flags |= OF_SHOULD_BE_DEAD;
1007 }
1008
1009 return result;
1010 }
1011 }
1012 }
1013
1014 namespace {
1015
1016 static void collide_debris_and_wall(const d_robot_info_array &Robot_info, const vmobjptridx_t debris, const unique_segment &hitseg, const sidenum_t hitwall, const vms_vector &)
1017 {
1018 auto &TmapInfo = LevelUniqueTmapInfoState.TmapInfo;
1019 if (!PERSISTENT_DEBRIS || TmapInfo[get_texture_index(hitseg.sides[hitwall].tmap_num)].damage)
1020 explode_object(LevelUniqueObjectState, Robot_info, LevelSharedSegmentState, LevelUniqueSegmentState, debris, 0);
1021 }
1022
1023 // -------------------------------------------------------------------------------------------------------------------
1024 static void collide_robot_and_robot(const d_robot_info_array &Robot_info, const vmobjptridx_t robot1, const vmobjptridx_t robot2, const vms_vector &)
1025 {
1026 bump_two_objects(Robot_info, robot1, robot2, 1);
1027 }
1028
1029 static void collide_robot_and_controlcen(const d_robot_info_array &, object_base &obj_robot, const object_base &obj_cc, const vms_vector &)
1030 {
1031 assert(obj_cc.type == OBJ_CNTRLCEN);
1032 assert(obj_robot.type == OBJ_ROBOT);
1033 const auto &&hitvec = vm_vec_normalized(vm_vec_build_sub(obj_cc.pos, obj_robot.pos));
1034 bump_one_object(obj_robot, hitvec, 0);
1035 }
1036
1037 }
1038
1039 namespace dsx {
1040
1041 namespace {
1042
1043 static void collide_robot_and_player(const d_robot_info_array &Robot_info, const vmobjptridx_t robot, const vmobjptridx_t playerobj, const vms_vector &collision_point)
1044 {
1045 #if DXX_BUILD_DESCENT == 2
1046 int steal_attempt{0};
1047
1048 if (robot->flags&OF_EXPLODING)
1049 return;
1050 #endif
1051
1052 if (get_player_id(playerobj) == Player_num) {
1053 #if DXX_BUILD_DESCENT == 2
1054 auto &robptr = Robot_info[get_robot_id(robot)];
1055 if (robot_is_companion(robptr)) // Player and companion don't collide.
1056 return;
1057 if (robptr.kamikaze) {
1058 apply_damage_to_robot(Robot_info, robot, robot->shields+1, playerobj);
1059 if (playerobj == ConsoleObject)
1060 add_points_to_score(playerobj->ctype.player_info, robptr.score_value, Game_mode);
1061 }
1062
1063 if (robot_is_thief(robptr)) {
1064 static fix64 Last_thief_hit_time;
1065 ai_local *ailp = &robot->ctype.ai_info.ail;
1066 if (ailp->mode == ai_mode::AIM_THIEF_ATTACK)
1067 {
1068 Last_thief_hit_time = {GameTime64};
1069 attempt_to_steal_item(robot, robptr, playerobj);
1070 steal_attempt = 1;
1071 } else if (GameTime64 - Last_thief_hit_time < F1_0*2)
1072 return; // ZOUNDS! BRILLIANT! Thief not collide with player if not stealing!
1073 // NO! VERY DUMB! makes thief look very stupid if player hits him while cloaked! -AP
1074 else
1075 Last_thief_hit_time = {GameTime64};
1076 }
1077 #endif
1078
1079 create_awareness_event(playerobj, player_awareness_type_t::PA_PLAYER_COLLISION, LevelUniqueRobotAwarenessState); // object robot can attract attention to player
1080 do_ai_robot_hit_attack(Robot_info, robot, playerobj, collision_point);
1081 do_ai_robot_hit(robot, robptr, player_awareness_type_t::PA_WEAPON_ROBOT_COLLISION);
1082 }
1083 else
1084 {
1085 multi_robot_request_change(robot, get_player_id(playerobj));
1086 return; // only controlling player should make damage otherwise we might juggle robot back and forth, killing it instantly
1087 }
1088
1089 if (check_collision_delayfunc_exec())
1090 {
1091 const auto &&player_segp = Segments.vmptridx(playerobj->segnum);
1092 const auto &&collision_seg = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, collision_point, player_segp);
1093
1094 #if DXX_BUILD_DESCENT == 2
1095 // added this if to remove the bump sound if it's the thief.
1096 // A "steal" sound was added and it was getting obscured by the bump. -AP 10/3/95
1097 // Changed by MK to make this sound unless the robot stole.
1098 if ((!steal_attempt) && !Robot_info[get_robot_id(robot)].energy_drain)
1099 #endif
1100 digi_link_sound_to_pos(sound_effect::SOUND_ROBOT_HIT_PLAYER, player_segp, sidenum_t::WLEFT, collision_point, 0, F1_0);
1101
1102 if (collision_seg != segment_none)
1103 object_create_explosion_without_damage(Vclip, collision_seg, collision_point, Weapon_info[0].impact_size, Weapon_info[0].wall_hit_vclip);
1104 }
1105
1106 bump_two_objects(Robot_info, robot, playerobj, 1);
1107 }
1108
1109 }
1110
1111 // Provide a way for network message to instantly destroy the control center
1112 // without awarding points or anything.
1113
1114 // if controlcen == NULL, that means don't do the explosion because the control center
1115 // was actually in another object.
1116 void net_destroy_controlcen_object(const d_robot_info_array &Robot_info, const imobjptridx_t controlcen)
1117 {
1118 auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState;
1119 if (LevelUniqueControlCenterState.Control_center_destroyed != 1)
1120 {
1121 do_controlcen_destroyed_stuff(controlcen);
1122
1123 if ((controlcen != object_none) && !(controlcen->flags&(OF_EXPLODING|OF_DESTROYED))) {
1124 digi_link_sound_to_pos(sound_effect::SOUND_CONTROL_CENTER_DESTROYED, vmsegptridx(controlcen->segnum), sidenum_t::WLEFT, controlcen->pos, 0, F1_0);
1125 explode_object(LevelUniqueObjectState, Robot_info, LevelSharedSegmentState, LevelUniqueSegmentState, controlcen, 0);
1126 }
1127 }
1128 }
1129
1130 // -----------------------------------------------------------------------------
1131 void apply_damage_to_controlcen(const d_robot_info_array &Robot_info, const vmobjptridx_t controlcen, fix damage, const object &who)
1132 {
1133 auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState;
1134 int whotype;
1135
1136 // Only allow a player to damage the control center.
1137 whotype = who.type;
1138 if (whotype != OBJ_PLAYER) {
1139 return;
1140 }
1141
1142 if ((Game_mode & GM_MULTI) &&
1143 !(Game_mode & GM_MULTI_COOP))
1144 {
1145 auto &player = get_local_player();
1146 const auto t = i2f(player.hours_level * 3600) + player.time_level;
1147 if (t < Netgame.control_invul_time)
1148 {
1149 if (get_player_id(who) == Player_num) {
1150 const auto r = f2i(Netgame.control_invul_time - t);
1151 HUD_init_message(HM_DEFAULT, "%s %d:%02d.", TXT_CNTRLCEN_INVUL, r / 60, r % 60);
1152 }
1153 return;
1154 }
1155 }
1156
1157 if (get_player_id(who) == Player_num) {
1158 LevelUniqueControlCenterState.Control_center_been_hit = 1;
1159 ai_do_cloak_stuff();
1160 }
1161
1162 if ( controlcen->shields >= 0 )
1163 controlcen->shields -= damage;
1164
1165 if ( (controlcen->shields < 0) && !(controlcen->flags&(OF_EXPLODING|OF_DESTROYED)) ) {
1166 do_controlcen_destroyed_stuff(controlcen);
1167
1168 if (Game_mode & GM_MULTI) {
1169 if (get_player_id(who) == Player_num)
1170 add_points_to_score(ConsoleObject->ctype.player_info, CONTROL_CEN_SCORE, Game_mode);
1171 multi_send_destroy_controlcen(controlcen, get_player_id(who) );
1172 }
1173 else
1174 add_points_to_score(ConsoleObject->ctype.player_info, CONTROL_CEN_SCORE, Game_mode);
1175
1176 digi_link_sound_to_pos(sound_effect::SOUND_CONTROL_CENTER_DESTROYED, vmsegptridx(controlcen->segnum), sidenum_t::WLEFT, controlcen->pos, 0, F1_0);
1177
1178 explode_object(LevelUniqueObjectState, Robot_info, LevelSharedSegmentState, LevelUniqueSegmentState, controlcen, 0);
1179 }
1180 }
1181
1182 namespace {
1183
1184 static void collide_player_and_controlcen(const d_robot_info_array &Robot_info, const vmobjptridx_t playerobj, const vmobjptridx_t controlcen, const vms_vector &collision_point)
1185 {
1186 auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState;
1187 if (get_player_id(playerobj) == Player_num) {
1188 LevelUniqueControlCenterState.Control_center_been_hit = 1;
1189 ai_do_cloak_stuff(); // In case player cloaked, make control center know where he is.
1190 }
1191
1192 if (check_collision_delayfunc_exec())
1193 digi_link_sound_to_pos(sound_effect::SOUND_ROBOT_HIT_PLAYER, vmsegptridx(playerobj->segnum), sidenum_t::WLEFT, collision_point, 0, F1_0);
1194
1195 bump_two_objects(Robot_info, controlcen, playerobj, 1);
1196 }
1197
1198 #if DXX_BUILD_DESCENT == 2
1199 static void collide_player_and_marker(const d_robot_info_array &, const object_base &playerobj, const vmobjptridx_t marker, const vms_vector &)
1200 {
1201 if (get_player_id(playerobj)==Player_num) {
1202 int drawn;
1203
1204 const auto marker_id = get_marker_id(marker);
1205 auto &msg = MarkerState.message[marker_id];
1206 auto &p = msg[0u];
1207 if (Game_mode & GM_MULTI)
1208 {
1209 const auto marker_owner = get_marker_owner(Game_mode, marker_id, Netgame.max_numplayers);
1210 drawn = HUD_init_message(HM_DEFAULT|HM_MAYDUPL, "MARKER %s: %s", vcplayerptr(marker_owner)->callsign.operator const char *(), &p);
1211 }
1212 else
1213 {
1214 const auto displayed_marker_id = static_cast<unsigned>(marker_id) + 1;
1215 if (p)
1216 drawn = HUD_init_message(HM_DEFAULT|HM_MAYDUPL, "MARKER %d: %s", displayed_marker_id, &p);
1217 else
1218 drawn = HUD_init_message(HM_DEFAULT|HM_MAYDUPL, "MARKER %d", displayed_marker_id);
1219 }
1220
1221 if (drawn)
1222 digi_play_sample( sound_effect::SOUND_MARKER_HIT, F1_0 );
1223
1224 detect_escort_goal_accomplished(marker);
1225 }
1226 }
1227 #endif
1228
1229 // If a persistent weapon and other object is not a weapon, weaken it, else kill it.
1230 // If both objects are weapons, weaken the weapon.
1231 static void maybe_kill_weapon(object_base &weapon, const object_base &other_obj)
1232 {
1233 if (is_proximity_bomb_or_player_smart_mine_or_placed_mine(get_weapon_id(weapon))) {
1234 weapon.flags |= OF_SHOULD_BE_DEAD;
1235 return;
1236 }
1237
1238 #if DXX_BUILD_DESCENT == 1
1239 if (weapon.mtype.phys_info.flags & PF_PERSISTENT || other_obj.type == OBJ_WEAPON)
1240 #elif DXX_BUILD_DESCENT == 2
1241 // Changed, 10/12/95, MK: Make weapon-weapon collisions always kill both weapons if not persistent.
1242 // Reason: Otherwise you can't use proxbombs to detonate incoming homing missiles (or mega missiles).
1243 if (weapon.mtype.phys_info.flags & PF_PERSISTENT)
1244 #endif
1245 {
1246 // Weapons do a lot of damage to weapons, other objects do much less.
1247 if (!(weapon.mtype.phys_info.flags & PF_PERSISTENT)) {
1248 if (other_obj.type == OBJ_WEAPON)
1249 weapon.shields -= other_obj.shields/2;
1250 else
1251 weapon.shields -= other_obj.shields/4;
1252
1253 if (weapon.shields <= 0) {
1254 weapon.shields = 0;
1255 weapon.flags |= OF_SHOULD_BE_DEAD;
1256 }
1257 }
1258 } else
1259 weapon.flags |= OF_SHOULD_BE_DEAD;
1260
1261 // -- if ((weapon->mtype.phys_info.flags & PF_PERSISTENT) || (other_obj->type == OBJ_WEAPON)) {
1262 // -- // Weapons do a lot of damage to weapons, other objects do much less.
1263 // -- if (!(weapon->mtype.phys_info.flags & PF_PERSISTENT)) {
1264 // -- if (other_obj->type == OBJ_WEAPON)
1265 // -- weapon->shields -= other_obj->shields/2;
1266 // -- else
1267 // -- weapon->shields -= other_obj->shields/4;
1268 // --
1269 // -- if (weapon->shields <= 0) {
1270 // -- weapon->shields = 0;
1271 // -- weapon->flags |= OF_SHOULD_BE_DEAD;
1272 // -- }
1273 // -- }
1274 // -- } else
1275 // -- weapon->flags |= OF_SHOULD_BE_DEAD;
1276 }
1277
1278 static void collide_weapon_and_controlcen(const d_robot_info_array &Robot_info, const vmobjptridx_t weapon, const vmobjptridx_t controlcen, vms_vector &collision_point)
1279 {
1280 auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState;
1281 auto &Objects = LevelUniqueObjectState.Objects;
1282 auto &vcobjptr = Objects.vcptr;
1283
1284 #if DXX_BUILD_DESCENT == 1
1285 fix explosion_size = ((controlcen->size/3)*3)/4;
1286 #elif DXX_BUILD_DESCENT == 2
1287 if (get_weapon_id(weapon) == weapon_id_type::OMEGA_ID)
1288 if (!ok_to_do_omega_damage(weapon)) // see comment in laser.c
1289 return;
1290
1291 fix explosion_size = controlcen->size*3/20;
1292 #endif
1293 if (weapon->ctype.laser_info.parent_type == OBJ_PLAYER) {
1294 fix damage = weapon->shields;
1295
1296 /*
1297 * Check if persistent weapon already hit this object. If yes, abort.
1298 * If no, add this object to hitobj_list and do it's damage.
1299 */
1300 if (weapon->mtype.phys_info.flags & PF_PERSISTENT)
1301 {
1302 damage = weapon->shields*2; // to not alter Gameplay too much, multiply damage by 2.
1303 if (weapon->ctype.laser_info.test_set_hitobj(controlcen))
1304 return;
1305 }
1306
1307 if (get_player_id(vcobjptr(weapon->ctype.laser_info.parent_num)) == Player_num)
1308 LevelUniqueControlCenterState.Control_center_been_hit = 1;
1309
1310 if ( Weapon_info[get_weapon_id(weapon)].damage_radius )
1311 {
1312 const auto obj2weapon{vm_vec_build_sub(collision_point, controlcen->pos)};
1313 const auto mag = vm_vec_mag(obj2weapon);
1314 if(mag < controlcen->size && mag > 0) // FVI code does not necessarily update the collision point for object2object collisions. Do that now.
1315 {
1316 collision_point = vm_vec_scale_add(controlcen->pos, obj2weapon, fixdiv(controlcen->size, mag));
1317 weapon->pos = collision_point;
1318 }
1319 #if DXX_BUILD_DESCENT == 1
1320 explode_badass_weapon(Robot_info, weapon, weapon->pos);
1321 #elif DXX_BUILD_DESCENT == 2
1322 explode_badass_weapon(Robot_info, weapon, collision_point);
1323 #endif
1324 }
1325 else
1326 object_create_explosion_without_damage(Vclip, vmsegptridx(controlcen->segnum), collision_point, explosion_size, vclip_index::small_explosion);
1327
1328 digi_link_sound_to_pos(sound_effect::SOUND_CONTROL_CENTER_HIT, vmsegptridx(controlcen->segnum), sidenum_t::WLEFT, collision_point, 0, F1_0);
1329
1330 damage = fixmul(damage, weapon->ctype.laser_info.multiplier);
1331
1332 apply_damage_to_controlcen(Robot_info, controlcen, damage, vcobjptr(weapon->ctype.laser_info.parent_num));
1333
1334 maybe_kill_weapon(weapon,controlcen);
1335 } else { // If robot weapon hits control center, blow it up, make it go away, but do no damage to control center.
1336 object_create_explosion_without_damage(Vclip, vmsegptridx(controlcen->segnum), collision_point, explosion_size, vclip_index::small_explosion);
1337 maybe_kill_weapon(weapon,controlcen);
1338 }
1339 }
1340
1341 static void collide_weapon_and_clutter(const d_robot_info_array &Robot_info, object_base &weapon, const vmobjptridx_t clutter, const vms_vector &collision_point)
1342 {
1343 if ( clutter->shields >= 0 )
1344 clutter->shields -= weapon.shields;
1345
1346 digi_link_sound_to_pos(sound_effect::SOUND_LASER_HIT_CLUTTER, vmsegptridx(weapon.segnum), sidenum_t::WLEFT, collision_point, 0, F1_0);
1347
1348 object_create_explosion_without_damage(Vclip, vmsegptridx(clutter->segnum), collision_point, ((clutter->size / 3) * 3) / 4, vclip_index::small_explosion);
1349
1350 if ( (clutter->shields < 0) && !(clutter->flags&(OF_EXPLODING|OF_DESTROYED)))
1351 explode_object(LevelUniqueObjectState, Robot_info, LevelSharedSegmentState, LevelUniqueSegmentState, clutter, STANDARD_EXPL_DELAY);
1352
1353 maybe_kill_weapon(weapon,clutter);
1354 }
1355
1356 }
1357
1358 //--mk, 121094 -- extern void spin_robot(object *robot, vms_vector *collision_point);
1359
1360 #if DXX_BUILD_DESCENT == 2
1361 // ------------------------------------------------------------------------------------------------------
1362 window_event_result do_final_boss_frame(void)
1363 {
1364 auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState;
1365 auto Final_boss_countdown_time = GameUniqueState.Final_boss_countdown_time;
1366 if (!Final_boss_countdown_time)
1367 return window_event_result::ignored;
1368
1369 if (!LevelUniqueControlCenterState.Control_center_destroyed)
1370 return window_event_result::ignored;
1371
1372 Final_boss_countdown_time -= FrameTime;
1373 if (Final_boss_countdown_time > 0)
1374 {
1375 if (Final_boss_countdown_time <= F1_0*2)
1376 {
1377 int flash_value = f2i(((F1_0*2)-Final_boss_countdown_time)*16); // during final 2 seconds of final boss countdown (as set by do_final_boss_hacks()), make a flash value that maxes out over that time
1378 PALETTE_FLASH_SET(-flash_value,-flash_value,-flash_value); // set palette to inverted flash_value to fade to black
1379 }
1380 GameUniqueState.Final_boss_countdown_time = Final_boss_countdown_time;
1381 return window_event_result::ignored;
1382 }
1383 /* Ensure that GameUniqueState.Final_boss_countdown_time is not 0,
1384 * so that the flag remains raised. If Final_boss_countdown_time
1385 * were greater than 0, the function already returned. If
1386 * Final_boss_countdown_time is exactly 0, write -1. Otherwise,
1387 * write a slightly more negative number. If
1388 * Final_boss_countdown_time is INT32_MIN, this would roll over, but
1389 * that would only happen if FrameTime had an absurd value.
1390 */
1391 GameUniqueState.Final_boss_countdown_time = --Final_boss_countdown_time;
1392
1393 return start_endlevel_sequence(); //pretend we hit the exit trigger
1394 }
1395
1396 // ------------------------------------------------------------------------------------------------------
1397 // This is all the ugly stuff we do when you kill the final boss so that you don't die or something
1398 // which would ruin the logic of the cut sequence.
1399 void do_final_boss_hacks(void)
1400 {
1401 auto &Objects = LevelUniqueObjectState.Objects;
1402 auto &vmobjptr = Objects.vmptr;
1403 if (Player_dead_state != player_dead_state::no)
1404 {
1405 Int3(); // Uh-oh, player is dead. Try to rescue him.
1406 Player_dead_state = player_dead_state::no;
1407 }
1408
1409 auto &plrobj = get_local_plrobj();
1410 auto &player_info = plrobj.ctype.player_info;
1411 {
1412 auto &shields = plrobj.shields;
1413 if (shields <= 0)
1414 shields = 1;
1415 }
1416
1417 // If you're not invulnerable, get invulnerable!
1418 auto &pl_flags = player_info.powerup_flags;
1419 if (!(pl_flags & PLAYER_FLAGS_INVULNERABLE)) {
1420 pl_flags |= PLAYER_FLAGS_INVULNERABLE;
1421 player_info.FakingInvul = 0;
1422 player_info.invulnerable_time = {GameTime64};
1423 }
1424 if (!(Game_mode & GM_MULTI))
1425 buddy_message("Nice job, %s!", static_cast<const char *>(get_local_player().callsign));
1426
1427 GameUniqueState.Final_boss_countdown_time = F1_0*4; // was F1_0*2 originally. extended to F1_0*4 to play fade to black which happened after countdown ended in the original game.
1428 }
1429 #endif
1430
1431 // ------------------------------------------------------------------------------------------------------
1432 // Return 1 if robot died, else return 0
1433 int apply_damage_to_robot(const d_robot_info_array &Robot_info, const vmobjptridx_t robot, fix damage, objnum_t killer_objnum)
1434 {
1435 if ( robot->flags&OF_EXPLODING) return 0;
1436
1437 if (robot->shields < 0 ) return 0; //robot already dead...
1438
1439 auto &robptr = Robot_info[get_robot_id(robot)];
1440 #if DXX_BUILD_DESCENT == 2
1441 auto &BossUniqueState = LevelUniqueObjectState.BossState;
1442 auto &Objects = LevelUniqueObjectState.Objects;
1443 auto &vmobjptr = Objects.vmptr;
1444 if (robptr.boss_flag != boss_robot_id::None)
1445 BossUniqueState.Boss_hit_time = {GameTime64};
1446
1447 // Buddy invulnerable on level 24 so he can give you his important messages. Bah.
1448 // Also invulnerable if his cheat for firing weapons is in effect.
1449 if (robot_is_companion(robptr)) {
1450 if (PLAYING_BUILTIN_MISSION && Current_level_num == Current_mission->last_level)
1451 return 0;
1452 }
1453 #endif
1454
1455 robot->shields -= damage;
1456
1457 #if DXX_BUILD_DESCENT == 2
1458 // Do unspeakable hacks to make sure player doesn't die after killing boss. Or before, sort of.
1459 if (robptr.boss_flag != boss_robot_id::None)
1460 if (PLAYING_BUILTIN_MISSION && Current_level_num == Current_mission->last_level)
1461 if (robot->shields < 0)
1462 {
1463 if (Game_mode & GM_MULTI)
1464 {
1465 if (!multi_all_players_alive(Objects.vcptr, partial_range(Players, N_players))) // everyones gotta be alive
1466 robot->shields=1;
1467 else
1468 {
1469 multi_send_finish_game();
1470 do_final_boss_hacks();
1471 }
1472
1473 }
1474 else
1475 { // NOTE LINK TO ABOVE!!!
1476 if (get_local_plrobj().shields < 0 || Player_dead_state != player_dead_state::no)
1477 robot->shields = 1; // Sorry, we can't allow you to kill the final boss after you've died. Rough luck.
1478 else
1479 do_final_boss_hacks();
1480 }
1481 }
1482 #endif
1483
1484 if (robot->shields < 0) {
1485 auto &plr = get_local_player();
1486 plr.num_kills_level++;
1487 plr.num_kills_total++;
1488 if (Game_mode & GM_MULTI) {
1489 if (multi_explode_robot_sub(Robot_info, robot))
1490 {
1491 multi_send_robot_explode(robot, killer_objnum);
1492 return 1;
1493 }
1494 else
1495 return 0;
1496 }
1497
1498 if (robptr.boss_flag != boss_robot_id::None)
1499 {
1500 start_boss_death_sequence(LevelUniqueObjectState.BossState, Robot_info, robot);
1501 }
1502 #if DXX_BUILD_DESCENT == 2
1503 else if (robptr.death_roll) {
1504 start_robot_death_sequence(robot); //do_controlcen_destroyed_stuff(NULL);
1505 }
1506 #endif
1507 else {
1508 #if DXX_BUILD_DESCENT == 2
1509 if (get_robot_id(robot) == robot_id::special_reactor)
1510 special_reactor_stuff();
1511 #endif
1512 // Kamikaze, explode right away, IN YOUR FACE!
1513 explode_object(LevelUniqueObjectState, Robot_info, LevelSharedSegmentState, LevelUniqueSegmentState, robot,
1514 #if DXX_BUILD_DESCENT == 2
1515 robptr.kamikaze ? 1 :
1516 #endif
1517 STANDARD_EXPL_DELAY);
1518 }
1519 return 1;
1520 } else
1521 return 0;
1522 }
1523
1524 namespace {
1525
1526 #if DXX_BUILD_DESCENT == 2
1527 enum class boss_weapon_collision_result : uint8_t
1528 {
1529 normal,
1530 invulnerable,
1531 reflect, // implies invulnerable
1532 };
1533
1534 static inline int Boss_invulnerable_dot()
1535 {
1536 return F1_0 / 4 - i2f(underlying_value(GameUniqueState.Difficulty_level)) / 8;
1537 }
1538
1539 // ------------------------------------------------------------------------------------------------------
1540 // Return true if damage done to boss, else return false.
1541 static boss_weapon_collision_result do_boss_weapon_collision(const d_robot_info_array &Robot_info, const d_level_shared_segment_state &LevelSharedSegmentState, d_level_unique_segment_state &LevelUniqueSegmentState, const vcobjptridx_t robotptridx, object &weapon, const vms_vector &collision_point)
1542 {
1543 auto &BossUniqueState = LevelUniqueObjectState.BossState;
1544 auto &BuddyState = LevelUniqueObjectState.BuddyState;
1545 const object_base &robot = robotptridx;
1546 const auto d2_boss_index = build_boss_robot_index_from_boss_robot_id(Robot_info[get_robot_id(robot)].boss_flag);
1547 assert(Boss_spew_more.valid_index(d2_boss_index));
1548
1549 // See if should spew a bot.
1550 if (weapon.ctype.laser_info.parent_type == OBJ_PLAYER && weapon.ctype.laser_info.parent_num == get_local_player().objnum)
1551 if ((Weapon_info[get_weapon_id(weapon)].matter != weapon_info::matter_flag::energy
1552 ? Boss_spews_bots_matter
1553 : Boss_spews_bots_energy)[d2_boss_index])
1554 {
1555 if (Boss_spew_more[d2_boss_index])
1556 if (d_rand() > 16384) {
1557 const auto &&spew = boss_spew_robot(Robot_info, robot, collision_point);
1558 if (spew != object_none)
1559 {
1560 BossUniqueState.Last_gate_time = GameTime64 - GameUniqueState.Boss_gate_interval - 1; // Force allowing spew of another bot.
1561 if (Game_mode & GM_MULTI)
1562 multi_send_boss_create_robot(robotptridx, spew);
1563 }
1564 }
1565 const auto &&spew = boss_spew_robot(Robot_info, robot, collision_point);
1566 if (spew != object_none)
1567 {
1568 if (Game_mode & GM_MULTI)
1569 multi_send_boss_create_robot(robotptridx, spew);
1570 }
1571 }
1572
1573 if (Boss_invulnerable_spot[d2_boss_index]) {
1574 fix dot;
1575 // Boss only vulnerable in back. See if hit there.
1576 // Note, if BOSS_INVULNERABLE_DOT is close to F1_0 (in magnitude), then should probably use non-quick version.
1577 const auto tvec1 = vm_vec_normalized_quick(vm_vec_build_sub(collision_point, robot.pos));
1578 dot = vm_vec_build_dot(tvec1, robot.orient.fvec);
1579 if (dot > Boss_invulnerable_dot()) {
1580 if (const auto &&segp = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, collision_point, Segments.vmptridx(robot.segnum)))
1581 digi_link_sound_to_pos(sound_effect::SOUND_WEAPON_HIT_DOOR, segp, sidenum_t::WLEFT, collision_point, 0, F1_0);
1582 if (BuddyState.Buddy_objnum != object_none)
1583 {
1584 if (BuddyState.Buddy_gave_hint_count) {
1585 if (BuddyState.Last_time_buddy_gave_hint + F1_0*20 < GameTime64) {
1586 int sval;
1587
1588 BuddyState.Buddy_gave_hint_count--;
1589 BuddyState.Last_time_buddy_gave_hint = {GameTime64};
1590 sval = (d_rand()*4) >> 15;
1591 const char *msg;
1592 switch (sval) {
1593 case 0:
1594 msg = "Hit him in the back!";
1595 break;
1596 case 1:
1597 msg = "He's invulnerable there!";
1598 break;
1599 case 2:
1600 msg = "Get behind him and fire!";
1601 break;
1602 case 3:
1603 default:
1604 msg = "Hit the glowing spot!";
1605 break;
1606 }
1607 buddy_message_str(msg);
1608 }
1609 }
1610 }
1611
1612 // Cause weapon to bounce.
1613 if (Weapon_info[get_weapon_id(weapon)].matter == weapon_info::matter_flag::energy)
1614 {
1615 auto vec_to_point = vm_vec_normalized_quick(vm_vec_build_sub(collision_point, robot.pos));
1616 const auto &&[speed, weap_vec] = vm_vec_normalize_quick_with_magnitude(weapon.mtype.phys_info.velocity);
1617 vm_vec_scale_add2(vec_to_point, weap_vec, -F1_0*2);
1618 vm_vec_scale(vec_to_point, speed/4);
1619 weapon.mtype.phys_info.velocity = vec_to_point;
1620 return boss_weapon_collision_result::reflect;
1621 }
1622 return boss_weapon_collision_result::invulnerable;
1623 }
1624 }
1625 else if ((Weapon_info[get_weapon_id(weapon)].matter != weapon_info::matter_flag::energy
1626 ? Boss_invulnerable_matter
1627 : Boss_invulnerable_energy)[d2_boss_index])
1628 {
1629 if (const auto &&segp = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, collision_point, Segments.vmptridx(robot.segnum)))
1630 digi_link_sound_to_pos(sound_effect::SOUND_WEAPON_HIT_DOOR, segp, sidenum_t::WLEFT, collision_point, 0, F1_0);
1631 return boss_weapon_collision_result::invulnerable;
1632 }
1633 return boss_weapon_collision_result::normal;
1634 }
1635 #endif
1636 // ------------------------------------------------------------------------------------------------------
1637 static void collide_robot_and_weapon(const d_robot_info_array &Robot_info, const vmobjptridx_t robot, const vmobjptridx_t weapon, vms_vector &collision_point)
1638 {
1639 auto &BossUniqueState = LevelUniqueObjectState.BossState;
1640 auto &Objects = LevelUniqueObjectState.Objects;
1641 auto &vcobjptr = Objects.vcptr;
1642 #if DXX_BUILD_DESCENT == 2
1643 auto &icobjptridx = Objects.icptridx;
1644 if (get_weapon_id(weapon) == weapon_id_type::OMEGA_ID)
1645 if (!ok_to_do_omega_damage(weapon)) // see comment in laser.c
1646 return;
1647 #endif
1648
1649 auto &robptr = Robot_info[get_robot_id(robot)];
1650 if (robptr.boss_flag != boss_robot_id::None)
1651 {
1652 BossUniqueState.Boss_hit_this_frame = 1;
1653 #if DXX_BUILD_DESCENT == 2
1654 BossUniqueState.Boss_hit_time = {GameTime64};
1655 #endif
1656 }
1657 #if DXX_BUILD_DESCENT == 2
1658 const boss_weapon_collision_result damage_flag = Boss_invulnerable_matter.valid_index(build_boss_robot_index_from_boss_robot_id(robptr.boss_flag))
1659 ? do_boss_weapon_collision(Robot_info, LevelSharedSegmentState, LevelUniqueSegmentState, robot, weapon, collision_point)
1660 : boss_weapon_collision_result::normal;
1661 #endif
1662
1663 #if DXX_BUILD_DESCENT == 2
1664 // Put in at request of Jasen (and Adam) because the Buddy-Bot gets in their way.
1665 // MK has so much fun whacking his butt around the mine he never cared...
1666 if ((robot_is_companion(robptr)) && ((weapon->ctype.laser_info.parent_type != OBJ_ROBOT) && !cheats.robotskillrobots))
1667 return;
1668
1669 if (get_weapon_id(weapon) == weapon_id_type::EARTHSHAKER_ID)
1670 smega_rock_stuff();
1671 #endif
1672
1673 /*
1674 * Check if persistent weapon already hit this object. If yes, abort.
1675 * If no, add this object to hitobj_list and do it's damage.
1676 */
1677 if (weapon->mtype.phys_info.flags & PF_PERSISTENT)
1678 {
1679 if (weapon->ctype.laser_info.test_set_hitobj(robot))
1680 return;
1681 }
1682
1683 if (laser_parent_is_object(weapon->ctype.laser_info, robot))
1684 return;
1685
1686 #if DXX_BUILD_DESCENT == 2
1687 const auto Difficulty_level = GameUniqueState.Difficulty_level;
1688 // Changed, 10/04/95, put out blobs based on skill level and power of weapon doing damage.
1689 // Also, only a weapon hit from a player weapon causes smart blobs.
1690 if ((weapon->ctype.laser_info.parent_type == OBJ_PLAYER) && (robptr.energy_blobs))
1691 if ((robot->shields > 0) && Weapon_is_energy[get_weapon_id(weapon)]) {
1692 fix probval;
1693 int num_blobs;
1694
1695 probval = (underlying_value(Difficulty_level) + 2) * min(weapon->shields, robot->shields);
1696 probval = robptr.energy_blobs * probval/(NDL*32);
1697
1698 num_blobs = probval >> 16;
1699 if (2*d_rand() < (probval & 0xffff))
1700 num_blobs++;
1701
1702 if (num_blobs)
1703 create_robot_smart_children(robot, num_blobs);
1704 }
1705
1706 // Note: If weapon hits an invulnerable boss, it will still do badass damage, including to the boss,
1707 // unless this is trapped elsewhere.
1708 #endif
1709 weapon_info *wi = &Weapon_info[get_weapon_id(weapon)];
1710 if ( wi->damage_radius )
1711 {
1712 const auto obj2weapon{vm_vec_build_sub(collision_point, robot->pos)};
1713 const auto mag = vm_vec_mag(obj2weapon);
1714 if(mag < robot->size && mag > 0) // FVI code does not necessarily update the collision point for object2object collisions. Do that now.
1715 {
1716 collision_point = vm_vec_scale_add(robot->pos, obj2weapon, fixdiv(robot->size, mag));
1717 weapon->pos = collision_point;
1718 }
1719 #if DXX_BUILD_DESCENT == 1
1720 explode_badass_weapon(Robot_info, weapon, weapon->pos);
1721 #elif DXX_BUILD_DESCENT == 2
1722 if (damage_flag != boss_weapon_collision_result::normal) { //don't make badass sound
1723
1724 //this code copied from explode_badass_weapon()
1725
1726 object_create_badass_explosion(Robot_info, weapon, vmsegptridx(weapon->segnum), collision_point,
1727 wi->impact_size,
1728 wi->robot_hit_vclip,
1729 wi->strength[Difficulty_level],
1730 wi->damage_radius,wi->strength[Difficulty_level],
1731 icobjptridx(weapon->ctype.laser_info.parent_num));
1732
1733 }
1734 else //normal badass explosion
1735 explode_badass_weapon(Robot_info, weapon, collision_point);
1736 #endif
1737 }
1738
1739 #if DXX_BUILD_DESCENT == 1
1740 if ( (weapon->ctype.laser_info.parent_type==OBJ_PLAYER) && !(robot->flags & OF_EXPLODING) )
1741 #elif DXX_BUILD_DESCENT == 2
1742 if ( ((weapon->ctype.laser_info.parent_type==OBJ_PLAYER) || cheats.robotskillrobots) && !(robot->flags & OF_EXPLODING) )
1743 #endif
1744 {
1745 if (weapon->ctype.laser_info.parent_num == get_local_player().objnum) {
1746 create_awareness_event(weapon, player_awareness_type_t::PA_WEAPON_ROBOT_COLLISION, LevelUniqueRobotAwarenessState); // object "weapon" can attract attention to player
1747 do_ai_robot_hit(robot, robptr, player_awareness_type_t::PA_WEAPON_ROBOT_COLLISION);
1748 }
1749 else
1750 multi_robot_request_change(robot, get_player_id(vcobjptr(weapon->ctype.laser_info.parent_num)));
1751
1752 std::pair<fix, vclip_index> explosion_size_and_vclip;
1753 if ((Vclip.valid_index(robptr.exp1_vclip_num) && (explosion_size_and_vclip = {(robot->size / 2 * 3) / 4, robptr.exp1_vclip_num}, true))
1754 #if DXX_BUILD_DESCENT == 2
1755 || (Vclip.valid_index(wi->robot_hit_vclip) && (explosion_size_and_vclip = {wi->impact_size, wi->robot_hit_vclip}, true))
1756 #endif
1757 )
1758 {
1759 const auto &&expl_obj = object_create_explosion_without_damage(Vclip, vmsegptridx(weapon->segnum), collision_point, explosion_size_and_vclip.first, explosion_size_and_vclip.second);
1760
1761 if (expl_obj != object_none)
1762 obj_attach(Objects, robot, expl_obj);
1763 }
1764
1765 #if DXX_BUILD_DESCENT == 2
1766 if (damage_flag == boss_weapon_collision_result::normal)
1767 #endif
1768 {
1769 if (const auto exp1_sound_num{robptr.exp1_sound_num}; exp1_sound_num != sound_effect::None)
1770 digi_link_sound_to_pos(exp1_sound_num, vcsegptridx(robot->segnum), sidenum_t::WLEFT, collision_point, 0, F1_0);
1771 fix damage = weapon->shields;
1772
1773 damage = fixmul(damage, weapon->ctype.laser_info.multiplier);
1774
1775 #if DXX_BUILD_DESCENT == 2
1776 // Cut Gauss damage on bosses because it just breaks the game. Bosses are so easy to
1777 // hit, and missing a robot is what prevents the Gauss from being game-breaking.
1778 if (get_weapon_id(weapon) == weapon_id_type::GAUSS_ID)
1779 if (robptr.boss_flag != boss_robot_id::None)
1780 damage = damage * (2 * NDL - underlying_value(Difficulty_level)) / (2 * NDL);
1781 #endif
1782
1783 if (!apply_damage_to_robot(Robot_info, robot, damage, weapon->ctype.laser_info.parent_num))
1784 bump_two_objects(Robot_info, robot, weapon, 0); //only bump if not dead. no damage from bump
1785 else if (laser_parent_is_player(vcobjptr, weapon->ctype.laser_info, *ConsoleObject))
1786 {
1787 add_points_to_score(ConsoleObject->ctype.player_info, robptr.score_value, Game_mode);
1788 detect_escort_goal_accomplished(robot);
1789 }
1790 }
1791
1792 #if DXX_BUILD_DESCENT == 2
1793 // If Gauss Cannon, spin robot.
1794 if (!robot_is_companion(robptr) && robptr.boss_flag == boss_robot_id::None && (get_weapon_id(weapon) == weapon_id_type::GAUSS_ID))
1795 {
1796 ai_static *aip = &robot->ctype.ai_info;
1797
1798 if (aip->SKIP_AI_COUNT * FrameTime < F1_0) {
1799 aip->SKIP_AI_COUNT++;
1800 robot->mtype.phys_info.rotthrust.x = fixmul((d_rand() - 16384), FrameTime * aip->SKIP_AI_COUNT);
1801 robot->mtype.phys_info.rotthrust.y = fixmul((d_rand() - 16384), FrameTime * aip->SKIP_AI_COUNT);
1802 robot->mtype.phys_info.rotthrust.z = fixmul((d_rand() - 16384), FrameTime * aip->SKIP_AI_COUNT);
1803 robot->mtype.phys_info.flags |= PF_USES_THRUST;
1804
1805 }
1806 }
1807 #endif
1808
1809 }
1810
1811 #if DXX_BUILD_DESCENT == 2
1812 if (damage_flag != boss_weapon_collision_result::reflect)
1813 #endif
1814 maybe_kill_weapon(weapon,robot);
1815
1816 return;
1817 }
1818
1819 static void collide_hostage_and_player(const d_robot_info_array &, const vmobjptridx_t hostage, object &player, const vms_vector &)
1820 {
1821 // Give player points, etc.
1822 if (&player == ConsoleObject)
1823 {
1824 detect_escort_goal_accomplished(hostage);
1825 add_points_to_score(player.ctype.player_info, HOSTAGE_SCORE, Game_mode);
1826
1827 // Do effect
1828 hostage_rescue();
1829
1830 // Remove the hostage object.
1831 hostage->flags |= OF_SHOULD_BE_DEAD;
1832
1833 if (Game_mode & GM_MULTI)
1834 multi_send_remobj(hostage);
1835 }
1836 return;
1837 }
1838
1839 static void collide_player_and_player(const d_robot_info_array &Robot_info, const vmobjptridx_t player1, const vmobjptridx_t player2, const vms_vector &collision_point)
1840 {
1841 int damage_flag{1};
1842
1843 if (check_collision_delayfunc_exec())
1844 digi_link_sound_to_pos(sound_effect::SOUND_ROBOT_HIT_PLAYER, vcsegptridx(player1->segnum), sidenum_t::WLEFT, collision_point, 0, F1_0);
1845
1846 // Multi is special - as always. Clients do the bump damage locally but the remote players do their collision result (change velocity) on their own. So after our initial collision, ignore further collision damage till remote players (hopefully) react.
1847 if (Game_mode & GM_MULTI)
1848 {
1849 damage_flag = 0;
1850 if (!(get_player_id(player1) == Player_num || get_player_id(player2) == Player_num))
1851 return;
1852 auto &last_player_bump = ((get_player_id(player1) == Player_num)
1853 ? player2
1854 : player1)->ctype.player_info.Last_bumped_local_player;
1855 if (last_player_bump + (F1_0/Netgame.PacketsPerSec) < GameTime64 || last_player_bump > GameTime64)
1856 {
1857 last_player_bump = {GameTime64};
1858 damage_flag = 1;
1859 }
1860 }
1861 bump_two_objects(Robot_info, player1, player2, damage_flag);
1862 }
1863
1864 static imobjptridx_t maybe_drop_primary_weapon_egg(const object &playerobj, const primary_weapon_index_t weapon_index)
1865 {
1866 int weapon_flag = HAS_PRIMARY_FLAG(weapon_index);
1867 const auto powerup_num = Primary_weapon_to_powerup[weapon_index];
1868 auto &player_info = playerobj.ctype.player_info;
1869 if (player_info.primary_weapon_flags & weapon_flag)
1870 return call_object_create_egg(playerobj, powerup_num);
1871 else
1872 return object_none;
1873 }
1874
1875 static void maybe_drop_secondary_weapon_egg(const object_base &playerobj, const secondary_weapon_index_t weapon_index, const unsigned count)
1876 {
1877 const auto powerup_num = Secondary_weapon_to_powerup[weapon_index];
1878 call_object_create_egg(playerobj, std::min(count, 3u), powerup_num);
1879 }
1880
1881 static void maybe_drop_primary_weapon_with_adjustment(const object &playerobj, const primary_weapon_index_t weapon_index, auto adjust)
1882 {
1883 if (const auto objp{maybe_drop_primary_weapon_egg(playerobj, weapon_index)}; objp != object_none)
1884 adjust(*objp);
1885 }
1886
1887 static void maybe_drop_primary_vulcan_based_weapon(const object &playerobj, const primary_weapon_index_t weapon_index, const uint16_t ammo)
1888 {
1889 maybe_drop_primary_weapon_with_adjustment(playerobj, weapon_index,
1890 [ammo](object &weapon) {
1891 //make sure gun has at least as much as a powerup
1892 weapon.ctype.powerup_info.count = std::max(ammo, VULCAN_AMMO_AMOUNT);
1893 }
1894 );
1895 }
1896
1897 static void maybe_drop_primary_vulcan_weapons(const object &playerobj)
1898 {
1899 auto &player_info = playerobj.ctype.player_info;
1900 const auto has_weapons{player_info.primary_weapon_flags & HAS_VULCAN_AND_GAUSS_FLAGS};
1901 if (!has_weapons)
1902 return;
1903 const auto total_vulcan_ammo = player_info.vulcan_ammo;
1904 auto vulcan_ammo = total_vulcan_ammo;
1905 #if DXX_BUILD_DESCENT == 2
1906 auto gauss_ammo = vulcan_ammo;
1907 if (has_weapons == HAS_VULCAN_AND_GAUSS_FLAGS)
1908 {
1909 //if both vulcan & gauss, each gets half
1910 vulcan_ammo /= 2;
1911 gauss_ammo -= vulcan_ammo;
1912 }
1913 #endif
1914 maybe_drop_primary_vulcan_based_weapon(playerobj, primary_weapon_index_t::VULCAN_INDEX, vulcan_ammo);
1915 #if DXX_BUILD_DESCENT == 2
1916 maybe_drop_primary_vulcan_based_weapon(playerobj, primary_weapon_index_t::GAUSS_INDEX, gauss_ammo);
1917 #endif
1918 }
1919
1920 #if DXX_BUILD_DESCENT == 2
1921 static void maybe_drop_primary_omega_weapon(const object &playerobj)
1922 {
1923 maybe_drop_primary_weapon_with_adjustment(playerobj, primary_weapon_index_t::OMEGA_INDEX,
1924 [&playerobj](object &weapon) {
1925 weapon.ctype.powerup_info.count = (get_player_id(playerobj) == Player_num) ? playerobj.ctype.player_info.Omega_charge : MAX_OMEGA_CHARGE;
1926 }
1927 );
1928 }
1929 #endif
1930
1931 static void drop_missile_1_or_4(const object &playerobj, const secondary_weapon_index_t missile_index)
1932 {
1933 unsigned num_missiles = playerobj.ctype.player_info.secondary_ammo[missile_index];
1934 const auto powerup_id = Secondary_weapon_to_powerup[missile_index];
1935
1936 if (num_missiles > 10)
1937 num_missiles = 10;
1938
1939 call_object_create_egg(playerobj, num_missiles / 4, static_cast<powerup_type_t>(underlying_value(powerup_id) + 1));
1940 call_object_create_egg(playerobj, num_missiles % 4, powerup_id);
1941 }
1942
1943 }
1944
1945 void drop_player_eggs(const vmobjptridx_t playerobj)
1946 {
1947 if ((playerobj->type == OBJ_PLAYER) || (playerobj->type == OBJ_GHOST)) {
1948 // Seed the random number generator so in net play the eggs will always
1949 // drop the same way
1950 if (Game_mode & GM_MULTI)
1951 {
1952 Net_create_loc = 0;
1953 }
1954 auto &player_info = playerobj->ctype.player_info;
1955 auto &plr_laser_level = player_info.laser_level;
1956 if (const auto GrantedItems = (Game_mode & GM_MULTI) ? (d_srand(5483L), Netgame.SpawnGrantedItems) : netgrant_flag::None)
1957 {
1958 if (const auto granted_laser_level = map_granted_flags_to_laser_level(GrantedItems); granted_laser_level != laser_level::_1)
1959 {
1960 if (plr_laser_level <= granted_laser_level)
1961 /* All levels were from grant */
1962 plr_laser_level = laser_level::_1;
1963 #if DXX_BUILD_DESCENT == 2
1964 else if (granted_laser_level > MAX_LASER_LEVEL)
1965 {
1966 /* Grant gives super laser 5.
1967 * Player has super laser 6.
1968 */
1969 -- plr_laser_level;
1970 }
1971 else if (plr_laser_level > MAX_LASER_LEVEL)
1972 {
1973 /* Grant gives only regular lasers.
1974 * Player has super lasers, will drop only
1975 * super lasers.
1976 */
1977 }
1978 #endif
1979 else
1980 plr_laser_level -= granted_laser_level;
1981 }
1982 if (uint16_t subtract_vulcan_ammo = map_granted_flags_to_vulcan_ammo(GrantedItems))
1983 {
1984 auto &v = playerobj->ctype.player_info.vulcan_ammo;
1985 if (v < subtract_vulcan_ammo)
1986 v = 0;
1987 else
1988 v -= subtract_vulcan_ammo;
1989 }
1990 player_info.powerup_flags &= ~map_granted_flags_to_player_flags(GrantedItems);
1991 player_info.primary_weapon_flags &= ~map_granted_flags_to_primary_weapon_flags(GrantedItems);
1992 }
1993
1994 auto &secondary_ammo = playerobj->ctype.player_info.secondary_ammo;
1995 #if DXX_BUILD_DESCENT == 2
1996 // If the player had smart mines, maybe arm one of them.
1997 const auto drop_armed_bomb = [&](uint8_t mines, weapon_id_type id) {
1998 mines %= 4;
1999 for (int rthresh = 30000; mines && d_rand() < rthresh; rthresh /= 2)
2000 {
2001 const auto randvec = make_random_vector();
2002 const auto tvec = vm_vec_build_add(playerobj->pos, randvec);
2003 const auto &&newseg = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, tvec, Segments.vmptridx(playerobj->segnum));
2004 if (newseg != segment_none)
2005 {
2006 -- mines;
2007 /* As a special case, decrease the count of mines regardless of
2008 * whether the object creation succeeds. These mines will be
2009 * lost at the end of the death sequence if they are not
2010 * dropped here, so there is no need to count them accurately.
2011 */
2012 Laser_create_new(randvec, tvec, newseg, playerobj, id, weapon_sound_flag::silent);
2013 }
2014 }
2015 };
2016 drop_armed_bomb(secondary_ammo[secondary_weapon_index_t::SMART_MINE_INDEX], weapon_id_type::SUPERPROX_ID);
2017
2018 // If the player had proximity bombs, maybe arm one of them.
2019 if (Game_mode & GM_MULTI)
2020 drop_armed_bomb(secondary_ammo[secondary_weapon_index_t::PROXIMITY_INDEX], weapon_id_type::PROXIMITY_ID);
2021 #endif
2022
2023 // If the player dies and he has powerful lasers, create the powerups here.
2024
2025 if (plr_laser_level != laser_level::_1)
2026 {
2027 auto &&[laser_count, laser_powerup] =
2028 #if DXX_BUILD_DESCENT == 2
2029 (plr_laser_level > MAX_LASER_LEVEL)
2030 ? std::tuple(static_cast<unsigned>(plr_laser_level) - static_cast<unsigned>(MAX_LASER_LEVEL), powerup_type_t::POW_SUPER_LASER)
2031 :
2032 #endif
2033 std::tuple(static_cast<unsigned>(plr_laser_level), powerup_type_t::POW_LASER);
2034 call_object_create_egg(playerobj, laser_count, laser_powerup);
2035 }
2036
2037 // Drop quad laser if appropos
2038 if (player_info.powerup_flags & PLAYER_FLAGS_QUAD_LASERS)
2039 call_object_create_egg(playerobj, powerup_type_t::POW_QUAD_FIRE);
2040
2041 if (player_info.powerup_flags & PLAYER_FLAGS_CLOAKED)
2042 call_object_create_egg(playerobj, powerup_type_t::POW_CLOAK);
2043
2044 #if DXX_BUILD_DESCENT == 2
2045 if (player_info.powerup_flags & PLAYER_FLAGS_MAP_ALL)
2046 call_object_create_egg(playerobj, powerup_type_t::POW_FULL_MAP);
2047
2048 if (player_info.powerup_flags & PLAYER_FLAGS_AFTERBURNER)
2049 call_object_create_egg(playerobj, powerup_type_t::POW_AFTERBURNER);
2050
2051 if (player_info.powerup_flags & PLAYER_FLAGS_AMMO_RACK)
2052 call_object_create_egg(playerobj, powerup_type_t::POW_AMMO_RACK);
2053
2054 if (player_info.powerup_flags & PLAYER_FLAGS_CONVERTER)
2055 call_object_create_egg(playerobj, powerup_type_t::POW_CONVERTER);
2056
2057 if (player_info.powerup_flags & PLAYER_FLAGS_HEADLIGHT)
2058 call_object_create_egg(playerobj, powerup_type_t::POW_HEADLIGHT);
2059
2060 // drop the other enemies flag if you have it
2061
2062 if (game_mode_capture_flag(Game_mode) && (player_info.powerup_flags & PLAYER_FLAGS_FLAG))
2063 {
2064 call_object_create_egg(playerobj, multi_get_team_from_player(Netgame, get_player_id(playerobj)) == team_number::blue ? powerup_type_t::POW_FLAG_RED : powerup_type_t::POW_FLAG_BLUE);
2065 }
2066
2067
2068 if (game_mode_hoard(Game_mode))
2069 {
2070 // Drop hoard orbs
2071 call_object_create_egg(playerobj, std::min(player_info.hoard.orbs, player_info.max_hoard_orbs), powerup_type_t::POW_HOARD_ORB);
2072 }
2073 #endif
2074
2075 /* Drop the Vulcan Cannon and Gauss Cannon, if the player has them.
2076 */
2077 maybe_drop_primary_vulcan_weapons(playerobj);
2078
2079 // Drop the rest of the primary weapons
2080 maybe_drop_primary_weapon_egg(playerobj, primary_weapon_index_t::SPREADFIRE_INDEX);
2081 maybe_drop_primary_weapon_egg(playerobj, primary_weapon_index_t::PLASMA_INDEX);
2082 maybe_drop_primary_weapon_egg(playerobj, primary_weapon_index_t::FUSION_INDEX);
2083
2084 #if DXX_BUILD_DESCENT == 2
2085 maybe_drop_primary_weapon_egg(playerobj, primary_weapon_index_t::HELIX_INDEX);
2086 maybe_drop_primary_weapon_egg(playerobj, primary_weapon_index_t::PHOENIX_INDEX);
2087 maybe_drop_primary_omega_weapon(playerobj);
2088 #endif
2089
2090 // Drop the secondary weapons
2091 // Note, proximity weapon only comes in packets of 4. So drop n/2, but a max of 3 (handled inside maybe_drop..) Make sense?
2092
2093 maybe_drop_secondary_weapon_egg(playerobj, secondary_weapon_index_t::PROXIMITY_INDEX, secondary_ammo[secondary_weapon_index_t::PROXIMITY_INDEX] / 4);
2094
2095 maybe_drop_secondary_weapon_egg(playerobj, secondary_weapon_index_t::SMART_INDEX, secondary_ammo[secondary_weapon_index_t::SMART_INDEX]);
2096 maybe_drop_secondary_weapon_egg(playerobj, secondary_weapon_index_t::MEGA_INDEX, secondary_ammo[secondary_weapon_index_t::MEGA_INDEX]);
2097
2098 #if DXX_BUILD_DESCENT == 2
2099 maybe_drop_secondary_weapon_egg(playerobj, secondary_weapon_index_t::SMART_MINE_INDEX, secondary_ammo[secondary_weapon_index_t::SMART_MINE_INDEX] / 4);
2100 maybe_drop_secondary_weapon_egg(playerobj, secondary_weapon_index_t::SMISSILE5_INDEX, secondary_ammo[secondary_weapon_index_t::SMISSILE5_INDEX]);
2101 #endif
2102
2103 // Drop the player's missiles in packs of 1 and/or 4
2104 drop_missile_1_or_4(playerobj, secondary_weapon_index_t::HOMING_INDEX);
2105 #if DXX_BUILD_DESCENT == 2
2106 drop_missile_1_or_4(playerobj, secondary_weapon_index_t::GUIDED_INDEX);
2107 #endif
2108 drop_missile_1_or_4(playerobj, secondary_weapon_index_t::CONCUSSION_INDEX);
2109 #if DXX_BUILD_DESCENT == 2
2110 drop_missile_1_or_4(playerobj, secondary_weapon_index_t::SMISSILE1_INDEX);
2111 drop_missile_1_or_4(playerobj, secondary_weapon_index_t::SMISSILE4_INDEX);
2112 #endif
2113
2114 // If player has vulcan ammo, but no vulcan or gauss cannon, drop the ammo.
2115 if (!(player_info.primary_weapon_flags & HAS_VULCAN_AND_GAUSS_FLAGS))
2116 {
2117 auto amount = player_info.vulcan_ammo;
2118 if (amount > 200) {
2119 amount = 200;
2120 }
2121 if (amount)
2122 for (;;)
2123 {
2124 call_object_create_egg(playerobj, powerup_type_t::POW_VULCAN_AMMO);
2125 if (amount <= VULCAN_AMMO_AMOUNT)
2126 break;
2127 amount -= VULCAN_AMMO_AMOUNT;
2128 }
2129 }
2130
2131 // Always drop a shield and energy powerup.
2132 if (Game_mode & GM_MULTI) {
2133 call_object_create_egg(playerobj, powerup_type_t::POW_SHIELD_BOOST);
2134 call_object_create_egg(playerobj, powerup_type_t::POW_ENERGY);
2135 }
2136 }
2137 }
2138
2139 void apply_damage_to_player(object &playerobj, const icobjptridx_t killer, const fix damage, const apply_damage_player possibly_friendly)
2140 {
2141 #if DXX_BUILD_DESCENT == 2
2142 auto &BuddyState = LevelUniqueObjectState.BuddyState;
2143 #endif
2144 if (Player_dead_state != player_dead_state::no)
2145 return;
2146
2147 auto &player_info = playerobj.ctype.player_info;
2148 if (player_info.powerup_flags & PLAYER_FLAGS_INVULNERABLE)
2149 return;
2150
2151 if (possibly_friendly != apply_damage_player::always && multi_maybe_disable_friendly_fire(static_cast<const object *>(killer)))
2152 return;
2153
2154 if (Endlevel_sequence)
2155 return;
2156
2157 //for the player, the 'real' shields are maintained in the Players[]
2158 //array. The shields value in the player's object are, I think, not
2159 //used anywhere. This routine, however, sets the objects shields to
2160 //be a mirror of the value in the Player structure.
2161
2162 if (get_player_id(playerobj) == Player_num) { //is this the local player?
2163 PALETTE_FLASH_ADD(f2i(damage)*4,-f2i(damage/2),-f2i(damage/2)); //flash red
2164 if (playerobj.shields < 0)
2165 {
2166 /*
2167 * If player is already dead (but Player_dead_state has not
2168 * caught up yet), preserve the original killer data.
2169 */
2170 assert(playerobj.flags & OF_SHOULD_BE_DEAD);
2171 return;
2172 }
2173 const auto shields = (playerobj.shields -= damage);
2174
2175 if (shields < 0)
2176 {
2177 playerobj.ctype.player_info.killer_objnum = killer;
2178 playerobj.flags |= OF_SHOULD_BE_DEAD;
2179
2180 #if DXX_BUILD_DESCENT == 2
2181 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
2182 if (killer && killer->type == OBJ_ROBOT && robot_is_companion(Robot_info[get_robot_id(killer)]))
2183 BuddyState.Buddy_sorry_time = {GameTime64};
2184 #endif
2185 }
2186 }
2187 }
2188
2189 namespace {
2190
2191 static void collide_player_and_weapon(const d_robot_info_array &Robot_info, const vmobjptridx_t playerobj, const vmobjptridx_t weapon, vms_vector &collision_point)
2192 {
2193 auto &Objects = LevelUniqueObjectState.Objects;
2194 auto &imobjptridx = Objects.imptridx;
2195 fix damage = weapon->shields;
2196
2197 #if DXX_BUILD_DESCENT == 2
2198 if (get_weapon_id(weapon) == weapon_id_type::OMEGA_ID)
2199 if (!ok_to_do_omega_damage(weapon)) // see comment in laser.c
2200 return;
2201
2202 // Don't collide own smart mines unless direct hit.
2203 if (get_weapon_id(weapon) == weapon_id_type::SUPERPROX_ID)
2204 if (playerobj == weapon->ctype.laser_info.parent_num)
2205 if (vm_vec_dist_quick(collision_point, playerobj->pos) > playerobj->size)
2206 return;
2207
2208 if (get_weapon_id(weapon) == weapon_id_type::EARTHSHAKER_ID)
2209 smega_rock_stuff();
2210 #endif
2211
2212 damage = fixmul(damage, weapon->ctype.laser_info.multiplier);
2213 #if DXX_BUILD_DESCENT == 2
2214 if (Game_mode & GM_MULTI)
2215 damage = fixmul(damage, Weapon_info[get_weapon_id(weapon)].multi_damage_scale);
2216 #endif
2217
2218 #if 1
2219 /*
2220 * Check if persistent weapon already hit this object. If yes, abort.
2221 * If no, add this object to hitobj_list and do it's damage.
2222 */
2223 if (weapon->mtype.phys_info.flags & PF_PERSISTENT)
2224 {
2225 if (weapon->ctype.laser_info.test_set_hitobj(playerobj))
2226 return;
2227 }
2228 #endif
2229
2230 const auto &&player_segp = vmsegptridx(playerobj->segnum);
2231 if (get_player_id(playerobj) == Player_num)
2232 {
2233 auto &player_info = playerobj->ctype.player_info;
2234 multi_digi_link_sound_to_pos((player_info.powerup_flags & PLAYER_FLAGS_INVULNERABLE) ? sound_effect::SOUND_WEAPON_HIT_DOOR : sound_effect::SOUND_PLAYER_GOT_HIT, player_segp, sidenum_t::WLEFT, collision_point, 0, F1_0);
2235 }
2236
2237 object_create_explosion_without_damage(Vclip, player_segp, collision_point, i2f(10) / 2, vclip_index::player_hit);
2238 if ( Weapon_info[get_weapon_id(weapon)].damage_radius )
2239 {
2240 const auto obj2weapon{vm_vec_build_sub(collision_point, playerobj->pos)};
2241 const auto mag = vm_vec_mag(obj2weapon);
2242 if(mag > 0) // FVI code does not necessarily update the collision point for object2object collisions. Do that now.
2243 {
2244 collision_point = vm_vec_scale_add(playerobj->pos, obj2weapon, fixdiv(playerobj->size, mag));
2245 weapon->pos = collision_point;
2246 }
2247 #if DXX_BUILD_DESCENT == 1
2248 explode_badass_weapon(Robot_info, weapon, weapon->pos);
2249 #elif DXX_BUILD_DESCENT == 2
2250 explode_badass_weapon(Robot_info, weapon, collision_point);
2251 #endif
2252 }
2253
2254 maybe_kill_weapon(weapon,playerobj);
2255
2256 bump_two_objects(Robot_info, playerobj, weapon, 0); //no damage from bump
2257
2258 if ( !Weapon_info[get_weapon_id(weapon)].damage_radius ) {
2259 imobjptridx_t killer = object_none;
2260 if ( weapon->ctype.laser_info.parent_num != object_none )
2261 killer = imobjptridx(weapon->ctype.laser_info.parent_num);
2262
2263 // if (weapon->id == SMART_HOMING_ID)
2264 // damage /= 4;
2265
2266 apply_damage_to_player(playerobj, killer, damage, apply_damage_player::check_for_friendly);
2267 }
2268
2269 // Robots become aware of you if you get hit.
2270 ai_do_cloak_stuff();
2271
2272 return;
2273 }
2274
2275 }
2276
2277 // Nasty robots are the ones that attack you by running into you and doing lots of damage.
2278 void collide_player_and_nasty_robot(const d_robot_info_array &Robot_info, const vmobjptridx_t playerobj, const vmobjptridx_t robot, const vms_vector &collision_point)
2279 {
2280 const auto &&player_segp = vmsegptridx(playerobj->segnum);
2281 digi_link_sound_to_pos(Robot_info[get_robot_id(robot)].claw_sound, player_segp, sidenum_t::WLEFT, collision_point, 0, F1_0);
2282 object_create_explosion_without_damage(Vclip, player_segp, collision_point, i2f(10) / 2, vclip_index::player_hit);
2283
2284 bump_two_objects(Robot_info, playerobj, robot, 0); //no damage from bump
2285
2286 apply_damage_to_player(playerobj, robot, F1_0 * (underlying_value(GameUniqueState.Difficulty_level) + 1), apply_damage_player::always);
2287 }
2288
2289 namespace {
2290
2291 static vms_vector find_exit_direction(vms_vector result, const object &objp, const cscusegment segp)
2292 {
2293 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
2294 auto &Vertices = LevelSharedVertexState.get_vertices();
2295 auto &vcvertptr = Vertices.vcptr;
2296 auto &Walls = LevelUniqueWallSubsystemState.Walls;
2297 auto &vcwallptr = Walls.vcptr;
2298 for (const auto side : MAX_SIDES_PER_SEGMENT)
2299 if (WALL_IS_DOORWAY(GameBitmaps, Textures, vcwallptr, segp, side) & WALL_IS_DOORWAY_FLAG::fly)
2300 {
2301 const auto exit_point{compute_center_point_on_side(vcvertptr, segp, side)};
2302 vm_vec_add2(result, vm_vec_normalized_quick(vm_vec_build_sub(exit_point, objp.pos)));
2303 break;
2304 }
2305 return result;
2306 }
2307
2308 }
2309
2310 void collide_player_and_materialization_center(const vmobjptridx_t objp)
2311 {
2312 const auto &&segp = vmsegptridx(objp->segnum);
2313 digi_link_sound_to_pos(sound_effect::SOUND_PLAYER_GOT_HIT, segp, sidenum_t::WLEFT, objp->pos, 0, F1_0);
2314 object_create_explosion_without_damage(Vclip, segp, objp->pos, i2f(10) / 2, vclip_index::player_hit);
2315
2316 if (get_player_id(objp) != Player_num)
2317 return;
2318
2319 auto rand_vec = make_random_vector();
2320 rand_vec.x /= 4;
2321 rand_vec.y /= 4;
2322 rand_vec.z /= 4;
2323 auto exit_dir = find_exit_direction(rand_vec, objp, segp);
2324 vm_vec_normalize_quick(exit_dir);
2325 bump_one_object(objp, exit_dir, 64*F1_0);
2326
2327 #if DXX_BUILD_DESCENT == 1
2328 const auto killer{object_none};
2329 #elif DXX_BUILD_DESCENT == 2
2330 auto &&killer = objp; // Changed, MK, 2/19/96, make killer the player, so if you die in matcen, will say you killed yourself
2331 #endif
2332 apply_damage_to_player(objp, killer, 4*F1_0, apply_damage_player::always);
2333 }
2334
2335 void collide_robot_and_materialization_center(const d_robot_info_array &Robot_info, const vmobjptridx_t objp)
2336 {
2337 const auto &&segp = vmsegptridx(objp->segnum);
2338 digi_link_sound_to_pos(sound_effect::SOUND_ROBOT_HIT, segp, sidenum_t::WLEFT, objp->pos, 0, F1_0);
2339
2340 if (const auto exp1_vclip_num = Robot_info[get_robot_id(objp)].exp1_vclip_num; Vclip.valid_index(exp1_vclip_num))
2341 object_create_explosion_without_damage(Vclip, segp, objp->pos, (objp->size / 2 * 3) / 4, exp1_vclip_num);
2342
2343 auto exit_dir = find_exit_direction({}, objp, segp);
2344 bump_one_object(objp, exit_dir, 8*F1_0);
2345
2346 apply_damage_to_robot(Robot_info, objp, F1_0, object_none);
2347 }
2348
2349 void collide_live_local_player_and_powerup(const vmobjptridx_t powerup)
2350 {
2351 if (do_powerup(powerup))
2352 {
2353 powerup->flags |= OF_SHOULD_BE_DEAD;
2354 if (Game_mode & GM_MULTI)
2355 multi_send_remobj(powerup);
2356 }
2357 }
2358
2359 }
2360
2361 namespace {
2362
2363 static void collide_player_and_powerup(const d_robot_info_array &, object &playerobj, const vmobjptridx_t powerup, const vms_vector &)
2364 {
2365 if (!Endlevel_sequence &&
2366 Player_dead_state == player_dead_state::no &&
2367 get_player_id(playerobj) == Player_num)
2368 {
2369 collide_live_local_player_and_powerup(powerup);
2370 }
2371 else if ((Game_mode & GM_MULTI_COOP) && (get_player_id(playerobj) != Player_num))
2372 {
2373 switch (get_powerup_id(powerup)) {
2374 case powerup_type_t::POW_KEY_BLUE:
2375 playerobj.ctype.player_info.powerup_flags |= PLAYER_FLAGS_BLUE_KEY;
2376 break;
2377 case powerup_type_t::POW_KEY_RED:
2378 playerobj.ctype.player_info.powerup_flags |= PLAYER_FLAGS_RED_KEY;
2379 break;
2380 case powerup_type_t::POW_KEY_GOLD:
2381 playerobj.ctype.player_info.powerup_flags |= PLAYER_FLAGS_GOLD_KEY;
2382 break;
2383 default:
2384 break;
2385 }
2386 }
2387 return;
2388 }
2389
2390 static void collide_player_and_clutter(const d_robot_info_array &Robot_info, const vmobjptridx_t playerobj, const vmobjptridx_t clutter, const vms_vector &collision_point)
2391 {
2392 if (check_collision_delayfunc_exec())
2393 digi_link_sound_to_pos(sound_effect::SOUND_ROBOT_HIT_PLAYER, vcsegptridx(playerobj->segnum), sidenum_t::WLEFT, collision_point, 0, F1_0);
2394 bump_two_objects(Robot_info, clutter, playerobj, 1);
2395 }
2396
2397 }
2398
2399 // See if weapon1 creates a badass explosion. If so, create the explosion
2400 // Return true if weapon does proximity (as opposed to only contact) damage when it explodes.
2401 namespace dsx {
2402
2403 namespace {
2404
2405 int maybe_detonate_weapon(const d_robot_info_array &Robot_info, const vmobjptridx_t weapon1, object &weapon2, const vms_vector &collision_point)
2406 {
2407 if ( Weapon_info[get_weapon_id(weapon1)].damage_radius ) {
2408 auto dist = vm_vec_dist_quick(weapon1->pos, weapon2.pos);
2409 if (dist < F1_0*5) {
2410 maybe_kill_weapon(weapon1,weapon2);
2411 if (weapon1->flags & OF_SHOULD_BE_DEAD) {
2412 #if DXX_BUILD_DESCENT == 1
2413 explode_badass_weapon(Robot_info, weapon1, weapon1->pos);
2414 #elif DXX_BUILD_DESCENT == 2
2415 explode_badass_weapon(Robot_info, weapon1, collision_point);
2416 #endif
2417 digi_link_sound_to_pos(Weapon_info[get_weapon_id(weapon1)].robot_hit_sound, vcsegptridx(weapon1->segnum), sidenum_t::WLEFT, collision_point, 0, F1_0);
2418 }
2419 return 1;
2420 } else {
2421 weapon1->lifeleft = min(static_cast<fix>(dist) / 64, F1_0);
2422 return 1;
2423 }
2424 } else
2425 return 0;
2426 }
2427
2428 static void collide_weapon_and_weapon(const d_robot_info_array &Robot_info, const vmobjptridx_t weapon1, const vmobjptridx_t weapon2, const vms_vector &collision_point)
2429 {
2430 #if DXX_BUILD_DESCENT == 2
2431 // -- Does this look buggy??: if (weapon1->id == PMINE_ID && weapon1->id == PMINE_ID)
2432 if (get_weapon_id(weapon1) == weapon_id_type::PMINE_ID && get_weapon_id(weapon2) == weapon_id_type::PMINE_ID)
2433 return; //these can't blow each other up
2434
2435 if (get_weapon_id(weapon1) == weapon_id_type::OMEGA_ID) {
2436 if (!ok_to_do_omega_damage(weapon1)) // see comment in laser.c
2437 return;
2438 } else if (get_weapon_id(weapon2) == weapon_id_type::OMEGA_ID) {
2439 if (!ok_to_do_omega_damage(weapon2)) // see comment in laser.c
2440 return;
2441 }
2442 #endif
2443
2444 if ((Weapon_info[get_weapon_id(weapon1)].destroyable) || (Weapon_info[get_weapon_id(weapon2)].destroyable)) {
2445 // shooting Plasma will make bombs explode one drops at the same time since hitboxes overlap. Small HACK to get around this issue. if the player moves away from the bomb at least...
2446 if ((GameTime64 < weapon1->ctype.laser_info.creation_time + (F1_0/5)) && (GameTime64 < weapon2->ctype.laser_info.creation_time + (F1_0/5)) && (weapon1->ctype.laser_info.parent_num == weapon2->ctype.laser_info.parent_num))
2447 return;
2448 // Bug reported by Adam Q. Pletcher on September 9, 1994, smart bomb homing missiles were toasting each other.
2449 if ((get_weapon_id(weapon1) == get_weapon_id(weapon2)) && (weapon1->ctype.laser_info.parent_num == weapon2->ctype.laser_info.parent_num))
2450 return;
2451
2452 #if DXX_BUILD_DESCENT == 1
2453 if (Weapon_info[get_weapon_id(weapon1)].destroyable)
2454 if (maybe_detonate_weapon(Robot_info, weapon1, weapon2, collision_point))
2455 maybe_kill_weapon(weapon2,weapon1);
2456
2457 if (Weapon_info[get_weapon_id(weapon2)].destroyable)
2458 if (maybe_detonate_weapon(Robot_info, weapon2, weapon1, collision_point))
2459 maybe_kill_weapon(weapon1,weapon2);
2460 #elif DXX_BUILD_DESCENT == 2
2461 if (Weapon_info[get_weapon_id(weapon1)].destroyable)
2462 if (maybe_detonate_weapon(Robot_info, weapon1, weapon2, collision_point))
2463 maybe_detonate_weapon(Robot_info, weapon2, weapon1, collision_point);
2464
2465 if (Weapon_info[get_weapon_id(weapon2)].destroyable)
2466 if (maybe_detonate_weapon(Robot_info, weapon2, weapon1, collision_point))
2467 maybe_detonate_weapon(Robot_info, weapon1, weapon2, collision_point);
2468 #endif
2469 }
2470 }
2471
2472 static void collide_weapon_and_debris(const d_robot_info_array &Robot_info, const vmobjptridx_t weapon, const vmobjptridx_t debris, const vms_vector &collision_point)
2473 {
2474 #if DXX_BUILD_DESCENT == 2
2475 // Hack! Prevent debris from causing bombs spewed at player death to detonate!
2476 if ((get_weapon_id(weapon) == weapon_id_type::PROXIMITY_ID) || (get_weapon_id(weapon) == weapon_id_type::SUPERPROX_ID)) {
2477 if (weapon->ctype.laser_info.creation_time + F1_0/2 > GameTime64)
2478 return;
2479 }
2480 #endif
2481 if ( (weapon->ctype.laser_info.parent_type==OBJ_PLAYER) && !(debris->flags & OF_EXPLODING) ) {
2482 digi_link_sound_to_pos(sound_effect::SOUND_ROBOT_HIT, vcsegptridx(weapon->segnum), sidenum_t::WLEFT, collision_point, 0, F1_0);
2483
2484 explode_object(LevelUniqueObjectState, Robot_info, LevelSharedSegmentState, LevelUniqueSegmentState, debris, 0);
2485 if ( Weapon_info[get_weapon_id(weapon)].damage_radius )
2486 explode_badass_weapon(Robot_info, weapon, collision_point);
2487 maybe_kill_weapon(weapon,debris);
2488 if (!(weapon->mtype.phys_info.flags & PF_PERSISTENT))
2489 weapon->flags |= OF_SHOULD_BE_DEAD;
2490 }
2491 return;
2492 }
2493
2494 #if DXX_BUILD_DESCENT == 1
2495 #define DXX_COLLISION_TABLE(NO,DO) \
2496
2497 #elif DXX_BUILD_DESCENT == 2
2498 #define DXX_COLLISION_TABLE(NO,DO) \
2499 NO##_SAME_COLLISION(OBJ_MARKER) \
2500 DO##_COLLISION(OBJ_PLAYER, OBJ_MARKER, collide_player_and_marker) \
2501 NO##_COLLISION(OBJ_ROBOT, OBJ_MARKER) \
2502 NO##_COLLISION(OBJ_HOSTAGE, OBJ_MARKER) \
2503 NO##_COLLISION(OBJ_WEAPON, OBJ_MARKER) \
2504 NO##_COLLISION(OBJ_CAMERA, OBJ_MARKER) \
2505 NO##_COLLISION(OBJ_POWERUP, OBJ_MARKER) \
2506 NO##_COLLISION(OBJ_DEBRIS, OBJ_MARKER) \
2507
2508 #endif
2509
2510 #define COLLIDE_IGNORE_COLLISION(Robot_info,O1,O2,C)
2511
2512 #define COLLISION_TABLE(NO,DO) \
2513 NO##_SAME_COLLISION(OBJ_FIREBALL) \
2514 DO##_SAME_COLLISION(OBJ_ROBOT, collide_robot_and_robot) \
2515 NO##_SAME_COLLISION(OBJ_HOSTAGE) \
2516 DO##_SAME_COLLISION(OBJ_PLAYER, collide_player_and_player) \
2517 DO##_SAME_COLLISION(OBJ_WEAPON, collide_weapon_and_weapon) \
2518 NO##_SAME_COLLISION(OBJ_CAMERA) \
2519 NO##_SAME_COLLISION(OBJ_POWERUP) \
2520 NO##_SAME_COLLISION(OBJ_DEBRIS) \
2521 DO##_COLLISION(OBJ_WALL, OBJ_ROBOT, COLLIDE_IGNORE_COLLISION) \
2522 DO##_COLLISION(OBJ_WALL, OBJ_WEAPON, COLLIDE_IGNORE_COLLISION) \
2523 DO##_COLLISION(OBJ_WALL, OBJ_PLAYER, COLLIDE_IGNORE_COLLISION) \
2524 DO##_COLLISION(OBJ_WALL, OBJ_POWERUP, COLLIDE_IGNORE_COLLISION) \
2525 DO##_COLLISION(OBJ_WALL, OBJ_DEBRIS, COLLIDE_IGNORE_COLLISION) \
2526 NO##_COLLISION(OBJ_FIREBALL, OBJ_ROBOT) \
2527 NO##_COLLISION(OBJ_FIREBALL, OBJ_HOSTAGE) \
2528 NO##_COLLISION(OBJ_FIREBALL, OBJ_PLAYER) \
2529 NO##_COLLISION(OBJ_FIREBALL, OBJ_WEAPON) \
2530 NO##_COLLISION(OBJ_FIREBALL, OBJ_CAMERA) \
2531 NO##_COLLISION(OBJ_FIREBALL, OBJ_POWERUP) \
2532 NO##_COLLISION(OBJ_FIREBALL, OBJ_DEBRIS) \
2533 NO##_COLLISION(OBJ_ROBOT, OBJ_HOSTAGE) \
2534 DO##_COLLISION(OBJ_ROBOT, OBJ_PLAYER, collide_robot_and_player) \
2535 DO##_COLLISION(OBJ_ROBOT, OBJ_WEAPON, collide_robot_and_weapon) \
2536 NO##_COLLISION(OBJ_ROBOT, OBJ_CAMERA) \
2537 NO##_COLLISION(OBJ_ROBOT, OBJ_POWERUP) \
2538 NO##_COLLISION(OBJ_ROBOT, OBJ_DEBRIS) \
2539 DO##_COLLISION(OBJ_HOSTAGE, OBJ_PLAYER, collide_hostage_and_player) \
2540 DO##_COLLISION(OBJ_HOSTAGE, OBJ_WEAPON, COLLIDE_IGNORE_COLLISION) \
2541 NO##_COLLISION(OBJ_HOSTAGE, OBJ_CAMERA) \
2542 NO##_COLLISION(OBJ_HOSTAGE, OBJ_POWERUP) \
2543 NO##_COLLISION(OBJ_HOSTAGE, OBJ_DEBRIS) \
2544 DO##_COLLISION(OBJ_PLAYER, OBJ_WEAPON, collide_player_and_weapon) \
2545 NO##_COLLISION(OBJ_PLAYER, OBJ_CAMERA) \
2546 DO##_COLLISION(OBJ_PLAYER, OBJ_POWERUP, collide_player_and_powerup) \
2547 NO##_COLLISION(OBJ_PLAYER, OBJ_DEBRIS) \
2548 DO##_COLLISION(OBJ_PLAYER, OBJ_CNTRLCEN, collide_player_and_controlcen) \
2549 DO##_COLLISION(OBJ_PLAYER, OBJ_CLUTTER, collide_player_and_clutter) \
2550 NO##_COLLISION(OBJ_WEAPON, OBJ_CAMERA) \
2551 NO##_COLLISION(OBJ_WEAPON, OBJ_POWERUP) \
2552 DO##_COLLISION(OBJ_WEAPON, OBJ_DEBRIS, collide_weapon_and_debris) \
2553 NO##_COLLISION(OBJ_CAMERA, OBJ_POWERUP) \
2554 NO##_COLLISION(OBJ_CAMERA, OBJ_DEBRIS) \
2555 NO##_COLLISION(OBJ_POWERUP, OBJ_DEBRIS) \
2556 DO##_COLLISION(OBJ_WEAPON, OBJ_CNTRLCEN, collide_weapon_and_controlcen) \
2557 DO##_COLLISION(OBJ_ROBOT, OBJ_CNTRLCEN, collide_robot_and_controlcen) \
2558 DO##_COLLISION(OBJ_WEAPON, OBJ_CLUTTER, collide_weapon_and_clutter) \
2559 DXX_COLLISION_TABLE(NO,DO) \
2560
2561
2562 /* DPH: Put these macros on one long line to avoid CR/LF problems on linux */
2563 #define COLLISION_OF(a,b) (((a)<<4) + (b))
2564
2565 #define DO_COLLISION(type1,type2,collision_function) \
2566 case COLLISION_OF( (type1), (type2) ): \
2567 static_assert(type1 < type2, "do " #type1 " < " #type2); \
2568 collision_function(Robot_info, (A), (B), collision_point); \
2569 break;
2570 #define DO_SAME_COLLISION(type1,collision_function) \
2571 case COLLISION_OF( (type1), (type1) ): \
2572 collision_function(Robot_info, (A), (B), collision_point); \
2573 break;
2574
2575 //these next two macros define a case that does nothing
2576 #define NO_COLLISION(type1,type2) \
2577 case COLLISION_OF( (type1), (type2) ): \
2578 static_assert(type1 < type2, "no " #type1 " < " #type2); \
2579 break;
2580
2581 #define NO_SAME_COLLISION(type1) \
2582 case COLLISION_OF( (type1), (type1) ): \
2583 break;
2584
2585 template <typename T, std::size_t V>
2586 struct assert_no_truncation
2587 {
2588 static_assert(static_cast<T>(V) == V, "truncation error");
2589 };
2590
2591 }
2592
2593 void collide_two_objects(const d_robot_info_array &Robot_info, vmobjptridx_t A, vmobjptridx_t B, vms_vector &collision_point)
2594 {
2595 if (B->type < A->type)
2596 {
2597 using std::swap;
2598 swap(A, B);
2599 }
2600 uint_fast8_t at, bt;
2601 const char *emsg;
2602 if (((at = A->type) >= MAX_OBJECT_TYPES && (emsg = "illegal object type A", true)) ||
2603 ((bt = B->type) >= MAX_OBJECT_TYPES && (emsg = "illegal object type B", true)))
2604 throw std::runtime_error(emsg);
2605 uint_fast8_t collision_type = COLLISION_OF(at, bt);
2606 struct assert_object_type_not_truncated : std::pair<assert_no_truncation<decltype(at), MAX_OBJECT_TYPES>, assert_no_truncation<decltype(bt), MAX_OBJECT_TYPES>> {};
2607 struct assert_collision_of_not_truncated : assert_no_truncation<decltype(collision_type), COLLISION_OF(MAX_OBJECT_TYPES - 1, MAX_OBJECT_TYPES - 1)> {};
2608 switch( collision_type ) {
2609 COLLISION_TABLE(NO,DO)
2610 default:
2611 Int3(); //Error( "Unhandled collision_type in collide.c!\n" );
2612 }
2613 }
2614
2615 }
2616
2617 #define ENABLE_COLLISION(type1,type2,f) \
2618 COLLISION_RESULT(type1, type2, collision_result::check); \
2619 COLLISION_RESULT(type2, type1, collision_result::check); \
2620
2621 #define DISABLE_COLLISION(type1,type2) \
2622 COLLISION_RESULT(type1, type2, collision_result::ignore); \
2623 COLLISION_RESULT(type2, type1, collision_result::ignore); \
2624
2625 #define ENABLE_SAME_COLLISION(type,f) COLLISION_RESULT(type, type, collision_result::check);
2626 #define DISABLE_SAME_COLLISION(type) COLLISION_RESULT(type, type, collision_result::ignore);
2627
2628 namespace {
2629
2630 /* If not otherwise specified, collision results are ignored. */
2631 template <object_type_t A, object_type_t B>
2632 inline constexpr collision_result collision_result_t{collision_result::ignore};
2633
2634 /* Create symmetric collisions. For any <A,B> where (B < A), use the result of
2635 * <B,A>.
2636 */
2637 template <object_type_t A, object_type_t B>
2638 requires(B < A)
2639 inline constexpr collision_result collision_result_t<A, B>{collision_result_t<B, A>};
2640
2641 #define COLLISION_RESULT(type1,type2,result) \
2642 template <> \
2643 inline constexpr collision_result collision_result_t<type1, type2>{result}
2644
2645 COLLISION_TABLE(DISABLE, ENABLE);
2646
2647 template <std::size_t R, typename C>
2648 [[deprecated("only specializations can be used")]]
2649 inline constexpr collision_inner_array_t collision_inner_array
2650 #ifdef __clang__
2651 {}
2652 #endif
2653 ;
2654
2655 template <std::size_t R, std::size_t... C>
2656 requires(
2657 (COLLISION_OF(R, 0) < COLLISION_OF(R, sizeof...(C) - 1)) &&
2658 (COLLISION_OF(R, sizeof...(C) - 1) < COLLISION_OF(R + 1, 0))
2659 )
2660 inline constexpr collision_inner_array_t collision_inner_array<R, std::index_sequence<C...>>{{
2661 collision_result_t<static_cast<object_type_t>(R), static_cast<object_type_t>(C)>...
2662 }};
2663
2664 template <typename R, typename C>
2665 [[deprecated("only specializations can be used")]]
2666 inline constexpr collision_outer_array_t collision_outer_array
2667 #ifdef __clang__
2668 {}
2669 #endif
2670 ;
2671
2672 template <std::size_t... R, typename C>
2673 inline constexpr collision_outer_array_t collision_outer_array<std::index_sequence<R...>, C>{{
2674 collision_inner_array<static_cast<object_type_t>(R), C>...
2675 }};
2676
2677 }
2678
2679 namespace dsx {
2680
2681 constexpr collision_outer_array_t CollisionResult{collision_outer_array<std::make_index_sequence<MAX_OBJECT_TYPES>, std::make_index_sequence<MAX_OBJECT_TYPES>>};
2682
2683 }
2684
2685 #undef DISABLE_COLLISION
2686 #undef ENABLE_COLLISION
2687
2688 #define ENABLE_COLLISION(T1,T2) static_assert( \
2689 collision_result_t<T1, T2> == collision_result::check && \
2690 collision_result_t<T2, T1> == collision_result::check, \
2691 #T1 " " #T2 \
2692 )
2693 #define DISABLE_COLLISION(T1,T2) static_assert( \
2694 collision_result_t<T1, T2> == collision_result::ignore && \
2695 collision_result_t<T2, T1> == collision_result::ignore, \
2696 #T1 " " #T2 \
2697 )
2698
2699 ENABLE_COLLISION( OBJ_WALL, OBJ_ROBOT );
2700 ENABLE_COLLISION( OBJ_WALL, OBJ_WEAPON );
2701 ENABLE_COLLISION( OBJ_WALL, OBJ_PLAYER );
2702 DISABLE_COLLISION( OBJ_FIREBALL, OBJ_FIREBALL );
2703
2704 ENABLE_COLLISION( OBJ_ROBOT, OBJ_ROBOT );
2705 // DISABLE_COLLISION( OBJ_ROBOT, OBJ_ROBOT ); // ALERT: WARNING: HACK: MK = RESPONSIBLE! TESTING!!
2706 DISABLE_COLLISION( OBJ_HOSTAGE, OBJ_HOSTAGE );
2707 ENABLE_COLLISION( OBJ_PLAYER, OBJ_PLAYER );
2708 ENABLE_COLLISION( OBJ_WEAPON, OBJ_WEAPON );
2709 DISABLE_COLLISION( OBJ_CAMERA, OBJ_CAMERA );
2710 DISABLE_COLLISION( OBJ_POWERUP, OBJ_POWERUP );
2711 DISABLE_COLLISION( OBJ_DEBRIS, OBJ_DEBRIS );
2712 DISABLE_COLLISION( OBJ_FIREBALL, OBJ_ROBOT );
2713 DISABLE_COLLISION( OBJ_FIREBALL, OBJ_HOSTAGE );
2714 DISABLE_COLLISION( OBJ_FIREBALL, OBJ_PLAYER );
2715 DISABLE_COLLISION( OBJ_FIREBALL, OBJ_WEAPON );
2716 DISABLE_COLLISION( OBJ_FIREBALL, OBJ_CAMERA );
2717 DISABLE_COLLISION( OBJ_FIREBALL, OBJ_POWERUP );
2718 DISABLE_COLLISION( OBJ_FIREBALL, OBJ_DEBRIS );
2719 DISABLE_COLLISION( OBJ_ROBOT, OBJ_HOSTAGE );
2720 ENABLE_COLLISION( OBJ_ROBOT, OBJ_PLAYER );
2721 ENABLE_COLLISION( OBJ_ROBOT, OBJ_WEAPON );
2722 DISABLE_COLLISION( OBJ_ROBOT, OBJ_CAMERA );
2723 DISABLE_COLLISION( OBJ_ROBOT, OBJ_POWERUP );
2724 DISABLE_COLLISION( OBJ_ROBOT, OBJ_DEBRIS );
2725 ENABLE_COLLISION( OBJ_HOSTAGE, OBJ_PLAYER );
2726 ENABLE_COLLISION( OBJ_HOSTAGE, OBJ_WEAPON );
2727 DISABLE_COLLISION( OBJ_HOSTAGE, OBJ_CAMERA );
2728 DISABLE_COLLISION( OBJ_HOSTAGE, OBJ_POWERUP );
2729 DISABLE_COLLISION( OBJ_HOSTAGE, OBJ_DEBRIS );
2730 ENABLE_COLLISION( OBJ_PLAYER, OBJ_WEAPON );
2731 DISABLE_COLLISION( OBJ_PLAYER, OBJ_CAMERA );
2732 ENABLE_COLLISION( OBJ_PLAYER, OBJ_POWERUP );
2733 DISABLE_COLLISION( OBJ_PLAYER, OBJ_DEBRIS );
2734 DISABLE_COLLISION( OBJ_WEAPON, OBJ_CAMERA );
2735 DISABLE_COLLISION( OBJ_WEAPON, OBJ_POWERUP );
2736 ENABLE_COLLISION( OBJ_WEAPON, OBJ_DEBRIS );
2737 DISABLE_COLLISION( OBJ_CAMERA, OBJ_POWERUP );
2738 DISABLE_COLLISION( OBJ_CAMERA, OBJ_DEBRIS );
2739 DISABLE_COLLISION( OBJ_POWERUP, OBJ_DEBRIS );
2740 ENABLE_COLLISION( OBJ_POWERUP, OBJ_WALL );
2741 ENABLE_COLLISION( OBJ_WEAPON, OBJ_CNTRLCEN );
2742 ENABLE_COLLISION( OBJ_WEAPON, OBJ_CLUTTER );
2743 ENABLE_COLLISION( OBJ_PLAYER, OBJ_CNTRLCEN );
2744 ENABLE_COLLISION( OBJ_ROBOT, OBJ_CNTRLCEN );
2745 ENABLE_COLLISION( OBJ_PLAYER, OBJ_CLUTTER );
2746 #if DXX_BUILD_DESCENT == 2
2747 ENABLE_COLLISION( OBJ_PLAYER, OBJ_MARKER );
2748 #endif
2749 ENABLE_COLLISION( OBJ_DEBRIS, OBJ_WALL );
2750
2751 namespace dsx {
2752
2753 window_event_result collide_object_with_wall(
2754 #if DXX_BUILD_DESCENT == 2
2755 const d_level_shared_destructible_light_state &LevelSharedDestructibleLightState,
2756 #endif
2757 const d_robot_info_array &Robot_info, const vmobjptridx_t A, fix hitspeed, const vmsegptridx_t hitseg, const sidenum_t hitwall, const vms_vector &hitpt)
2758 {
2759 auto &Objects = LevelUniqueObjectState.Objects;
2760
2761 switch( A->type ) {
2762 case OBJ_NONE:
2763 Error( "A object of type NONE hit a wall!\n");
2764 break;
2765 case OBJ_PLAYER: collide_player_and_wall(A,hitspeed,hitseg,hitwall,hitpt); break;
2766 case OBJ_WEAPON:
2767 return collide_weapon_and_wall(
2768 #if DXX_BUILD_DESCENT == 2
2769 LevelSharedDestructibleLightState,
2770 #endif
2771 Robot_info, Objects, vmsegptridx, A, hitseg, hitwall, hitpt);
2772 case OBJ_DEBRIS:
2773 collide_debris_and_wall(Robot_info, A, hitseg, hitwall, hitpt);
2774 break;
2775
2776 case OBJ_FIREBALL: break; //collide_fireball_and_wall(A,hitspeed,hitseg,hitwall,hitpt);
2777 case OBJ_ROBOT:
2778 {
2779 auto &Walls = LevelUniqueWallSubsystemState.Walls;
2780 auto &vcwallptr = Walls.vcptr;
2781 collide_robot_and_wall(vcwallptr, A, hitseg, hitwall, hitpt);
2782 break;
2783 }
2784 case OBJ_HOSTAGE: break; //collide_hostage_and_wall(A,hitspeed,hitseg,hitwall,hitpt);
2785 case OBJ_CAMERA: break; //collide_camera_and_wall(A,hitspeed,hitseg,hitwall,hitpt);
2786 case OBJ_POWERUP: break; //collide_powerup_and_wall(A,hitspeed,hitseg,hitwall,hitpt);
2787 case OBJ_GHOST: break; //do nothing
2788
2789 default:
2790 Error( "Unhandled object type hit wall in collide.c\n" );
2791 }
2792
2793 return window_event_result::handled; // assume handled
2794 }
2795
2796 }