Debian Codegraph @

similar/main/fuelcen.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 * Functions for refueling centers.
23 *
24 */
25
26 #include "dxxsconf.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <math.h>
30 #include <string.h>
31 #include <ranges>
32
33 #include "fuelcen.h"
34 #include "gameseg.h"
35 #include "game.h" // For FrameTime
36 #include "dxxerror.h"
37 #include "vclip.h"
38 #include "fireball.h"
39 #include "robot.h"
40 #include "powerup.h"
41
42 #include "sounds.h"
43 #include "morph.h"
44 #include "3d.h"
45 #include "physfs-serial.h"
46 #include "bm.h"
47 #include "polyobj.h"
48 #include "ai.h"
49 #include "gamesave.h"
50 #include "player.h"
51 #include "collide.h"
52 #include "multi.h"
53 #include "multibot.h"
54 #include "escort.h"
55 #include "compiler-poison.h"
56 #include "d_construct.h"
57 #include "d_enumerate.h"
58 #include "compiler-range_for.h"
59 #include "d_levelstate.h"
60 #include "partial_range.h"
61 #include "segiter.h"
62
63 // The max number of fuel stations per mine.
64
65 namespace dcx {
66 constexpr fix Fuelcen_give_amount = i2f(25);
67 constexpr fix Fuelcen_max_amount = i2f(100);
68
69 // Every time a robot is created in the morphing code, it decreases capacity of the morpher
70 // by this amount... when capacity gets to 0, no more morphers...
71 constexpr fix EnergyToCreateOneRobot = i2f(1);
72
73 constexpr std::integral_constant<int, 15> Num_extry_robots{};
74 }
75 namespace dsx {
76 #if DXX_USE_EDITOR
77 const enumerated_array<char[11], MAX_CENTER_TYPES, segment_special> Special_names = {{{
78 {"NOTHING"},
79 {"FUELCEN"},
80 {"REPAIRCEN"},
81 {"CONTROLCEN"},
82 {"ROBOTMAKER"},
83 #if DXX_BUILD_DESCENT == 2
84 {"GOAL_RED"},
85 {"GOAL_BLUE"},
86 #endif
87 }}};
88 #endif
89
90 //------------------------------------------------------------
91 // Resets all fuel center info
92 void fuelcen_reset()
93 {
94 auto &RobotCenters = LevelSharedRobotcenterState.RobotCenters;
95 auto &Station = LevelUniqueFuelcenterState.Station;
96 DXX_MAKE_MEM_UNDEFINED(std::span(Station));
97 DXX_MAKE_MEM_UNDEFINED(std::span(RobotCenters));
98 LevelSharedRobotcenterState.Num_robot_centers = 0;
99 LevelUniqueFuelcenterState.Num_fuelcenters = 0;
100 range_for (shared_segment &i, Segments)
101 i.special = segment_special::nothing;
102 }
103
104 #ifndef NDEBUG //this is sometimes called by people from the debugger
105 dxx_compiler_attribute_used
106 static void reset_all_robot_centers()
107 {
108 // Remove all materialization centers
109 range_for (shared_segment &i, partial_range(Segments, LevelSharedSegmentState.Num_segments))
110 if (i.special == segment_special::robotmaker)
111 {
112 i.special = segment_special::nothing;
113 i.matcen_num = materialization_center_number::None;
114 }
115 }
116 #endif
117
118 //------------------------------------------------------------
119 // Turns a segment into a fully charged up fuel center...
120 void fuelcen_create(const vmsegptridx_t segp)
121 {
122 auto &Station = LevelUniqueFuelcenterState.Station;
123 auto station_type = segp->special;
124 switch(station_type)
125 {
126 case segment_special::nothing:
127 #if DXX_BUILD_DESCENT == 2
128 case segment_special::goal_blue:
129 case segment_special::goal_red:
130 #endif
131 return;
132 case segment_special::fuelcen:
133 case segment_special::repaircen:
134 case segment_special::controlcen:
135 case segment_special::robotmaker:
136 break;
137 default:
138 Error("Invalid station type %d in fuelcen.c\n", underlying_value(station_type));
139 }
140
141 const auto next_fuelcenter_idx = static_cast<station_number>(LevelUniqueFuelcenterState.Num_fuelcenters++);
142 segp->station_idx = next_fuelcenter_idx;
143 auto &station = Station.at(next_fuelcenter_idx);
144 station.Type = station_type;
145 station.Capacity = Fuelcen_max_amount;
146 station.segnum = segp;
147 station.Timer = -1;
148 station.Flag = 0;
149 }
150
151 namespace {
152
153 //------------------------------------------------------------
154 // Adds a matcen that already is a special type into the Station array.
155 // This function is separate from other fuelcens because we don't want values reset.
156 static void matcen_create(const vmsegptridx_t segp)
157 {
158 auto &RobotCenters = LevelSharedRobotcenterState.RobotCenters;
159 auto &Station = LevelUniqueFuelcenterState.Station;
160 const auto station_type = segp->special;
161
162 assert(station_type == segment_special::robotmaker);
163
164 const auto next_fuelcenter_idx = static_cast<station_number>(LevelUniqueFuelcenterState.Num_fuelcenters++);
165 segp->station_idx = next_fuelcenter_idx;
166 auto &station = Station.at(next_fuelcenter_idx);
167
168 station.Type = station_type;
169 station.Capacity = i2f(underlying_value(GameUniqueState.Difficulty_level) + 3);
170 station.segnum = segp;
171 station.Timer = -1;
172 station.Flag = 0;
173
174 const auto next_robot_center_idx = static_cast<materialization_center_number>(LevelSharedRobotcenterState.Num_robot_centers++);
175 segp->matcen_num = next_robot_center_idx;
176 auto &robotcenter = RobotCenters[next_robot_center_idx];
177 robotcenter.fuelcen_num = next_fuelcenter_idx;
178 robotcenter.segnum = segp;
179 }
180
181 }
182
183 //------------------------------------------------------------
184 // Adds a segment that already is a special type into the Station array.
185 void fuelcen_activate(const vmsegptridx_t segp)
186 {
187 if (segp->special == segment_special::robotmaker)
188 matcen_create( segp);
189 else
190 fuelcen_create( segp);
191 }
192
193 //------------------------------------------------------------
194 // Trigger (enable) the materialization center in segment segnum
195 void trigger_matcen(const vmsegptridx_t segp)
196 {
197 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
198 auto &RobotCenters = LevelSharedRobotcenterState.RobotCenters;
199 auto &Station = LevelUniqueFuelcenterState.Station;
200 auto &Vertices = LevelSharedVertexState.get_vertices();
201 FuelCenter *robotcen;
202
203 assert(segp->special == segment_special::robotmaker);
204 assert(underlying_value(segp->matcen_num) < LevelUniqueFuelcenterState.Num_fuelcenters);
205
206 robotcen = &Station[RobotCenters[segp->matcen_num].fuelcen_num];
207
208 if (robotcen->Enabled == 1)
209 return;
210
211 if (!robotcen->Lives)
212 return;
213
214 const auto Difficulty_level = GameUniqueState.Difficulty_level;
215 #if DXX_BUILD_DESCENT == 2
216 // MK: 11/18/95, At insane, matcens work forever!
217 if (Difficulty_level != Difficulty_level_type::_4)
218 #endif
219 robotcen->Lives--;
220
221 robotcen->Timer = F1_0*1000; // Make sure the first robot gets emitted right away.
222 robotcen->Enabled = 1; // Say this center is enabled, it can create robots.
223 robotcen->Capacity = i2f(underlying_value(Difficulty_level) + 3);
224 // The lower this number is, the more quickly the center can be re-triggered.
225 // If it's too low, it can mean all the robots won't be put out, but for about 5
226 // robots, that's not real likely.
227 const auto MATCEN_LIFE = i2f(30 - 2 * underlying_value(Difficulty_level));
228 robotcen->Disable_time = MATCEN_LIFE;
229
230 // Create a bright object in the segment.
231 auto &vcvertptr = Vertices.vcptr;
232 auto pos{compute_segment_center(vcvertptr, segp)};
233 const auto &&delta{vm_vec_build_sub(vcvertptr(segp->verts.front()), pos)};
234 vm_vec_scale_add2(pos, delta, F1_0/2);
235 const auto &&objnum = obj_create(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, OBJ_LIGHT, 0, segp, pos, nullptr, 0, object::control_type::light, object::movement_type::None, render_type::RT_NONE);
236 if (objnum != object_none) {
237 objnum->lifeleft = MATCEN_LIFE;
238 objnum->ctype.light_info.intensity = i2f(8); // Light cast by a fuelcen.
239 } else {
240 Int3();
241 }
242 }
243
244 #if DXX_USE_EDITOR
245 //------------------------------------------------------------
246 // Takes away a segment's fuel center properties.
247 // Deletes the segment point entry in the FuelCenter list.
248 void fuelcen_delete(shared_segment &segp)
249 {
250 auto &RobotCenters = LevelSharedRobotcenterState.RobotCenters;
251 auto &Station = LevelUniqueFuelcenterState.Station;
252 auto Num_fuelcenters = LevelUniqueFuelcenterState.Num_fuelcenters;
253 Restart: ;
254 segp.special = segment_special::nothing;
255
256 for (auto &&[i, fi] : enumerate(partial_range(Station, Num_fuelcenters)))
257 {
258 if (vmsegptr(fi.segnum) == &segp)
259 {
260
261 auto &Num_robot_centers = LevelSharedRobotcenterState.Num_robot_centers;
262 // If Robot maker is deleted, fix Segments and RobotCenters.
263 if (fi.Type == segment_special::robotmaker)
264 {
265 if (!Num_robot_centers)
266 {
267 con_printf(CON_URGENT, "%s:%u: error: Num_robot_centers=0 while deleting robot maker", __FILE__, __LINE__);
268 return;
269 }
270 const auto &&range = partial_range(RobotCenters, underlying_value(segp.matcen_num), Num_robot_centers--);
271
272 std::move(std::next(range.begin()), range.end(), range.begin());
273 range_for (auto &fj, partial_const_range(Station, Num_fuelcenters))
274 {
275 if (fj.Type == segment_special::robotmaker)
276 {
277 shared_segment &sfj = vmsegptr(fj.segnum);
278 if (sfj.matcen_num > segp.matcen_num)
279 sfj.matcen_num = static_cast<materialization_center_number>(underlying_value(sfj.matcen_num) - 1);
280 }
281 }
282 }
283
284 //fix RobotCenters so they point to correct fuelcenter
285 range_for (auto &j, partial_range(RobotCenters, Num_robot_centers))
286 if (j.fuelcen_num > i) //this robotcenter's fuelcen is changing
287 j.fuelcen_num = static_cast<station_number>(underlying_value(j.fuelcen_num) - 1);
288 Assert(Num_fuelcenters > 0);
289 Num_fuelcenters--;
290 for (auto &&[j, fj] : enumerate(partial_range(Station, underlying_value(i), Num_fuelcenters)))
291 {
292 fj = std::move(Station[static_cast<station_number>(underlying_value(j) + 1)]);
293 Segments[fj.segnum].station_idx = j;
294 }
295 goto Restart;
296 }
297 }
298 LevelUniqueFuelcenterState.Num_fuelcenters = Num_fuelcenters;
299 }
300 #endif
301
302 #define ROBOT_GEN_TIME (i2f(5))
303
304 imobjptridx_t create_morph_robot(const d_robot_info_array &Robot_info, const vmsegptridx_t segp, const vms_vector &object_pos, const robot_id object_id)
305 {
306 ai_behavior default_behavior;
307 auto &robptr = Robot_info[object_id];
308 #if DXX_BUILD_DESCENT == 1
309 default_behavior = ai_behavior::AIB_NORMAL;
310 if (object_id == robot_id::toaster) // This is a toaster guy!
311 default_behavior = ai_behavior::AIB_RUN_FROM;
312 #elif DXX_BUILD_DESCENT == 2
313 default_behavior = robptr.behavior;
314 #endif
315
316 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
317 auto obj = robot_create(Robot_info, object_id, segp, object_pos,
318 &vmd_identity_matrix, Polygon_models[robptr.model_num].rad,
319 default_behavior);
320
321 if (obj == object_none)
322 {
323 Int3();
324 return object_none;
325 }
326
327 ++LevelUniqueObjectState.accumulated_robots;
328 ++GameUniqueState.accumulated_robots;
329 //Set polygon-object-specific data
330
331 obj->rtype.pobj_info.model_num = robptr.model_num;
332 obj->rtype.pobj_info.subobj_flags = 0;
333
334 //set Physics info
335
336 obj->mtype.phys_info.mass = robptr.mass;
337 obj->mtype.phys_info.drag = robptr.drag;
338
339 obj->mtype.phys_info.flags |= (PF_LEVELLING);
340
341 obj->shields = robptr.strength;
342
343 create_n_segment_path(obj, robptr, 6, segment_none); // Create a 6 segment path from creation point.
344
345 #if DXX_BUILD_DESCENT == 1
346 if (default_behavior == ai_behavior::AIB_RUN_FROM)
347 obj->ctype.ai_info.ail.mode = ai_mode::AIM_RUN_FROM_OBJECT;
348 #elif DXX_BUILD_DESCENT == 2
349 obj->ctype.ai_info.ail.mode = ai_behavior_to_mode(default_behavior);
350 #endif
351
352 return obj;
353 }
354
355 }
356
357 namespace {
358
359 // ----------------------------------------------------------------------------------------------------------
360 static void robotmaker_proc(const d_robot_info_array &Robot_info, const d_vclip_array &Vclip, fvmsegptridx &vmsegptridx, FuelCenter *const robotcen, const station_number numrobotcen)
361 {
362 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
363 auto &LevelUniqueMorphObjectState = LevelUniqueObjectState.MorphObjectState;
364 auto &Objects = LevelUniqueObjectState.Objects;
365 auto &Vertices = LevelSharedVertexState.get_vertices();
366 auto &vcobjptr = Objects.vcptr;
367 auto &vmobjptridx = Objects.vmptridx;
368 auto &RobotCenters = LevelSharedRobotcenterState.RobotCenters;
369 fix top_time;
370
371 if (robotcen->Enabled == 0)
372 return;
373
374 if (robotcen->Disable_time > 0) {
375 robotcen->Disable_time -= FrameTime;
376 if (robotcen->Disable_time <= 0) {
377 robotcen->Enabled = 0;
378 }
379 }
380
381 // No robot making in multiplayer mode.
382 if ((Game_mode & GM_MULTI) && (!(Game_mode & GM_MULTI_ROBOTS) || !multi_i_am_master()))
383 return;
384
385 // Wait until transmorgafier has capacity to make a robot...
386 if ( robotcen->Capacity <= 0 ) {
387 return;
388 }
389
390 const auto &&segp = vmsegptr(robotcen->segnum);
391 const auto matcen_num = segp->matcen_num;
392
393 if (!RobotCenters.valid_index(matcen_num))
394 return;
395
396 matcen_info *mi = &RobotCenters[matcen_num];
397 for (unsigned i = 0;; ++i)
398 {
399 if (i >= mi->robot_flags.size())
400 return;
401 if (mi->robot_flags[i])
402 break;
403 }
404
405 // Wait until we have a free slot for this puppy...
406 // <<<<<<<<<<<<<<<< Num robots in mine >>>>>>>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<< Max robots in mine >>>>>>>>>>>>>>>
407 {
408 auto &plr = get_local_player();
409 if (LevelUniqueObjectState.accumulated_robots - plr.num_kills_level >= Gamesave_num_org_robots + Num_extry_robots)
410 {
411 return;
412 }
413 }
414
415 robotcen->Timer += FrameTime;
416
417 auto &vcvertptr = Vertices.vcptr;
418 switch( robotcen->Flag ) {
419 case 0: // Wait until next robot can generate
420 if (Game_mode & GM_MULTI)
421 {
422 top_time = ROBOT_GEN_TIME;
423 }
424 else
425 {
426 const auto center{compute_segment_center(vcvertptr, segp)};
427 const auto dist_to_player = vm_vec_dist_quick( ConsoleObject->pos, center );
428 top_time = dist_to_player/64 + d_rand() * 2 + F1_0*2;
429 if ( top_time > ROBOT_GEN_TIME )
430 top_time = ROBOT_GEN_TIME + d_rand();
431 if ( top_time < F1_0*2 )
432 top_time = F1_0*3/2 + d_rand()*2;
433 }
434
435 if (robotcen->Timer > top_time ) {
436 int count{0};
437 const auto biased_matcen_creator = underlying_value(numrobotcen) ^ 0x80;
438
439 // Make sure this robotmaker hasn't put out its max without having any of them killed.
440 for (auto &obj : vcobjptr)
441 {
442 if (obj.type == OBJ_ROBOT)
443 if (obj.matcen_creator == biased_matcen_creator)
444 count++;
445 }
446 if (count > underlying_value(GameUniqueState.Difficulty_level) + 3)
447 {
448 robotcen->Timer /= 2;
449 return;
450 }
451
452 // Whack on any robot or player in the matcen segment.
453 count=0;
454 auto segnum = robotcen->segnum;
455 const auto &&csegp = vmsegptr(segnum);
456 range_for (const auto objp, objects_in(csegp, vmobjptridx, vmsegptr))
457 {
458 count++;
459 if ( count > MAX_OBJECTS ) {
460 Int3();
461 return;
462 }
463 if (objp->type==OBJ_ROBOT) {
464 collide_robot_and_materialization_center(Robot_info, objp);
465 robotcen->Timer = top_time/2;
466 return;
467 } else if (objp->type==OBJ_PLAYER ) {
468 collide_player_and_materialization_center(objp);
469 robotcen->Timer = top_time/2;
470 return;
471 }
472 }
473
474 const auto cur_object_loc{compute_segment_center(vcvertptr, csegp)};
475 const auto &&robotcen_segp = vmsegptridx(robotcen->segnum);
476 // HACK!!! The 10 under here should be something equal to the 1/2 the size of the segment.
477 auto obj = object_create_explosion_without_damage(Vclip, robotcen_segp, cur_object_loc, i2f(10), vclip_index::morphing_robot);
478
479 if (obj != object_none)
480 reconstruct_at(obj->orient, extract_orient_from_segment, vcvertptr, robotcen_segp);
481
482 digi_link_sound_to_pos(Vclip[vclip_index::morphing_robot].sound_num, robotcen_segp, sidenum_t::WLEFT, cur_object_loc, 0, F1_0);
483 robotcen->Flag = 1;
484 robotcen->Timer = 0;
485
486 }
487 break;
488 case 1: // Wait until 1/2 second after VCLIP started.
489 if (robotcen->Timer > Vclip[vclip_index::morphing_robot].play_time / 2)
490 {
491
492 robotcen->Capacity -= EnergyToCreateOneRobot;
493 robotcen->Flag = 0;
494
495 robotcen->Timer = 0;
496 const auto cur_object_loc{compute_segment_center(vcvertptr, vcsegptr(robotcen->segnum))};
497
498 // If this is the first materialization, set to valid robot.
499 {
500 std::array<robot_id, sizeof(mi->robot_flags) * 8> legal_types; // the width of robot_flags[].
501 int num_types;
502
503 num_types = 0;
504 for (unsigned i = 0;; ++i)
505 {
506 if (i >= mi->robot_flags.size())
507 break;
508 uint32_t flags = mi->robot_flags[i];
509 for (unsigned j = 0; flags && j < 8 * sizeof(flags); ++j)
510 {
511 if (flags & 1)
512 legal_types[num_types++] = static_cast<robot_id>((i * 32) + j);
513 flags >>= 1;
514 }
515 }
516
517 const robot_id type = (num_types == 1)
518 ? legal_types[0]
519 : legal_types[(d_rand() * num_types) / 32768];
520
521 const auto &&obj = create_morph_robot(Robot_info, vmsegptridx(robotcen->segnum), cur_object_loc, type );
522 if (obj != object_none) {
523 if (Game_mode & GM_MULTI)
524 multi_send_create_robot(numrobotcen, obj, type);
525 obj->matcen_creator = underlying_value(numrobotcen) | 0x80;
526
527 /* Copy `objp.orient.uvec` into a temporary and pass the temporary,
528 * since it is undefined behavior to access `objp.orient` after the old
529 * object is destroyed until the new value is constructed.
530 */
531 {
532 // Make object faces player...
533 auto old_uvec{obj->orient.uvec};
534 reconstruct_at(obj->orient, vm_vector_to_matrix_u, vm_vec_build_sub(ConsoleObject->pos, obj->pos), std::move(old_uvec));
535 }
536
537 morph_start(LevelUniqueMorphObjectState, LevelSharedPolygonModelState, obj);
538 //robotcen->last_created_obj = obj;
539 //robotcen->last_created_sig = robotcen->last_created_obj->signature;
540 }
541 }
542
543 }
544 break;
545 default:
546 robotcen->Flag = 0;
547 robotcen->Timer = 0;
548 }
549 }
550
551 }
552
553 //-------------------------------------------------------------
554 // Called once per frame, replenishes fuel supply.
555 void fuelcen_update_all(const d_robot_info_array &Robot_info)
556 {
557 auto &Station = LevelUniqueFuelcenterState.Station;
558 for (auto &&[idx, i] : enumerate(partial_range(Station, LevelUniqueFuelcenterState.Num_fuelcenters)))
559 {
560 if (i.Type == segment_special::robotmaker)
561 {
562 if (! (Game_suspended & SUSP_ROBOTS))
563 robotmaker_proc(Robot_info, Vclip, vmsegptridx, &i, idx);
564 }
565 }
566 }
567
568 namespace dsx {
569
570 #if DXX_BUILD_DESCENT == 1
571 constexpr std::integral_constant<unsigned, F1_0 / 3> FUELCEN_SOUND_DELAY{};
572 #elif DXX_BUILD_DESCENT == 2
573 //play every half second
574 constexpr std::integral_constant<unsigned, F1_0 / 4> FUELCEN_SOUND_DELAY{};
575 #endif
576
577 //-------------------------------------------------------------
578 fix fuelcen_give_fuel(const shared_segment &segp, fix MaxAmountCanTake)
579 {
580 static fix64 last_play_time{};
581
582 if (segp.special == segment_special::fuelcen)
583 {
584 fix amount;
585
586 #if DXX_BUILD_DESCENT == 2
587 detect_escort_goal_fuelcen_accomplished();
588 #endif
589
590 if (MaxAmountCanTake <= 0 ) {
591 return 0;
592 }
593
594 amount = fixmul(FrameTime,Fuelcen_give_amount);
595
596 if (amount > MaxAmountCanTake )
597 amount = MaxAmountCanTake;
598
599 if (last_play_time + FUELCEN_SOUND_DELAY < GameTime64 || last_play_time > GameTime64)
600 {
601 last_play_time = {GameTime64};
602 multi_digi_play_sample(sound_effect::SOUND_REFUEL_STATION_GIVING_FUEL, F1_0/2);
603 }
604 return amount;
605 } else {
606 return 0;
607 }
608 }
609
610 }
611
612 namespace dcx {
613
614 //-------------------------------------------------------------
615 // DM/050904
616 // Repair centers
617 // use same values as fuel centers
618 fix repaircen_give_shields(const shared_segment &segp, const fix MaxAmountCanTake)
619 {
620 static fix64 last_play_time{};
621
622 if (segp.special == segment_special::repaircen)
623 {
624 fix amount;
625 if (MaxAmountCanTake <= 0 ) {
626 return 0;
627 }
628 amount = fixmul(FrameTime,Fuelcen_give_amount);
629 if (amount > MaxAmountCanTake )
630 amount = MaxAmountCanTake;
631 if (last_play_time > GameTime64)
632 last_play_time = 0;
633 if (GameTime64 > last_play_time+FUELCEN_SOUND_DELAY) {
634 last_play_time = {GameTime64};
635 multi_digi_play_sample(sound_effect::SOUND_REFUEL_STATION_GIVING_FUEL, F1_0/2);
636 }
637 return amount;
638 } else {
639 return 0;
640 }
641 }
642
643 }
644
645 namespace dsx {
646
647 // --------------------------------------------------------------------------------------------
648 void disable_matcens(void)
649 {
650 auto &Station = LevelUniqueFuelcenterState.Station;
651 range_for (auto &s, partial_range(Station, LevelUniqueFuelcenterState.Num_fuelcenters))
652 if (s.Type == segment_special::robotmaker)
653 {
654 s.Enabled = 0;
655 s.Disable_time = 0;
656 }
657 }
658
659 // --------------------------------------------------------------------------------------------
660 // Initialize all materialization centers.
661 // Give them all the right number of lives.
662 void init_all_matcens(void)
663 {
664 auto &RobotCenters = LevelSharedRobotcenterState.RobotCenters;
665 auto &Station = LevelUniqueFuelcenterState.Station;
666 const auto Num_fuelcenters = LevelUniqueFuelcenterState.Num_fuelcenters;
667 const auto &&robot_range = partial_const_range(RobotCenters, LevelSharedRobotcenterState.Num_robot_centers);
668 for (auto &&[i, station] : enumerate(partial_range(Station, Num_fuelcenters)))
669 if (station.Type == segment_special::robotmaker)
670 {
671 station.Lives = 3;
672 station.Enabled = 0;
673 station.Disable_time = 0;
674 // Make sure this fuelcen is pointed at by a matcen.
675 if (std::ranges::find(robot_range, i, &matcen_info::fuelcen_num) == robot_range.end())
676 {
677 station.Lives = 0;
678 LevelError("Station %i has type robotmaker, but no robotmaker uses it; ignoring.", underlying_value(i));
679 }
680 }
681
682 #ifndef NDEBUG
683 // Make sure all matcens point at a fuelcen
684 range_for (auto &i, robot_range)
685 {
686 auto fuelcen_num = i.fuelcen_num;
687 assert(underlying_value(fuelcen_num) < Num_fuelcenters);
688 assert(Station[fuelcen_num].Type == segment_special::robotmaker);
689 }
690 #endif
691
692 }
693
694 }
695
696 namespace {
697
698 struct d1mi_v25
699 {
700 matcen_info *m;
701 d1mi_v25(matcen_info &mi) : m(&mi) {}
702 };
703
704 struct d1cmi_v25
705 {
706 const matcen_info *m;
707 d1cmi_v25(const matcen_info &mi) : m(&mi) {}
708 };
709
710 #define D1_MATCEN_V25_MEMBERLIST (p.m->robot_flags[0], serial::pad<sizeof(fix) * 2>(), p.m->segnum, p.m->fuelcen_num, serial::pad<1>())
711 DEFINE_SERIAL_UDT_TO_MESSAGE(d1mi_v25, p, D1_MATCEN_V25_MEMBERLIST);
712 DEFINE_SERIAL_UDT_TO_MESSAGE(d1cmi_v25, p, D1_MATCEN_V25_MEMBERLIST);
713 ASSERT_SERIAL_UDT_MESSAGE_SIZE(d1mi_v25, 16);
714
715 }
716
717 namespace dsx {
718 #if DXX_BUILD_DESCENT == 1
719
720 namespace {
721
722 struct d1mi_v26
723 {
724 matcen_info *m;
725 d1mi_v26(matcen_info &mi) : m(&mi) {}
726 };
727
728 struct d1cmi_v26
729 {
730 const matcen_info *m;
731 d1cmi_v26(const matcen_info &mi) : m(&mi) {}
732 };
733
734 #define D1_MATCEN_V26_MEMBERLIST (p.m->robot_flags[0], serial::pad<sizeof(uint32_t)>(), serial::pad<sizeof(fix) * 2>(), p.m->segnum, p.m->fuelcen_num, serial::pad<1>())
735 DEFINE_SERIAL_UDT_TO_MESSAGE(d1mi_v26, p, D1_MATCEN_V26_MEMBERLIST);
736 DEFINE_SERIAL_UDT_TO_MESSAGE(d1cmi_v26, p, D1_MATCEN_V26_MEMBERLIST);
737 ASSERT_SERIAL_UDT_MESSAGE_SIZE(d1mi_v26, 20);
738
739 }
740
741 void matcen_info_read(const NamedPHYSFS_File fp, matcen_info &mi, int version)
742 {
743 if (version > 25)
744 PHYSFSX_serialize_read<const d1mi_v26>(fp, mi);
745 else
746 PHYSFSX_serialize_read<const d1mi_v25>(fp, mi);
747 }
748 #elif DXX_BUILD_DESCENT == 2
749 void fuelcen_check_for_goal(object &plrobj, const shared_segment &segp)
750 {
751 assert(game_mode_capture_flag(Game_mode));
752
753 team_number check_team;
754 powerup_type_t powerup_to_drop;
755 switch(segp.special)
756 {
757 case segment_special::goal_blue:
758 check_team = team_number::blue;
759 powerup_to_drop = powerup_type_t::POW_FLAG_RED;
760 break;
761 case segment_special::goal_red:
762 check_team = team_number::red;
763 powerup_to_drop = powerup_type_t::POW_FLAG_BLUE;
764 break;
765 default:
766 return;
767 }
768 if (multi_get_team_from_player(Netgame, Player_num) != check_team)
769 return;
770 auto &player_info = plrobj.ctype.player_info;
771 if (player_info.powerup_flags & PLAYER_FLAGS_FLAG)
772 {
773 player_info.powerup_flags &= ~PLAYER_FLAGS_FLAG;
774 multi_send_capture_bonus (Player_num);
775 maybe_drop_net_powerup(powerup_to_drop, 1, 0);
776 }
777 }
778
779 void fuelcen_check_for_hoard_goal(object &plrobj, const shared_segment &segp)
780 {
781 assert(game_mode_hoard(Game_mode));
782
783 if (Player_dead_state != player_dead_state::no)
784 return;
785
786 const auto special{segp.special};
787 if (special == segment_special::goal_blue || special == segment_special::goal_red)
788 {
789 auto &player_info = plrobj.ctype.player_info;
790 if (auto &hoard_orbs = player_info.hoard.orbs)
791 {
792 player_info.powerup_flags &= ~PLAYER_FLAGS_FLAG;
793 multi_send_orb_bonus(Player_num, std::exchange(hoard_orbs, 0));
794 }
795 }
796
797 }
798
799
800 /*
801 * reads an d1_matcen_info structure from a PHYSFS_File
802 */
803 void d1_matcen_info_read(const NamedPHYSFS_File fp, matcen_info &mi)
804 {
805 PHYSFSX_serialize_read<const d1mi_v25>(fp, mi);
806 mi.robot_flags[1] = 0;
807 }
808
809 DEFINE_SERIAL_UDT_TO_MESSAGE(matcen_info, m, (m.robot_flags, serial::pad<sizeof(fix) * 2>(), m.segnum, m.fuelcen_num, serial::pad<1>()));
810 ASSERT_SERIAL_UDT_MESSAGE_SIZE(matcen_info, 20);
811
812 void matcen_info_read(const NamedPHYSFS_File fp, matcen_info &mi)
813 {
814 PHYSFSX_serialize_read(fp, mi);
815 }
816 #endif
817
818 void matcen_info_write(PHYSFS_File *fp, const matcen_info &mi, short version)
819 {
820 if (version >= 27)
821 #if DXX_BUILD_DESCENT == 1
822 PHYSFSX_serialize_write<d1cmi_v26>(fp, mi);
823 #elif DXX_BUILD_DESCENT == 2
824 PHYSFSX_serialize_write(fp, mi);
825 #endif
826 else
827 PHYSFSX_serialize_write<d1cmi_v25>(fp, mi);
828 }
829 }
830
831 DEFINE_SERIAL_UDT_TO_MESSAGE(FuelCenter, fc, (fc.Type, serial::pad<3>(), serial::sign_extend<int>(fc.segnum), fc.Flag, fc.Enabled, fc.Lives, serial::pad<1>(), fc.Capacity, serial::pad<sizeof(fix)>(), fc.Timer, fc.Disable_time, serial::pad<3 * sizeof(fix)>()));
832 ASSERT_SERIAL_UDT_MESSAGE_SIZE(FuelCenter, 40);
833
834 void fuelcen_read(const NamedPHYSFS_File fp, FuelCenter &fc)
835 {
836 PHYSFSX_serialize_read(fp, fc);
837 }
838
839 void fuelcen_write(PHYSFS_File *fp, const FuelCenter &fc)
840 {
841 PHYSFSX_serialize_write(fp, fc);
842 }