Debian Codegraph @

similar/main/laser.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 * This will contain the laser code
23 *
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <time.h>
29
30 #include "inferno.h"
31 #include "game.h"
32 #include "bm.h"
33 #include "object.h"
34 #include "laser.h"
35 #include "segment.h"
36 #include "fvi.h"
37 #include "dxxerror.h"
38 #include "gameseg.h"
39 #include "textures.h"
40 #include "fireball.h"
41 #include "polyobj.h"
42 #include "robot.h"
43 #include "weapon.h"
44 #include "newdemo.h"
45 #include "timer.h"
46 #include "player.h"
47 #include "sounds.h"
48 #include "ai.h"
49 #include "powerup.h"
50 #include "multi.h"
51 #include "physics.h"
52 #include "multi.h"
53 #include "fwd-wall.h"
54 #include "playsave.h"
55 #include "vclip.h"
56
57 #include "compiler-range_for.h"
58 #include "d_construct.h"
59 #include "d_levelstate.h"
60 #include "d_underlying_value.h"
61 #include "partial_range.h"
62
63 namespace {
64 #ifdef NEWHOMER
65 #define HOMING_TRACKABLE_DOT_FRAME_TIME HOMING_TURN_TIME
66 static ubyte d_homer_tick_step = 0;
67 static fix d_homer_tick_count = 0;
68 #else
69 #define HOMING_TRACKABLE_DOT_FRAME_TIME FrameTime
70 #endif
71
72 static int Muzzle_queue_index;
73 }
74
75 namespace dsx {
76 namespace {
77 static imobjptridx_t find_homing_object_complete(const vms_vector &curpos, const vmobjptridx_t tracker, int track_obj_type1, int track_obj_type2);
78 static imobjptridx_t find_homing_object(const vms_vector &curpos, vmobjptridx_t tracker);
79 }
80
81 //---------------------------------------------------------------------------------
82 // Called by render code.... determines if the laser is from a robot or the
83 // player and calls the appropriate routine.
84
85 void Laser_render(grs_canvas &canvas, const object_base &obj)
86 {
87 auto &wi = Weapon_info[get_weapon_id(obj)];
88 switch(wi.render)
89 {
90 case WEAPON_RENDER_LASER:
91 Int3(); // Not supported anymore!
92 //Laser_draw_one(obj-Objects, Weapon_info[obj->id].bitmap );
93 break;
94 case WEAPON_RENDER_BLOB:
95 draw_object_blob(GameBitmaps, *Viewer, canvas, obj, wi.bitmap);
96 break;
97 case WEAPON_RENDER_POLYMODEL:
98 break;
99 case WEAPON_RENDER_VCLIP:
100 Int3(); // Oops, not supported, type added by mk on 09/09/94, but not for lasers...
101 [[fallthrough]];
102 default:
103 Error( "Invalid weapon render type in Laser_render\n" );
104 }
105 }
106
107 //---------------------------------------------------------------------------------
108 // Draws a texture-mapped laser bolt
109
110 //void Laser_draw_one( int objnum, grs_bitmap * bmp )
111 //{
112 // int t1, t2, t3;
113 // g3s_point p1, p2;
114 // object *obj;
115 // vms_vector start_pos,end_pos;
116 //
117 // obj = &Objects[objnum];
118 //
119 // start_pos = obj->pos;
120 // vm_vec_scale_add(&end_pos,&start_pos,&obj->orient.fvec,-Laser_length);
121 //
122 // g3_rotate_point(&p1,&start_pos);
123 // g3_rotate_point(&p2,&end_pos);
124 //
125 // t1 = Lighting_on;
126 // t2 = Interpolation_method;
127 // t3 = Transparency_on;
128 //
129 // Lighting_on = 0;
130 // //Interpolation_method = 3; // Full perspective
131 // Interpolation_method = 1; // Linear
132 // Transparency_on = 1;
133 //
134 // //gr_setcolor( gr_getcolor(31,15,0));
135 // //g3_draw_line_ptrs(p1,p2);
136 // //g3_draw_rod(p1,0x2000,p2,0x2000);
137 // //g3_draw_rod(p1,Laser_width,p2,Laser_width);
138 // g3_draw_rod_tmap(bmp,&p2,Laser_width,&p1,Laser_width,0);
139 // Lighting_on = t1;
140 // Interpolation_method = t2;
141 // Transparency_on = t3;
142 //
143 //}
144
145 namespace {
146
147 #if DXX_BUILD_DESCENT == 2
148 static constexpr unsigned LASER_HELIX_MASK{7}; // must match number of bits in flags
149 #endif
150
151 static bool ignore_proximity_weapon(const object &o)
152 {
153 if (!is_proximity_bomb_or_player_smart_mine(get_weapon_id(o)))
154 return false;
155 #if DXX_BUILD_DESCENT == 1
156 return GameTime64 > o.ctype.laser_info.creation_time + F1_0*2;
157 #elif DXX_BUILD_DESCENT == 2
158 return GameTime64 > o.ctype.laser_info.creation_time + F1_0*4;
159 #endif
160 }
161
162 #if DXX_BUILD_DESCENT == 1
163 static bool ignore_phoenix_weapon(const object &)
164 {
165 return false;
166 }
167
168 static bool ignore_guided_missile_weapon(const object &)
169 {
170 return false;
171 }
172 #elif DXX_BUILD_DESCENT == 2
173 static bool ignore_phoenix_weapon(const object &o)
174 {
175 return get_weapon_id(o) == weapon_id_type::PHOENIX_ID && GameTime64 > o.ctype.laser_info.creation_time + F1_0/4;
176 }
177
178 static bool ignore_guided_missile_weapon(const object &o)
179 {
180 return get_weapon_id(o) == weapon_id_type::GUIDEDMISS_ID && GameTime64 > o.ctype.laser_info.creation_time + F1_0*2;
181 }
182 #endif
183 }
184
185 // Changed by MK on 09/07/94
186 // I want you to be able to blow up your own bombs.
187 // AND...Your proximity bombs can blow you up if they're 2.0 seconds or more old.
188 // Changed by MK on 06/06/95: Now must be 4.0 seconds old. Much valid Net-complaining.
189 bool laser_are_related(const vcobjptridx_t o1, const vcobjptridx_t o2)
190 {
191 // See if o2 is the parent of o1
192 if (o1->type == OBJ_WEAPON)
193 if (laser_parent_is_object(o1->ctype.laser_info, o2))
194 {
195 // o1 is a weapon, o2 is the parent of 1, so if o1 is PROXIMITY_BOMB and o2 is player, they are related only if o1 < 2.0 seconds old
196 if (ignore_proximity_weapon(o1) || ignore_guided_missile_weapon(o1) || ignore_phoenix_weapon(o1))
197 {
198 return 0;
199 } else
200 return 1;
201 }
202
203 // See if o1 is the parent of o2
204 if (o2->type == OBJ_WEAPON)
205 {
206 if (laser_parent_is_object(o2->ctype.laser_info, o1))
207 {
208 #if DXX_BUILD_DESCENT == 2
209 // o2 is a weapon, o1 is the parent of 2, so if o2 is PROXIMITY_BOMB and o1 is player, they are related only if o1 < 2.0 seconds old
210 if (ignore_proximity_weapon(o2) || ignore_guided_missile_weapon(o2) || ignore_phoenix_weapon(o2))
211 {
212 return 0;
213 } else
214 #endif
215 return 1;
216 }
217 }
218
219 // They must both be weapons
220 if (o1->type != OBJ_WEAPON || o2->type != OBJ_WEAPON)
221 return 0;
222
223 // Here is the 09/07/94 change -- Siblings must be identical, others can hurt each other
224 // See if they're siblings...
225 // MK: 06/08/95, Don't allow prox bombs to detonate for 3/4 second. Else too likely to get toasted by your own bomb if hit by opponent.
226 const auto o1id{get_weapon_id(o1)};
227 const auto o2id{get_weapon_id(o2)};
228 auto &o1li = o1->ctype.laser_info;
229 auto &o2li = o2->ctype.laser_info;
230 if (o1li.parent_num == o2li.parent_num && o1li.parent_signature == o2li.parent_signature)
231 {
232 if (is_proximity_bomb_or_player_smart_mine(o1id) || is_proximity_bomb_or_player_smart_mine(o2id))
233 {
234 // If neither is older than 1/2 second, then can't blow up!
235 #if DXX_BUILD_DESCENT == 2
236 if (!(GameTime64 > o1li.creation_time + F1_0/2 || GameTime64 > o2li.creation_time + F1_0/2))
237 return 1;
238 else
239 #endif
240 return 0;
241 } else
242 return 1;
243 }
244
245 #if DXX_BUILD_DESCENT == 2
246 // Anything can cause a collision with a robot super prox mine.
247 if (!(
248 o1id == weapon_id_type::ROBOT_SUPERPROX_ID || o2id == weapon_id_type::ROBOT_SUPERPROX_ID ||
249 o1id == weapon_id_type::PROXIMITY_ID || o2id == weapon_id_type::PROXIMITY_ID ||
250 o1id == weapon_id_type::SUPERPROX_ID || o2id == weapon_id_type::SUPERPROX_ID ||
251 o1id == weapon_id_type::PMINE_ID || o2id == weapon_id_type::PMINE_ID
252 ))
253 return 1;
254 #endif
255 return 0;
256 }
257
258 }
259
260 namespace dcx {
261 namespace {
262
263 constexpr vm_distance MAX_SMART_DISTANCE(F1_0*150);
264 constexpr vm_distance_squared MAX_SMART_DISTANCE_SQUARED{MAX_SMART_DISTANCE * MAX_SMART_DISTANCE};
265 static void do_muzzle_stuff(segnum_t segnum, const vms_vector &pos)
266 {
267 auto &m = Muzzle_data[Muzzle_queue_index];
268 Muzzle_queue_index++;
269 if (Muzzle_queue_index >= MUZZLE_QUEUE_MAX)
270 Muzzle_queue_index = 0;
271 m.segnum = segnum;
272 m.pos = pos;
273 m.create_time = timer_query();
274 }
275
276 [[noreturn]]
277 dxx_compiler_attribute_cold
278 static void report_invalid_weapon_render_type(const int weapon_type, const weapon_info::render_type render)
279 {
280 char buf[96];
281 snprintf(buf, sizeof(buf), "invalid weapon render type %u on weapon %i", static_cast<unsigned>(render), weapon_type);
282 throw std::runtime_error(buf);
283 }
284
285 [[nodiscard]]
286 static vms_vector build_homing_weapon_initial_vector(const object_base &parent, const object_base *const goal_obj)
287 {
288 if (goal_obj == nullptr)
289 return make_random_vector();
290 vms_vector vector_to_goal;
291 // Create a vector towards the goal, then add some noise to it.
292 vm_vec_normalized_dir_quick(vector_to_goal, goal_obj->pos, parent.pos);
293 vm_vec_scale_add2(vector_to_goal, make_random_vector(), F1_0 / 4);
294 vm_vec_normalize_quick(vector_to_goal);
295 return vector_to_goal;
296 }
297
298 }
299 }
300
301 namespace dsx {
302 namespace {
303
304 //creates a weapon object
305 static imobjptridx_t create_weapon_object(int weapon_type,const vmsegptridx_t segnum, const vms_vector &position)
306 {
307 render_type rtype;
308 fix laser_radius;
309
310 switch(Weapon_info[weapon_type].render)
311 {
312 case WEAPON_RENDER_BLOB:
313 rtype = render_type::RT_LASER; // Render as a laser even if blob (see render code above for explanation)
314 laser_radius = Weapon_info[weapon_type].blob_size;
315 break;
316 case WEAPON_RENDER_POLYMODEL:
317 laser_radius = 0; // Filled in below.
318 rtype = render_type::RT_POLYOBJ;
319 break;
320 case WEAPON_RENDER_LASER:
321 Int3(); // Not supported anymore
322 return object_none;
323 case WEAPON_RENDER_NONE:
324 rtype = render_type::RT_NONE;
325 laser_radius = F1_0;
326 break;
327 case WEAPON_RENDER_VCLIP:
328 rtype = render_type::RT_WEAPON_VCLIP;
329 laser_radius = Weapon_info[weapon_type].blob_size;
330 break;
331 default:
332 report_invalid_weapon_render_type(weapon_type, Weapon_info[weapon_type].render);
333 }
334
335 const auto &&obj = obj_weapon_create(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, Weapon_info, weapon_type, segnum, position, laser_radius, rtype);
336 if (obj == object_none)
337 return object_none;
338
339 if (Weapon_info[weapon_type].render == WEAPON_RENDER_POLYMODEL) {
340 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
341 obj->rtype.pobj_info.model_num = Weapon_info[get_weapon_id(obj)].model_num;
342 obj->size = fixdiv(Polygon_models[obj->rtype.pobj_info.model_num].rad,Weapon_info[get_weapon_id(obj)].po_len_to_width_ratio);
343 }
344
345 obj->mtype.phys_info.mass = Weapon_info[weapon_type].mass;
346 obj->mtype.phys_info.drag = Weapon_info[weapon_type].drag;
347 obj->mtype.phys_info.thrust = {};
348
349 const auto bounce{Weapon_info[weapon_type].bounce};
350 if (bounce == weapon_info::bounce_type::always)
351 obj->mtype.phys_info.flags |= PF_BOUNCE;
352
353 #if DXX_BUILD_DESCENT == 2
354 if (bounce == weapon_info::bounce_type::twice || cheats.bouncyfire)
355 obj->mtype.phys_info.flags |= PF_BOUNCE+PF_BOUNCES_TWICE;
356 #endif
357
358
359 return obj;
360 }
361 }
362
363 #if DXX_BUILD_DESCENT == 2
364 namespace {
365 // -------------------------------------------------------------------------------------------------------------------------------
366 // ***** HEY ARTISTS!! *****
367 // Here are the constants you're looking for! --MK
368
369 // Change the following constants to affect the look of the omega cannon.
370 // Changing these constants will not affect the damage done.
371 // WARNING: If you change DESIRED_OMEGA_DIST and MAX_OMEGA_BLOBS, you don't merely change the look of the cannon,
372 // you change its range. If you decrease DESIRED_OMEGA_DIST, you decrease how far the gun can fire.
373 constexpr std::integral_constant<fix, F1_0/20> OMEGA_BASE_TIME{}; // How many blobs per second!! No FPS-based blob creation anymore, no FPS-based damage anymore!
374 constexpr std::integral_constant<unsigned, 3> MIN_OMEGA_BLOBS{}; // No matter how close the obstruction, at this many blobs created.
375 constexpr std::integral_constant<fix, F1_0*3> MIN_OMEGA_DIST{}; // At least this distance between blobs, unless doing so would violate MIN_OMEGA_BLOBS
376 constexpr std::integral_constant<fix, F1_0*5> DESIRED_OMEGA_DIST{}; // This is the desired distance between blobs. For distances > MIN_OMEGA_BLOBS*DESIRED_OMEGA_DIST, but not very large, this will apply.
377 constexpr std::integral_constant<unsigned, 16> MAX_OMEGA_BLOBS{}; // No matter how far away the obstruction, this is the maximum number of blobs.
378 constexpr vm_distance MAX_OMEGA_DIST{MAX_OMEGA_BLOBS * DESIRED_OMEGA_DIST}; // Maximum extent of lightning blobs.
379 constexpr vm_distance_squared MAX_OMEGA_DIST_SQUARED{MAX_OMEGA_DIST * MAX_OMEGA_DIST};
380
381 // Additionally, several constants which apply to homing objects in general control the behavior of the Omega Cannon.
382 // They are defined in laser.h. They are copied here for reference. These values are valid on 1/10/96:
383 // If you want the Omega Cannon view cone to be different than the Homing Missile viewcone, contact MK to make the change.
384 // (Unless you are a programmer, in which case, do it yourself!)
385 #define OMEGA_MIN_TRACKABLE_DOT (15*F1_0/16) // Larger values mean narrower cone. F1_0 means damn near impossible. 0 means 180 degree field of view.
386 constexpr vm_distance OMEGA_MAX_TRACKABLE_DIST{MAX_OMEGA_DIST}; // An object must be at least this close to be tracked.
387
388 // Note, you don't need to change these constants. You can control damage and energy consumption by changing the
389 // usual bitmaps.tbl parameters.
390 #define OMEGA_DAMAGE_SCALE 32 // Controls how much damage is done. This gets multiplied by the damage specified in bitmaps.tbl in the $WEAPON line.
391 #define OMEGA_ENERGY_CONSUMPTION 16 // Controls how much energy is consumed. This gets multiplied by the energy parameter from bitmaps.tbl.
392 // -------------------------------------------------------------------------------------------------------------------------------
393
394 // Delete omega blobs further away than MAX_OMEGA_DIST
395 // Since last omega blob has VERY high velocity it's impossible to ensure a constant travel distance on varying FPS. So delete if they exceed their maximum distance.
396 static int omega_cleanup(fvcobjptr &vcobjptr, const vmobjptridx_t weapon)
397 {
398 if (weapon->type != OBJ_WEAPON || get_weapon_id(weapon) != weapon_id_type::OMEGA_ID)
399 return 0;
400 auto &weapon_laser_info = weapon->ctype.laser_info;
401 auto &obj = *vcobjptr(weapon_laser_info.parent_num);
402 if (laser_parent_is_matching_signature(weapon_laser_info, obj))
403 if (vm_vec_dist2(weapon->pos, obj.pos) > MAX_OMEGA_DIST_SQUARED)
404 {
405 obj_delete(LevelUniqueObjectState, Segments, weapon);
406 return 1;
407 }
408
409 return 0;
410 }
411
412 }
413
414 // Return true if ok to do Omega damage. For Multiplayer games. See comment for omega_cleanup()
415 int ok_to_do_omega_damage(const object &weapon)
416 {
417 auto &Objects = LevelUniqueObjectState.Objects;
418 auto &vcobjptr = Objects.vcptr;
419 if (weapon.type != OBJ_WEAPON || get_weapon_id(weapon) != weapon_id_type::OMEGA_ID)
420 return 1;
421 if (!(Game_mode & GM_MULTI))
422 return 1;
423 auto &weapon_laser_info = weapon.ctype.laser_info;
424 auto &obj = *vcobjptr(weapon_laser_info.parent_num);
425 if (laser_parent_is_matching_signature(weapon_laser_info, obj))
426 if (vm_vec_dist2(obj.pos, weapon.pos) > MAX_OMEGA_DIST_SQUARED)
427 return 0;
428
429 return 1;
430 }
431
432 namespace {
433
434 // ---------------------------------------------------------------------------------
435 static bool create_omega_blobs(d_level_unique_object_state &LevelUniqueObjectState, const d_level_shared_segment_state &LevelSharedSegmentState, d_level_unique_segment_state &LevelUniqueSegmentState, const weapon_info_array &Weapon_info, const Difficulty_level_type Difficulty_level, const imsegptridx_t firing_segnum, const vms_vector &firing_pos, const vms_vector &goal_pos, const vmobjptridx_t parent_objp)
436 {
437 const auto &&[magnitude_to_goal, vec_to_goal] = vm_vec_normalize_quick_with_magnitude(vm_vec_build_sub(goal_pos, firing_pos));
438 const fix dist_to_goal{magnitude_to_goal};
439 const auto &&[omega_blob_dist, num_omega_blobs] = [](const unsigned dist_to_goal) -> std::pair<fix, unsigned> {
440 if (dist_to_goal < MIN_OMEGA_BLOBS * MIN_OMEGA_DIST)
441 {
442 constexpr unsigned omega_blob_dist{MIN_OMEGA_DIST};
443 const unsigned num_omega_blobs{dist_to_goal / omega_blob_dist};
444 return std::pair(omega_blob_dist, num_omega_blobs ? num_omega_blobs : 1u);
445 }
446 else
447 {
448 constexpr unsigned omega_blob_dist{DESIRED_OMEGA_DIST};
449 const unsigned num_omega_blobs{dist_to_goal / omega_blob_dist};
450 if (num_omega_blobs > MAX_OMEGA_BLOBS)
451 return std::pair(dist_to_goal / MAX_OMEGA_BLOBS, MAX_OMEGA_BLOBS);
452 else if (num_omega_blobs < MIN_OMEGA_BLOBS)
453 return std::pair(dist_to_goal / MIN_OMEGA_BLOBS, MIN_OMEGA_BLOBS);
454 else
455 return std::pair(dist_to_goal / omega_blob_dist, num_omega_blobs);
456 }
457 }(dist_to_goal);
458
459 const auto omega_delta_vector{vm_vec_copy_scale(vec_to_goal, omega_blob_dist)};
460
461 // Now, create all the blobs
462 auto blob_pos{firing_pos};
463 auto last_segnum{firing_segnum};
464
465 // If nearby, don't perturb vector. If not nearby, start halfway out.
466 std::array<fix, MAX_OMEGA_BLOBS> perturb_array;
467 if (dist_to_goal < MIN_OMEGA_DIST*4) {
468 perturb_array = {};
469 } else {
470 vm_vec_scale_add2(blob_pos, omega_delta_vector, F1_0/2); // Put first blob half way out.
471 for (int i=0; i<num_omega_blobs/2; i++) {
472 perturb_array[i] = F1_0*i + F1_0/4;
473 perturb_array[num_omega_blobs-1-i] = F1_0*i;
474 }
475 }
476
477 // Create random perturbation vector, but favor _not_ going up in player's reference.
478 auto perturb_vec{make_random_vector()};
479 vm_vec_scale_add2(perturb_vec, parent_objp->orient.uvec, -F1_0/2);
480
481 Doing_lighting_hack_flag = lighting_hack::ignore_out_of_mine_location; // Ugly, but prevents blobs which are probably outside the mine from killing framerate.
482
483 imobjptridx_t last_created_objnum{object_none};
484 for (const auto i : xrange(num_omega_blobs))
485 {
486 // This will put the last blob right at the destination object, causing damage.
487 if (i == num_omega_blobs-1)
488 vm_vec_scale_add2(blob_pos, omega_delta_vector, 15*F1_0/32); // Move last blob another (almost) half section
489
490 // Every so often, re-perturb blobs
491 if ((i % 4) == 3) {
492 vm_vec_scale_add2(perturb_vec, make_random_vector(), F1_0/4);
493 }
494
495 const auto temp_pos{vm_vec_scale_add(blob_pos, perturb_vec, perturb_array[i])};
496
497 const auto &&segnum = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, temp_pos, last_segnum);
498 if (segnum != segment_none) {
499 last_segnum = segnum;
500 const auto &&objp = obj_weapon_create(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, Weapon_info, weapon_id_type::OMEGA_ID, segnum, temp_pos, 0, render_type::RT_WEAPON_VCLIP);
501 if (objp == object_none)
502 break;
503
504 last_created_objnum = objp;
505
506 objp->lifeleft = OMEGA_BASE_TIME+(d_rand()/8); // add little randomness so the lighting effect becomes a little more interesting
507 objp->mtype.phys_info.velocity = vec_to_goal;
508
509 // Only make the last one move fast, else multiple blobs might collide with target.
510 vm_vec_scale(objp->mtype.phys_info.velocity, F1_0*4);
511
512 const auto &weapon_info = Weapon_info[get_weapon_id(objp)];
513 objp->size = weapon_info.blob_size;
514
515 objp->shields = fixmul(OMEGA_DAMAGE_SCALE*OMEGA_BASE_TIME, weapon_info.strength[Difficulty_level]);
516
517 objp->ctype.laser_info.parent_type = parent_objp->type;
518 objp->ctype.laser_info.parent_signature = parent_objp->signature;
519 objp->ctype.laser_info.parent_num = parent_objp;
520 objp->movement_source = object::movement_type::None; // Only last one moves, that will get bashed below.
521
522 }
523 vm_vec_add2(blob_pos, omega_delta_vector);
524 }
525
526 Doing_lighting_hack_flag = lighting_hack::normal;
527
528 // Make last one move faster, but it's already moving at speed = F1_0*4.
529 if (last_created_objnum != object_none) {
530 vm_vec_scale(last_created_objnum->mtype.phys_info.velocity, Weapon_info[weapon_id_type::OMEGA_ID].speed[Difficulty_level]/4);
531 last_created_objnum->movement_source = object::movement_type::physics;
532 return true;
533 }
534 return false;
535 }
536
537 }
538
539 #define MIN_OMEGA_CHARGE (MAX_OMEGA_CHARGE/8)
540 #define OMEGA_CHARGE_SCALE 4 // FrameTime / OMEGA_CHARGE_SCALE added to Omega_charge every frame.
541
542 fix get_omega_energy_consumption(const fix delta_charge)
543 {
544 const fix energy_used{fixmul(F1_0 * 190 / 17, delta_charge)};
545 const auto Difficulty_level = GameUniqueState.Difficulty_level;
546 return (Difficulty_level == Difficulty_level_type::_0 || Difficulty_level == Difficulty_level_type::_1)
547 ? fixmul(energy_used, i2f(underlying_value(Difficulty_level) + 2) / 4)
548 : energy_used;
549 }
550
551 // ---------------------------------------------------------------------------------
552 // Call this every frame to recharge the Omega Cannon.
553 void omega_charge_frame(player_info &player_info)
554 {
555 if (!(player_info.primary_weapon_flags & HAS_PRIMARY_FLAG(primary_weapon_index_t::OMEGA_INDEX)))
556 return;
557 auto &Omega_charge = player_info.Omega_charge;
558 if (Omega_charge >= MAX_OMEGA_CHARGE)
559 return;
560
561 if (Player_dead_state != player_dead_state::no)
562 return;
563
564 // Don't charge while firing. Wait 1/3 second after firing before recharging
565 auto &Omega_recharge_delay = player_info.Omega_recharge_delay;
566 if (Omega_recharge_delay)
567 {
568 if (Omega_recharge_delay > FrameTime)
569 {
570 Omega_recharge_delay -= FrameTime;
571 return;
572 }
573 Omega_recharge_delay = 0;
574 }
575
576 if (auto &energy = player_info.energy)
577 {
578 const auto old_omega_charge{Omega_charge};
579 Omega_charge += FrameTime/OMEGA_CHARGE_SCALE;
580 if (Omega_charge > MAX_OMEGA_CHARGE)
581 Omega_charge = MAX_OMEGA_CHARGE;
582
583 const auto energy_used{get_omega_energy_consumption(Omega_charge - old_omega_charge)};
584 energy = (energy > energy_used) ? energy - energy_used : 0;
585 }
586 }
587
588 namespace {
589
590 // ---------------------------------------------------------------------------------
591 // *objp is the object firing the omega cannon
592 // *pos is the location from which the omega bolt starts
593 static void do_omega_stuff(fvmsegptridx &vmsegptridx, const vmobjptridx_t parent_objp, const vms_vector &firing_pos, const vmobjptridx_t weapon_objp)
594 {
595 vms_vector goal_pos;
596 player_info *pl_info{};
597 if (parent_objp->type == OBJ_PLAYER && get_player_id(parent_objp) == Player_num)
598 {
599 // If charge >= min, or (some charge and zero energy), allow to fire.
600 auto &player_info = parent_objp->ctype.player_info;
601 if (!(player_info.Omega_charge >= MIN_OMEGA_CHARGE || (player_info.Omega_charge && !player_info.energy)))
602 {
603 obj_delete(LevelUniqueObjectState, Segments, weapon_objp);
604 return;
605 }
606 pl_info = &player_info;
607 }
608
609 weapon_objp->ctype.laser_info.parent_type = parent_objp->type;
610 weapon_objp->ctype.laser_info.parent_num = parent_objp.get_unchecked_index();
611 weapon_objp->ctype.laser_info.parent_signature = parent_objp->signature;
612
613 const auto &&lock_objnum = find_homing_object(firing_pos, weapon_objp);
614
615 const auto &&firing_segnum = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, firing_pos, Segments.vmptridx(parent_objp->segnum));
616
617 // -- if ((Last_omega_muzzle_flash_time + F1_0/4 < GameTime) || (Last_omega_muzzle_flash_time > GameTime)) {
618 // -- do_muzzle_stuff(firing_segnum, firing_pos);
619 // -- Last_omega_muzzle_flash_time = GameTime;
620 // -- }
621
622 // Delete the original object. Its only purpose in life was to determine which object to home in on.
623 obj_delete(LevelUniqueObjectState, Segments, weapon_objp);
624
625 // If couldn't lock on anything, fire straight ahead.
626 if (lock_objnum == object_none) {
627 fvi_info hit_data;
628 const auto perturbed_fvec{vm_vec_scale_add(parent_objp->orient.fvec, make_random_vector(), F1_0 / 16)};
629 goal_pos = vm_vec_scale_add(firing_pos, perturbed_fvec, MAX_OMEGA_DIST);
630 if (firing_segnum == segment_none)
631 return;
632 const auto fate = find_vector_intersection(fvi_query{
633 firing_pos,
634 goal_pos,
635 fvi_query::unused_ignore_obj_list,
636 &LevelUniqueObjectState,
637 &LevelSharedRobotInfoState.Robot_info,
638 FQ_IGNORE_POWERUPS | FQ_TRANSPOINT, //what about trans walls???
639 parent_objp,
640 }, firing_segnum, 0, hit_data);
641 if (fate != fvi_hit_type::None)
642 {
643 Assert(hit_data.hit_seg != segment_none); // How can this be? We went from inside the mine to outside without hitting anything?
644 goal_pos = hit_data.hit_pnt;
645 }
646 } else
647 goal_pos = lock_objnum->pos;
648
649 // This is where we create a pile of omega blobs!
650 if (!create_omega_blobs(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, Weapon_info, GameUniqueState.Difficulty_level, firing_segnum, firing_pos, goal_pos, parent_objp))
651 return;
652
653 // Play sound.
654 {
655 const auto flash_sound{Weapon_info[get_weapon_id(weapon_objp)].flash_sound};
656 if (parent_objp == Viewer)
657 digi_play_sample(flash_sound, F1_0);
658 else
659 digi_link_sound_to_pos(flash_sound, vmsegptridx(weapon_objp->segnum), sidenum_t::WLEFT, weapon_objp->pos, 0, F1_0);
660 }
661 if (pl_info)
662 {
663 if (auto &Omega_charge = pl_info->Omega_charge; Omega_charge > OMEGA_BASE_TIME)
664 Omega_charge -= OMEGA_BASE_TIME;
665 else
666 Omega_charge = 0;
667 pl_info->Omega_recharge_delay = F1_0 / 3;
668 }
669 }
670
671 /* Descent 2 attempted to penalize super lasers, but did so incorrectly, so the
672 * penalty only applied successfully on non-super lasers. Therefore, this test
673 * excludes super lasers from the penalty, in order to behave more like the
674 * original game.
675 */
676 // Note that this is only used for determining if the quad laser per-bolt damage penalty should be applied, and consequently excludes super lasers
677 static int is_laser_weapon_type(const weapon_id_type weapon_type)
678 {
679 return weapon_type == weapon_id_type::LASER_ID_L1 ||
680 weapon_type == weapon_id_type::LASER_ID_L2 ||
681 weapon_type == weapon_id_type::LASER_ID_L3 ||
682 weapon_type == weapon_id_type::LASER_ID_L4;
683 }
684
685 }
686 #endif
687
688 // ---------------------------------------------------------------------------------
689 // Initializes a laser after Fire is pressed
690 // Returns object number.
691 imobjptridx_t Laser_create_new(const vms_vector &direction, const vms_vector &position, const vmsegptridx_t segnum, const vmobjptridx_t parent, weapon_id_type weapon_type, const weapon_sound_flag make_sound)
692 {
693 auto &Objects = LevelUniqueObjectState.Objects;
694 auto &vmobjptr = Objects.vmptr;
695 fix parent_speed;
696
697 if (weapon_type >= N_weapon_types)
698 {
699 con_printf(CON_URGENT, DXX_STRINGIZE_FL(__FILE__, __LINE__, "invalid weapon id %u fired by parent %hu (type %u) in segment %hu"), weapon_type, parent.get_unchecked_index(), parent->type, segnum.get_unchecked_index());
700 weapon_type = weapon_id_type::LASER_ID_L1;
701 }
702
703 // Don't let homing blobs make muzzle flash.
704 if (parent->type == OBJ_ROBOT)
705 do_muzzle_stuff(segnum, position);
706
707 const imobjptridx_t obj = create_weapon_object(weapon_type,segnum,position);
708
709 if (obj == object_none)
710 {
711 return object_none;
712 }
713 const auto &weapon_info = Weapon_info[weapon_type];
714
715 #if DXX_BUILD_DESCENT == 2
716 // Do the special Omega Cannon stuff. Then return on account of everything that follows does
717 // not apply to the Omega Cannon.
718 if (weapon_type == weapon_id_type::OMEGA_ID) {
719 // Create orientation matrix for tracking purposes.
720 reconstruct_at(obj->orient, vm_vector_to_matrix_u, direction, parent->orient.uvec);
721
722 if (parent != Viewer && parent->type != OBJ_WEAPON) {
723 // Muzzle flash
724 if (const auto flash_vclip = weapon_info.flash_vclip; Vclip.valid_index(flash_vclip))
725 object_create_explosion_without_damage(Vclip, vmsegptridx(obj->segnum), obj->pos, weapon_info.flash_size, flash_vclip);
726 }
727
728 do_omega_stuff(vmsegptridx, parent, position, obj);
729
730 return obj;
731 }
732 #endif
733
734 if (parent->type == OBJ_PLAYER) {
735 if (weapon_type == weapon_id_type::FUSION_ID) {
736 int fusion_scale;
737 #if DXX_BUILD_DESCENT == 1
738 if ((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_COOP))
739 fusion_scale = 2;
740 else
741 #endif
742 fusion_scale = 4;
743
744 auto &player_info = parent->ctype.player_info;
745 const auto Fusion_charge = player_info.Fusion_charge;
746 if (Fusion_charge <= 0)
747 obj->ctype.laser_info.multiplier = F1_0;
748 else if (Fusion_charge <= F1_0*fusion_scale)
749 obj->ctype.laser_info.multiplier = F1_0 + Fusion_charge/2;
750 else
751 obj->ctype.laser_info.multiplier = F1_0*fusion_scale;
752
753 #if DXX_BUILD_DESCENT == 1
754 // Fusion damage was boosted by mk on 3/27 (for reg 1.1 release), but we only want it to apply to single player games.
755 if ((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_COOP))
756 obj->ctype.laser_info.multiplier /= 2;
757 #endif
758 }
759 #if DXX_BUILD_DESCENT == 2
760 /* Descent 1 attempted to penalize laser damage, but did so
761 * incorrectly, so it never applied to the player's weapons.
762 * Therefore, this penalty check is applied only in the Descent 2
763 * build, and then only if not playing a Descent 1 mission, so that
764 * when the Descent 2 build plays a Descent 1 mission, the weapons work
765 * more like they would in a Descent 1 build playing that mission.
766 */
767 else if (!EMULATING_D1 && is_laser_weapon_type(weapon_type) && (parent->ctype.player_info.powerup_flags & PLAYER_FLAGS_QUAD_LASERS))
768 obj->ctype.laser_info.multiplier = F1_0*3/4;
769 else if (weapon_type == weapon_id_type::GUIDEDMISS_ID) {
770 LevelUniqueObjectState.Guided_missile.set_player_active_guided_missile(obj, get_player_id(parent));
771 if (parent==get_local_player().objnum) {
772 if (Newdemo_state==ND_STATE_RECORDING)
773 newdemo_record_guided_start();
774 }
775 }
776 #endif
777 }
778
779 // Make children of smart bomb bounce so if they hit a wall right away, they
780 // won't detonate. The frame interval code will clear this bit after 1/2 second.
781 #if DXX_BUILD_DESCENT == 1
782 if ((weapon_type == weapon_id_type::PLAYER_SMART_HOMING_ID) || (weapon_type == weapon_id_type::ROBOT_SMART_HOMING_ID))
783 #elif DXX_BUILD_DESCENT == 2
784 if ((weapon_type == weapon_id_type::PLAYER_SMART_HOMING_ID) || (weapon_type == weapon_id_type::SMART_MINE_HOMING_ID) || (weapon_type == weapon_id_type::ROBOT_SMART_HOMING_ID) || (weapon_type == weapon_id_type::ROBOT_SMART_MINE_HOMING_ID) || (weapon_type == weapon_id_type::EARTHSHAKER_MEGA_ID))
785 #endif
786 obj->mtype.phys_info.flags |= PF_BOUNCE;
787
788 const fix laser_length{
789 (weapon_info.render == WEAPON_RENDER_POLYMODEL)
790 ? (LevelSharedPolygonModelState.Polygon_models[obj->rtype.pobj_info.model_num].rad * 2)
791 : 0
792 };
793
794 if (weapon_type == weapon_id_type::FLARE_ID)
795 obj->mtype.phys_info.flags |= PF_STICK; //this obj sticks to walls
796
797 const auto Difficulty_level = GameUniqueState.Difficulty_level;
798 obj->shields = weapon_info.strength[Difficulty_level];
799
800 // Fill in laser-specific data
801
802 obj->lifeleft = weapon_info.lifetime;
803 obj->ctype.laser_info.parent_type = parent->type;
804 obj->ctype.laser_info.parent_signature = parent->signature;
805 obj->ctype.laser_info.parent_num = parent;
806
807 // Assign parent type to highest level creator. This propagates parent type down from
808 // the original creator through weapons which create children of their own (ie, smart missile)
809 if (parent->type == OBJ_WEAPON) {
810 auto highest_parent = parent;
811 int count;
812
813 count = 0;
814 while ((count++ < 10) && (highest_parent->type == OBJ_WEAPON)) {
815 const auto next_parent = highest_parent->ctype.laser_info.parent_num;
816 const auto &&parent_objp = parent.absolute_sibling(next_parent);
817 if (!laser_parent_is_object(highest_parent->ctype.laser_info, parent_objp))
818 break; // Probably means parent was killed. Just continue.
819
820 if (next_parent == highest_parent) {
821 Int3(); // Hmm, object is parent of itself. This would seem to be bad, no?
822 break;
823 }
824
825 highest_parent = parent_objp;
826
827 obj->ctype.laser_info.parent_num = highest_parent;
828 obj->ctype.laser_info.parent_type = highest_parent->type;
829 obj->ctype.laser_info.parent_signature = highest_parent->signature;
830 }
831 }
832
833 // Create orientation matrix so we can look from this pov
834 // Homing missiles also need an orientation matrix so they know if they can make a turn.
835 if ((weapon_info.homing_flag && (obj->ctype.laser_info.track_goal = object_none, true)) || obj->render_type == render_type::RT_POLYOBJ)
836 reconstruct_at(obj->orient, vm_vector_to_matrix_u, direction, parent->orient.uvec);
837
838 if (( parent != Viewer ) && (parent->type != OBJ_WEAPON)) {
839 // Muzzle flash
840 if (const auto flash_vclip = weapon_info.flash_vclip; Vclip.valid_index(flash_vclip))
841 object_create_explosion_without_damage(Vclip, segnum.absolute_sibling(obj->segnum), obj->pos, weapon_info.flash_size, flash_vclip);
842 }
843
844 if (weapon_info.flash_sound != sound_effect::None)
845 {
846 if (make_sound != weapon_sound_flag::silent)
847 {
848 if (parent == Viewer)
849 {
850 // Make your own vulcan gun 1/2 as loud.
851 digi_play_sample(weapon_info.flash_sound, weapon_type == weapon_id_type::VULCAN_ID ? F0_5 : F1_0);
852 } else {
853 digi_link_sound_to_pos(weapon_info.flash_sound, segnum.absolute_sibling(obj->segnum), sidenum_t::WLEFT, obj->pos, 0, F1_0);
854 }
855 }
856 }
857
858 // Fire the laser from the gun tip so that the back end of the laser bolt is at the gun tip.
859 // Move 1 frame, so that the end-tip of the laser is touching the gun barrel.
860 // This also jitters the laser a bit so that it doesn't alias.
861 // Don't do for weapons created by weapons.
862 #if DXX_BUILD_DESCENT == 1
863 if (parent->type != OBJ_WEAPON && weapon_info.render != WEAPON_RENDER_NONE && weapon_type != weapon_id_type::FLARE_ID)
864 #elif DXX_BUILD_DESCENT == 2
865 if (parent->type == OBJ_PLAYER && weapon_info.render != WEAPON_RENDER_NONE && weapon_type != weapon_id_type::FLARE_ID)
866 #endif
867 {
868 const auto end_pos{vm_vec_scale_add(obj->pos, direction, (laser_length / 2))};
869 const auto &&end_segnum = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, end_pos, Segments.vmptridx(obj->segnum));
870 if (end_segnum != obj->segnum) {
871 if (end_segnum != segment_none) {
872 obj->pos = end_pos;
873 obj_relink(vmobjptr, vmsegptr, obj, end_segnum);
874 }
875 } else
876 obj->pos = end_pos;
877 }
878
879 // Here's where to fix the problem with objects which are moving backwards imparting higher velocity to their weaponfire.
880 // Find out if moving backwards.
881 if (is_proximity_bomb_or_player_smart_mine(weapon_type)) {
882 parent_speed = vm_vec_mag_quick(parent->mtype.phys_info.velocity);
883 if (vm_vec_build_dot(parent->mtype.phys_info.velocity, parent->orient.fvec) < 0)
884 parent_speed = -parent_speed;
885 } else
886 parent_speed = 0;
887
888 auto weapon_speed{weapon_info.speed[Difficulty_level]};
889 #if DXX_BUILD_DESCENT == 2
890 if (weapon_info.speedvar != 128)
891 {
892 fix randval;
893
894 // Get a scale factor between speedvar% and 1.0.
895 randval = F1_0 - ((d_rand() * weapon_info.speedvar) >> 6);
896 weapon_speed = fixmul(weapon_speed, randval);
897 }
898 #endif
899
900 // Ugly hack (too bad we're on a deadline), for homing missiles dropped by smart bomb, start them out slower.
901 #if DXX_BUILD_DESCENT == 1
902 if (weapon_type == weapon_id_type::PLAYER_SMART_HOMING_ID || weapon_type == weapon_id_type::ROBOT_SMART_HOMING_ID)
903 #elif DXX_BUILD_DESCENT == 2
904 if (weapon_type == weapon_id_type::PLAYER_SMART_HOMING_ID || weapon_type == weapon_id_type::SMART_MINE_HOMING_ID || weapon_type == weapon_id_type::ROBOT_SMART_HOMING_ID || weapon_type == weapon_id_type::ROBOT_SMART_MINE_HOMING_ID || weapon_type == weapon_id_type::EARTHSHAKER_MEGA_ID)
905 #endif
906 weapon_speed /= 4;
907
908 if (weapon_info.thrust)
909 weapon_speed /= 2;
910
911 obj->mtype.phys_info.velocity = vm_vec_copy_scale(direction, weapon_speed + parent_speed);
912
913 // Set thrust
914 if (weapon_info.thrust)
915 {
916 obj->mtype.phys_info.thrust = obj->mtype.phys_info.velocity;
917 vm_vec_scale(obj->mtype.phys_info.thrust, fixdiv(weapon_info.thrust, weapon_speed+parent_speed));
918 }
919
920 if (obj->type == OBJ_WEAPON && weapon_type == weapon_id_type::FLARE_ID)
921 obj->lifeleft += (d_rand()-16384) << 2; // add in -2..2 seconds
922
923 return obj;
924 }
925
926 // -----------------------------------------------------------------------------------------------------------
927 // Calls Laser_create_new, but takes care of the segment and point computation for you.
928 imobjptridx_t Laser_create_new_easy(const d_robot_info_array &Robot_info, const vms_vector &direction, const vms_vector &position, const vmobjptridx_t parent, weapon_id_type weapon_type, const weapon_sound_flag make_sound)
929 {
930 fvi_info hit_data;
931
932 // Find segment containing laser fire position. If the robot is straddling a segment, the position from
933 // which it fires may be in a different segment, which is bad news for find_vector_intersection. So, cast
934 // a ray from the object center (whose segment we know) to the laser position. Then, in the call to Laser_create_new
935 // use the data returned from this call to find_vector_intersection.
936 // Note that while find_vector_intersection is pretty slow, it is not terribly slow if the destination point is
937 // in the same segment as the source point.
938
939 const auto fate = find_vector_intersection(fvi_query{
940 parent->pos,
941 position,
942 fvi_query::unused_ignore_obj_list,
943 &LevelUniqueObjectState,
944 &Robot_info,
945 FQ_TRANSWALL, //what about trans walls???
946 parent,
947 }, parent->segnum, 0, hit_data);
948 if (fate != fvi_hit_type::None || hit_data.hit_seg == segment_none)
949 {
950 return object_none;
951 }
952
953 return Laser_create_new(direction, hit_data.hit_pnt, vmsegptridx(hit_data.hit_seg), parent, weapon_type, make_sound);
954 }
955
956 }
957
958 namespace dcx {
959
960 std::array<muzzle_info, MUZZLE_QUEUE_MAX> Muzzle_data;
961
962 namespace {
963
964 static fix get_weapon_energy_usage_with_difficulty(const weapon_info &wi, const Difficulty_level_type Difficulty_level)
965 {
966 const auto energy_usage{wi.energy_usage};
967 if (Difficulty_level == Difficulty_level_type::_0 || Difficulty_level == Difficulty_level_type::_1)
968 return fixmul(energy_usage, i2f(underlying_value(Difficulty_level) + 2) / 4);
969 return energy_usage;
970 }
971
972 }
973
974 }
975
976 namespace d1x {
977 namespace {
978
979 static fix get_scaled_min_trackable_dot()
980 {
981 const fix curFT{HOMING_TRACKABLE_DOT_FRAME_TIME};
982 if (curFT <= F1_0 / 16)
983 return (3 * (F1_0 - HOMING_MIN_TRACKABLE_DOT) / 4 + HOMING_MIN_TRACKABLE_DOT);
984 else if (curFT < F1_0 / 4)
985 return (fixmul(F1_0 - HOMING_MIN_TRACKABLE_DOT, F1_0 - 4 * curFT) + HOMING_MIN_TRACKABLE_DOT);
986 else
987 return (HOMING_MIN_TRACKABLE_DOT);
988 }
989
990 }
991 }
992
993 namespace dsx {
994
995 // -----------------------------------------------------------------------------------------------------------
996 // Determine if two objects are on a line of sight. If so, return true, else return false.
997 // Calls fvi.
998 int object_to_object_visibility(const vcobjptridx_t obj1, const object_base &obj2, int trans_type)
999 {
1000 fvi_info hit_data;
1001 switch(const auto fate = find_vector_intersection(fvi_query{
1002 obj1->pos,
1003 obj2.pos,
1004 fvi_query::unused_ignore_obj_list,
1005 fvi_query::unused_LevelUniqueObjectState,
1006 fvi_query::unused_Robot_info,
1007 trans_type,
1008 obj1,
1009 }, obj1->segnum, 0x10, hit_data))
1010 {
1011 case fvi_hit_type::None:
1012 return 1;
1013 case fvi_hit_type::Wall:
1014 return 0;
1015 default:
1016 con_printf(CON_VERBOSE, "object_to_object_visibility: fate=%u for object %hu{%hu/%i,%i,%i} to {%i,%i,%i}", underlying_value(fate), static_cast<vcobjptridx_t::integral_type>(obj1), obj1->segnum, obj1->pos.x, obj1->pos.y, obj1->pos.z, obj2.pos.x, obj2.pos.y, obj2.pos.z);
1017 // Int3(); // Contact Mike: Oops, what happened? What is fate?
1018 // 2 = hit object (impossible), 3 = bad starting point (bad)
1019 break;
1020 }
1021 return 0;
1022 }
1023
1024 namespace {
1025
1026 #if DXX_BUILD_DESCENT == 2
1027 static fix get_scaled_min_trackable_dot()
1028 {
1029 if (EMULATING_D1)
1030 return ::d1x::get_scaled_min_trackable_dot();
1031 const fix curFT{HOMING_TRACKABLE_DOT_FRAME_TIME};
1032 if (curFT <= F1_0/64)
1033 return (HOMING_MIN_TRACKABLE_DOT);
1034 else if (curFT < F1_0/32)
1035 return (HOMING_MIN_TRACKABLE_DOT + F1_0/64 - 2*curFT);
1036 else if (curFT < F1_0/4)
1037 return (HOMING_MIN_TRACKABLE_DOT + F1_0/64 - F1_0/16 - curFT);
1038 else
1039 return (HOMING_MIN_TRACKABLE_DOT + F1_0/64 - F1_0/8);
1040 }
1041 #endif
1042
1043 // -----------------------------------------------------------------------------------------------------------
1044 // Return true if weapon *tracker is able to track object Objects[track_goal], else return false.
1045 // In order for the object to be trackable, it must be within a reasonable turning radius for the missile
1046 // and it must not be obstructed by a wall.
1047 static int object_is_trackable(const imobjptridx_t objp, const vmobjptridx_t tracker, fix *dot)
1048 {
1049 if (objp == object_none)
1050 return 0;
1051 if (Game_mode & GM_MULTI_COOP)
1052 return 0;
1053 // Don't track player if he's cloaked.
1054 if ((objp == get_local_player().objnum) && (objp->ctype.player_info.powerup_flags & PLAYER_FLAGS_CLOAKED))
1055 return 0;
1056 #if DXX_BUILD_DESCENT == 2
1057 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
1058 #endif
1059
1060 // Can't track AI object if he's cloaked.
1061 if (objp->type == OBJ_ROBOT) {
1062 if (objp->ctype.ai_info.CLOAKED)
1063 return 0;
1064 #if DXX_BUILD_DESCENT == 2
1065 // Your missiles don't track your escort.
1066 if (Robot_info[get_robot_id(objp)].companion)
1067 if (tracker->ctype.laser_info.parent_type == OBJ_PLAYER)
1068 return 0;
1069 #endif
1070 }
1071 auto vector_to_goal{vm_vec_normalized_quick(vm_vec_build_sub(objp->pos, tracker->pos))};
1072 *dot = vm_vec_build_dot(vector_to_goal, tracker->orient.fvec);
1073
1074 #if DXX_BUILD_DESCENT == 2
1075 if ((*dot < get_scaled_min_trackable_dot()) && (*dot > F1_0*9/10)) {
1076 vm_vec_normalize(vector_to_goal);
1077 *dot = vm_vec_build_dot(vector_to_goal, tracker->orient.fvec);
1078 }
1079 #endif
1080
1081 if (*dot >= get_scaled_min_trackable_dot()) {
1082 // dot is in legal range, now see if object is visible
1083 return object_to_object_visibility(tracker, objp, FQ_TRANSWALL);
1084 } else {
1085 return 0;
1086 }
1087 }
1088
1089 // --------------------------------------------------------------------------------------------
1090 static imobjptridx_t call_find_homing_object_complete(const vms_vector &curpos, const vmobjptridx_t tracker)
1091 {
1092 if (Game_mode & GM_MULTI) {
1093 if (tracker->ctype.laser_info.parent_type == OBJ_PLAYER) {
1094 // It's fired by a player, so if robots present, track robot, else track player.
1095 if (Game_mode & GM_MULTI_COOP)
1096 return find_homing_object_complete( curpos, tracker, OBJ_ROBOT, -1);
1097 else
1098 return find_homing_object_complete( curpos, tracker, OBJ_PLAYER, OBJ_ROBOT);
1099 } else {
1100 int goal2_type{
1101 #if DXX_BUILD_DESCENT == 2
1102 (cheats.robotskillrobots)
1103 ? OBJ_ROBOT
1104 :
1105 #endif
1106 -1
1107 };
1108 assert(tracker->ctype.laser_info.parent_type == OBJ_ROBOT);
1109 return find_homing_object_complete(curpos, tracker, OBJ_PLAYER, goal2_type);
1110 }
1111 } else
1112 return find_homing_object_complete( curpos, tracker, OBJ_ROBOT, -1);
1113 }
1114
1115 // --------------------------------------------------------------------------------------------
1116 // Find object to home in on.
1117 // Scan list of objects rendered last frame, find one that satisfies function of nearness to center and distance.
1118 static imobjptridx_t find_homing_object(const vms_vector &curpos, const vmobjptridx_t tracker)
1119 {
1120 // Contact Mike: This is a bad and stupid thing. Who called this routine with an illegal laser type??
1121 #ifndef NDEBUG
1122 const auto tracker_id{get_weapon_id(tracker)};
1123 #if DXX_BUILD_DESCENT == 2
1124 if (tracker_id != weapon_id_type::OMEGA_ID)
1125 #endif
1126 assert(Weapon_info[tracker_id].homing_flag);
1127 #endif
1128
1129 // Find an object to track based on game mode (eg, whether in network play) and who fired it.
1130
1131 return call_find_homing_object_complete(curpos, tracker);
1132 }
1133
1134 // --------------------------------------------------------------------------------------------
1135 // Find object to home in on.
1136 // Scan list of objects rendered last frame, find one that satisfies function of nearness to center and distance.
1137 // Can track two kinds of objects. If you are only interested in one type, set track_obj_type2 to NULL
1138 // Always track proximity bombs. --MK, 06/14/95
1139 // Make homing objects not track parent's prox bombs.
1140 imobjptridx_t find_homing_object_complete(const vms_vector &curpos, const vmobjptridx_t tracker, int track_obj_type1, int track_obj_type2)
1141 {
1142 auto &Objects = LevelUniqueObjectState.Objects;
1143 auto &vcobjptr = Objects.vcptr;
1144 auto &vmobjptridx = Objects.vmptridx;
1145
1146 const auto tracker_id{get_weapon_id(tracker)};
1147 #if DXX_BUILD_DESCENT == 2
1148 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
1149 if (tracker_id != weapon_id_type::OMEGA_ID)
1150 // Contact Mike: This is a bad and stupid thing. Who called this routine with an illegal laser type??
1151 #endif
1152 {
1153 if (!Weapon_info[tracker_id].homing_flag)
1154 throw std::logic_error("tracking without homing_flag");
1155 }
1156
1157 const fix64 HOMING_MAX_TRACKABLE_DIST{F1_0 * 250};
1158 const auto max_trackable_dist{
1159 #if DXX_BUILD_DESCENT == 2
1160 (tracker_id == weapon_id_type::OMEGA_ID)
1161 ? vm_distance_squared{(OMEGA_MAX_TRACKABLE_DIST * OMEGA_MAX_TRACKABLE_DIST)}
1162 :
1163 #endif
1164 vm_distance_squared{HOMING_MAX_TRACKABLE_DIST * HOMING_MAX_TRACKABLE_DIST}
1165 };
1166 const auto min_trackable_dot{
1167 #if DXX_BUILD_DESCENT == 2
1168 (tracker_id == weapon_id_type::OMEGA_ID)
1169 ? OMEGA_MIN_TRACKABLE_DOT
1170 :
1171 #endif
1172 HOMING_MIN_TRACKABLE_DOT
1173 };
1174
1175 imobjptridx_t best_objnum{object_none};
1176 fix max_dot{-F1_0 * 2};
1177 range_for (const auto &&curobjp, vmobjptridx)
1178 {
1179 int is_proximity{0};
1180
1181 if ((curobjp->type != track_obj_type1) && (curobjp->type != track_obj_type2))
1182 {
1183 #if DXX_BUILD_DESCENT == 2
1184 if ((curobjp->type == OBJ_WEAPON) && (is_proximity_bomb_or_player_smart_mine(get_weapon_id(curobjp)))) {
1185 auto &cur_laser_info = curobjp->ctype.laser_info;
1186 auto &tracker_laser_info = tracker->ctype.laser_info;
1187 if (cur_laser_info.parent_num != tracker_laser_info.parent_num || cur_laser_info.parent_signature != tracker_laser_info.parent_signature)
1188 is_proximity = 1;
1189 else
1190 continue;
1191 } else
1192 #endif
1193 continue;
1194 }
1195
1196 if (curobjp == tracker->ctype.laser_info.parent_num) // Don't track shooter
1197 continue;
1198
1199 // Don't track cloaked players.
1200 if (curobjp->type == OBJ_PLAYER)
1201 {
1202 if (curobjp->ctype.player_info.powerup_flags & PLAYER_FLAGS_CLOAKED)
1203 continue;
1204 // Don't track teammates in team games
1205 if (Game_mode & GM_TEAM)
1206 {
1207 const auto &&objparent = vcobjptr(tracker->ctype.laser_info.parent_num);
1208 if (objparent->type == OBJ_PLAYER && multi_get_team_from_player(Netgame, get_player_id(curobjp)) == multi_get_team_from_player(Netgame, get_player_id(objparent)))
1209 continue;
1210 }
1211 }
1212
1213 // Can't track AI object if he's cloaked.
1214 if (curobjp->type == OBJ_ROBOT) {
1215 if (curobjp->ctype.ai_info.CLOAKED)
1216 continue;
1217
1218 #if DXX_BUILD_DESCENT == 2
1219 // Your missiles don't track your escort.
1220 if (Robot_info[get_robot_id(curobjp)].companion)
1221 if (tracker->ctype.laser_info.parent_type == OBJ_PLAYER)
1222 continue;
1223 #endif
1224 }
1225
1226 auto vec_to_curobj{vm_vec_build_sub(curobjp->pos, curpos)};
1227 auto dist{vm_vec_mag2(vec_to_curobj)};
1228
1229 if (build_vm_distance_squared(dist) < max_trackable_dist) {
1230 vm_vec_normalize(vec_to_curobj);
1231 fix dot{vm_vec_build_dot(vec_to_curobj, tracker->orient.fvec)};
1232 if (is_proximity)
1233 dot = ((dot << 3) + dot) >> 3; // I suspect Watcom would be too stupid to figure out the obvious...
1234
1235 if (dot > min_trackable_dot) {
1236 if (dot > max_dot) {
1237 if (object_to_object_visibility(tracker, curobjp, FQ_TRANSWALL)) {
1238 max_dot = dot;
1239 best_objnum = curobjp;
1240 }
1241 }
1242 }
1243 }
1244 }
1245 return best_objnum;
1246 }
1247
1248 }
1249
1250 #ifdef NEWHOMER
1251 // Similar to calc_d_tick but made just for the homers.
1252 // Causes d_homer_tick_step to be true in intervals dictated by HOMING_TURN_TIME
1253 // and increments d_homer_tick_count accordingly
1254 void calc_d_homer_tick()
1255 {
1256 auto &Objects = LevelUniqueObjectState.Objects;
1257 auto &vmobjptr = Objects.vmptr;
1258 static fix timer = 0;
1259 auto t = timer + FrameTime;
1260 d_homer_tick_step = t >= HOMING_TURN_TIME;
1261 if (d_homer_tick_step)
1262 {
1263 d_homer_tick_count++;
1264 if (d_homer_tick_count > F1_0)
1265 d_homer_tick_count = 0;
1266 t -= HOMING_TURN_TIME;
1267 // Don't let slowdowns have a lasting impact; allow you to build up at most 3 frames worth
1268 if (t > HOMING_TURN_TIME*3)
1269 t = HOMING_TURN_TIME*3;
1270
1271 get_local_plrobj().ctype.player_info.homing_object_dist = -1; // Assume not being tracked. Laser_do_weapon_sequence modifies this. Let's do this here since the homers do not track every frame, we may not want to reset this ever frame.
1272 }
1273 timer = t;
1274 }
1275 #endif
1276
1277 namespace {
1278
1279 // ------------------------------------------------------------------------------------------------------------
1280 // See if legal to keep tracking currently tracked object. If not, see if another object is trackable. If not, return -1,
1281 // else return object number of tracking object.
1282 // Computes and returns a fairly precise dot product.
1283 static imobjptridx_t track_track_goal(fvcobjptr &vcobjptr, const imobjptridx_t track_goal, const vmobjptridx_t tracker, fix *dot, fix tick_count)
1284 {
1285 #if DXX_BUILD_DESCENT == 1
1286 if (object_is_trackable(track_goal, tracker, dot))
1287 #elif DXX_BUILD_DESCENT == 2
1288 // Every 8 frames for each object, scan all objects.
1289 if (object_is_trackable(track_goal, tracker, dot) && (((tracker ^ tick_count) % 8) != 0))
1290 #endif
1291 {
1292 return track_goal;
1293 } else if (((tracker ^ tick_count) % 4) == 0)
1294 {
1295 int goal_type, goal2_type;
1296 // If player fired missile, then search for an object, if not, then give up.
1297 if (vcobjptr(tracker->ctype.laser_info.parent_num)->type == OBJ_PLAYER)
1298 {
1299 if (track_goal == object_none)
1300 {
1301 if (Game_mode & GM_MULTI)
1302 {
1303 if (Game_mode & GM_MULTI_COOP)
1304 goal_type = OBJ_ROBOT, goal2_type = -1;
1305 else
1306 {
1307 goal_type = OBJ_PLAYER;
1308 goal2_type = (Game_mode & GM_MULTI_ROBOTS)
1309 ? OBJ_ROBOT // Not cooperative, if robots, track either robot or player
1310 : -1; // Not cooperative and no robots, track only a player
1311 }
1312 }
1313 else
1314 goal_type = OBJ_PLAYER, goal2_type = OBJ_ROBOT;
1315 }
1316 else
1317 {
1318 goal_type = vcobjptr(tracker->ctype.laser_info.track_goal)->type;
1319 if ((goal_type == OBJ_PLAYER) || (goal_type == OBJ_ROBOT))
1320 goal2_type = -1;
1321 else
1322 return object_none;
1323 }
1324 }
1325 else {
1326 goal2_type = -1;
1327
1328 #if DXX_BUILD_DESCENT == 2
1329 if (cheats.robotskillrobots)
1330 goal2_type = OBJ_ROBOT;
1331 #endif
1332
1333 if (track_goal == object_none)
1334 goal_type = OBJ_PLAYER;
1335 else {
1336 goal_type = vcobjptr(tracker->ctype.laser_info.track_goal)->type;
1337 assert(goal_type != OBJ_GHOST);
1338 }
1339 }
1340 return find_homing_object_complete(tracker->pos, tracker, goal_type, goal2_type);
1341 }
1342
1343 return object_none;
1344 }
1345
1346 //-------------- Initializes a laser after Fire is pressed -----------------
1347
1348 static imobjptridx_t Laser_player_fire_spread_delay(const d_robot_info_array &Robot_info, fvmsegptridx &vmsegptridx, const vmobjptridx_t obj, const weapon_id_type laser_type, const player_gun_number gun_num, const fix spreadr, const fix spreadu, const fix delay_time, const weapon_sound_flag make_sound, const vms_vector &shot_orientation, const icobjidx_t Network_laser_track)
1349 {
1350 fvi_info hit_data;
1351 // Find the initial position of the laser
1352 auto pnt{&Player_ship->gun_points[gun_num]};
1353 const auto gun_point{vm_vec_build_rotated(*pnt, vm_transposed_matrix(obj->orient))};
1354 auto LaserPos{vm_vec_build_add(obj->pos, gun_point)};
1355
1356 // If supposed to fire at a delayed time (delay_time), then move this point backwards.
1357 if (delay_time)
1358 vm_vec_scale_add2(LaserPos, shot_orientation, -fixmul(delay_time, Weapon_info[laser_type].speed[GameUniqueState.Difficulty_level]));
1359
1360 // do_muzzle_stuff(obj, &Pos);
1361
1362 //--------------- Find LaserPos and LaserSeg ------------------
1363 const auto Fate{find_vector_intersection(fvi_query{
1364 obj->pos,
1365 LaserPos,
1366 fvi_query::unused_ignore_obj_list,
1367 &LevelUniqueObjectState,
1368 &Robot_info,
1369 #if DXX_BUILD_DESCENT == 1
1370 0,
1371 #elif DXX_BUILD_DESCENT == 2
1372 FQ_IGNORE_POWERUPS,
1373 #endif
1374 obj,
1375 }, obj->segnum, 0x10, hit_data)};
1376
1377 auto LaserSeg{hit_data.hit_seg};
1378 if (LaserSeg == segment_none) //some sort of annoying error
1379 return object_none;
1380
1381 //SORT OF HACK... IF ABOVE WAS CORRECT THIS WOULDNT BE NECESSARY.
1382 if ( vm_vec_dist_quick(LaserPos, obj->pos) > 0x50000 )
1383 return object_none;
1384
1385 if (Fate == fvi_hit_type::Wall)
1386 return object_none;
1387
1388 if (Fate == fvi_hit_type::Object)
1389 {
1390 // if ( Objects[hit_data.hit_object].type == OBJ_ROBOT )
1391 // Objects[hit_data.hit_object].flags |= OF_SHOULD_BE_DEAD;
1392 // if ( Objects[hit_data.hit_object].type != OBJ_POWERUP )
1393 // return;
1394 //as of 12/6/94, we don't care if the laser is stuck in an object. We
1395 //just fire away normally
1396 }
1397
1398 // Now, make laser spread out.
1399 auto LaserDir{shot_orientation};
1400 if ((spreadr != 0) || (spreadu != 0)) {
1401 vm_vec_scale_add2(LaserDir, obj->orient.rvec, spreadr);
1402 vm_vec_scale_add2(LaserDir, obj->orient.uvec, spreadu);
1403 }
1404
1405 const auto &&objnum = Laser_create_new(LaserDir, LaserPos, vmsegptridx(LaserSeg), obj, laser_type, make_sound);
1406
1407 if (objnum == object_none)
1408 return object_none;
1409
1410 #if DXX_BUILD_DESCENT == 2
1411 create_awareness_event(obj, player_awareness_type_t::PA_WEAPON_WALL_COLLISION, LevelUniqueRobotAwarenessState);
1412
1413 // Omega cannon is a hack, not surprisingly. Don't want to do the rest of this stuff.
1414 if (laser_type == weapon_id_type::OMEGA_ID)
1415 return objnum;
1416
1417 if (laser_type == weapon_id_type::CONCUSSION_ID ||
1418 laser_type == weapon_id_type::HOMING_ID ||
1419 laser_type == weapon_id_type::SMART_ID ||
1420 laser_type == weapon_id_type::MEGA_ID ||
1421 laser_type == weapon_id_type::FLASH_ID ||
1422 //laser_type == GUIDEDMISS_ID ||
1423 //laser_type == SUPERPROX_ID ||
1424 laser_type == weapon_id_type::MERCURY_ID ||
1425 laser_type == weapon_id_type::EARTHSHAKER_ID)
1426 {
1427 const auto need_new_missile_viewer = [obj]{
1428 if (!Missile_viewer)
1429 return true;
1430 if (Missile_viewer->type != OBJ_WEAPON)
1431 return true;
1432 if (Missile_viewer->signature != Missile_viewer_sig)
1433 return true;
1434 if (get_player_id(obj) == Player_num && Missile_viewer->ctype.laser_info.parent_num != get_local_player().objnum)
1435 /* New missile fired-by local player &&
1436 * currently viewing missile not-fired-by local player
1437 */
1438 return true;
1439 return false;
1440 };
1441 const auto can_view_missile = [obj]{
1442 const auto obj_id{get_player_id(obj)};
1443 if (obj_id == Player_num)
1444 return true;
1445 if (PlayerCfg.MissileViewEnabled != MissileViewMode::EnabledSelfAndAllies)
1446 return false;
1447 {
1448 if (Game_mode & GM_MULTI_COOP)
1449 return true;
1450 if (Game_mode & GM_TEAM)
1451 return multi_get_team_from_player(Netgame, Player_num) == multi_get_team_from_player(Netgame, obj_id);
1452 }
1453 return false;
1454 };
1455 if (need_new_missile_viewer() && can_view_missile())
1456 {
1457 Missile_viewer = objnum;
1458 Missile_viewer_sig = objnum->signature;
1459 }
1460 }
1461 #endif
1462
1463 // If this weapon is supposed to be silent, set that bit!
1464 if (make_sound == weapon_sound_flag::silent)
1465 objnum->flags |= OF_SILENT;
1466
1467 // If the object firing the laser is the player, then indicate the laser object so robots can dodge.
1468 // New by MK on 6/8/95, don't let robots evade proximity bombs, thereby decreasing uselessness of bombs.
1469 if (obj == ConsoleObject)
1470 #if DXX_BUILD_DESCENT == 2
1471 if (!is_proximity_bomb_or_player_smart_mine(get_weapon_id(objnum)))
1472 #endif
1473 Player_fired_laser_this_frame = objnum;
1474
1475 if (Weapon_info[laser_type].homing_flag) {
1476 if (obj == ConsoleObject)
1477 {
1478 objnum->ctype.laser_info.track_goal = find_homing_object(LaserPos, objnum);
1479 }
1480 else // Some other player shot the homing thing
1481 {
1482 Assert(Game_mode & GM_MULTI);
1483 objnum->ctype.laser_info.track_goal = Network_laser_track;
1484 }
1485 }
1486
1487 return objnum;
1488 }
1489
1490 // -----------------------------------------------------------------------------------------------------------
1491 static imobjptridx_t Laser_player_fire_spread(const d_robot_info_array &Robot_info, const vmobjptridx_t obj, const weapon_id_type laser_type, const player_gun_number gun_num, const fix spreadr, const fix spreadu, const weapon_sound_flag make_sound, const vms_vector &shot_orientation, const icobjidx_t Network_laser_track)
1492 {
1493 return Laser_player_fire_spread_delay(Robot_info, vmsegptridx, obj, laser_type, gun_num, spreadr, spreadu, 0, make_sound, shot_orientation, Network_laser_track);
1494 }
1495
1496 }
1497
1498 // -----------------------------------------------------------------------------------------------------------
1499 imobjptridx_t Laser_player_fire(const d_robot_info_array &Robot_info, const vmobjptridx_t obj, const weapon_id_type laser_type, const player_gun_number gun_num, const weapon_sound_flag make_sound, const vms_vector &shot_orientation, const icobjidx_t Network_laser_track)
1500 {
1501 return Laser_player_fire_spread(Robot_info, obj, laser_type, gun_num, 0, 0, make_sound, shot_orientation, Network_laser_track);
1502 }
1503
1504 // -----------------------------------------------------------------------------------------------------------
1505 void Flare_create(const vmobjptridx_t obj)
1506 {
1507 const auto energy_usage{get_weapon_energy_usage_with_difficulty(Weapon_info[weapon_id_type::FLARE_ID], GameUniqueState.Difficulty_level)};
1508
1509 // MK, 11/04/95: Allowed to fire flare even if no energy.
1510 // -- if (Players[Player_num].energy >= energy_usage)
1511 auto &&plrobj = *obj;
1512 auto &player_info = plrobj.ctype.player_info;
1513 auto &energy = player_info.energy;
1514 #if DXX_BUILD_DESCENT == 1
1515 if (energy > 0)
1516 #endif
1517 {
1518 const auto &&flare = Laser_player_fire(LevelSharedRobotInfoState.Robot_info, obj, weapon_id_type::FLARE_ID, player_gun_number::center, weapon_sound_flag::audible, plrobj.orient.fvec, object_none);
1519 if (flare == object_none)
1520 return;
1521 const fix next_energy{
1522 energy > energy_usage
1523 ? energy - energy_usage
1524 : 0
1525 };
1526 energy = next_energy;
1527 #if DXX_BUILD_DESCENT == 1
1528 if (next_energy == 0)
1529 auto_select_primary_weapon(player_info);
1530 #endif
1531
1532 if (Game_mode & GM_MULTI)
1533 multi_send_fire(plrobj.orient, FLARE_ADJUST, laser_level::_1 /* unused */, 0, object_none, object_none);
1534 }
1535
1536 }
1537
1538 namespace {
1539
1540 #if DXX_BUILD_DESCENT == 1
1541 static constexpr int HOMING_MISSILE_SCALE{8};
1542 #elif DXX_BUILD_DESCENT == 2
1543 static constexpr int HOMING_MISSILE_SCALE{16};
1544
1545 static bool is_active_guided_missile(d_level_unique_object_state &LevelUniqueObjectState, const vcobjptridx_t obj)
1546 {
1547 if (obj->ctype.laser_info.parent_type != OBJ_PLAYER)
1548 return false;
1549 auto &vcobjptr = LevelUniqueObjectState.get_objects().vcptr;
1550 auto &parent_obj = *vcobjptr(obj->ctype.laser_info.parent_num);
1551 if (parent_obj.type != OBJ_PLAYER)
1552 return false;
1553 return LevelUniqueObjectState.Guided_missile.get_player_active_guided_missile(get_player_id(parent_obj)) == obj;
1554 }
1555 #endif
1556
1557 //--------------------------------------------------------------------
1558 // Set object *objp's orientation to (or towards if I'm ambitious) its velocity.
1559 static void homing_missile_turn_towards_velocity(object_base &obj, vms_vector new_fvec, fix ft)
1560 {
1561 vm_vec_scale(new_fvec, ft * HOMING_MISSILE_SCALE);
1562 vm_vec_add2(new_fvec, obj.orient.fvec);
1563 reconstruct_at(obj.orient, vm_vector_to_matrix, vm_vec_normalized_quick(new_fvec));
1564 }
1565
1566 }
1567
1568 //-------------------------------------------------------------------------------------------
1569 //sequence this laser object for this _frame_ (underscores added here to aid MK in his searching!)
1570 void Laser_do_weapon_sequence(const d_robot_info_array &Robot_info, const vmobjptridx_t obj)
1571 {
1572 auto &Objects = LevelUniqueObjectState.Objects;
1573 auto &imobjptridx = Objects.imptridx;
1574 auto &vcobjptr = Objects.vcptr;
1575 auto &vmobjptr = Objects.vmptr;
1576 assert(obj->control_source == object::control_type::weapon);
1577
1578 if (obj->lifeleft < 0 ) { // We died of old age
1579 obj->flags |= OF_SHOULD_BE_DEAD;
1580 if ( Weapon_info[get_weapon_id(obj)].damage_radius )
1581 explode_badass_weapon(Robot_info, obj, obj->pos);
1582 return;
1583 }
1584
1585 #if DXX_BUILD_DESCENT == 2
1586 if (omega_cleanup(vcobjptr, obj))
1587 return;
1588 #endif
1589
1590 //delete weapons that are not moving
1591 const auto Difficulty_level{GameUniqueState.Difficulty_level};
1592 if ( !((d_tick_count ^ static_cast<uint16_t>(obj->signature)) & 3) &&
1593 (get_weapon_id(obj) != weapon_id_type::FLARE_ID) &&
1594 (Weapon_info[get_weapon_id(obj)].speed[Difficulty_level] > 0) &&
1595 (vm_vec_mag_quick(obj->mtype.phys_info.velocity) < F2_0)) {
1596 obj_delete(LevelUniqueObjectState, Segments, obj);
1597 return;
1598 }
1599
1600 if (get_weapon_id(obj) == weapon_id_type::FUSION_ID)
1601 { //always set fusion weapon to max vel
1602 vm_vec_normalize_quick(obj->mtype.phys_info.velocity);
1603 vm_vec_scale(obj->mtype.phys_info.velocity, Weapon_info[weapon_id_type::FUSION_ID].speed[Difficulty_level]);
1604 }
1605
1606 // For homing missiles, turn towards target. (unless it's the guided missile)
1607 #if DXX_BUILD_DESCENT == 1
1608 if (Weapon_info[get_weapon_id(obj)].homing_flag)
1609 #elif DXX_BUILD_DESCENT == 2
1610 if (Weapon_info[get_weapon_id(obj)].homing_flag && !is_active_guided_missile(LevelUniqueObjectState, obj))
1611 #endif
1612 {
1613 fix dot{F1_0};
1614
1615 // For first 125ms of life, missile flies straight.
1616 if (obj->ctype.laser_info.creation_time + HOMING_FLY_STRAIGHT_TIME < GameTime64)
1617 {
1618 // If it's time to do tracking, then it's time to grow up, stop bouncing and start exploding!.
1619 #if DXX_BUILD_DESCENT == 1
1620 if ((get_weapon_id(obj) == weapon_id_type::ROBOT_SMART_HOMING_ID) || (get_weapon_id(obj) == weapon_id_type::PLAYER_SMART_HOMING_ID))
1621 #elif DXX_BUILD_DESCENT == 2
1622 if ((get_weapon_id(obj) == weapon_id_type::ROBOT_SMART_MINE_HOMING_ID) || (get_weapon_id(obj) == weapon_id_type::ROBOT_SMART_HOMING_ID) || (get_weapon_id(obj) == weapon_id_type::SMART_MINE_HOMING_ID) || (get_weapon_id(obj) == weapon_id_type::PLAYER_SMART_HOMING_ID) || (get_weapon_id(obj) == weapon_id_type::EARTHSHAKER_MEGA_ID))
1623 #endif
1624 {
1625 obj->mtype.phys_info.flags &= ~PF_BOUNCE;
1626 }
1627
1628 #ifdef NEWHOMER
1629 if (d_homer_tick_step)
1630 {
1631 const auto &&track_goal = track_track_goal(vcobjptr, imobjptridx(obj->ctype.laser_info.track_goal), obj, &dot, d_homer_tick_count);
1632
1633 if (track_goal == get_local_player().objnum) {
1634 const fix dist_to_player{vm_vec_dist_quick(obj->pos, track_goal->pos)};
1635 auto &homing_object_dist = get_local_plrobj().ctype.player_info.homing_object_dist;
1636 if (dist_to_player < homing_object_dist || homing_object_dist < 0)
1637 homing_object_dist = dist_to_player;
1638 }
1639
1640 if (track_goal != object_none)
1641 {
1642 auto vector_to_object{vm_vec_build_sub(track_goal->pos, obj->pos)};
1643 vm_vec_normalize_quick(vector_to_object);
1644 auto &&[speed_magnitude, temp_vec] = vm_vec_normalize_quick_with_magnitude(obj->mtype.phys_info.velocity);
1645 fix speed{speed_magnitude};
1646 const auto max_speed{Weapon_info[get_weapon_id(obj)].speed[Difficulty_level]};
1647 if (speed+F1_0 < max_speed) {
1648 speed += fixmul(max_speed, HOMING_TURN_TIME/2);
1649 if (speed > max_speed)
1650 speed = max_speed;
1651 }
1652 #if DXX_BUILD_DESCENT == 1
1653 dot = vm_vec_build_dot(temp_vec, vector_to_object);
1654 #endif
1655 vm_vec_add2(temp_vec, vector_to_object);
1656 // The boss' smart children track better...
1657 if (Weapon_info[get_weapon_id(obj)].render != WEAPON_RENDER_POLYMODEL)
1658 vm_vec_add2(temp_vec, vector_to_object);
1659 vm_vec_normalize_quick(temp_vec);
1660 #if DXX_BUILD_DESCENT == 1
1661 vm_vec_scale(temp_vec, speed);
1662 obj->mtype.phys_info.velocity = temp_vec;
1663 #elif DXX_BUILD_DESCENT == 2
1664 obj->mtype.phys_info.velocity = temp_vec;
1665 vm_vec_scale(obj->mtype.phys_info.velocity, speed);
1666 #endif
1667
1668 // Subtract off life proportional to amount turned.
1669 // For hardest turn, it will lose 2 seconds per second.
1670 {
1671 fix absdot{abs(F1_0 - dot)};
1672 #if DXX_BUILD_DESCENT == 1
1673 if (absdot > F1_0/8) {
1674 if (absdot > F1_0/4)
1675 absdot = F1_0/4;
1676 obj->lifeleft -= fixmul(absdot*16, HOMING_TURN_TIME);
1677 }
1678 #elif DXX_BUILD_DESCENT == 2
1679 obj->lifeleft -= fixmul(absdot*32, HOMING_TURN_TIME);
1680 #endif
1681 }
1682
1683 // Only polygon objects have visible orientation, so only they should turn.
1684 if (Weapon_info[get_weapon_id(obj)].render == WEAPON_RENDER_POLYMODEL)
1685 homing_missile_turn_towards_velocity(obj, temp_vec, HOMING_TURN_TIME); // temp_vec is normalized velocity.
1686 }
1687 }
1688 #else // old FPS-dependent homers - NOTE: I know this is very redundant but I want to keep the historical code 100% preserved to compare against potential changes in the above.
1689 // Make sure the object we are tracking is still trackable.
1690 const auto &&track_goal = track_track_goalvcobjptr, (imobjptridx(obj->ctype.laser_info.track_goal), obj, &dot, d_tick_count);
1691
1692 if (track_goal == get_local_player().objnum) {
1693 fix dist_to_player;
1694
1695 dist_to_player = vm_vec_dist_quick(obj->pos, track_goal->pos);
1696 auto &homing_object_dist = get_local_plrobj().ctype.player_info.homing_object_dist;
1697 if (dist_to_player < homing_object_dist || homing_object_dist < 0)
1698 homing_object_dist = dist_to_player;
1699 }
1700
1701 if (track_goal != object_none)
1702 {
1703 auto vector_to_object{vm_vec_build_sub(track_goal->pos, obj->pos)};
1704 vm_vec_normalize_quick(vector_to_object);
1705 temp_vec = obj->mtype.phys_info.velocity;
1706 speed = vm_vec_normalize_quick(temp_vec);
1707 max_speed = Weapon_info[get_weapon_id(obj)].speed[Difficulty_level];
1708 if (speed+F1_0 < max_speed) {
1709 speed += fixmul(max_speed, FrameTime/2);
1710 if (speed > max_speed)
1711 speed = max_speed;
1712 }
1713 #if DXX_BUILD_DESCENT == 1
1714 dot = vm_vec_build_dot(temp_vec, vector_to_object);
1715 #endif
1716 vm_vec_add2(temp_vec, vector_to_object);
1717 // The boss' smart children track better...
1718 if (Weapon_info[get_weapon_id(obj)].render != WEAPON_RENDER_POLYMODEL)
1719 vm_vec_add2(temp_vec, vector_to_object);
1720 vm_vec_normalize_quick(temp_vec);
1721 #if DXX_BUILD_DESCENT == 1
1722 vm_vec_scale(temp_vec, speed);
1723 obj->mtype.phys_info.velocity = temp_vec;
1724 #elif DXX_BUILD_DESCENT == 2
1725 obj->mtype.phys_info.velocity = temp_vec;
1726 vm_vec_scale(obj->mtype.phys_info.velocity, speed);
1727 #endif
1728
1729 // Subtract off life proportional to amount turned.
1730 // For hardest turn, it will lose 2 seconds per second.
1731 {
1732 fix lifelost, absdot;
1733
1734 absdot = abs(F1_0 - dot);
1735 #if DXX_BUILD_DESCENT == 1
1736 if (absdot > F1_0/8) {
1737 if (absdot > F1_0/4)
1738 absdot = F1_0/4;
1739 lifelost = fixmul(absdot*16, FrameTime);
1740 obj->lifeleft -= lifelost;
1741 }
1742 #elif DXX_BUILD_DESCENT == 2
1743 lifelost = fixmul(absdot*32, FrameTime);
1744 obj->lifeleft -= lifelost;
1745 #endif
1746 }
1747
1748 // Only polygon objects have visible orientation, so only they should turn.
1749 if (Weapon_info[get_weapon_id(obj)].render == WEAPON_RENDER_POLYMODEL)
1750 homing_missile_turn_towards_velocity(obj, temp_vec, FrameTime); // temp_vec is normalized velocity.
1751 }
1752 #endif
1753 }
1754 }
1755
1756 // Make sure weapon is not moving faster than allowed speed.
1757 const auto &weapon_info = Weapon_info[get_weapon_id(obj)];
1758 #if DXX_BUILD_DESCENT == 1
1759 if (weapon_info.thrust != 0)
1760 #endif
1761 {
1762 fix weapon_speed{vm_vec_mag_quick(obj->mtype.phys_info.velocity)};
1763 if (weapon_speed > weapon_info.speed[Difficulty_level])
1764 {
1765 // Only slow down if not allowed to move. Makes sense, huh? Allows proxbombs to get moved by physics force. --MK, 2/13/96
1766 #if DXX_BUILD_DESCENT == 2
1767 if (weapon_info.speed[Difficulty_level])
1768 #endif
1769 {
1770 const fix scale_factor{fixdiv(weapon_info.speed[Difficulty_level], weapon_speed)};
1771 vm_vec_scale(obj->mtype.phys_info.velocity, scale_factor);
1772 }
1773 }
1774 }
1775 }
1776
1777 }
1778
1779 namespace dcx {
1780 namespace {
1781
1782 constexpr std::integral_constant<unsigned, 30> MAX_OBJDISTS{};
1783
1784 static inline int sufficient_energy(int energy_used, fix energy)
1785 {
1786 return !energy_used || (energy >= energy_used);
1787 }
1788
1789 static inline int sufficient_ammo(int ammo_used, int uses_vulcan_ammo, ushort vulcan_ammo)
1790 {
1791 return !ammo_used || (!uses_vulcan_ammo || vulcan_ammo >= ammo_used);
1792 }
1793
1794 }
1795 }
1796
1797 namespace dsx {
1798
1799 // --------------------------------------------------------------------------------------------------
1800 // Assumption: This is only called by the actual console player, not for network players
1801
1802 void do_laser_firing_player(object &plrobj)
1803 {
1804 auto &Objects = LevelUniqueObjectState.Objects;
1805 auto &vmobjptridx = Objects.vmptridx;
1806 int rval{0};
1807
1808 if (Player_dead_state != player_dead_state::no)
1809 return;
1810
1811 auto &player_info = plrobj.ctype.player_info;
1812 const auto Primary_weapon{player_info.Primary_weapon};
1813 const auto weapon_index{Primary_weapon_to_weapon_info[Primary_weapon]};
1814 int ammo_used{Weapon_info[weapon_index].ammo_usage};
1815 const auto uses_vulcan_ammo{weapon_index_uses_vulcan_ammo(Primary_weapon)};
1816
1817 auto &pl_energy = player_info.energy;
1818 const auto base_energy_used{
1819 #if DXX_BUILD_DESCENT == 2
1820 (Primary_weapon == primary_weapon_index_t::OMEGA_INDEX)
1821 ? 0 // Omega consumes energy when recharging, not when firing.
1822 :
1823 #endif
1824 get_weapon_energy_usage_with_difficulty(Weapon_info[weapon_index], GameUniqueState.Difficulty_level)
1825 };
1826
1827 const auto energy_used{
1828 #if DXX_BUILD_DESCENT == 2
1829 // MK, 01/26/96, Helix use 2x energy in multiplayer. bitmaps.tbl parm should have been reduced for single player.
1830 (weapon_index == weapon_id_type::HELIX_ID && (Game_mode & GM_MULTI))
1831 ? base_energy_used * 2
1832 :
1833 #endif
1834 base_energy_used
1835 };
1836
1837 if (!(sufficient_energy(energy_used, pl_energy) && sufficient_ammo(ammo_used, uses_vulcan_ammo, player_info.vulcan_ammo)))
1838 auto_select_primary_weapon(player_info); // Make sure the player can fire from this weapon.
1839
1840 auto &Next_laser_fire_time = player_info.Next_laser_fire_time;
1841 while (Next_laser_fire_time <= GameTime64) {
1842 if (sufficient_energy(energy_used, pl_energy) && sufficient_ammo(ammo_used, uses_vulcan_ammo, player_info.vulcan_ammo)) {
1843 int fire_frame_overhead{0};
1844
1845 if (GameTime64 - Next_laser_fire_time <= FrameTime) // if firing is prolonged by FrameTime overhead, let's try to fix that.
1846 fire_frame_overhead = GameTime64 - Next_laser_fire_time;
1847
1848 auto laser_level = player_info.laser_level;
1849 unsigned flags{};
1850
1851 switch (Primary_weapon)
1852 {
1853 case primary_weapon_index_t::LASER_INDEX:
1854 if (player_info.powerup_flags & PLAYER_FLAGS_QUAD_LASERS)
1855 flags |= LASER_QUAD;
1856 break;
1857 case primary_weapon_index_t::SPREADFIRE_INDEX:
1858 flags |= (player_info.Spreadfire_toggle ^= LASER_SPREADFIRE_TOGGLED) & LASER_SPREADFIRE_TOGGLED;
1859 break;
1860 case primary_weapon_index_t::VULCAN_INDEX:
1861 case primary_weapon_index_t::PLASMA_INDEX:
1862 case primary_weapon_index_t::FUSION_INDEX:
1863 default:
1864 break;
1865 #if DXX_BUILD_DESCENT == 2
1866 case primary_weapon_index_t::HELIX_INDEX:
1867 flags |= (player_info.Helix_orientation++ & LASER_HELIX_MASK);
1868 break;
1869 case primary_weapon_index_t::SUPER_LASER_INDEX:
1870 case primary_weapon_index_t::GAUSS_INDEX:
1871 case primary_weapon_index_t::PHOENIX_INDEX:
1872 case primary_weapon_index_t::OMEGA_INDEX:
1873 break;
1874 #endif
1875 }
1876
1877 const auto shot_fired{do_laser_firing(vmobjptridx(get_local_player().objnum), Primary_weapon, laser_level, flags, plrobj.orient.fvec, object_none)};
1878 if (!shot_fired)
1879 break;
1880 rval += shot_fired;
1881 Next_laser_fire_time = GameTime64 - fire_frame_overhead + (unlikely(cheats.rapidfire)
1882 ? (F1_0 / 25)
1883 : (
1884 #if DXX_BUILD_DESCENT == 2
1885 weapon_index == weapon_id_type::OMEGA_ID
1886 ? OMEGA_BASE_TIME
1887 :
1888 #endif
1889 Weapon_info[weapon_index].fire_wait
1890 )
1891 );
1892
1893 if (const auto all_shots_energy_used{(energy_used * rval) / Weapon_info[weapon_index].fire_count}; pl_energy > all_shots_energy_used)
1894 pl_energy -= all_shots_energy_used;
1895 else
1896 pl_energy = 0;
1897
1898 if (uses_vulcan_ammo) {
1899 auto &v = player_info.vulcan_ammo;
1900 if (v < ammo_used)
1901 v = 0;
1902 else
1903 v -= ammo_used;
1904 maybe_drop_net_powerup(powerup_type_t::POW_VULCAN_AMMO, 1, 0);
1905 }
1906 } else {
1907 #if DXX_BUILD_DESCENT == 2
1908 Next_laser_fire_time = {GameTime64}; // Prevents shots-to-fire from building up.
1909 #endif
1910 break; // Couldn't fire weapon, so abort.
1911 }
1912 }
1913 auto_select_primary_weapon(player_info); // Make sure the player can fire from this weapon.
1914 }
1915
1916 // --------------------------------------------------------------------------------------------------
1917 // Object "objnum" fires weapon "weapon_num" of level "level". (Right now (9/24/94) level is used only for type 0 laser.
1918 // Flags are the player flags. For network mode, set to 0.
1919 // It is assumed that this is a player object (as in multiplayer), and therefore the gun positions are known.
1920 // Returns whether the weapon was fired. This is 1 on success, 0 on failure.
1921 // More than one shot is fired with a pseudo-delay so that players on slow machines can fire (for themselves
1922 // or other players) often enough for things like the vulcan cannon.
1923 int do_laser_firing(vmobjptridx_t objp, const primary_weapon_index_t weapon_num, const laser_level level, unsigned flags, vms_vector shot_orientation, const icobjidx_t Network_laser_track)
1924 {
1925 switch (weapon_num) {
1926 case primary_weapon_index_t::LASER_INDEX: {
1927 weapon_id_type weapon_type;
1928
1929 switch(level)
1930 {
1931 case laser_level::_1:
1932 weapon_type = weapon_id_type::LASER_ID_L1;
1933 break;
1934 case laser_level::_2:
1935 weapon_type = weapon_id_type::LASER_ID_L2;
1936 break;
1937 case laser_level::_3:
1938 weapon_type = weapon_id_type::LASER_ID_L3;
1939 break;
1940 case laser_level::_4:
1941 weapon_type = weapon_id_type::LASER_ID_L4;
1942 break;
1943 #if DXX_BUILD_DESCENT == 2
1944 case laser_level::_5:
1945 weapon_type = weapon_id_type::LASER_ID_L5;
1946 break;
1947 case laser_level::_6:
1948 weapon_type = weapon_id_type::LASER_ID_L6;
1949 break;
1950 #endif
1951 default:
1952 /* Invalid laser level. Cancel the shot. */
1953 return 0;
1954 }
1955 if (Laser_player_fire(LevelSharedRobotInfoState.Robot_info, objp, weapon_type, player_gun_number::_0, weapon_sound_flag::audible, shot_orientation, object_none) == object_none)
1956 /* If the first one fails, assume all will fail. Tell the
1957 * caller that no shots were fired, so that the player is not
1958 * charged for the shot.
1959 */
1960 return 0;
1961 Laser_player_fire(LevelSharedRobotInfoState.Robot_info, objp, weapon_type, player_gun_number::_1, weapon_sound_flag::silent, shot_orientation, object_none);
1962
1963 if (flags & LASER_QUAD) {
1964 // hideous system to make quad laser 1.5x powerful as normal laser, make every other quad laser bolt harmless
1965 Laser_player_fire(LevelSharedRobotInfoState.Robot_info, objp, weapon_type, player_gun_number::_2, weapon_sound_flag::silent, shot_orientation, object_none);
1966 Laser_player_fire(LevelSharedRobotInfoState.Robot_info, objp, weapon_type, player_gun_number::_3, weapon_sound_flag::silent, shot_orientation, object_none);
1967 }
1968 break;
1969 }
1970 case primary_weapon_index_t::VULCAN_INDEX: {
1971 if (Laser_player_fire_spread(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::VULCAN_ID, player_gun_number::center, d_rand()/8 - 32767/16, d_rand()/8 - 32767/16, weapon_sound_flag::audible, shot_orientation, object_none) == object_none)
1972 return 0;
1973 break;
1974 }
1975 case primary_weapon_index_t::SPREADFIRE_INDEX:
1976 {
1977 fix spreadr0, spreadu0, spreadr1, spreadu1;
1978 if (flags & LASER_SPREADFIRE_TOGGLED)
1979 spreadr0 = F1_0 / 16, spreadr1 = -F1_0 / 16, spreadu0 = spreadu1 = 0;
1980 else
1981 spreadu0 = F1_0 / 16, spreadu1 = -F1_0 / 16, spreadr0 = spreadr1 = 0;
1982 if (Laser_player_fire_spread(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::SPREADFIRE_ID, player_gun_number::center, spreadr0, spreadu0, weapon_sound_flag::silent, shot_orientation, object_none) == object_none)
1983 return 0;
1984 Laser_player_fire_spread(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::SPREADFIRE_ID, player_gun_number::center, spreadr1, spreadu1, weapon_sound_flag::silent, shot_orientation, object_none);
1985 Laser_player_fire_spread(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::SPREADFIRE_ID, player_gun_number::center, 0, 0, weapon_sound_flag::audible, shot_orientation, object_none);
1986 }
1987 break;
1988
1989 case primary_weapon_index_t::PLASMA_INDEX:
1990 if (Laser_player_fire(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::PLASMA_ID, player_gun_number::_0, weapon_sound_flag::audible, shot_orientation, object_none) == object_none)
1991 return 0;
1992 Laser_player_fire(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::PLASMA_ID, player_gun_number::_1, weapon_sound_flag::silent, shot_orientation, object_none);
1993 break;
1994
1995 case primary_weapon_index_t::FUSION_INDEX: {
1996 auto &&weapon_obj = Laser_player_fire(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::FUSION_ID, player_gun_number::_0, weapon_sound_flag::audible, shot_orientation, object_none);
1997 Laser_player_fire(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::FUSION_ID, player_gun_number::_1, weapon_sound_flag::audible, shot_orientation, object_none);
1998
1999 assert(objp->type == OBJ_PLAYER);
2000 auto &Fusion_charge = objp->ctype.player_info.Fusion_charge;
2001 flags = static_cast<int8_t>(Fusion_charge >> 12);
2002
2003 Fusion_charge = 0;
2004 if (weapon_obj == object_none)
2005 /* Clear the fusion charge even if the shot fails. This is bad
2006 * for the player, but it is safer than leaving Fusion_charge
2007 * set while the cannon is not charging.
2008 */
2009 return 0;
2010
2011 const auto &obj_fvec = objp->orient.fvec;
2012 const vms_vector backward_vec{
2013 .x = -(obj_fvec.x << 7),
2014 .y = -(obj_fvec.y << 7),
2015 .z = -(obj_fvec.z << 7)
2016 };
2017 phys_apply_force(objp, backward_vec);
2018
2019 const vms_vector rotation_vec{
2020 .x = (backward_vec.x >> 4) + d_rand() - 16384,
2021 .y = (backward_vec.y >> 4) + d_rand() - 16384,
2022 .z = (backward_vec.z >> 4) + d_rand() - 16384
2023 };
2024 phys_apply_rot(objp, rotation_vec);
2025
2026 }
2027 break;
2028 #if DXX_BUILD_DESCENT == 2
2029 case primary_weapon_index_t::GAUSS_INDEX: {
2030 if (Laser_player_fire_spread(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::GAUSS_ID, player_gun_number::center, (d_rand()/8 - 32767/16)/5, (d_rand()/8 - 32767/16)/5, weapon_sound_flag::audible, shot_orientation, object_none) == object_none)
2031 return 0;
2032 break;
2033 }
2034 case primary_weapon_index_t::HELIX_INDEX: {
2035 static constexpr std::array<std::pair<fix, fix>, 8> spread{{
2036 { F1_0/16, 0}, // Vertical
2037 { F1_0/17, F1_0/42}, // 22.5 degrees
2038 { F1_0/22, F1_0/22}, // 45 degrees
2039 { F1_0/42, F1_0/17}, // 67.5 degrees
2040 { 0, F1_0/16}, // 90 degrees
2041 {-F1_0/42, F1_0/17}, // 112.5 degrees
2042 {-F1_0/22, F1_0/22}, // 135 degrees
2043 {-F1_0/17, F1_0/42}, // 157.5 degrees
2044 }};
2045 const unsigned helix_orient{flags & LASER_HELIX_MASK};
2046 if (helix_orient >= spread.size())
2047 break;
2048 auto &&[spreadr, spreadu] = spread[helix_orient];
2049 if (Laser_player_fire_spread(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::HELIX_ID, player_gun_number::center, 0, 0, weapon_sound_flag::audible, shot_orientation, object_none) == object_none)
2050 return 0;
2051 Laser_player_fire_spread(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::HELIX_ID, player_gun_number::center, spreadr, spreadu, weapon_sound_flag::silent, shot_orientation, object_none);
2052 Laser_player_fire_spread(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::HELIX_ID, player_gun_number::center, -spreadr, -spreadu, weapon_sound_flag::silent, shot_orientation, object_none);
2053 Laser_player_fire_spread(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::HELIX_ID, player_gun_number::center, spreadr*2, spreadu*2, weapon_sound_flag::silent, shot_orientation, object_none);
2054 Laser_player_fire_spread(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::HELIX_ID, player_gun_number::center, -spreadr*2, -spreadu*2, weapon_sound_flag::silent, shot_orientation, object_none);
2055 break;
2056 }
2057
2058 case primary_weapon_index_t::PHOENIX_INDEX:
2059 if (Laser_player_fire(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::PHOENIX_ID, player_gun_number::_0, weapon_sound_flag::audible, shot_orientation, object_none) == object_none)
2060 return 0;
2061 Laser_player_fire(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::PHOENIX_ID, player_gun_number::_1, weapon_sound_flag::silent, shot_orientation, object_none);
2062 break;
2063
2064 case primary_weapon_index_t::OMEGA_INDEX:
2065 if (Laser_player_fire(LevelSharedRobotInfoState.Robot_info, objp, weapon_id_type::OMEGA_ID, player_gun_number::_1, weapon_sound_flag::audible, shot_orientation, Network_laser_track) == object_none)
2066 return 0;
2067 break;
2068
2069 case primary_weapon_index_t::SUPER_LASER_INDEX:
2070 #endif
2071 default:
2072 Int3(); // Contact Yuan: Unknown Primary weapon type, setting to 0.
2073 return 0;
2074 }
2075
2076 // Set values to be recognized during comunication phase, if we are the
2077 // one shooting
2078 if ((Game_mode & GM_MULTI) && objp == get_local_player().objnum)
2079 multi_send_fire(objp->orient, underlying_value(weapon_num), level, flags, Network_laser_track, object_none);
2080 return 1;
2081 }
2082
2083 }
2084
2085 namespace dcx {
2086
2087 uint_fast8_t laser_info::test_set_hitobj(const vcobjidx_t o)
2088 {
2089 if (const auto r{test_hitobj(o)})
2090 return r;
2091 const std::size_t values_size{hitobj_values.size()};
2092 assert(hitobj_count <= values_size);
2093 assert(hitobj_pos < values_size);
2094 hitobj_values[hitobj_pos++] = o;
2095 if (unlikely(hitobj_pos == values_size))
2096 hitobj_pos = 0;
2097 if (likely(hitobj_count != values_size))
2098 ++ hitobj_count;
2099 return false;
2100 }
2101
2102 uint_fast8_t laser_info::test_hitobj(const vcobjidx_t o) const
2103 {
2104 /* Search backward so that the highest added element is the first
2105 * one considered. This preserves most of the benefit of tracking
2106 * the most recent hit separately, without storing it separately or
2107 * requiring expensive shift operations as new elements are added.
2108 *
2109 * This efficiency hack becomes ineffective (but not incorrect) if
2110 * the list wraps. Wrapping should be very rare, and even a full
2111 * search is relatively cheap, so it is not worth complicating the
2112 * code to ensure that elements are always searched in
2113 * most-recently-added order.
2114 */
2115 const auto &&r = partial_range(hitobj_values, hitobj_count).reversed();
2116 const auto &&e = r.end();
2117 return std::find(r.begin(), e, o) != e;
2118 }
2119
2120 }
2121
2122 namespace dsx {
2123 namespace {
2124
2125 // -------------------------------------------------------------------------------------------
2126 // if goal_obj == -1, then create random vector
2127 static imobjptridx_t create_homing_missile(fvmsegptridx &vmsegptridx, const vmobjptridx_t objp, const imobjptridx_t goal_obj, weapon_id_type objtype, const weapon_sound_flag make_sound)
2128 {
2129 const auto &&objnum = Laser_create_new(build_homing_weapon_initial_vector(objp, static_cast<const object *>(goal_obj)), objp->pos, vmsegptridx(objp->segnum), objp, objtype, make_sound);
2130 if (objnum != object_none)
2131 {
2132 // Fixed to make sure the right person gets credit for the kill
2133
2134 // Objects[objnum].ctype.laser_info.parent_num = objp->ctype.laser_info.parent_num;
2135 // Objects[objnum].ctype.laser_info.parent_type = objp->ctype.laser_info.parent_type;
2136 // Objects[objnum].ctype.laser_info.parent_signature = objp->ctype.laser_info.parent_signature;
2137 objnum->ctype.laser_info.track_goal = goal_obj;
2138 }
2139 return objnum;
2140 }
2141
2142 struct miniparent
2143 {
2144 short type;
2145 objnum_t num;
2146 };
2147
2148 //-----------------------------------------------------------------------------
2149 // Create the children of a smart bomb, which is a bunch of homing missiles.
2150 static void create_smart_children(object_array &Objects, const vmobjptridx_t objp, const uint_fast32_t num_smart_children, const miniparent parent)
2151 {
2152 auto &vcobjptridx = Objects.vcptridx;
2153 auto &vcobjptr = Objects.vcptr;
2154 unsigned numobjs{0};
2155 weapon_id_type blob_id;
2156
2157 std::array<objnum_t, MAX_OBJDISTS> objlist;
2158 #if DXX_BUILD_DESCENT == 2
2159 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
2160 #endif
2161 {
2162 if (Game_mode & GM_MULTI)
2163 d_srand(8321L);
2164
2165 range_for (const auto &&curobjp, vcobjptridx)
2166 {
2167 if (((curobjp->type == OBJ_ROBOT && !curobjp->ctype.ai_info.CLOAKED) || curobjp->type == OBJ_PLAYER) && curobjp != parent.num)
2168 {
2169 if (curobjp->type == OBJ_PLAYER)
2170 {
2171 if (parent.type == OBJ_PLAYER && (Game_mode & GM_MULTI_COOP))
2172 continue;
2173 if ((Game_mode & GM_TEAM) && multi_get_team_from_player(Netgame, get_player_id(curobjp)) == multi_get_team_from_player(Netgame, get_player_id(vcobjptr(parent.num))))
2174 continue;
2175 if (curobjp->ctype.player_info.powerup_flags & PLAYER_FLAGS_CLOAKED)
2176 continue;
2177 }
2178
2179 // Robot blobs can't track robots.
2180 if (curobjp->type == OBJ_ROBOT) {
2181 if (parent.type == OBJ_ROBOT)
2182 continue;
2183
2184 #if DXX_BUILD_DESCENT == 2
2185 // Your shots won't track the buddy.
2186 if (parent.type == OBJ_PLAYER)
2187 if (Robot_info[get_robot_id(curobjp)].companion)
2188 continue;
2189 #endif
2190 }
2191
2192 const auto &&dist = vm_vec_dist2(objp->pos, curobjp->pos);
2193 if (dist < MAX_SMART_DISTANCE_SQUARED)
2194 {
2195 if (object_to_object_visibility(objp, curobjp, FQ_TRANSWALL))
2196 {
2197 objlist[numobjs] = curobjp;
2198 numobjs++;
2199 if (numobjs >= MAX_OBJDISTS) {
2200 numobjs = MAX_OBJDISTS;
2201 break;
2202 }
2203 }
2204 }
2205 }
2206 }
2207
2208 // Get type of weapon for child from parent.
2209 #if DXX_BUILD_DESCENT == 1
2210 if (parent.type == OBJ_PLAYER)
2211 {
2212 blob_id = weapon_id_type::PLAYER_SMART_HOMING_ID;
2213 } else {
2214 blob_id = ((N_weapon_types<weapon_id_type::ROBOT_SMART_HOMING_ID)?(weapon_id_type::PLAYER_SMART_HOMING_ID):(weapon_id_type::ROBOT_SMART_HOMING_ID)); // NOTE: Shareware & reg 1.0 do not have their own Smart structure for bots. It was introduced in 1.4 to make Smart blobs from lvl 7 boss easier to dodge. So if we do not have this type, revert to player's Smart behaviour..,
2215 }
2216 #elif DXX_BUILD_DESCENT == 2
2217 if (objp->type == OBJ_WEAPON) {
2218 blob_id = Weapon_info[get_weapon_id(objp)].children;
2219 Assert(blob_id != weapon_none); // Hmm, missing data in bitmaps.tbl. Need "children=NN" parameter.
2220 } else {
2221 Assert(objp->type == OBJ_ROBOT);
2222 blob_id = weapon_id_type::ROBOT_SMART_HOMING_ID;
2223 }
2224 #endif
2225
2226 objnum_t last_sel_objnum{object_none};
2227 const auto get_random_different_object = [&]{
2228 for (;;)
2229 {
2230 const auto r{objlist[(d_rand() * numobjs) >> 15]};
2231 if (last_sel_objnum != r)
2232 return last_sel_objnum = r;
2233 }
2234 };
2235 for (auto i = num_smart_children; i--;)
2236 {
2237 const auto &&sel_objnum = numobjs
2238 ? Objects.imptridx((numobjs == 1)
2239 ? objlist[0]
2240 : get_random_different_object())
2241 : object_none;
2242 create_homing_missile(vmsegptridx, objp, sel_objnum, blob_id, (i==0) ? weapon_sound_flag::audible : weapon_sound_flag::silent);
2243 }
2244 }
2245 }
2246
2247 }
2248
2249 void create_weapon_smart_children(const vmobjptridx_t objp)
2250 {
2251 auto &Objects = LevelUniqueObjectState.Objects;
2252 const auto wid{get_weapon_id(objp)};
2253 #if DXX_BUILD_DESCENT == 1
2254 if (wid != weapon_id_type::SMART_ID)
2255 #elif DXX_BUILD_DESCENT == 2
2256 if (Weapon_info[wid].children == weapon_id_type::unspecified)
2257 #endif
2258 return;
2259 #if DXX_BUILD_DESCENT == 2
2260 if (wid == weapon_id_type::EARTHSHAKER_ID)
2261 blast_nearby_glass(objp, Weapon_info[weapon_id_type::EARTHSHAKER_ID].strength[GameUniqueState.Difficulty_level]);
2262 #endif
2263 create_smart_children(Objects, objp, NUM_SMART_CHILDREN, {objp->ctype.laser_info.parent_type, objp->ctype.laser_info.parent_num});
2264 }
2265
2266 #if DXX_BUILD_DESCENT == 2
2267
2268 void create_robot_smart_children(const vmobjptridx_t objp, const uint_fast32_t num_smart_children)
2269 {
2270 auto &Objects = LevelUniqueObjectState.Objects;
2271 create_smart_children(Objects, objp, num_smart_children, {OBJ_ROBOT, objp});
2272 }
2273
2274 //give up control of the guided missile
2275 void release_local_guided_missile(d_level_unique_object_state &LevelUniqueObjectState, const playernum_t player_num, object &missile)
2276 {
2277 Missile_viewer = &missile;
2278 if (Game_mode & GM_MULTI)
2279 multi_send_guided_info(missile, 1);
2280 if (Newdemo_state == ND_STATE_RECORDING)
2281 newdemo_record_guided_end();
2282 LevelUniqueObjectState.Guided_missile.clear_player_active_guided_missile(player_num);
2283 }
2284
2285 void release_remote_guided_missile(d_level_unique_object_state &LevelUniqueObjectState, const playernum_t player_num)
2286 {
2287 LevelUniqueObjectState.Guided_missile.clear_player_active_guided_missile(player_num);
2288 }
2289 #endif
2290
2291 // -------------------------------------------------------------------------------------------
2292 //changed on 31/3/10 by kreatordxx to distinguish between drop bomb and secondary fire
2293 void do_missile_firing(const secondary_weapon_index_t weapon, const vmobjptridx_t plrobjidx)
2294 {
2295 int gun_flag{0};
2296 fix fire_frame_overhead{0};
2297
2298 auto &plrobj = *plrobjidx;
2299 auto &player_info = plrobj.ctype.player_info;
2300 auto &Next_missile_fire_time = player_info.Next_missile_fire_time;
2301 if (GameTime64 - Next_missile_fire_time <= FrameTime) // if firing is prolonged by FrameTime overhead, let's try to fix that.
2302 fire_frame_overhead = GameTime64 - Next_missile_fire_time;
2303
2304 #if DXX_BUILD_DESCENT == 2
2305 const auto &&gimobj = LevelUniqueObjectState.Guided_missile.get_player_active_guided_missile(LevelUniqueObjectState.get_objects().vmptr, Player_num);
2306 if (gimobj != nullptr)
2307 {
2308 release_local_guided_missile(LevelUniqueObjectState, Player_num, *gimobj);
2309 Next_missile_fire_time = GameTime64 + Weapon_info[Secondary_weapon_to_weapon_info[weapon]].fire_wait - fire_frame_overhead;
2310 return;
2311 }
2312 #endif
2313
2314 if (Player_dead_state != player_dead_state::no)
2315 return;
2316 if (auto &secondary_weapon_ammo = player_info.secondary_ammo[weapon])
2317 {
2318 const auto weapon_index{Secondary_weapon_to_weapon_info[weapon]};
2319 const auto base_weapon_gun{Secondary_weapon_to_gun_num[weapon]};
2320 auto &Missile_gun = plrobj.ctype.player_info.missile_gun;
2321 const auto weapon_gun{(base_weapon_gun == player_gun_number::_4)
2322 ? static_cast<player_gun_number>(static_cast<uint8_t>(base_weapon_gun) + (gun_flag = (Missile_gun & 1)))
2323 : base_weapon_gun
2324 };
2325 const auto &&objnum = Laser_player_fire(LevelSharedRobotInfoState.Robot_info, plrobjidx, weapon_index, weapon_gun, weapon_sound_flag::audible, plrobj.orient.fvec, object_none);
2326 if (objnum == object_none)
2327 /* If the missile was not created, return early. Do not charge for
2328 * it, and do not report it to other players.
2329 */
2330 return;
2331 /* Toggle between the left and right missile guns.
2332 */
2333 if (base_weapon_gun == player_gun_number::_4)
2334 Missile_gun++;
2335 if (!cheats.rapidfire)
2336 Next_missile_fire_time = GameTime64 + Weapon_info[weapon_index].fire_wait - fire_frame_overhead;
2337 else
2338 Next_missile_fire_time = GameTime64 + (F1_0/25) - fire_frame_overhead;
2339
2340 -- secondary_weapon_ammo;
2341 if (weapon != secondary_weapon_index_t::CONCUSSION_INDEX)
2342 maybe_drop_net_powerup(Secondary_weapon_to_powerup[weapon], 1, 0);
2343
2344 #if DXX_BUILD_DESCENT == 1
2345 if (weapon == secondary_weapon_index_t::MEGA_INDEX)
2346 #elif DXX_BUILD_DESCENT == 2
2347 if (weapon == secondary_weapon_index_t::MEGA_INDEX || weapon == secondary_weapon_index_t::SMISSILE5_INDEX)
2348 #endif
2349 {
2350 const vms_vector backward_vec{
2351 -(plrobj.orient.fvec.x << 7),
2352 -(plrobj.orient.fvec.y << 7),
2353 -(plrobj.orient.fvec.z << 7)
2354 };
2355 phys_apply_force(plrobj, backward_vec);
2356
2357 const vms_vector rotation_vec{
2358 (backward_vec.x >> 4) + d_rand() - 16384,
2359 (backward_vec.y >> 4) + d_rand() - 16384,
2360 (backward_vec.z >> 4) + d_rand() - 16384
2361 };
2362 phys_apply_rot(plrobj, rotation_vec);
2363 }
2364
2365 if (Game_mode & GM_MULTI)
2366 {
2367 const object &obj = *objnum;
2368 multi_send_fire(plrobj.orient, underlying_value(weapon) + MISSILE_ADJUST, laser_level::_1 /* unused */, gun_flag, obj.ctype.laser_info.track_goal, weapon_index_is_player_bomb(weapon) ? objnum : object_none);
2369 }
2370
2371 // don't autoselect if dropping prox and prox not current weapon
2372 if (player_info.Secondary_weapon == weapon)
2373 auto_select_secondary_weapon(player_info); //select next missile, if this one out of ammo
2374 }
2375 }
2376 }