Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 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 | * Code for rendering & otherwise dealing with explosions |
||
23 | * |
||
24 | */ |
||
25 | |||
26 | #include <algorithm> |
||
27 | #include <numeric> |
||
28 | #include <stdlib.h> |
||
29 | #include <stdio.h> |
||
30 | #include <string.h> |
||
31 | |||
32 | #include "dxxerror.h" |
||
33 | #include "maths.h" |
||
34 | #include "vecmat.h" |
||
35 | #include "gr.h" |
||
36 | #include "3d.h" |
||
37 | |||
38 | #include "inferno.h" |
||
39 | #include "object.h" |
||
40 | #include "vclip.h" |
||
41 | #include "game.h" |
||
42 | #include "robot.h" |
||
43 | #include "sounds.h" |
||
44 | #include "player.h" |
||
45 | #include "gauges.h" |
||
46 | #include "powerup.h" |
||
47 | #include "bm.h" |
||
48 | #include "ai.h" |
||
49 | #include "weapon.h" |
||
50 | #include "fireball.h" |
||
51 | #include "collide.h" |
||
52 | #include "newmenu.h" |
||
53 | #include "gameseq.h" |
||
54 | #include "physics.h" |
||
55 | #include "scores.h" |
||
56 | #include "laser.h" |
||
57 | #include "wall.h" |
||
58 | #include "multi.h" |
||
59 | #include "endlevel.h" |
||
60 | #include "timer.h" |
||
61 | #include "fuelcen.h" |
||
62 | #include "playsave.h" |
||
63 | #include "cntrlcen.h" |
||
64 | #include "gameseg.h" |
||
65 | #include "automap.h" |
||
66 | #include "byteutil.h" |
||
67 | |||
68 | #include "compiler-range_for.h" |
||
69 | #include "partial_range.h" |
||
70 | #include "segiter.h" |
||
71 | |||
72 | using std::min; |
||
73 | |||
74 | #define EXPLOSION_SCALE (F1_0*5/2) //explosion is the obj size times this |
||
75 | |||
76 | //--unused-- ubyte Frame_processed[MAX_OBJECTS]; |
||
77 | |||
78 | namespace dcx { |
||
79 | |||
80 | unsigned Num_exploding_walls; |
||
81 | |||
82 | void init_exploding_walls() |
||
83 | { |
||
84 | Num_exploding_walls = 0; |
||
85 | } |
||
86 | |||
87 | } |
||
88 | |||
89 | namespace dsx { |
||
90 | |||
91 | #if defined(DXX_BUILD_DESCENT_II) |
||
92 | fix Flash_effect=0; |
||
93 | constexpr int PK1=1, PK2=8; |
||
94 | #endif |
||
95 | |||
96 | static bool can_collide(const object *const weapon_object, const object_base &iter_object, const object *const parent_object) |
||
97 | { |
||
98 | /* `weapon_object` may not be a weapon in some cases, though the |
||
99 | * caller originally expected it would be. For this function, the |
||
100 | * distinction is currently irrelevant. |
||
101 | */ |
||
102 | #if defined(DXX_BUILD_DESCENT_I) |
||
103 | (void)weapon_object; |
||
104 | #elif defined(DXX_BUILD_DESCENT_II) |
||
105 | if (weapon_object == &iter_object) |
||
106 | return false; |
||
107 | if (iter_object.type == OBJ_NONE) |
||
108 | return false; |
||
109 | if (iter_object.flags & OF_SHOULD_BE_DEAD) |
||
110 | return false; |
||
111 | #endif |
||
112 | switch (iter_object.type) |
||
113 | { |
||
114 | #if defined(DXX_BUILD_DESCENT_II) |
||
115 | case OBJ_WEAPON: |
||
116 | return is_proximity_bomb_or_player_smart_mine_or_placed_mine(get_weapon_id(iter_object)); |
||
117 | #endif |
||
118 | case OBJ_CNTRLCEN: |
||
119 | case OBJ_PLAYER: |
||
120 | return true; |
||
121 | case OBJ_ROBOT: |
||
122 | if (parent_object == nullptr) |
||
123 | return false; |
||
124 | if (parent_object->type != OBJ_ROBOT) |
||
125 | return true; |
||
126 | return get_robot_id(*parent_object) != iter_object.id; |
||
127 | default: |
||
128 | return false; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | static imobjptridx_t object_create_explosion_sub(const d_vclip_array &Vclip, fvmobjptridx &vmobjptridx, const imobjptridx_t obj_explosion_origin, const vmsegptridx_t segnum, const vms_vector &position, const fix size, const int vclip_type, const fix maxdamage, const fix maxdistance, const fix maxforce, const icobjptridx_t parent) |
||
133 | { |
||
134 | /* `obj_explosion_origin` may not be a weapon in some cases, though |
||
135 | * this function originally expected it would be. |
||
136 | */ |
||
137 | #if defined(DXX_BUILD_DESCENT_II) |
||
138 | auto &Objects = LevelUniqueObjectState.Objects; |
||
139 | #endif |
||
140 | auto &Robot_info = LevelSharedRobotInfoState.Robot_info; |
||
141 | const auto &&obj_fireball = obj_create(OBJ_FIREBALL, vclip_type, segnum, position, &vmd_identity_matrix, size, |
||
142 | CT_EXPLOSION,MT_NONE,RT_FIREBALL); |
||
143 | |||
144 | if (obj_fireball == object_none) |
||
145 | { |
||
146 | return object_none; |
||
147 | } |
||
148 | |||
149 | //now set explosion-specific data |
||
150 | |||
151 | obj_fireball->lifeleft = Vclip[vclip_type ].play_time; |
||
152 | obj_fireball->ctype.expl_info.spawn_time = -1; |
||
153 | obj_fireball->ctype.expl_info.delete_objnum = object_none; |
||
154 | obj_fireball->ctype.expl_info.delete_time = -1; |
||
155 | |||
156 | if (maxdamage > 0) { |
||
157 | fix force; |
||
158 | vms_vector pos_hit, vforce; |
||
159 | fix damage; |
||
160 | // -- now legal for badass explosions on a wall. Assert(obj_explosion_origin != NULL); |
||
161 | |||
162 | range_for (const auto &&obj_iter, vmobjptridx) |
||
163 | { |
||
164 | // Weapons used to be affected by badass explosions, but this introduces serious problems. |
||
165 | // When a smart bomb blows up, if one of its children goes right towards a nearby wall, it will |
||
166 | // blow up, blowing up all the children. So I remove it. MK, 09/11/94 |
||
167 | if (can_collide(obj_explosion_origin, obj_iter, parent)) |
||
168 | { |
||
169 | const auto dist = vm_vec_dist_quick(obj_iter->pos, obj_fireball->pos); |
||
170 | // Make damage be from 'maxdamage' to 0.0, where 0.0 is 'maxdistance' away; |
||
171 | if ( dist < maxdistance ) { |
||
172 | if (object_to_object_visibility(obj_fireball, obj_iter, FQ_TRANSWALL)) |
||
173 | { |
||
174 | damage = maxdamage - fixmuldiv( dist, maxdamage, maxdistance ); |
||
175 | force = maxforce - fixmuldiv( dist, maxforce, maxdistance ); |
||
176 | |||
177 | // Find the force vector on the object |
||
178 | vm_vec_normalized_dir_quick(vforce, obj_iter->pos, obj_fireball->pos); |
||
179 | vm_vec_scale(vforce, force ); |
||
180 | |||
181 | // Find where the point of impact is... ( pos_hit ) |
||
182 | vm_vec_scale(vm_vec_sub(pos_hit, obj_fireball->pos, obj_iter->pos), fixdiv(obj_iter->size, obj_iter->size + dist)); |
||
183 | switch (obj_iter->type) |
||
184 | { |
||
185 | #if defined(DXX_BUILD_DESCENT_II) |
||
186 | case OBJ_WEAPON: |
||
187 | phys_apply_force(obj_iter, vforce); |
||
188 | |||
189 | if (is_proximity_bomb_or_player_smart_mine(get_weapon_id(obj_iter))) |
||
190 | { //prox bombs have chance of blowing up |
||
191 | if (fixmul(dist,force) > i2f(8000)) { |
||
192 | obj_iter->flags |= OF_SHOULD_BE_DEAD; |
||
193 | explode_badass_weapon(obj_iter, obj_iter->pos); |
||
194 | } |
||
195 | } |
||
196 | break; |
||
197 | #endif |
||
198 | case OBJ_ROBOT: |
||
199 | { |
||
200 | phys_apply_force(obj_iter, vforce); |
||
201 | #if defined(DXX_BUILD_DESCENT_II) |
||
202 | // If not a boss, stun for 2 seconds at 32 force, 1 second at 16 force |
||
203 | fix flash; |
||
204 | if (obj_explosion_origin != object_none && obj_explosion_origin->type == OBJ_WEAPON && !Robot_info[get_robot_id(obj_iter)].boss_flag && (flash = Weapon_info[get_weapon_id(obj_explosion_origin)].flash)) |
||
205 | { |
||
206 | ai_static *const aip = &obj_iter->ctype.ai_info; |
||
207 | |||
208 | if (obj_fireball->ctype.ai_info.SKIP_AI_COUNT * FrameTime < F1_0) |
||
209 | { |
||
210 | const int force_val = f2i(fixdiv(vm_vec_mag_quick(vforce) * flash, FrameTime) / 128) + 2; |
||
211 | aip->SKIP_AI_COUNT += force_val; |
||
212 | obj_iter->mtype.phys_info.rotthrust.x = ((d_rand() - 16384) * force_val) / 16; |
||
213 | obj_iter->mtype.phys_info.rotthrust.y = ((d_rand() - 16384) * force_val) / 16; |
||
214 | obj_iter->mtype.phys_info.rotthrust.z = ((d_rand() - 16384) * force_val) / 16; |
||
215 | obj_iter->mtype.phys_info.flags |= PF_USES_THRUST; |
||
216 | } else |
||
217 | aip->SKIP_AI_COUNT--; |
||
218 | } |
||
219 | #endif |
||
220 | |||
221 | const auto Difficulty_level = GameUniqueState.Difficulty_level; |
||
222 | // When a robot gets whacked by a badass force, he looks towards it because robots tend to get blasted from behind. |
||
223 | { |
||
224 | vms_vector neg_vforce; |
||
225 | neg_vforce.x = vforce.x * -2 * (7 - Difficulty_level)/8; |
||
226 | neg_vforce.y = vforce.y * -2 * (7 - Difficulty_level)/8; |
||
227 | neg_vforce.z = vforce.z * -2 * (7 - Difficulty_level)/8; |
||
228 | phys_apply_rot(obj_iter, neg_vforce); |
||
229 | } |
||
230 | if (obj_iter->shields >= 0) |
||
231 | { |
||
232 | #if defined(DXX_BUILD_DESCENT_II) |
||
233 | const auto &robot_info = Robot_info[get_robot_id(obj_iter)]; |
||
234 | if (robot_info.boss_flag >= BOSS_D2 && Boss_invulnerable_matter[robot_info.boss_flag - BOSS_D2]) |
||
235 | damage /= 4; |
||
236 | #endif |
||
237 | if (apply_damage_to_robot(obj_iter, damage, parent)) |
||
238 | if (obj_explosion_origin != object_none && parent == get_local_player().objnum) |
||
239 | add_points_to_score(ConsoleObject->ctype.player_info, Robot_info[get_robot_id(obj_iter)].score_value); |
||
240 | } |
||
241 | #if defined(DXX_BUILD_DESCENT_II) |
||
242 | if (obj_explosion_origin != object_none && Robot_info[get_robot_id(obj_iter)].companion && !(obj_explosion_origin->type == OBJ_WEAPON && Weapon_info[get_weapon_id(obj_explosion_origin)].flash)) |
||
243 | { |
||
244 | static const char ouch_str[] = "ouch! " "ouch! " "ouch! " "ouch! "; |
||
245 | int count; |
||
246 | |||
247 | count = f2i(damage/8); |
||
248 | if (count > 4) |
||
249 | count = 4; |
||
250 | else if (count <= 0) |
||
251 | count = 1; |
||
252 | buddy_message_str(&ouch_str[(4 - count) * 6]); |
||
253 | } |
||
254 | #endif |
||
255 | break; |
||
256 | } |
||
257 | case OBJ_CNTRLCEN: |
||
258 | if (parent != object_none && obj_iter->shields >= 0) |
||
259 | { |
||
260 | apply_damage_to_controlcen(obj_iter, damage, parent); |
||
261 | } |
||
262 | break; |
||
263 | case OBJ_PLAYER: { |
||
264 | icobjptridx_t killer = object_none; |
||
265 | #if defined(DXX_BUILD_DESCENT_II) |
||
266 | // Hack! Warning! Test code! |
||
267 | fix flash; |
||
268 | if (obj_explosion_origin != object_none && obj_explosion_origin->type == OBJ_WEAPON && (flash = Weapon_info[get_weapon_id(obj_explosion_origin)].flash) && get_player_id(obj_iter) == Player_num) |
||
269 | { |
||
270 | int fe = min(F1_0 * 4, force * flash / 32); // For four seconds or less |
||
271 | |||
272 | if (laser_parent_is_player(Objects.vcptr, obj_explosion_origin->ctype.laser_info, *ConsoleObject)) |
||
273 | { |
||
274 | fe /= 2; |
||
275 | force /= 2; |
||
276 | } |
||
277 | if (force > F1_0) { |
||
278 | Flash_effect = fe; |
||
279 | PALETTE_FLASH_ADD(PK1 + f2i(PK2*force), PK1 + f2i(PK2*force), PK1 + f2i(PK2*force)); |
||
280 | } |
||
281 | } |
||
282 | #endif |
||
283 | if (obj_explosion_origin != object_none && (Game_mode & GM_MULTI) && obj_explosion_origin->type == OBJ_PLAYER) |
||
284 | { |
||
285 | killer = obj_explosion_origin; |
||
286 | } |
||
287 | auto vforce2 = vforce; |
||
288 | if (parent != object_none ) { |
||
289 | killer = parent; |
||
290 | if (killer != ConsoleObject) // if someone else whacks you, cut force by 2x |
||
291 | { |
||
292 | vforce2.x /= 2; vforce2.y /= 2; vforce2.z /= 2; |
||
293 | } |
||
294 | } |
||
295 | vforce2.x /= 2; vforce2.y /= 2; vforce2.z /= 2; |
||
296 | |||
297 | phys_apply_force(obj_iter, vforce); |
||
298 | phys_apply_rot(obj_iter, vforce2); |
||
299 | if (obj_iter->shields >= 0) |
||
300 | { |
||
301 | #if defined(DXX_BUILD_DESCENT_II) |
||
302 | if (GameUniqueState.Difficulty_level == 0) |
||
303 | damage /= 4; |
||
304 | #endif |
||
305 | apply_damage_to_player(obj_iter, killer, damage, 1 ); |
||
306 | } |
||
307 | } |
||
308 | break; |
||
309 | |||
310 | default: |
||
311 | Int3(); // Illegal object type |
||
312 | } // end switch |
||
313 | } else { |
||
314 | ; |
||
315 | } // end if (object_to_object_visibility... |
||
316 | } // end if (dist < maxdistance) |
||
317 | } |
||
318 | } // end for |
||
319 | } // end if (maxdamage... |
||
320 | return obj_fireball; |
||
321 | } |
||
322 | |||
323 | void object_create_muzzle_flash(const vmsegptridx_t segnum, const vms_vector &position, fix size, int vclip_type ) |
||
324 | { |
||
325 | auto &Objects = LevelUniqueObjectState.Objects; |
||
326 | auto &vmobjptridx = Objects.vmptridx; |
||
327 | object_create_explosion_sub(Vclip, vmobjptridx, object_none, segnum, position, size, vclip_type, 0, 0, 0, object_none ); |
||
328 | } |
||
329 | |||
330 | imobjptridx_t object_create_explosion(const vmsegptridx_t segnum, const vms_vector &position, fix size, int vclip_type ) |
||
331 | { |
||
332 | auto &Objects = LevelUniqueObjectState.Objects; |
||
333 | auto &vmobjptridx = Objects.vmptridx; |
||
334 | return object_create_explosion_sub(Vclip, vmobjptridx, object_none, segnum, position, size, vclip_type, 0, 0, 0, object_none ); |
||
335 | } |
||
336 | |||
337 | imobjptridx_t object_create_badass_explosion(const imobjptridx_t objp, const vmsegptridx_t segnum, const vms_vector &position, fix size, int vclip_type, fix maxdamage, fix maxdistance, fix maxforce, const icobjptridx_t parent ) |
||
338 | { |
||
339 | auto &Objects = LevelUniqueObjectState.Objects; |
||
340 | auto &vmobjptridx = Objects.vmptridx; |
||
341 | const imobjptridx_t rval = object_create_explosion_sub(Vclip, vmobjptridx, objp, segnum, position, size, vclip_type, maxdamage, maxdistance, maxforce, parent); |
||
342 | |||
343 | if ((objp != object_none) && (objp->type == OBJ_WEAPON)) |
||
344 | create_weapon_smart_children(objp); |
||
345 | return rval; |
||
346 | } |
||
347 | |||
348 | //blows up a badass weapon, creating the badass explosion |
||
349 | //return the explosion object |
||
350 | void explode_badass_weapon(const vmobjptridx_t obj,const vms_vector &pos) |
||
351 | { |
||
352 | auto &Objects = LevelUniqueObjectState.Objects; |
||
353 | auto &imobjptridx = Objects.imptridx; |
||
354 | const auto weapon_id = get_weapon_id(obj); |
||
355 | const weapon_info *wi = &Weapon_info[weapon_id]; |
||
356 | |||
357 | Assert(wi->damage_radius); |
||
358 | #if defined(DXX_BUILD_DESCENT_II) |
||
359 | if (weapon_id == weapon_id_type::EARTHSHAKER_ID || weapon_id == weapon_id_type::ROBOT_EARTHSHAKER_ID) |
||
360 | smega_rock_stuff(); |
||
361 | #endif |
||
362 | digi_link_sound_to_object(SOUND_BADASS_EXPLOSION, obj, 0, F1_0, sound_stack::allow_stacking); |
||
363 | |||
364 | const auto Difficulty_level = GameUniqueState.Difficulty_level; |
||
365 | object_create_badass_explosion(obj, vmsegptridx(obj->segnum), pos, |
||
366 | wi->impact_size, |
||
367 | wi->robot_hit_vclip, |
||
368 | wi->strength[Difficulty_level], |
||
369 | wi->damage_radius,wi->strength[Difficulty_level], |
||
370 | imobjptridx(obj->ctype.laser_info.parent_num)); |
||
371 | |||
372 | } |
||
373 | |||
374 | static void explode_badass_object(fvmsegptridx &vmsegptridx, const vmobjptridx_t objp, fix damage, fix distance, fix force) |
||
375 | { |
||
376 | const auto &&rval = object_create_badass_explosion(objp, vmsegptridx(objp->segnum), objp->pos, objp->size, |
||
377 | get_explosion_vclip(objp, explosion_vclip_stage::s0), |
||
378 | damage, distance, force, |
||
379 | objp); |
||
380 | if (rval != object_none) |
||
381 | digi_link_sound_to_object(SOUND_BADASS_EXPLOSION, rval, 0, F1_0, sound_stack::allow_stacking); |
||
382 | } |
||
383 | |||
384 | //blows up the player with a badass explosion |
||
385 | //return the explosion object |
||
386 | void explode_badass_player(const vmobjptridx_t objp) |
||
387 | { |
||
388 | explode_badass_object(vmsegptridx, objp, F1_0*50, F1_0*40, F1_0*150); |
||
389 | } |
||
390 | |||
391 | |||
392 | #define DEBRIS_LIFE (f1_0 * (PERSISTENT_DEBRIS?60:2)) //lifespan in seconds |
||
393 | |||
394 | static void object_create_debris(fvmsegptridx &vmsegptridx, const object_base &parent, int subobj_num) |
||
395 | { |
||
396 | auto &Objects = LevelUniqueObjectState.Objects; |
||
397 | Assert(parent.type == OBJ_ROBOT || parent.type == OBJ_PLAYER); |
||
398 | |||
399 | auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models; |
||
400 | const auto &&obj = obj_create(OBJ_DEBRIS, 0, vmsegptridx(parent.segnum), parent.pos, &parent.orient, Polygon_models[parent.rtype.pobj_info.model_num].submodel_rads[subobj_num], |
||
401 | CT_DEBRIS,MT_PHYSICS,RT_POLYOBJ); |
||
402 | |||
403 | if ((obj == object_none ) && (Highest_object_index >= MAX_OBJECTS-1)) { |
||
404 | // Int3(); // this happens often and is normal :-) |
||
405 | return; |
||
406 | } |
||
407 | if ( obj == object_none ) |
||
408 | return; // Not enough debris slots! |
||
409 | |||
410 | Assert(subobj_num < 32); |
||
411 | |||
412 | //Set polygon-object-specific data |
||
413 | |||
414 | obj->rtype.pobj_info.model_num = parent.rtype.pobj_info.model_num; |
||
415 | obj->rtype.pobj_info.subobj_flags = 1<<subobj_num; |
||
416 | obj->rtype.pobj_info.tmap_override = parent.rtype.pobj_info.tmap_override; |
||
417 | |||
418 | //Set physics data for this object |
||
419 | |||
420 | obj->mtype.phys_info.velocity.x = D_RAND_MAX/2 - d_rand(); |
||
421 | obj->mtype.phys_info.velocity.y = D_RAND_MAX/2 - d_rand(); |
||
422 | obj->mtype.phys_info.velocity.z = D_RAND_MAX/2 - d_rand(); |
||
423 | vm_vec_normalize_quick(obj->mtype.phys_info.velocity); |
||
424 | vm_vec_scale(obj->mtype.phys_info.velocity,i2f(10 + (30 * d_rand() / D_RAND_MAX))); |
||
425 | |||
426 | vm_vec_add2(obj->mtype.phys_info.velocity, parent.mtype.phys_info.velocity); |
||
427 | |||
428 | // -- used to be: Notice, not random! vm_vec_make(&obj->mtype.phys_info.rotvel,10*0x2000/3,10*0x4000/3,10*0x7000/3); |
||
429 | obj->mtype.phys_info.rotvel = {d_rand() + 0x1000, d_rand()*2 + 0x4000, d_rand()*3 + 0x2000}; |
||
430 | vm_vec_zero(obj->mtype.phys_info.rotthrust); |
||
431 | |||
432 | obj->lifeleft = 3*DEBRIS_LIFE/4 + fixmul(d_rand(), DEBRIS_LIFE); // Some randomness, so they don't all go away at the same time. |
||
433 | |||
434 | obj->mtype.phys_info.mass = fixmuldiv(parent.mtype.phys_info.mass, obj->size, parent.size); |
||
435 | obj->mtype.phys_info.drag = 0; //fl2f(0.2); //parent->mtype.phys_info.drag; |
||
436 | |||
437 | if (PERSISTENT_DEBRIS) |
||
438 | { |
||
439 | obj->mtype.phys_info.flags |= PF_BOUNCE; |
||
440 | obj->mtype.phys_info.drag = 128; |
||
441 | } |
||
442 | } |
||
443 | |||
444 | void draw_fireball(const d_vclip_array &Vclip, grs_canvas &canvas, const vcobjptridx_t obj) |
||
445 | { |
||
446 | const auto lifeleft = obj->lifeleft; |
||
447 | if (lifeleft > 0) |
||
448 | draw_vclip_object(canvas, obj, lifeleft, Vclip[get_fireball_id(obj)]); |
||
449 | } |
||
450 | |||
451 | // -------------------------------------------------------------------------------------------------------------------- |
||
452 | // Return true if there is a door here and it is openable |
||
453 | // It is assumed that the player has all keys. |
||
454 | static int door_is_openable_by_player(fvcwallptr &vcwallptr, const shared_segment &segp, const unsigned sidenum) |
||
455 | { |
||
456 | const auto wall_num = segp.sides[sidenum].wall_num; |
||
457 | |||
458 | if (wall_num == wall_none) |
||
459 | return 0; // no wall here. |
||
460 | |||
461 | auto &w = *vcwallptr(wall_num); |
||
462 | const auto wall_type = w.type; |
||
463 | // Can't open locked doors. |
||
464 | if ((wall_type == WALL_DOOR && (w.flags & WALL_DOOR_LOCKED)) || wall_type == WALL_CLOSED) |
||
465 | return 0; |
||
466 | |||
467 | return 1; |
||
468 | } |
||
469 | |||
470 | // -------------------------------------------------------------------------------------------------------------------- |
||
471 | // Return a segment %i segments away from initial segment. |
||
472 | // Returns -1 if can't find a segment that distance away. |
||
473 | imsegidx_t pick_connected_segment(const vcsegidx_t start_seg, int max_depth) |
||
474 | { |
||
475 | using std::swap; |
||
476 | int i; |
||
477 | int cur_depth; |
||
478 | int head, tail; |
||
479 | constexpr unsigned QUEUE_SIZE = 64; |
||
480 | std::array<segnum_t, QUEUE_SIZE * 2> seg_queue{}; |
||
481 | std::array<uint8_t, MAX_SEGMENTS> depth{}; |
||
482 | std::array<uint8_t, MAX_SIDES_PER_SEGMENT> side_rand; |
||
483 | |||
484 | visited_segment_bitarray_t visited; |
||
485 | |||
486 | head = 0; |
||
487 | tail = 0; |
||
488 | seg_queue[head++] = start_seg; |
||
489 | |||
490 | cur_depth = 0; |
||
491 | |||
492 | std::iota(side_rand.begin(), side_rand.end(), 0); |
||
493 | |||
494 | // Now, randomize a bit to start, so we don't always get started in the same direction. |
||
495 | for (i=0; i<4; i++) { |
||
496 | int ind1; |
||
497 | |||
498 | ind1 = (d_rand() * MAX_SIDES_PER_SEGMENT) >> 15; |
||
499 | swap(side_rand[ind1], side_rand[i]); |
||
500 | } |
||
501 | |||
502 | auto &Walls = LevelUniqueWallSubsystemState.Walls; |
||
503 | auto &vcwallptr = Walls.vcptr; |
||
504 | while (tail != head) { |
||
505 | int sidenum, count; |
||
506 | int ind1, ind2; |
||
507 | |||
508 | if (cur_depth >= max_depth) { |
||
509 | return seg_queue[tail]; |
||
510 | } |
||
511 | |||
512 | const auto &&segp = vcsegptr(seg_queue[tail++]); |
||
513 | tail &= QUEUE_SIZE-1; |
||
514 | |||
515 | // to make random, switch a pair of entries in side_rand. |
||
516 | ind1 = (d_rand() * MAX_SIDES_PER_SEGMENT) >> 15; |
||
517 | ind2 = (d_rand() * MAX_SIDES_PER_SEGMENT) >> 15; |
||
518 | swap(side_rand[ind1], side_rand[ind2]); |
||
519 | count = 0; |
||
520 | for (sidenum=ind1; count<MAX_SIDES_PER_SEGMENT; count++) { |
||
521 | int snrand; |
||
522 | |||
523 | if (sidenum == MAX_SIDES_PER_SEGMENT) |
||
524 | sidenum = 0; |
||
525 | |||
526 | snrand = side_rand[sidenum]; |
||
527 | auto wall_num = segp->shared_segment::sides[snrand].wall_num; |
||
528 | sidenum++; |
||
529 | |||
530 | if ((wall_num == wall_none || door_is_openable_by_player(vcwallptr, segp, snrand)) && IS_CHILD(segp->children[snrand])) |
||
531 | { |
||
532 | if (!visited[segp->children[snrand]]) { |
||
533 | seg_queue[head++] = segp->children[snrand]; |
||
534 | visited[segp->children[snrand]] = true; |
||
535 | depth[segp->children[snrand]] = cur_depth+1; |
||
536 | head &= QUEUE_SIZE-1; |
||
537 | if (head > tail) { |
||
538 | if (head == tail + QUEUE_SIZE-1) |
||
539 | Int3(); // queue overflow. Make it bigger! |
||
540 | } else |
||
541 | if (head+QUEUE_SIZE == tail + QUEUE_SIZE-1) |
||
542 | Int3(); // queue overflow. Make it bigger! |
||
543 | } |
||
544 | } |
||
545 | } |
||
546 | |||
547 | if (seg_queue[tail] > Highest_segment_index) |
||
548 | { |
||
549 | // -- Int3(); // Something bad has happened. Queue is trashed. --MK, 12/13/94 |
||
550 | return segment_none; |
||
551 | } |
||
552 | cur_depth = depth[seg_queue[tail]]; |
||
553 | } |
||
554 | return segment_none; |
||
555 | } |
||
556 | |||
557 | #if defined(DXX_BUILD_DESCENT_I) |
||
558 | #define BASE_NET_DROP_DEPTH 10 |
||
559 | #elif defined(DXX_BUILD_DESCENT_II) |
||
560 | #define BASE_NET_DROP_DEPTH 8 |
||
561 | #endif |
||
562 | |||
563 | static imsegidx_t pick_connected_drop_segment(const segment_array &Segments, fvcvertptr &vcvertptr, const vcsegidx_t start_seg, const unsigned cur_drop_depth, const vms_vector &player_pos, const vcsegptridx_t &player_seg) |
||
564 | { |
||
565 | const auto segnum = pick_connected_segment(start_seg, cur_drop_depth); |
||
566 | if (segnum == segment_none) |
||
567 | return segnum; |
||
568 | const auto &&segp = Segments.vcptridx(segnum); |
||
569 | if (segp->special == SEGMENT_IS_CONTROLCEN) |
||
570 | return segment_none; |
||
571 | //don't drop in any children of control centers |
||
572 | range_for (const auto ch, segp->children) |
||
573 | { |
||
574 | if (!IS_CHILD(ch)) |
||
575 | continue; |
||
576 | const shared_segment &childsegp = *Segments.vcptr(ch); |
||
577 | if (childsegp.special == SEGMENT_IS_CONTROLCEN) |
||
578 | return segment_none; |
||
579 | } |
||
580 | //bail if not far enough from original position |
||
581 | const auto &&tempv = compute_segment_center(vcvertptr, segp); |
||
582 | if (find_connected_distance(player_pos, player_seg, tempv, segp, -1, WID_FLY_FLAG) < static_cast<fix>(i2f(20) * cur_drop_depth)) |
||
583 | return segment_none; |
||
584 | return segnum; |
||
585 | } |
||
586 | |||
587 | // ------------------------------------------------------------------------------------------------------ |
||
588 | // Choose segment to drop a powerup in. |
||
589 | // For all active net players, try to create a N segment path from the player. If possible, return that |
||
590 | // segment. If not possible, try another player. After a few tries, use a random segment. |
||
591 | // Don't drop if control center in segment. |
||
592 | static vmsegptridx_t choose_drop_segment(fvcsegptridx &vcsegptridx, fvmsegptridx &vmsegptridx, const playernum_t drop_pnum) |
||
593 | { |
||
594 | auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
||
595 | auto &Objects = LevelUniqueObjectState.Objects; |
||
596 | auto &Vertices = LevelSharedVertexState.get_vertices(); |
||
597 | auto &vcobjptr = Objects.vcptr; |
||
598 | auto &vmobjptr = Objects.vmptr; |
||
599 | playernum_t pnum = 0; |
||
600 | int cur_drop_depth; |
||
601 | int count; |
||
602 | auto &drop_player = *vcplayerptr(drop_pnum); |
||
603 | auto &drop_playerobj = *vmobjptr(drop_player.objnum); |
||
604 | |||
605 | d_srand(static_cast<fix>(timer_query())); |
||
606 | |||
607 | cur_drop_depth = BASE_NET_DROP_DEPTH + ((d_rand() * BASE_NET_DROP_DEPTH*2) >> 15); |
||
608 | |||
609 | auto &player_pos = drop_playerobj.pos; |
||
610 | const auto &&player_seg = vcsegptridx(drop_playerobj.segnum); |
||
611 | |||
612 | segnum_t segnum = segment_none; |
||
613 | auto &vcvertptr = Vertices.vcptr; |
||
614 | for (; (segnum == segment_none) && (cur_drop_depth > BASE_NET_DROP_DEPTH/2); --cur_drop_depth) |
||
615 | { |
||
616 | pnum = (d_rand() * N_players) >> 15; |
||
617 | count = 0; |
||
618 | #if defined(DXX_BUILD_DESCENT_I) |
||
619 | while (count < N_players && (vcplayerptr(pnum)->connected == CONNECT_DISCONNECTED || pnum == drop_pnum)) |
||
620 | #elif defined(DXX_BUILD_DESCENT_II) |
||
621 | while (count < N_players && (vcplayerptr(pnum)->connected == CONNECT_DISCONNECTED || pnum == drop_pnum || ((Game_mode & (GM_TEAM|GM_CAPTURE)) && (get_team(pnum)==get_team(drop_pnum))))) |
||
622 | #endif |
||
623 | { |
||
624 | pnum = (pnum+1)%N_players; |
||
625 | count++; |
||
626 | } |
||
627 | |||
628 | if (count == N_players) { |
||
629 | //if can't valid non-player person, use the player |
||
630 | pnum = drop_pnum; |
||
631 | } |
||
632 | |||
633 | segnum = pick_connected_drop_segment(Segments, vcvertptr, vcobjptr(vcplayerptr(pnum)->objnum)->segnum, cur_drop_depth, player_pos, player_seg); |
||
634 | } |
||
635 | |||
636 | if (segnum == segment_none) { |
||
637 | cur_drop_depth = BASE_NET_DROP_DEPTH; |
||
638 | while (cur_drop_depth > 0 && segnum == segment_none) // before dropping in random segment, try to find ANY segment which is connected to the player responsible for the drop so object will not spawn in inaccessible areas |
||
639 | { |
||
640 | segnum = pick_connected_segment(vcobjptr(vcplayerptr(drop_pnum)->objnum)->segnum, --cur_drop_depth); |
||
641 | if (segnum != segment_none && vcsegptr(segnum)->special == SEGMENT_IS_CONTROLCEN) |
||
642 | segnum = segment_none; |
||
643 | } |
||
644 | if (segnum == segment_none) // basically it should be impossible segnum == -1 now... but oh well... |
||
645 | return vmsegptridx(static_cast<segnum_t>((d_rand() * Highest_segment_index) >> 15)); |
||
646 | } |
||
647 | return vmsegptridx(segnum); |
||
648 | } |
||
649 | |||
650 | // ------------------------------------------------------------------------------------------------------ |
||
651 | // (Re)spawns powerup if in a network game. |
||
652 | void maybe_drop_net_powerup(powerup_type_t powerup_type, bool adjust_cap, bool random_player) |
||
653 | { |
||
654 | auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
||
655 | auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState; |
||
656 | auto &Objects = LevelUniqueObjectState.Objects; |
||
657 | auto &Vertices = LevelSharedVertexState.get_vertices(); |
||
658 | auto &vmobjptr = Objects.vmptr; |
||
659 | playernum_t pnum = Player_num; |
||
660 | if ((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_COOP)) { |
||
661 | if ((Game_mode & GM_NETWORK) && adjust_cap) |
||
662 | { |
||
663 | MultiLevelInv_Recount(); // recount current items |
||
664 | if (!MultiLevelInv_AllowSpawn(powerup_type)) |
||
665 | return; |
||
666 | } |
||
667 | |||
668 | if (LevelUniqueControlCenterState.Control_center_destroyed || (Network_status == NETSTAT_ENDLEVEL)) |
||
669 | return; |
||
670 | |||
671 | if (random_player) |
||
672 | { |
||
673 | uint_fast32_t failsafe_count = 0; |
||
674 | do { |
||
675 | pnum = d_rand() % MAX_PLAYERS; |
||
676 | if (failsafe_count > MAX_PLAYERS*4) // that was plenty of tries to find a good player... |
||
677 | { |
||
678 | pnum = Player_num; // ... go with Player_num instead |
||
679 | break; |
||
680 | } |
||
681 | failsafe_count++; |
||
682 | } while (vcplayerptr(pnum)->connected != CONNECT_PLAYING); |
||
683 | } |
||
684 | |||
685 | //--old-- segnum = (d_rand() * Highest_segment_index) >> 15; |
||
686 | //--old-- Assert((segnum >= 0) && (segnum <= Highest_segment_index)); |
||
687 | //--old-- if (segnum < 0) |
||
688 | //--old-- segnum = -segnum; |
||
689 | //--old-- while (segnum > Highest_segment_index) |
||
690 | //--old-- segnum /= 2; |
||
691 | |||
692 | Net_create_loc = 0; |
||
693 | const auto &&objnum = call_object_create_egg(vmobjptr(vcplayerptr(pnum)->objnum), 1, powerup_type); |
||
694 | |||
695 | if (objnum == object_none) |
||
696 | return; |
||
697 | |||
698 | const auto &&segnum = choose_drop_segment(LevelSharedSegmentState.get_segments().vcptridx, LevelUniqueSegmentState.get_segments().vmptridx, pnum); |
||
699 | auto &vcvertptr = Vertices.vcptr; |
||
700 | const auto &&new_pos = pick_random_point_in_seg(vcvertptr, segnum); |
||
701 | multi_send_create_powerup(powerup_type, segnum, objnum, new_pos); |
||
702 | objnum->pos = new_pos; |
||
703 | vm_vec_zero(objnum->mtype.phys_info.velocity); |
||
704 | obj_relink(vmobjptr, vmsegptr, objnum, segnum); |
||
705 | |||
706 | object_create_explosion(segnum, new_pos, i2f(5), VCLIP_POWERUP_DISAPPEARANCE ); |
||
707 | } |
||
708 | } |
||
709 | |||
710 | // ------------------------------------------------------------------------------------------------------ |
||
711 | // Return true if current segment contains some object. |
||
712 | static const object *segment_contains_powerup(fvcobjptridx &vcobjptridx, fvcsegptr &vcsegptr, const unique_segment &segnum, const powerup_type_t obj_id) |
||
713 | { |
||
714 | range_for (const object &objp, objects_in(segnum, vcobjptridx, vcsegptr)) |
||
715 | { |
||
716 | auto &o = objp; |
||
717 | if (o.type == OBJ_POWERUP && get_powerup_id(o) == obj_id) |
||
718 | return &o; |
||
719 | } |
||
720 | return nullptr; |
||
721 | } |
||
722 | |||
723 | // ------------------------------------------------------------------------------------------------------ |
||
724 | static const object *powerup_nearby_aux(fvcobjptridx &vcobjptridx, fvcsegptr &vcsegptr, const vcsegidx_t segnum, const powerup_type_t object_id, uint_fast32_t depth) |
||
725 | { |
||
726 | auto &&segp = vcsegptr(segnum); |
||
727 | if (auto r = segment_contains_powerup(vcobjptridx, vcsegptr, segp, object_id)) |
||
728 | return r; |
||
729 | if (! -- depth) |
||
730 | return nullptr; |
||
731 | range_for (const auto seg2, segp->children) |
||
732 | { |
||
733 | if (seg2 != segment_none) |
||
734 | if (auto r = powerup_nearby_aux(vcobjptridx, vcsegptr, seg2, object_id, depth)) |
||
735 | return r; |
||
736 | } |
||
737 | return nullptr; |
||
738 | } |
||
739 | |||
740 | // ------------------------------------------------------------------------------------------------------ |
||
741 | // Return true if some powerup is nearby (within 3 segments). |
||
742 | static const object *weapon_nearby(fvcobjptridx &vcobjptridx, fvcsegptr &vcsegptr, const object_base &objp, const powerup_type_t weapon_id) |
||
743 | { |
||
744 | return powerup_nearby_aux(vcobjptridx, vcsegptr, objp.segnum, weapon_id, 2); |
||
745 | } |
||
746 | |||
747 | // ------------------------------------------------------------------------------------------------------ |
||
748 | void maybe_replace_powerup_with_energy(object_base &del_obj) |
||
749 | { |
||
750 | auto &Objects = LevelUniqueObjectState.Objects; |
||
751 | auto &vcobjptridx = Objects.vcptridx; |
||
752 | auto &vmobjptr = Objects.vmptr; |
||
753 | int weapon_index=-1; |
||
754 | |||
755 | if (del_obj.contains_type != OBJ_POWERUP) |
||
756 | return; |
||
757 | |||
758 | switch (del_obj.contains_id) { |
||
759 | case POW_CLOAK: |
||
760 | if (weapon_nearby(vcobjptridx, vcsegptr, del_obj, POW_CLOAK) != nullptr) |
||
761 | { |
||
762 | del_obj.contains_count = 0; |
||
763 | } |
||
764 | return; |
||
765 | case POW_VULCAN_WEAPON: |
||
766 | weapon_index = primary_weapon_index_t::VULCAN_INDEX; |
||
767 | break; |
||
768 | case POW_SPREADFIRE_WEAPON: |
||
769 | weapon_index = primary_weapon_index_t::SPREADFIRE_INDEX; |
||
770 | break; |
||
771 | case POW_PLASMA_WEAPON: |
||
772 | weapon_index = primary_weapon_index_t::PLASMA_INDEX; |
||
773 | break; |
||
774 | case POW_FUSION_WEAPON: |
||
775 | weapon_index = primary_weapon_index_t::FUSION_INDEX; |
||
776 | break; |
||
777 | #if defined(DXX_BUILD_DESCENT_II) |
||
778 | case POW_GAUSS_WEAPON: |
||
779 | weapon_index = primary_weapon_index_t::GAUSS_INDEX; |
||
780 | break; |
||
781 | case POW_HELIX_WEAPON: |
||
782 | weapon_index = primary_weapon_index_t::HELIX_INDEX; |
||
783 | break; |
||
784 | case POW_PHOENIX_WEAPON: |
||
785 | weapon_index = primary_weapon_index_t::PHOENIX_INDEX; |
||
786 | break; |
||
787 | case POW_OMEGA_WEAPON: |
||
788 | weapon_index = primary_weapon_index_t::OMEGA_INDEX; |
||
789 | break; |
||
790 | #endif |
||
791 | } |
||
792 | |||
793 | // Don't drop vulcan ammo if player maxed out. |
||
794 | auto &player_info = get_local_plrobj().ctype.player_info; |
||
795 | if ((weapon_index_uses_vulcan_ammo(weapon_index) || del_obj.contains_id == POW_VULCAN_AMMO) && |
||
796 | player_info.vulcan_ammo >= VULCAN_AMMO_MAX) |
||
797 | del_obj.contains_count = 0; |
||
798 | else if (weapon_index != -1) { |
||
799 | if (player_has_primary_weapon(player_info, weapon_index).has_weapon() || weapon_nearby(vcobjptridx, vcsegptr, del_obj, static_cast<powerup_type_t>(del_obj.contains_id)) != nullptr) |
||
800 | { |
||
801 | if (d_rand() > 16384) { |
||
802 | #if defined(DXX_BUILD_DESCENT_I) |
||
803 | del_obj.contains_count = 1; |
||
804 | #endif |
||
805 | del_obj.contains_type = OBJ_POWERUP; |
||
806 | if (weapon_index_uses_vulcan_ammo(weapon_index)) { |
||
807 | del_obj.contains_id = POW_VULCAN_AMMO; |
||
808 | } |
||
809 | else { |
||
810 | del_obj.contains_id = POW_ENERGY; |
||
811 | } |
||
812 | } else { |
||
813 | #if defined(DXX_BUILD_DESCENT_I) |
||
814 | del_obj.contains_count = 0; |
||
815 | #elif defined(DXX_BUILD_DESCENT_II) |
||
816 | del_obj.contains_type = OBJ_POWERUP; |
||
817 | del_obj.contains_id = POW_SHIELD_BOOST; |
||
818 | #endif |
||
819 | } |
||
820 | } |
||
821 | } else if (del_obj.contains_id == POW_QUAD_FIRE) |
||
822 | { |
||
823 | if ((player_info.powerup_flags & PLAYER_FLAGS_QUAD_LASERS) || weapon_nearby(vcobjptridx, vcsegptr, del_obj, static_cast<powerup_type_t>(del_obj.contains_id)) != nullptr) |
||
824 | { |
||
825 | if (d_rand() > 16384) { |
||
826 | #if defined(DXX_BUILD_DESCENT_I) |
||
827 | del_obj.contains_count = 1; |
||
828 | #endif |
||
829 | del_obj.contains_type = OBJ_POWERUP; |
||
830 | del_obj.contains_id = POW_ENERGY; |
||
831 | } else { |
||
832 | #if defined(DXX_BUILD_DESCENT_I) |
||
833 | del_obj.contains_count = 0; |
||
834 | #elif defined(DXX_BUILD_DESCENT_II) |
||
835 | del_obj.contains_type = OBJ_POWERUP; |
||
836 | del_obj.contains_id = POW_SHIELD_BOOST; |
||
837 | #endif |
||
838 | } |
||
839 | } |
||
840 | } |
||
841 | |||
842 | // If this robot was gated in by the boss and it now contains energy, make it contain nothing, |
||
843 | // else the room gets full of energy. |
||
844 | if ( (del_obj.matcen_creator == BOSS_GATE_MATCEN_NUM) && (del_obj.contains_id == POW_ENERGY) && (del_obj.contains_type == OBJ_POWERUP) ) { |
||
845 | del_obj.contains_count = 0; |
||
846 | } |
||
847 | |||
848 | // Change multiplayer extra-lives into invulnerability |
||
849 | if ((Game_mode & GM_MULTI) && (del_obj.contains_id == POW_EXTRA_LIFE)) |
||
850 | { |
||
851 | del_obj.contains_id = POW_INVULNERABILITY; |
||
852 | } |
||
853 | } |
||
854 | |||
855 | #if defined(DXX_BUILD_DESCENT_I) |
||
856 | static |
||
857 | #endif |
||
858 | imobjptridx_t drop_powerup(const d_vclip_array &Vclip, int id, const unsigned num, const vms_vector &init_vel, const vms_vector &pos, const vmsegptridx_t segnum, const bool player) |
||
859 | { |
||
860 | imobjptridx_t objnum = object_none; |
||
861 | unsigned count; |
||
862 | |||
863 | for (count=0; count<num; count++) { |
||
864 | int rand_scale; |
||
865 | auto new_velocity = init_vel; |
||
866 | const auto old_mag = vm_vec_mag_quick(init_vel); |
||
867 | |||
868 | // We want powerups to move more in network mode. |
||
869 | if ((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_ROBOTS)) { |
||
870 | rand_scale = 4; |
||
871 | // extra life powerups are converted to invulnerability in multiplayer, for what is an extra life, anyway? |
||
872 | if (id == POW_EXTRA_LIFE) |
||
873 | id = POW_INVULNERABILITY; |
||
874 | } else |
||
875 | rand_scale = 2; |
||
876 | |||
877 | new_velocity.x += fixmul(old_mag+F1_0*32, d_rand()*rand_scale - 16384*rand_scale); |
||
878 | new_velocity.y += fixmul(old_mag+F1_0*32, d_rand()*rand_scale - 16384*rand_scale); |
||
879 | new_velocity.z += fixmul(old_mag+F1_0*32, d_rand()*rand_scale - 16384*rand_scale); |
||
880 | |||
881 | // Give keys zero velocity so they can be tracked better in multi |
||
882 | |||
883 | if ((Game_mode & GM_MULTI) && (id >= POW_KEY_BLUE) && (id <= POW_KEY_GOLD)) |
||
884 | vm_vec_zero(new_velocity); |
||
885 | |||
886 | auto new_pos = pos; |
||
887 | // new_pos.x += (d_rand()-16384)*8; |
||
888 | // new_pos.y += (d_rand()-16384)*8; |
||
889 | // new_pos.z += (d_rand()-16384)*8; |
||
890 | |||
891 | if (Game_mode & GM_MULTI) |
||
892 | { |
||
893 | if (Net_create_loc >= MAX_NET_CREATE_OBJECTS) |
||
894 | { |
||
895 | return object_none; |
||
896 | } |
||
897 | #if defined(DXX_BUILD_DESCENT_II) |
||
898 | if ((Game_mode & GM_NETWORK) && Network_status == NETSTAT_ENDLEVEL) |
||
899 | return object_none; |
||
900 | #endif |
||
901 | } |
||
902 | auto obj = obj_create( OBJ_POWERUP, id, segnum, new_pos, &vmd_identity_matrix, Powerup_info[id].size, CT_POWERUP, MT_PHYSICS, RT_POWERUP); |
||
903 | objnum = obj; |
||
904 | |||
905 | if (objnum == object_none) |
||
906 | { |
||
907 | Int3(); |
||
908 | return object_none; |
||
909 | } |
||
910 | #if defined(DXX_BUILD_DESCENT_II) |
||
911 | if (player) |
||
912 | obj->flags |= OF_PLAYER_DROPPED; |
||
913 | #endif |
||
914 | |||
915 | if (Game_mode & GM_MULTI) |
||
916 | { |
||
917 | Net_create_objnums[Net_create_loc++] = objnum; |
||
918 | } |
||
919 | obj->mtype.phys_info.velocity = new_velocity; |
||
920 | |||
921 | obj->mtype.phys_info.drag = 512; //1024; |
||
922 | obj->mtype.phys_info.mass = F1_0; |
||
923 | |||
924 | obj->mtype.phys_info.flags = PF_BOUNCE; |
||
925 | |||
926 | obj->rtype.vclip_info.vclip_num = Powerup_info[get_powerup_id(obj)].vclip_num; |
||
927 | obj->rtype.vclip_info.frametime = Vclip[obj->rtype.vclip_info.vclip_num].frame_time; |
||
928 | obj->rtype.vclip_info.framenum = 0; |
||
929 | |||
930 | switch (get_powerup_id(obj)) { |
||
931 | case POW_MISSILE_1: |
||
932 | case POW_MISSILE_4: |
||
933 | case POW_SHIELD_BOOST: |
||
934 | case POW_ENERGY: |
||
935 | obj->lifeleft = (d_rand() + F1_0*3) * 64; // Lives for 3 to 3.5 binary minutes (a binary minute is 64 seconds) |
||
936 | if (Game_mode & GM_MULTI) |
||
937 | obj->lifeleft /= 2; |
||
938 | break; |
||
939 | #if defined(DXX_BUILD_DESCENT_II) |
||
940 | case POW_OMEGA_WEAPON: |
||
941 | if (!player) |
||
942 | obj->ctype.powerup_info.count = MAX_OMEGA_CHARGE; |
||
943 | break; |
||
944 | case POW_GAUSS_WEAPON: |
||
945 | #endif |
||
946 | case POW_VULCAN_WEAPON: |
||
947 | if (!player) |
||
948 | obj->ctype.powerup_info.count = VULCAN_WEAPON_AMMO_AMOUNT; |
||
949 | break; |
||
950 | default: |
||
951 | // if (Game_mode & GM_MULTI) |
||
952 | // obj->lifeleft = (d_rand() + F1_0*3) * 64; // Lives for 5 to 5.5 binary minutes (a binary minute is 64 seconds) |
||
953 | break; |
||
954 | } |
||
955 | } |
||
956 | return objnum; |
||
957 | } |
||
958 | |||
959 | static imobjptridx_t drop_robot_egg(const int type, const int id, const unsigned num, const vms_vector &init_vel, const vms_vector &pos, const vmsegptridx_t segnum) |
||
960 | { |
||
961 | switch (type) |
||
962 | { |
||
963 | case OBJ_POWERUP: |
||
964 | return drop_powerup(Vclip, id, num, init_vel, pos, segnum, false); |
||
965 | case OBJ_ROBOT: |
||
966 | break; |
||
967 | default: |
||
968 | con_printf(CON_URGENT, DXX_STRINGIZE_FL(__FILE__, __LINE__, "ignoring invalid object type; expected OBJ_POWERUP or OBJ_ROBOT, got type=%i, id=%i"), type, id); |
||
969 | return object_none; |
||
970 | } |
||
971 | imobjptridx_t objnum = object_none; |
||
972 | unsigned count; |
||
973 | auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models; |
||
974 | auto &Robot_info = LevelSharedRobotInfoState.Robot_info; |
||
975 | |||
976 | for (count=0; count<num; count++) { |
||
977 | int rand_scale; |
||
978 | auto new_velocity = vm_vec_normalized_quick(init_vel); |
||
979 | const auto old_mag = vm_vec_mag_quick(init_vel); |
||
980 | |||
981 | // We want powerups to move more in network mode. |
||
982 | // if (Game_mode & GM_MULTI) |
||
983 | // rand_scale = 4; |
||
984 | // else |
||
985 | rand_scale = 2; |
||
986 | |||
987 | new_velocity.x += (d_rand()-16384)*2; |
||
988 | new_velocity.y += (d_rand()-16384)*2; |
||
989 | new_velocity.z += (d_rand()-16384)*2; |
||
990 | |||
991 | vm_vec_normalize_quick(new_velocity); |
||
992 | vm_vec_scale(new_velocity, (F1_0*32 + old_mag) * rand_scale); |
||
993 | auto new_pos = pos; |
||
994 | // This is dangerous, could be outside mine. |
||
995 | // new_pos.x += (d_rand()-16384)*8; |
||
996 | // new_pos.y += (d_rand()-16384)*7; |
||
997 | // new_pos.z += (d_rand()-16384)*6; |
||
998 | |||
999 | #if defined(DXX_BUILD_DESCENT_I) |
||
1000 | const auto robot_id = ObjId[type]; |
||
1001 | #elif defined(DXX_BUILD_DESCENT_II) |
||
1002 | const auto robot_id = id; |
||
1003 | #endif |
||
1004 | const auto &&obj = robot_create(id, segnum, new_pos, &vmd_identity_matrix, Polygon_models[Robot_info[robot_id].model_num].rad, ai_behavior::AIB_NORMAL); |
||
1005 | |||
1006 | objnum = obj; |
||
1007 | if (objnum == object_none) |
||
1008 | { |
||
1009 | Int3(); |
||
1010 | return object_none; |
||
1011 | } |
||
1012 | |||
1013 | ++LevelUniqueObjectState.accumulated_robots; |
||
1014 | ++GameUniqueState.accumulated_robots; |
||
1015 | if (Game_mode & GM_MULTI) |
||
1016 | { |
||
1017 | Net_create_objnums[Net_create_loc++] = objnum; |
||
1018 | } |
||
1019 | //Set polygon-object-specific data |
||
1020 | |||
1021 | obj->rtype.pobj_info.model_num = Robot_info[get_robot_id(obj)].model_num; |
||
1022 | obj->rtype.pobj_info.subobj_flags = 0; |
||
1023 | |||
1024 | //set Physics info |
||
1025 | |||
1026 | obj->mtype.phys_info.velocity = new_velocity; |
||
1027 | |||
1028 | obj->mtype.phys_info.mass = Robot_info[get_robot_id(obj)].mass; |
||
1029 | obj->mtype.phys_info.drag = Robot_info[get_robot_id(obj)].drag; |
||
1030 | |||
1031 | obj->mtype.phys_info.flags |= (PF_LEVELLING); |
||
1032 | |||
1033 | obj->shields = Robot_info[get_robot_id(obj)].strength; |
||
1034 | |||
1035 | ai_local *ailp = &obj->ctype.ai_info.ail; |
||
1036 | ailp->player_awareness_type = player_awareness_type_t::PA_WEAPON_ROBOT_COLLISION; |
||
1037 | ailp->player_awareness_time = F1_0*3; |
||
1038 | obj->ctype.ai_info.CURRENT_STATE = AIS_LOCK; |
||
1039 | obj->ctype.ai_info.GOAL_STATE = AIS_LOCK; |
||
1040 | obj->ctype.ai_info.REMOTE_OWNER = -1; |
||
1041 | } |
||
1042 | |||
1043 | #if defined(DXX_BUILD_DESCENT_II) |
||
1044 | // At JasenW's request, robots which contain robots |
||
1045 | // sometimes drop shields. |
||
1046 | if (d_rand() > 16384) |
||
1047 | drop_powerup(Vclip, POW_SHIELD_BOOST, 1, init_vel, pos, segnum, false); |
||
1048 | #endif |
||
1049 | return objnum; |
||
1050 | } |
||
1051 | |||
1052 | #if defined(DXX_BUILD_DESCENT_II) |
||
1053 | static bool skip_create_egg_powerup(const object &player, powerup_type_t powerup) |
||
1054 | { |
||
1055 | fix current; |
||
1056 | auto &player_info = player.ctype.player_info; |
||
1057 | if (powerup == POW_SHIELD_BOOST) |
||
1058 | current = player.shields; |
||
1059 | else if (powerup == POW_ENERGY) |
||
1060 | current = player_info.energy; |
||
1061 | else |
||
1062 | return false; |
||
1063 | int limit; |
||
1064 | if (current >= i2f(150)) |
||
1065 | limit = 8192; |
||
1066 | else if (current >= i2f(100)) |
||
1067 | limit = 16384; |
||
1068 | else |
||
1069 | return false; |
||
1070 | return d_rand() > limit; |
||
1071 | } |
||
1072 | #endif |
||
1073 | |||
1074 | imobjptridx_t object_create_robot_egg(const int type, const int id, const int num, const vms_vector &init_vel, const vms_vector &pos, const vmsegptridx_t segnum) |
||
1075 | { |
||
1076 | #if defined(DXX_BUILD_DESCENT_II) |
||
1077 | auto &Objects = LevelUniqueObjectState.Objects; |
||
1078 | auto &vmobjptr = Objects.vmptr; |
||
1079 | if (!(Game_mode & GM_MULTI)) |
||
1080 | { |
||
1081 | if (type == OBJ_POWERUP) |
||
1082 | { |
||
1083 | if (skip_create_egg_powerup(get_local_plrobj(), static_cast<powerup_type_t>(id))) |
||
1084 | return object_none; |
||
1085 | } |
||
1086 | } |
||
1087 | #endif |
||
1088 | return drop_robot_egg(type, id, num, init_vel, pos, segnum); |
||
1089 | } |
||
1090 | |||
1091 | imobjptridx_t object_create_robot_egg(object &objp) |
||
1092 | { |
||
1093 | return object_create_robot_egg(objp.contains_type, objp.contains_id, objp.contains_count, objp.mtype.phys_info.velocity, objp.pos, vmsegptridx(objp.segnum)); |
||
1094 | } |
||
1095 | |||
1096 | // ------------------------------------------------------------------------------------------------------- |
||
1097 | // Put count objects of type type (eg, powerup), id = id (eg, energy) into *objp, then drop them! Yippee! |
||
1098 | // Returns created object number. |
||
1099 | imobjptridx_t call_object_create_egg(const object_base &objp, const unsigned count, const int id) |
||
1100 | { |
||
1101 | if (count > 0) { |
||
1102 | return drop_powerup(Vclip, id, count, objp.mtype.phys_info.velocity, objp.pos, vmsegptridx(objp.segnum), true); |
||
1103 | } |
||
1104 | |||
1105 | return object_none; |
||
1106 | } |
||
1107 | |||
1108 | //what vclip does this explode with? |
||
1109 | int get_explosion_vclip(const object_base &obj, explosion_vclip_stage stage) |
||
1110 | { |
||
1111 | if (obj.type == OBJ_ROBOT) |
||
1112 | { |
||
1113 | const auto vclip_ptr = stage == explosion_vclip_stage::s0 |
||
1114 | ? &robot_info::exp1_vclip_num |
||
1115 | : &robot_info::exp2_vclip_num; |
||
1116 | auto &Robot_info = LevelSharedRobotInfoState.Robot_info; |
||
1117 | const auto vclip_num = Robot_info[get_robot_id(obj)].*vclip_ptr; |
||
1118 | if (vclip_num > -1) |
||
1119 | return vclip_num; |
||
1120 | } |
||
1121 | else if (obj.type == OBJ_PLAYER && Player_ship->expl_vclip_num > -1) |
||
1122 | return Player_ship->expl_vclip_num; |
||
1123 | |||
1124 | return VCLIP_SMALL_EXPLOSION; //default |
||
1125 | } |
||
1126 | |||
1127 | //blow up a polygon model |
||
1128 | static void explode_model(object_base &obj) |
||
1129 | { |
||
1130 | Assert(obj.render_type == RT_POLYOBJ); |
||
1131 | |||
1132 | const auto poly_model_num = obj.rtype.pobj_info.model_num; |
||
1133 | const auto dying_model_num = Dying_modelnums[poly_model_num]; |
||
1134 | const auto model_num = (dying_model_num != -1) |
||
1135 | ? (obj.rtype.pobj_info.model_num = dying_model_num) |
||
1136 | : poly_model_num; |
||
1137 | auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models; |
||
1138 | const auto n_models = Polygon_models[model_num].n_models; |
||
1139 | if (n_models > 1) { |
||
1140 | for (unsigned i = 1; i < n_models; ++i) |
||
1141 | #if defined(DXX_BUILD_DESCENT_II) |
||
1142 | if (!(i == 5 && obj.type == OBJ_ROBOT && get_robot_id(obj) == 44)) //energy sucker energy part |
||
1143 | #endif |
||
1144 | object_create_debris(vmsegptridx, obj, i); |
||
1145 | |||
1146 | //make parent object only draw center part |
||
1147 | obj.rtype.pobj_info.subobj_flags = 1; |
||
1148 | } |
||
1149 | } |
||
1150 | |||
1151 | //if the object has a destroyed model, switch to it. Otherwise, delete it. |
||
1152 | static void maybe_delete_object(object_base &del_obj) |
||
1153 | { |
||
1154 | const auto dead_modelnum = Dead_modelnums[del_obj.rtype.pobj_info.model_num]; |
||
1155 | if (dead_modelnum != -1) |
||
1156 | { |
||
1157 | del_obj.rtype.pobj_info.model_num = dead_modelnum; |
||
1158 | del_obj.flags |= OF_DESTROYED; |
||
1159 | } |
||
1160 | else { //normal, multi-stage explosion |
||
1161 | if (del_obj.type == OBJ_PLAYER) |
||
1162 | del_obj.render_type = RT_NONE; |
||
1163 | else |
||
1164 | del_obj.flags |= OF_SHOULD_BE_DEAD; |
||
1165 | } |
||
1166 | } |
||
1167 | |||
1168 | // ------------------------------------------------------------------------------------------------------- |
||
1169 | //blow up an object. Takes the object to destroy, and the point of impact |
||
1170 | void explode_object(const vmobjptridx_t hitobj,fix delay_time) |
||
1171 | { |
||
1172 | if (hitobj->flags & OF_EXPLODING) return; |
||
1173 | |||
1174 | if (delay_time) { //wait a little while before creating explosion |
||
1175 | //create a placeholder object to do the delay, with id==-1 |
||
1176 | auto obj = obj_create(OBJ_FIREBALL, -1, vmsegptridx(hitobj->segnum), hitobj->pos, &vmd_identity_matrix, 0, |
||
1177 | CT_EXPLOSION,MT_NONE,RT_NONE); |
||
1178 | if (obj == object_none ) { |
||
1179 | maybe_delete_object(hitobj); //no explosion, die instantly |
||
1180 | Int3(); |
||
1181 | return; |
||
1182 | } |
||
1183 | //now set explosion-specific data |
||
1184 | |||
1185 | obj->lifeleft = delay_time; |
||
1186 | obj->ctype.expl_info.delete_objnum = hitobj; |
||
1187 | obj->ctype.expl_info.delete_time = -1; |
||
1188 | obj->ctype.expl_info.spawn_time = 0; |
||
1189 | |||
1190 | } |
||
1191 | else { |
||
1192 | int vclip_num; |
||
1193 | |||
1194 | vclip_num = get_explosion_vclip(hitobj, explosion_vclip_stage::s0); |
||
1195 | |||
1196 | imobjptr_t expl_obj = object_create_explosion(vmsegptridx(hitobj->segnum), hitobj->pos, fixmul(hitobj->size,EXPLOSION_SCALE), vclip_num); |
||
1197 | |||
1198 | if (! expl_obj) { |
||
1199 | maybe_delete_object(hitobj); //no explosion, die instantly |
||
1200 | return; |
||
1201 | } |
||
1202 | |||
1203 | //don't make debris explosions have physics, because they often |
||
1204 | //happen when the debris has hit the wall, so the fireball is trying |
||
1205 | //to move into the wall, which shows off FVI problems. |
||
1206 | if (hitobj->type!=OBJ_DEBRIS && hitobj->movement_type==MT_PHYSICS) { |
||
1207 | expl_obj->movement_type = MT_PHYSICS; |
||
1208 | expl_obj->mtype.phys_info = hitobj->mtype.phys_info; |
||
1209 | } |
||
1210 | |||
1211 | if (hitobj->render_type==RT_POLYOBJ && hitobj->type!=OBJ_DEBRIS) |
||
1212 | explode_model(hitobj); |
||
1213 | |||
1214 | maybe_delete_object(hitobj); |
||
1215 | } |
||
1216 | |||
1217 | hitobj->flags |= OF_EXPLODING; //say that this is blowing up |
||
1218 | hitobj->control_type = CT_NONE; //become inert while exploding |
||
1219 | |||
1220 | } |
||
1221 | |||
1222 | |||
1223 | //do whatever needs to be done for this piece of debris for this frame |
||
1224 | void do_debris_frame(const vmobjptridx_t obj) |
||
1225 | { |
||
1226 | Assert(obj->control_type == CT_DEBRIS); |
||
1227 | |||
1228 | if (obj->lifeleft < 0) |
||
1229 | explode_object(obj,0); |
||
1230 | |||
1231 | } |
||
1232 | |||
1233 | //do whatever needs to be done for this explosion for this frame |
||
1234 | void do_explosion_sequence(object &obj) |
||
1235 | { |
||
1236 | auto &Objects = LevelUniqueObjectState.Objects; |
||
1237 | auto &vmobjptr = Objects.vmptr; |
||
1238 | auto &vmobjptridx = Objects.vmptridx; |
||
1239 | Assert(obj.control_type == CT_EXPLOSION); |
||
1240 | auto &Robot_info = LevelSharedRobotInfoState.Robot_info; |
||
1241 | |||
1242 | //See if we should die of old age |
||
1243 | if (obj.lifeleft <= 0 ) { // We died of old age |
||
1244 | obj.flags |= OF_SHOULD_BE_DEAD; |
||
1245 | obj.lifeleft = 0; |
||
1246 | } |
||
1247 | |||
1248 | //See if we should create a secondary explosion |
||
1249 | if (obj.lifeleft <= obj.ctype.expl_info.spawn_time) { |
||
1250 | auto del_obj = vmobjptridx(obj.ctype.expl_info.delete_objnum); |
||
1251 | auto &spawn_pos = del_obj->pos; |
||
1252 | Assert(del_obj->type==OBJ_ROBOT || del_obj->type==OBJ_CLUTTER || del_obj->type==OBJ_CNTRLCEN || del_obj->type == OBJ_PLAYER); |
||
1253 | Assert(del_obj->segnum != segment_none); |
||
1254 | |||
1255 | const auto &&expl_obj = [&]{ |
||
1256 | const auto vclip_num = get_explosion_vclip(del_obj, explosion_vclip_stage::s1); |
||
1257 | #if defined(DXX_BUILD_DESCENT_II) |
||
1258 | if (del_obj->type == OBJ_ROBOT) |
||
1259 | { |
||
1260 | const auto &ri = Robot_info[get_robot_id(del_obj)]; |
||
1261 | if (ri.badass) |
||
1262 | return object_create_badass_explosion(object_none, vmsegptridx(del_obj->segnum), spawn_pos, fixmul(del_obj->size, EXPLOSION_SCALE), vclip_num, F1_0 * ri.badass, i2f(4) * ri.badass, i2f(35) * ri.badass, object_none); |
||
1263 | } |
||
1264 | #endif |
||
1265 | return object_create_explosion(vmsegptridx(del_obj->segnum), spawn_pos, fixmul(del_obj->size, EXPLOSION_SCALE), vclip_num); |
||
1266 | }(); |
||
1267 | |||
1268 | if ((del_obj->contains_count > 0) && !(Game_mode & GM_MULTI)) { // Multiplayer handled outside of this code!! |
||
1269 | // If dropping a weapon that the player has, drop energy instead, unless it's vulcan, in which case drop vulcan ammo. |
||
1270 | if (del_obj->contains_type == OBJ_POWERUP) |
||
1271 | maybe_replace_powerup_with_energy(del_obj); |
||
1272 | object_create_robot_egg(del_obj); |
||
1273 | } else if ((del_obj->type == OBJ_ROBOT) && !(Game_mode & GM_MULTI)) { // Multiplayer handled outside this code!! |
||
1274 | auto &robptr = Robot_info[get_robot_id(del_obj)]; |
||
1275 | if (robptr.contains_count) { |
||
1276 | if (((d_rand() * 16) >> 15) < robptr.contains_prob) { |
||
1277 | del_obj->contains_count = ((d_rand() * robptr.contains_count) >> 15) + 1; |
||
1278 | del_obj->contains_type = robptr.contains_type; |
||
1279 | del_obj->contains_id = robptr.contains_id; |
||
1280 | maybe_replace_powerup_with_energy(del_obj); |
||
1281 | object_create_robot_egg(del_obj); |
||
1282 | } |
||
1283 | } |
||
1284 | #if defined(DXX_BUILD_DESCENT_II) |
||
1285 | if (robot_is_thief(robptr)) |
||
1286 | drop_stolen_items(del_obj); |
||
1287 | else if (robot_is_companion(robptr)) |
||
1288 | { |
||
1289 | DropBuddyMarker(del_obj); |
||
1290 | } |
||
1291 | #endif |
||
1292 | } |
||
1293 | |||
1294 | auto &robptr = Robot_info[get_robot_id(del_obj)]; |
||
1295 | if (robptr.exp2_sound_num > -1) |
||
1296 | digi_link_sound_to_pos(robptr.exp2_sound_num, vmsegptridx(del_obj->segnum), 0, spawn_pos, 0, F1_0); |
||
1297 | //PLAY_SOUND_3D( Robot_info[del_obj->id].exp2_sound_num, spawn_pos, del_obj->segnum ); |
||
1298 | |||
1299 | obj.ctype.expl_info.spawn_time = -1; |
||
1300 | |||
1301 | //make debris |
||
1302 | if (del_obj->render_type==RT_POLYOBJ) |
||
1303 | explode_model(del_obj); //explode a polygon model |
||
1304 | |||
1305 | //set some parm in explosion |
||
1306 | //If num_objects < MAX_USED_OBJECTS, expl_obj could be set to dead before this setting causing the delete_obj not to be removed. If so, directly delete del_obj |
||
1307 | if (expl_obj && !(expl_obj->flags & OF_SHOULD_BE_DEAD)) |
||
1308 | { |
||
1309 | if (del_obj->movement_type == MT_PHYSICS) { |
||
1310 | expl_obj->movement_type = MT_PHYSICS; |
||
1311 | expl_obj->mtype.phys_info = del_obj->mtype.phys_info; |
||
1312 | } |
||
1313 | |||
1314 | expl_obj->ctype.expl_info.delete_time = expl_obj->lifeleft/2; |
||
1315 | expl_obj->ctype.expl_info.delete_objnum = del_obj; |
||
1316 | } |
||
1317 | else { |
||
1318 | maybe_delete_object(del_obj); |
||
1319 | } |
||
1320 | |||
1321 | } |
||
1322 | |||
1323 | //See if we should delete an object |
||
1324 | if (obj.lifeleft <= obj.ctype.expl_info.delete_time) { |
||
1325 | const auto &&del_obj = vmobjptr(obj.ctype.expl_info.delete_objnum); |
||
1326 | obj.ctype.expl_info.delete_time = -1; |
||
1327 | maybe_delete_object(del_obj); |
||
1328 | } |
||
1329 | } |
||
1330 | |||
1331 | #define EXPL_WALL_TIME UINT16_MAX |
||
1332 | #define EXPL_WALL_TOTAL_FIREBALLS 32 |
||
1333 | #if defined(DXX_BUILD_DESCENT_I) |
||
1334 | #define EXPL_WALL_FIREBALL_SIZE 0x48000 //smallest size |
||
1335 | #elif defined(DXX_BUILD_DESCENT_II) |
||
1336 | #define EXPL_WALL_FIREBALL_SIZE (0x48000*6/10) //smallest size |
||
1337 | #endif |
||
1338 | |||
1339 | //explode the given wall |
||
1340 | void explode_wall(fvcvertptr &vcvertptr, const vcsegptridx_t segnum, const unsigned sidenum, wall &w) |
||
1341 | { |
||
1342 | if (w.flags & WALL_EXPLODING) |
||
1343 | /* Already exploding */ |
||
1344 | return; |
||
1345 | w.explode_time_elapsed = 0; |
||
1346 | w.flags |= WALL_EXPLODING; |
||
1347 | ++ Num_exploding_walls; |
||
1348 | |||
1349 | //play one long sound for whole door wall explosion |
||
1350 | const auto &&pos = compute_center_point_on_side(vcvertptr, segnum, sidenum); |
||
1351 | digi_link_sound_to_pos( SOUND_EXPLODING_WALL,segnum, sidenum, pos, 0, F1_0 ); |
||
1352 | } |
||
1353 | |||
1354 | unsigned do_exploding_wall_frame(wall &w1) |
||
1355 | { |
||
1356 | auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
||
1357 | auto &Vertices = LevelSharedVertexState.get_vertices(); |
||
1358 | auto &WallAnims = GameSharedState.WallAnims; |
||
1359 | assert(w1.flags & WALL_EXPLODING); |
||
1360 | fix w1_explode_time_elapsed = w1.explode_time_elapsed; |
||
1361 | const fix oldfrac = fixdiv(w1_explode_time_elapsed, EXPL_WALL_TIME); |
||
1362 | |||
1363 | w1_explode_time_elapsed += FrameTime; |
||
1364 | if (w1_explode_time_elapsed > EXPL_WALL_TIME) |
||
1365 | w1_explode_time_elapsed = EXPL_WALL_TIME; |
||
1366 | w1.explode_time_elapsed = w1_explode_time_elapsed; |
||
1367 | |||
1368 | const auto w1sidenum = w1.sidenum; |
||
1369 | const auto &&seg = vmsegptridx(w1.segnum); |
||
1370 | unsigned walls_updated = 0; |
||
1371 | if (w1_explode_time_elapsed > (EXPL_WALL_TIME * 3) / 4) |
||
1372 | { |
||
1373 | const auto &&csegp = seg.absolute_sibling(seg->shared_segment::children[w1sidenum]); |
||
1374 | const auto cside = find_connect_side(seg, csegp); |
||
1375 | |||
1376 | const auto a = w1.clip_num; |
||
1377 | auto &wa = WallAnims[a]; |
||
1378 | const auto n = wa.num_frames; |
||
1379 | wall_set_tmap_num(wa, seg, w1sidenum, csegp, cside, n - 1); |
||
1380 | |||
1381 | auto &Walls = LevelUniqueWallSubsystemState.Walls; |
||
1382 | auto &vmwallptr = Walls.vmptr; |
||
1383 | |||
1384 | auto cwall_num = csegp->shared_segment::sides[cside].wall_num; |
||
1385 | if (cwall_num != wall_none) |
||
1386 | { |
||
1387 | auto &w2 = *vmwallptr(cwall_num); |
||
1388 | assert(&w1 != &w2); |
||
1389 | w2.flags |= WALL_BLASTED; |
||
1390 | assert((w1.flags & WALL_EXPLODING) || (w2.flags & WALL_EXPLODING)); |
||
1391 | if (w1_explode_time_elapsed >= EXPL_WALL_TIME && w2.flags & WALL_EXPLODING) |
||
1392 | { |
||
1393 | w2.flags &= ~WALL_EXPLODING; |
||
1394 | ++ walls_updated; |
||
1395 | } |
||
1396 | } |
||
1397 | else |
||
1398 | assert(w1.flags & WALL_EXPLODING); |
||
1399 | |||
1400 | w1.flags |= WALL_BLASTED; |
||
1401 | if (w1_explode_time_elapsed >= EXPL_WALL_TIME && w1.flags & WALL_EXPLODING) |
||
1402 | { |
||
1403 | w1.flags &= ~WALL_EXPLODING; |
||
1404 | ++ walls_updated; |
||
1405 | } |
||
1406 | |||
1407 | Num_exploding_walls -= walls_updated; |
||
1408 | } |
||
1409 | |||
1410 | const fix newfrac = fixdiv(w1_explode_time_elapsed, EXPL_WALL_TIME); |
||
1411 | |||
1412 | const int old_count = f2i(EXPL_WALL_TOTAL_FIREBALLS * fixmul(oldfrac, oldfrac)); |
||
1413 | const int new_count = f2i(EXPL_WALL_TOTAL_FIREBALLS * fixmul(newfrac, newfrac)); |
||
1414 | if (old_count >= new_count) |
||
1415 | /* for loop would exit with zero iterations if this `if` is |
||
1416 | * true. Skip the setup for the loop in that case. |
||
1417 | */ |
||
1418 | return walls_updated; |
||
1419 | |||
1420 | const auto vertnum_list = get_side_verts(seg, w1sidenum); |
||
1421 | |||
1422 | auto &vcvertptr = Vertices.vcptr; |
||
1423 | auto &v0 = *vcvertptr(vertnum_list[0]); |
||
1424 | auto &v1 = *vcvertptr(vertnum_list[1]); |
||
1425 | auto &v2 = *vcvertptr(vertnum_list[2]); |
||
1426 | |||
1427 | const auto &&vv0 = vm_vec_sub(v0, v1); |
||
1428 | const auto &&vv1 = vm_vec_sub(v2, v1); |
||
1429 | |||
1430 | //now create all the next explosions |
||
1431 | |||
1432 | auto &w1normal0 = seg->shared_segment::sides[w1sidenum].normals[0]; |
||
1433 | for (int e = old_count; e < new_count; ++e) |
||
1434 | { |
||
1435 | //calc expl position |
||
1436 | |||
1437 | auto pos = vm_vec_scale_add(v1,vv0,d_rand() * 2); |
||
1438 | vm_vec_scale_add2(pos,vv1,d_rand() * 2); |
||
1439 | |||
1440 | const fix size = EXPL_WALL_FIREBALL_SIZE + (2 * EXPL_WALL_FIREBALL_SIZE * e / EXPL_WALL_TOTAL_FIREBALLS); |
||
1441 | |||
1442 | //fireballs start away from door, with subsequent ones getting closer |
||
1443 | vm_vec_scale_add2(pos, w1normal0, size * (EXPL_WALL_TOTAL_FIREBALLS - e) / EXPL_WALL_TOTAL_FIREBALLS); |
||
1444 | |||
1445 | if (e & 3) //3 of 4 are normal |
||
1446 | object_create_explosion(seg, pos, size, VCLIP_SMALL_EXPLOSION); |
||
1447 | else |
||
1448 | object_create_badass_explosion(object_none, seg, pos, |
||
1449 | size, |
||
1450 | VCLIP_SMALL_EXPLOSION, |
||
1451 | i2f(4), // damage strength |
||
1452 | i2f(20), // damage radius |
||
1453 | i2f(50), // damage force |
||
1454 | object_none // parent id |
||
1455 | ); |
||
1456 | } |
||
1457 | return walls_updated; |
||
1458 | } |
||
1459 | |||
1460 | #if defined(DXX_BUILD_DESCENT_II) |
||
1461 | //creates afterburner blobs behind the specified object |
||
1462 | void drop_afterburner_blobs(object &obj, int count, fix size_scale, fix lifetime) |
||
1463 | { |
||
1464 | auto pos_left = vm_vec_scale_add(obj.pos, obj.orient.fvec, -obj.size); |
||
1465 | vm_vec_scale_add2(pos_left, obj.orient.rvec, -obj.size/4); |
||
1466 | const auto pos_right = vm_vec_scale_add(pos_left, obj.orient.rvec, obj.size/2); |
||
1467 | |||
1468 | if (count == 1) |
||
1469 | vm_vec_avg(pos_left, pos_left, pos_right); |
||
1470 | |||
1471 | const auto &&objseg = Segments.vmptridx(obj.segnum); |
||
1472 | { |
||
1473 | const auto &&segnum = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, pos_left, objseg); |
||
1474 | if (segnum != segment_none) |
||
1475 | object_create_explosion(segnum, pos_left, size_scale, VCLIP_AFTERBURNER_BLOB ); |
||
1476 | } |
||
1477 | |||
1478 | if (count > 1) { |
||
1479 | const auto &&segnum = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, pos_right, objseg); |
||
1480 | if (segnum != segment_none) { |
||
1481 | auto blob_obj = object_create_explosion(segnum, pos_right, size_scale, VCLIP_AFTERBURNER_BLOB ); |
||
1482 | if (lifetime != -1 && blob_obj != object_none) |
||
1483 | blob_obj->lifeleft = lifetime; |
||
1484 | } |
||
1485 | } |
||
1486 | } |
||
1487 | |||
1488 | /* |
||
1489 | * reads n expl_wall structs from a PHYSFS_File and swaps if specified |
||
1490 | */ |
||
1491 | void expl_wall_read_n_swap(fvmwallptr &vmwallptr, PHYSFS_File *const fp, const int swap, const unsigned count) |
||
1492 | { |
||
1493 | assert(!Num_exploding_walls); |
||
1494 | unsigned num_exploding_walls = 0; |
||
1495 | /* Legacy versions of Descent always write a fixed number of |
||
1496 | * entries, even if some or all of those entries are empty. This |
||
1497 | * loop needs to count how many entries were valid, as well as load |
||
1498 | * them into the correct walls. |
||
1499 | */ |
||
1500 | for (unsigned i = count; i--;) |
||
1501 | { |
||
1502 | disk_expl_wall d; |
||
1503 | PHYSFS_readBytes(fp, &d, sizeof(d)); |
||
1504 | if (swap) |
||
1505 | { |
||
1506 | d.segnum = SWAPINT(d.segnum); |
||
1507 | d.sidenum = SWAPINT(d.sidenum); |
||
1508 | d.time = SWAPINT(d.time); |
||
1509 | } |
||
1510 | const icsegidx_t dseg = d.segnum; |
||
1511 | if (dseg == segment_none) |
||
1512 | continue; |
||
1513 | range_for (auto &&wp, vmwallptr) |
||
1514 | { |
||
1515 | auto &w = *wp; |
||
1516 | if (w.segnum != dseg) |
||
1517 | continue; |
||
1518 | if (w.sidenum != d.sidenum) |
||
1519 | continue; |
||
1520 | w.flags |= WALL_EXPLODING; |
||
1521 | w.explode_time_elapsed = d.time; |
||
1522 | ++ num_exploding_walls; |
||
1523 | break; |
||
1524 | } |
||
1525 | } |
||
1526 | Num_exploding_walls = num_exploding_walls; |
||
1527 | } |
||
1528 | |||
1529 | void expl_wall_write(fvcwallptr &vcwallptr, PHYSFS_File *const fp) |
||
1530 | { |
||
1531 | const unsigned num_exploding_walls = Num_exploding_walls; |
||
1532 | PHYSFS_writeBytes(fp, &num_exploding_walls, sizeof(unsigned)); |
||
1533 | range_for (auto &&wp, vcwallptr) |
||
1534 | { |
||
1535 | auto &e = *wp; |
||
1536 | if (!(e.flags & WALL_EXPLODING)) |
||
1537 | continue; |
||
1538 | disk_expl_wall d; |
||
1539 | d.segnum = e.segnum; |
||
1540 | d.sidenum = e.sidenum; |
||
1541 | d.time = e.explode_time_elapsed; |
||
1542 | PHYSFS_writeBytes(fp, &d, sizeof(d)); |
||
1543 | } |
||
1544 | } |
||
1545 | #endif |
||
1546 | } |