Debian Codegraph @

d2x-rebirth/main/escort.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 * Escort robot behavior.
23 *
24 */
25
26 #include <ranges>
27 #include "dxxsconf.h"
28 #include <stdio.h> // for printf()
29 #include <stdlib.h> // for rand() and qsort()
30 #include <string.h> // for memset()
31
32 #include "window.h"
33 #include "console.h"
34 #include "vecmat.h"
35 #include "gr.h"
36 #include "gameseg.h"
37 #include "3d.h"
38 #include "palette.h"
39 #include "timer.h"
40 #include "u_mem.h"
41 #include "weapon.h"
42
43 #include "object.h"
44 #include "dxxerror.h"
45 #include "ai.h"
46 #include "robot.h"
47 #include "fvi.h"
48 #include "physics.h"
49 #include "wall.h"
50 #include "player.h"
51 #include "fireball.h"
52 #include "game.h"
53 #include "powerup.h"
54 #include "hudmsg.h"
55 #include "cntrlcen.h"
56 #include "gauges.h"
57 #include "event.h"
58 #include "key.h"
59 #include "fuelcen.h"
60 #include "sounds.h"
61 #include "screens.h"
62 #include "text.h"
63 #include "gamefont.h"
64 #include "newmenu.h"
65 #include "playsave.h"
66 #include "gameseq.h"
67 #include "automap.h"
68 #include "laser.h"
69 #include "escort.h"
70 #include "vclip.h"
71 #include "segiter.h"
72 #include "compiler-range_for.h"
73 #include "d_enumerate.h"
74 #include "d_levelstate.h"
75 #include "d_range.h"
76 #include "d_zip.h"
77
78 #if DXX_USE_EDITOR
79 #include "editor/editor.h"
80 #endif
81
82 namespace dsx {
83
84 namespace {
85
86 constexpr fix MAX_ESCORT_TIME_AWAY{F1_0 * 4};
87
88 static void say_escort_goal(escort_goal_t goal_num);
89
90 constexpr std::array<char[12], ESCORT_GOAL_MARKER9> Escort_goal_text = {{
91 "BLUE KEY",
92 "YELLOW KEY",
93 "RED KEY",
94 "REACTOR",
95 "EXIT",
96 "ENERGY",
97 "ENERGYCEN",
98 "SHIELD",
99 "POWERUP",
100 "ROBOT",
101 "HOSTAGES",
102 "SPEW",
103 "SCRAM",
104 "EXIT",
105 "BOSS",
106 "MARKER 1",
107 "MARKER 2",
108 "MARKER 3",
109 "MARKER 4",
110 "MARKER 5",
111 "MARKER 6",
112 "MARKER 7",
113 "MARKER 8",
114 "MARKER 9",
115 // -- too much work -- "KAMIKAZE "
116 }};
117
118 constexpr std::integral_constant<unsigned, 200> Max_escort_length{};
119
120 #define DXX_GUIDEBOT_RENAME_MENU(VERB) \
121 DXX_MENUITEM(VERB, INPUT, guidebot_name_buffer, opt_name) \
122
123 struct rename_guidebot_menu_items
124 {
125 std::array<newmenu_item, DXX_GUIDEBOT_RENAME_MENU(COUNT)> m;
126 decltype(PlayerCfg.GuidebotName) guidebot_name_buffer;
127 enum
128 {
129 DXX_GUIDEBOT_RENAME_MENU(ENUM)
130 };
131 rename_guidebot_menu_items()
132 {
133 guidebot_name_buffer = PlayerCfg.GuidebotName;
134 DXX_GUIDEBOT_RENAME_MENU(ADD);
135 }
136 };
137
138 #undef DXX_GUIDEBOT_RENAME_MENU
139
140 struct rename_guidebot_menu : rename_guidebot_menu_items, passive_newmenu
141 {
142 rename_guidebot_menu(grs_canvas &src) :
143 passive_newmenu(menu_title{nullptr}, menu_subtitle{"Enter Guide-bot name:"}, menu_filename{nullptr}, tiny_mode_flag::normal, tab_processing_flag::ignore, adjusted_citem::create(m, 0), src)
144 {
145 }
146 virtual window_event_result event_handler(const d_event &event) override;
147 };
148
149 window_event_result rename_guidebot_menu::event_handler(const d_event &event)
150 {
151 switch (event.type)
152 {
153 case event_type::window_close:
154 {
155 uint8_t changed{0};
156 if (PlayerCfg.GuidebotName != guidebot_name_buffer)
157 {
158 PlayerCfg.GuidebotName = guidebot_name_buffer;
159 changed = 1;
160 }
161 if (PlayerCfg.GuidebotNameReal != guidebot_name_buffer)
162 {
163 PlayerCfg.GuidebotNameReal = guidebot_name_buffer;
164 changed = 1;
165 }
166 if (changed)
167 write_player_file();
168 }
169 return window_event_result::ignored;
170 default:
171 return newmenu::event_handler(event);
172 }
173 }
174
175 }
176
177 void init_buddy_for_level(void)
178 {
179 auto &Objects = LevelUniqueObjectState.Objects;
180 auto &vmobjptridx = Objects.vmptridx;
181 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
182 auto &BuddyState = LevelUniqueObjectState.BuddyState;
183 BuddyState = {};
184 BuddyState.Buddy_gave_hint_count = 5;
185 BuddyState.Looking_for_marker = game_marker_index::None;
186 BuddyState.Escort_goal_object = ESCORT_GOAL_UNSPECIFIED;
187 BuddyState.Escort_special_goal = ESCORT_GOAL_UNSPECIFIED;
188 BuddyState.Last_buddy_key = -1;
189 BuddyState.Buddy_sorry_time = -F1_0;
190 BuddyState.Buddy_last_seen_player = 0;
191 BuddyState.Buddy_last_missile_time = 0;
192 BuddyState.Last_time_buddy_gave_hint = 0;
193 BuddyState.Last_come_back_message_time = 0;
194 BuddyState.Escort_last_path_created = 0;
195 BuddyState.Buddy_last_player_path_created = 0;
196 BuddyState.Last_buddy_message_time = 0;
197 BuddyState.Buddy_objnum = find_escort(vmobjptridx, Robot_info);
198 }
199
200 namespace {
201
202 // -----------------------------------------------------------------------------
203 // See if segment from curseg through sidenum is reachable.
204 // Return true if it is reachable, else return false.
205 static int segment_is_reachable(const object &robot, const robot_info &robptr, const shared_segment &segp, const sidenum_t sidenum, const player_flags powerup_flags)
206 {
207 const auto wall_num = segp.sides[sidenum].wall_num;
208
209 // If no wall, then it is reachable
210 if (wall_num == wall_none)
211 return 1;
212
213 return ai_door_is_openable(robot, &robptr, powerup_flags, segp, sidenum);
214
215 // -- MK, 10/17/95 --
216 // -- MK, 10/17/95 -- // Hmm, a closed wall. I think this mean not reachable.
217 // -- MK, 10/17/95 -- if (Walls[wall_num].type == WALL_CLOSED)
218 // -- MK, 10/17/95 -- return 0;
219 // -- MK, 10/17/95 --
220 // -- MK, 10/17/95 -- if (Walls[wall_num].type == WALL_DOOR) {
221 // -- MK, 10/17/95 -- if (Walls[wall_num].keys == KEY_NONE) {
222 // -- MK, 10/17/95 -- return 1; // @MK, 10/17/95: Be consistent with ai_door_is_openable
223 // -- MK, 10/17/95 -- // -- if (Walls[wall_num].flags & WALL_DOOR_LOCKED)
224 // -- MK, 10/17/95 -- // -- return 0;
225 // -- MK, 10/17/95 -- // -- else
226 // -- MK, 10/17/95 -- // -- return 1;
227 // -- MK, 10/17/95 -- } else if (Walls[wall_num].keys == KEY_BLUE)
228 // -- MK, 10/17/95 -- return (Players[Player_num].flags & PLAYER_FLAGS_BLUE_KEY);
229 // -- MK, 10/17/95 -- else if (Walls[wall_num].keys == KEY_GOLD)
230 // -- MK, 10/17/95 -- return (Players[Player_num].flags & PLAYER_FLAGS_GOLD_KEY);
231 // -- MK, 10/17/95 -- else if (Walls[wall_num].keys == KEY_RED)
232 // -- MK, 10/17/95 -- return (Players[Player_num].flags & PLAYER_FLAGS_RED_KEY);
233 // -- MK, 10/17/95 -- else
234 // -- MK, 10/17/95 -- Int3(); // Impossible! Doesn't have no key, but doesn't have any key!
235 // -- MK, 10/17/95 -- } else
236 // -- MK, 10/17/95 -- return 1;
237 // -- MK, 10/17/95 --
238 // -- MK, 10/17/95 -- Int3(); // Hmm, thought 'if' above had to return!
239 // -- MK, 10/17/95 -- return 0;
240
241 }
242
243 }
244
245 // -----------------------------------------------------------------------------
246 // Create a breadth-first list of segments reachable from current segment.
247 // max_segs is maximum number of segments to search. Use MAX_SEGMENTS to search all.
248 // On exit, *length <= max_segs.
249 // Input:
250 // start_seg
251 // Output:
252 // bfs_list: array of shorts, each reachable segment. Includes start segment.
253 // length: number of elements in bfs_list
254 std::size_t create_bfs_list(const object &robot, const robot_info &robptr, const vcsegidx_t start_seg, const player_flags powerup_flags, const std::span<segnum_t> bfs_list)
255 {
256 std::size_t head = 0, tail = 0;
257 visited_segment_bitarray_t visited;
258 bfs_list[head++] = start_seg;
259 visited[start_seg] = true;
260
261 while (head != tail && head < bfs_list.size())
262 {
263 auto curseg = bfs_list[tail++];
264 const auto &&cursegp = vcsegptr(curseg);
265 for (const auto &&[i, connected_seg] : enumerate(cursegp->children))
266 {
267 if (IS_CHILD(connected_seg) && (!visited[connected_seg])) {
268 if (segment_is_reachable(robot, robptr, cursegp, static_cast<sidenum_t>(i), powerup_flags)) {
269 bfs_list[head++] = connected_seg;
270 if (head >= bfs_list.size())
271 break;
272 visited[connected_seg] = true;
273 Assert(head < MAX_SEGMENTS);
274 }
275 }
276 }
277 }
278 return head;
279 }
280
281 namespace {
282
283 // -----------------------------------------------------------------------------
284 // Return true if ok for buddy to talk, else return false.
285 // Buddy is allowed to talk if the segment he is in does not contain a blastable wall that has not been blasted
286 // AND he has never yet, since being initialized for level, been allowed to talk.
287 static uint8_t ok_for_buddy_to_talk(void)
288 {
289 auto &Objects = LevelUniqueObjectState.Objects;
290 auto &vmobjptridx = Objects.vmptridx;
291 auto &BuddyState = LevelUniqueObjectState.BuddyState;
292 const auto Buddy_objnum = BuddyState.Buddy_objnum;
293 if (Buddy_objnum == object_none)
294 return 0;
295
296 const vmobjptridx_t buddy = vmobjptridx(Buddy_objnum);
297 const auto buddy_type = buddy->type;
298 if (buddy_type != OBJ_ROBOT) {
299 BuddyState.Buddy_allowed_to_talk = 0;
300 BuddyState.Buddy_objnum = object_none;
301 con_printf(CON_URGENT, "BUG: buddy is object %u, but that object is type %u.", Buddy_objnum.get_unchecked_index(), buddy_type);
302 return 0;
303 }
304
305 if (const auto Buddy_allowed_to_talk = BuddyState.Buddy_allowed_to_talk)
306 return Buddy_allowed_to_talk;
307
308 const shared_segment &segp = vcsegptr(buddy->segnum);
309
310 auto &Walls = LevelUniqueWallSubsystemState.Walls;
311 auto &vcwallptr = Walls.vcptr;
312 for (const auto &&[side, child] : zip(segp.sides, segp.children))
313 {
314 const auto wall_num = side.wall_num;
315 if (wall_num != wall_none) {
316 auto &w = *vcwallptr(wall_num);
317 if (w.type == WALL_BLASTABLE && !(w.flags & wall_flag::blasted))
318 return 0;
319 }
320
321 // Check one level deeper.
322 if (IS_CHILD(child))
323 {
324 const shared_segment &cseg = *vcsegptr(child);
325 range_for (const auto &j, cseg.sides)
326 {
327 auto wall2 = j.wall_num;
328 if (wall2 != wall_none) {
329 auto &w = *vcwallptr(wall2);
330 if (w.type == WALL_BLASTABLE && !(w.flags & wall_flag::blasted))
331 return 0;
332 }
333 }
334 }
335 }
336
337 BuddyState.Buddy_allowed_to_talk = 1;
338 return 1;
339 }
340
341 static void record_escort_goal_accomplished()
342 {
343 auto &BuddyState = LevelUniqueObjectState.BuddyState;
344 if (ok_for_buddy_to_talk()) {
345 digi_play_sample_once(sound_effect::SOUND_BUDDY_MET_GOAL, F1_0);
346 BuddyState.Escort_goal_objidx = object_none;
347 BuddyState.Escort_goal_reachable = d_unique_buddy_state::Escort_goal_reachability::unreachable;
348 BuddyState.Looking_for_marker = game_marker_index::None;
349 BuddyState.Escort_goal_object = ESCORT_GOAL_UNSPECIFIED;
350 BuddyState.Escort_special_goal = ESCORT_GOAL_UNSPECIFIED;
351 }
352 }
353
354 }
355
356 // --------------------------------------------------------------------------------------------
357 void detect_escort_goal_fuelcen_accomplished()
358 {
359 auto &BuddyState = LevelUniqueObjectState.BuddyState;
360 if (!BuddyState.Buddy_allowed_to_talk)
361 return;
362 if (BuddyState.Escort_special_goal == ESCORT_GOAL_ENERGYCEN)
363 record_escort_goal_accomplished();
364 }
365
366 void detect_escort_goal_accomplished(const vmobjptridx_t index)
367 {
368 auto &Objects = LevelUniqueObjectState.Objects;
369 auto &vcobjptr = Objects.vcptr;
370 auto &BuddyState = LevelUniqueObjectState.BuddyState;
371 if (!BuddyState.Buddy_allowed_to_talk)
372 return;
373
374 // If goal is not an object (FUELCEN, EXIT, SCRAM), bail. FUELCEN is handled in detect_escort_goal_fuelcen_accomplished(), EXIT and SCRAM are never accomplished.
375 if (BuddyState.Escort_goal_reachable == d_unique_buddy_state::Escort_goal_reachability::unreachable)
376 return;
377 const auto Escort_goal_objidx = BuddyState.Escort_goal_objidx;
378 if (Escort_goal_objidx == object_none)
379 {
380 con_puts(CON_URGENT, "BUG: buddy goal is reachable, but goal object is object_none");
381 return;
382 }
383
384 // See if goal found was a key. Need to handle default goals differently.
385 // Note, no buddy_met_goal sound when blow up reactor or exit. Not great, but ok
386 // since for reactor, noisy, for exit, buddy is disappearing.
387 if (BuddyState.Escort_special_goal == ESCORT_GOAL_UNSPECIFIED && Escort_goal_objidx == index)
388 {
389 record_escort_goal_accomplished();
390 return;
391 }
392
393 if (index->type == OBJ_POWERUP) {
394 const auto index_id = get_powerup_id(index);
395 escort_goal_t goal_key;
396 if ((index_id == powerup_type_t::POW_KEY_BLUE && (goal_key = ESCORT_GOAL_BLUE_KEY, true)) ||
397 (index_id == powerup_type_t::POW_KEY_GOLD && (goal_key = ESCORT_GOAL_GOLD_KEY, true)) ||
398 (index_id == powerup_type_t::POW_KEY_RED && (goal_key = ESCORT_GOAL_RED_KEY, true))
399 )
400 {
401 if (BuddyState.Escort_goal_object == goal_key)
402 {
403 record_escort_goal_accomplished();
404 return;
405 }
406 }
407 }
408 if (BuddyState.Escort_special_goal != ESCORT_GOAL_UNSPECIFIED)
409 {
410 if (index->type == OBJ_POWERUP && BuddyState.Escort_special_goal == ESCORT_GOAL_POWERUP)
411 record_escort_goal_accomplished(); // Any type of powerup picked up will do.
412 else
413 {
414 auto &egi_obj = *vcobjptr(Escort_goal_objidx);
415 if (index->type == egi_obj.type && index->id == egi_obj.id)
416 // Note: This will help a little bit in making the buddy believe a goal is satisfied. Won't work for a general goal like "find any powerup"
417 // because of the insistence of both type and id matching.
418 record_escort_goal_accomplished();
419 }
420 }
421 }
422
423 void change_guidebot_name()
424 {
425 auto menu = window_create<rename_guidebot_menu>(grd_curscreen->sc_canvas);
426 (void)menu;
427 }
428
429 namespace {
430
431 // -----------------------------------------------------------------------------
432 static uint8_t show_buddy_message()
433 {
434 auto &BuddyState = LevelUniqueObjectState.BuddyState;
435 if (BuddyState.Buddy_messages_suppressed)
436 return 0;
437
438 #if DXX_USE_MULTIPLAYER
439 if (Game_mode & GM_MULTI)
440 {
441 if (!Netgame.AllowGuidebot)
442 return 0;
443 }
444 #endif
445
446 if (BuddyState.Last_buddy_message_time + F1_0 < GameTime64) {
447 if (const auto r = ok_for_buddy_to_talk())
448 return r;
449 }
450 return 0;
451 }
452
453 dxx_compiler_attribute_nonnull()
454 static void buddy_message_force_str(const char *str)
455 {
456 auto &BuddyState = LevelUniqueObjectState.BuddyState;
457 BuddyState.Last_buddy_message_time = {GameTime64};
458 HUD_init_message(HM_DEFAULT, "%c%c%s:%c%c %s", CC_COLOR, BM_XRGB(28, 0, 0), static_cast<const char *>(PlayerCfg.GuidebotName), CC_COLOR, BM_XRGB(0, 31, 0), str);
459 }
460
461 dxx_compiler_attribute_format_printf(1, 0)
462 static void buddy_message_force_va(const char *const fmt, va_list vl)
463 {
464 char buf[128];
465 vsnprintf(buf, sizeof(buf), fmt, vl);
466 buddy_message_force_str(buf);
467 }
468
469 dxx_compiler_attribute_format_printf(1, 2)
470 static void buddy_message_ignore_time(const char *const fmt, ...)
471 {
472 auto &BuddyState = LevelUniqueObjectState.BuddyState;
473 if (BuddyState.Buddy_messages_suppressed)
474 return;
475 if (!ok_for_buddy_to_talk())
476 return;
477 va_list args;
478 va_start(args, fmt);
479 buddy_message_force_va(fmt, args);
480 va_end(args);
481 }
482
483 }
484
485 void (buddy_message)(const char * format, ... )
486 {
487 if (!show_buddy_message())
488 return;
489
490 va_list args;
491
492 va_start(args, format );
493 buddy_message_force_va(format, args);
494 va_end(args);
495 }
496
497 void buddy_message_str(const char *str)
498 {
499 if (!show_buddy_message())
500 return;
501 buddy_message_force_str(str);
502 }
503
504 namespace {
505
506 // -----------------------------------------------------------------------------
507 static void thief_message_str(const char * str) dxx_compiler_attribute_nonnull();
508 static void thief_message_str(const char * str)
509 {
510 HUD_init_message(HM_DEFAULT, "%c%cTHIEF:%c%c %s", 1, BM_XRGB(28, 0, 0), 1, BM_XRGB(0, 31, 0), str);
511 }
512
513 static void thief_message(const char *) = delete;
514 dxx_compiler_attribute_format_printf(1, 2)
515 static void thief_message(const char * format, ... )
516 {
517
518 char new_format[128];
519 va_list args;
520
521 va_start(args, format );
522 vsnprintf(new_format, sizeof(new_format), format, args);
523 va_end(args);
524 thief_message_str(new_format);
525 }
526
527 // -----------------------------------------------------------------------------
528 // Return true if marker #id has been placed.
529 static int marker_exists_in_mine(const game_marker_index id)
530 {
531 auto &Objects = LevelUniqueObjectState.Objects;
532 auto &vcobjptr = Objects.vcptr;
533 for (auto &obj : vcobjptr)
534 {
535 if (obj.type == OBJ_MARKER)
536 if (get_marker_id(obj) == id)
537 return 1;
538 }
539 return 0;
540 }
541
542 }
543
544 // -----------------------------------------------------------------------------
545 void set_escort_special_goal(d_unique_buddy_state &BuddyState, const int raw_special_key)
546 {
547 auto &Objects = LevelUniqueObjectState.Objects;
548 auto &vmobjptridx = Objects.vmptridx;
549 int marker_key;
550
551 BuddyState.Buddy_messages_suppressed = 0;
552
553 if (!BuddyState.Buddy_allowed_to_talk) {
554 if (!ok_for_buddy_to_talk()) {
555 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
556 const auto &&o = find_escort(vmobjptridx, Robot_info);
557 if (o == object_none)
558 HUD_init_message_literal(HM_DEFAULT, "No Guide-Bot in mine.");
559 else
560 HUD_init_message(HM_DEFAULT, "%s has not been released.", static_cast<const char *>(PlayerCfg.GuidebotName));
561 return;
562 }
563 }
564
565 const int special_key = raw_special_key & (~KEY_SHIFTED);
566
567 marker_key = special_key;
568
569 if (BuddyState.Last_buddy_key == special_key)
570 {
571 auto &Looking_for_marker = BuddyState.Looking_for_marker;
572 if (Looking_for_marker == game_marker_index::None && special_key != KEY_0)
573 {
574 const unsigned zero_based_marker_id = marker_key - KEY_1;
575 const auto gmi = static_cast<game_marker_index>(zero_based_marker_id);
576 if (marker_exists_in_mine(gmi))
577 Looking_for_marker = gmi;
578 else {
579 buddy_message_ignore_time("Marker %i not placed.", zero_based_marker_id + 1);
580 Looking_for_marker = game_marker_index::None;
581 }
582 } else {
583 Looking_for_marker = game_marker_index::None;
584 }
585 }
586
587 BuddyState.Last_buddy_key = special_key;
588
589 if (special_key == KEY_0)
590 BuddyState.Looking_for_marker = game_marker_index::None;
591 else if (BuddyState.Looking_for_marker != game_marker_index::None)
592 {
593 BuddyState.Escort_special_goal = static_cast<escort_goal_t>(ESCORT_GOAL_MARKER1 + marker_key - KEY_1);
594 } else {
595 auto &Escort_special_goal = BuddyState.Escort_special_goal;
596 switch (special_key) {
597 case KEY_1: Escort_special_goal = ESCORT_GOAL_ENERGY; break;
598 case KEY_2: Escort_special_goal = ESCORT_GOAL_ENERGYCEN; break;
599 case KEY_3: Escort_special_goal = ESCORT_GOAL_SHIELD; break;
600 case KEY_4: Escort_special_goal = ESCORT_GOAL_POWERUP; break;
601 case KEY_5: Escort_special_goal = ESCORT_GOAL_ROBOT; break;
602 case KEY_6: Escort_special_goal = ESCORT_GOAL_HOSTAGE; break;
603 case KEY_7: Escort_special_goal = ESCORT_GOAL_SCRAM; break;
604 case KEY_8: Escort_special_goal = ESCORT_GOAL_PLAYER_SPEW; break;
605 case KEY_9: Escort_special_goal = ESCORT_GOAL_EXIT; break;
606 case KEY_0: Escort_special_goal = ESCORT_GOAL_UNSPECIFIED; break;
607 default:
608 Int3(); // Oops, called with illegal key value.
609 }
610 }
611
612 BuddyState.Last_buddy_message_time = GameTime64 - 2*F1_0; // Allow next message to come through.
613
614 say_escort_goal(BuddyState.Escort_special_goal);
615 BuddyState.Escort_goal_object = ESCORT_GOAL_UNSPECIFIED;
616 #if DXX_USE_MULTIPLAYER
617 if (Game_mode & GM_MULTI)
618 multi_send_escort_goal(BuddyState);
619 #endif
620 }
621
622 namespace {
623
624 // -----------------------------------------------------------------------------
625 // Return id of boss.
626 static robot_id get_boss_id(void)
627 {
628 auto &Objects = LevelUniqueObjectState.Objects;
629 auto &vcobjptr = Objects.vcptr;
630 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
631 for (auto &obj : vcobjptr)
632 {
633 if (obj.type == OBJ_ROBOT)
634 {
635 const auto objp_id{get_robot_id(obj)};
636 if (Robot_info[objp_id].boss_flag != boss_robot_id::None)
637 return objp_id;
638 }
639 }
640 return robot_id::None;
641 }
642
643 // -----------------------------------------------------------------------------
644 // Return object index if object of objtype, objid exists in mine, else return -1
645 // "special" is used to find objects spewed by player which is hacked into flags field of powerup.
646 static icobjidx_t exists_in_mine_2(const unique_segment &segp, const std::optional<object_type_t> objtype, const std::optional<uint8_t> objid, const int special)
647 {
648 auto &Objects = LevelUniqueObjectState.Objects;
649 auto &vcobjptridx = Objects.vcptridx;
650 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
651 range_for (const auto curobjp, objects_in(segp, vcobjptridx, vcsegptr))
652 {
653 if (special == ESCORT_GOAL_PLAYER_SPEW && curobjp->type == OBJ_POWERUP)
654 {
655 if (curobjp->flags & OF_PLAYER_DROPPED)
656 return curobjp;
657 }
658
659 if (!objtype)
660 continue;
661 const auto t = *objtype;
662 if (curobjp->type == t) {
663 // Don't find escort robots if looking for robot!
664 if (t == OBJ_ROBOT && (Robot_info[get_robot_id(curobjp)].companion))
665 ;
666 else if (!objid)
667 {
668 /* If no object id is specified, then any object of the
669 * correct type is acceptable.
670 */
671 return curobjp;
672 }
673 else if (curobjp->id == *objid)
674 /* A specific object id is required, and the current object
675 * satisfies the requirement.
676 */
677 return curobjp;
678 }
679
680 if (t == OBJ_POWERUP && objid && curobjp->type == OBJ_ROBOT)
681 /* As a special case, if seeking a specific powerup and the
682 * current object is a robot that will drop that powerup, then
683 * consider the robot to be a match.
684 */
685 if (curobjp->contains.count)
686 if (curobjp->contains.type == contained_object_type::powerup)
687 if (curobjp->contains.id.powerup == powerup_type_t{*objid})
688 return curobjp;
689 }
690 return object_none;
691 }
692
693 // -----------------------------------------------------------------------------
694 static std::pair<icsegidx_t, d_unique_buddy_state::Escort_goal_reachability> exists_fuelcen_in_mine(const object &Buddy_objp, const player_flags powerup_flags)
695 {
696 const vcsegidx_t start_seg{Buddy_objp.segnum};
697 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
698 std::array<segnum_t, MAX_SEGMENTS> bfs_list;
699 auto &robptr = Robot_info[get_robot_id(Buddy_objp)];
700 const auto length = create_bfs_list(Buddy_objp, robptr, start_seg, powerup_flags, bfs_list);
701 {
702 const auto &&rb = partial_const_range(bfs_list, length);
703 const auto &&i{std::ranges::find(rb, segment_special::fuelcen, [](const segnum_t &s) {
704 return vcsegptr(s)->special;
705 })};
706 if (i != rb.end())
707 return {*i, d_unique_buddy_state::Escort_goal_reachability::reachable};
708 }
709 {
710 const std::ranges::subrange rh{vcsegptridx};
711 const auto &&i{std::ranges::find(rh, segment_special::fuelcen, &shared_segment::special)};
712 if (i != rh.end())
713 return {*i, d_unique_buddy_state::Escort_goal_reachability::unreachable};
714 }
715 return {segment_none, d_unique_buddy_state::Escort_goal_reachability::unreachable};
716 }
717
718 // Return nearest object of interest.
719 // If special == ESCORT_GOAL_PLAYER_SPEW, then looking for any object spewed by player.
720 // -1 means object does not exist in mine.
721 // -2 means object does exist in mine, but buddy-bot can't reach it (eg, behind triggered wall)
722 static std::pair<icobjidx_t, d_unique_buddy_state::Escort_goal_reachability> exists_in_mine(const object &Buddy_objp, const vcsegidx_t start_seg, const std::optional<object_type_t> objtype, const std::optional<uint8_t> objid, const int special, const player_flags powerup_flags)
723 {
724 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
725 std::array<segnum_t, MAX_SEGMENTS> bfs_list;
726 auto &robptr = Robot_info[get_robot_id(Buddy_objp)];
727 const auto length = create_bfs_list(Buddy_objp, robptr, start_seg, powerup_flags, bfs_list);
728
729 range_for (const auto segnum, partial_const_range(bfs_list, length))
730 {
731 const auto &&objnum = exists_in_mine_2(vcsegptr(segnum), objtype, objid, special);
732 if (objnum != object_none)
733 return {objnum, d_unique_buddy_state::Escort_goal_reachability::reachable};
734 }
735
736 // Couldn't find what we're looking for by looking at connectivity.
737 // See if it's in the mine. It could be hidden behind a trigger or switch
738 // which the buddybot doesn't understand.
739 for (auto &seg : vcsegptr)
740 {
741 const auto &&objnum{exists_in_mine_2(seg, objtype, objid, special)};
742 if (objnum != object_none)
743 return {objnum, d_unique_buddy_state::Escort_goal_reachability::unreachable};
744 }
745 return {object_none, d_unique_buddy_state::Escort_goal_reachability::unreachable};
746 }
747
748 // -----------------------------------------------------------------------------
749 // Return true if it happened, else return false.
750 static imsegidx_t find_exit_segment()
751 {
752 // ---------- Find exit doors ----------
753 range_for (const auto &&segp, vcsegptridx)
754 {
755 range_for (const auto j, segp->children)
756 if (j == segment_exit)
757 return segp;
758 }
759 return segment_none;
760 }
761
762 // -----------------------------------------------------------------------------
763 static void say_escort_goal(const escort_goal_t goal_num)
764 {
765 if (Player_dead_state != player_dead_state::no)
766 return;
767
768 const char *str;
769 switch (goal_num) {
770 default:
771 case ESCORT_GOAL_UNSPECIFIED:
772 return;
773 case ESCORT_GOAL_BLUE_KEY:
774 str = "Finding BLUE KEY";
775 break;
776 case ESCORT_GOAL_GOLD_KEY:
777 str = "Finding YELLOW KEY";
778 break;
779 case ESCORT_GOAL_RED_KEY:
780 str = "Finding RED KEY";
781 break;
782 case ESCORT_GOAL_CONTROLCEN:
783 str = "Finding REACTOR";
784 break;
785 case ESCORT_GOAL_EXIT:
786 str = "Finding EXIT";
787 break;
788 case ESCORT_GOAL_ENERGY:
789 str = "Finding ENERGY";
790 break;
791 case ESCORT_GOAL_ENERGYCEN:
792 str = "Finding ENERGY CENTER";
793 break;
794 case ESCORT_GOAL_SHIELD:
795 str = "Finding a SHIELD";
796 break;
797 case ESCORT_GOAL_POWERUP:
798 str = "Finding a POWERUP";
799 break;
800 case ESCORT_GOAL_ROBOT:
801 str = "Finding a ROBOT";
802 break;
803 case ESCORT_GOAL_HOSTAGE:
804 str = "Finding a HOSTAGE";
805 break;
806 case ESCORT_GOAL_SCRAM:
807 str = "Staying away...";
808 break;
809 case ESCORT_GOAL_BOSS:
810 str = "Finding BOSS robot";
811 break;
812 case ESCORT_GOAL_PLAYER_SPEW:
813 str = "Finding your powerups";
814 break;
815 case ESCORT_GOAL_MARKER1:
816 case ESCORT_GOAL_MARKER2:
817 case ESCORT_GOAL_MARKER3:
818 case ESCORT_GOAL_MARKER4:
819 case ESCORT_GOAL_MARKER5:
820 case ESCORT_GOAL_MARKER6:
821 case ESCORT_GOAL_MARKER7:
822 case ESCORT_GOAL_MARKER8:
823 case ESCORT_GOAL_MARKER9:
824 {
825 const uint8_t zero_based_goal_num = goal_num - ESCORT_GOAL_MARKER1;
826 buddy_message("Finding marker %i: '%.24s'", zero_based_goal_num + 1, &MarkerState.message[(game_marker_index{zero_based_goal_num})][0u]);
827 }
828 return;
829 }
830 buddy_message_str(str);
831 }
832
833 static void clear_escort_goals()
834 {
835 auto &BuddyState = LevelUniqueObjectState.BuddyState;
836 BuddyState.Looking_for_marker = game_marker_index::None;
837 BuddyState.Escort_goal_object = ESCORT_GOAL_UNSPECIFIED;
838 BuddyState.Escort_special_goal = ESCORT_GOAL_UNSPECIFIED;
839 }
840
841 static void escort_goal_does_not_exist(const escort_goal_t goal)
842 {
843 buddy_message_ignore_time("No %s in mine.", Escort_goal_text[goal - 1]);
844 clear_escort_goals();
845 }
846
847 static void escort_goal_unreachable(const escort_goal_t goal)
848 {
849 buddy_message_ignore_time("Can't reach %s.", Escort_goal_text[goal - 1]);
850 clear_escort_goals();
851 }
852
853 static void escort_go_to_goal(const vmobjptridx_t objp, const robot_info &robptr, ai_static *const aip, const segnum_t goal_seg)
854 {
855 auto &BuddyState = LevelUniqueObjectState.BuddyState;
856 auto &Objects = LevelUniqueObjectState.Objects;
857 create_path_to_segment(objp, robptr, goal_seg, Max_escort_length, create_path_safety_flag::safe); // MK!: Last parm (safety_flag) used to be 1!!
858 if (aip->path_length > 3)
859 aip->path_length = polish_path(objp, &Point_segs[aip->hide_index], aip->path_length);
860 if ((aip->path_length > 0) && (Point_segs[aip->hide_index + aip->path_length - 1].segnum != goal_seg)) {
861 const unsigned goal_text_index = std::exchange(BuddyState.Escort_goal_object, ESCORT_GOAL_SCRAM) - 1;
862 BuddyState.Looking_for_marker = game_marker_index::None;
863 auto &plr = get_player_controlling_guidebot(BuddyState, Players);
864 if (plr.objnum == object_none)
865 return;
866 auto &plrobj = *Objects.vcptr(plr.objnum);
867 if (plrobj.type != OBJ_PLAYER)
868 return;
869 buddy_message_ignore_time("Cannot reach %s.", goal_text_index < Escort_goal_text.size() ? Escort_goal_text[goal_text_index] : "<unknown>");
870 const auto goal_segment{plrobj.segnum};
871 const fix dist_to_player = find_connected_distance(objp->pos, vmsegptridx(objp->segnum), plrobj.pos, vmsegptridx(goal_segment), 100, wall_is_doorway_mask::fly);
872 if (dist_to_player > MIN_ESCORT_DISTANCE)
873 create_path_to_segment(objp, robptr, Max_escort_length, create_path_safety_flag::safe, goal_segment);
874 else {
875 create_n_segment_path(objp, robptr, 8 + d_rand() * 8, segment_none);
876 aip->path_length = polish_path(objp, &Point_segs[aip->hide_index], aip->path_length);
877 }
878 }
879 }
880
881 // -----------------------------------------------------------------------------
882 static imsegidx_t escort_get_goal_segment(const object &buddy_obj, const object_type_t objtype, const std::optional<uint8_t> objid, const player_flags powerup_flags)
883 {
884 auto &BuddyState = LevelUniqueObjectState.BuddyState;
885 auto &Objects = LevelUniqueObjectState.Objects;
886 auto &vcobjptr = Objects.vcptr;
887 const auto &&eim = exists_in_mine(buddy_obj, buddy_obj.segnum, objtype, objid, -1, powerup_flags);
888 BuddyState.Escort_goal_objidx = eim.first;
889 BuddyState.Escort_goal_reachable = eim.second;
890 if (eim.second != d_unique_buddy_state::Escort_goal_reachability::unreachable)
891 return vcobjptr(eim.first)->segnum;
892 return segment_none;
893 }
894
895 static inline imsegidx_t escort_get_goal_segment(const object &buddy_obj, const powerup_type_t objid, const player_flags powerup_flags)
896 {
897 return escort_get_goal_segment(buddy_obj, OBJ_POWERUP, underlying_value(objid), powerup_flags);
898 }
899
900 static void set_escort_goal_non_object(d_unique_buddy_state &BuddyState)
901 {
902 BuddyState.Escort_goal_objidx = object_none;
903 BuddyState.Escort_goal_reachable = d_unique_buddy_state::Escort_goal_reachability::unreachable;
904 }
905
906 static void escort_create_path_to_goal(const vmobjptridx_t objp, const robot_info &robptr, const player_info &player_info)
907 {
908 auto &BuddyState = LevelUniqueObjectState.BuddyState;
909 auto &Objects = LevelUniqueObjectState.Objects;
910 auto &vcobjptr = Objects.vcptr;
911 segnum_t goal_seg = segment_none;
912 ai_static *aip = &objp->ctype.ai_info;
913 ai_local *ailp = &objp->ctype.ai_info.ail;
914
915 if (BuddyState.Escort_special_goal != ESCORT_GOAL_UNSPECIFIED)
916 BuddyState.Escort_goal_object = BuddyState.Escort_special_goal;
917
918 const auto powerup_flags = player_info.powerup_flags;
919 const auto Escort_goal_object = BuddyState.Escort_goal_object;
920 if (BuddyState.Looking_for_marker != game_marker_index::None)
921 {
922 goal_seg = escort_get_goal_segment(objp, OBJ_MARKER, Escort_goal_object - ESCORT_GOAL_MARKER1, powerup_flags);
923 } else {
924 switch (Escort_goal_object) {
925 case ESCORT_GOAL_BLUE_KEY:
926 goal_seg = escort_get_goal_segment(objp, powerup_type_t::POW_KEY_BLUE, powerup_flags);
927 break;
928 case ESCORT_GOAL_GOLD_KEY:
929 goal_seg = escort_get_goal_segment(objp, powerup_type_t::POW_KEY_GOLD, powerup_flags);
930 break;
931 case ESCORT_GOAL_RED_KEY:
932 goal_seg = escort_get_goal_segment(objp, powerup_type_t::POW_KEY_RED, powerup_flags);
933 break;
934 case ESCORT_GOAL_CONTROLCEN:
935 goal_seg = escort_get_goal_segment(objp, OBJ_CNTRLCEN, std::nullopt, powerup_flags);
936 break;
937 case ESCORT_GOAL_EXIT:
938 goal_seg = find_exit_segment();
939 set_escort_goal_non_object(BuddyState);
940 if (goal_seg == segment_none)
941 escort_goal_does_not_exist(ESCORT_GOAL_EXIT);
942 else if (goal_seg == segment_exit)
943 escort_goal_unreachable(ESCORT_GOAL_EXIT);
944 else
945 escort_go_to_goal(objp, robptr, aip, goal_seg);
946 return;
947 case ESCORT_GOAL_ENERGY:
948 goal_seg = escort_get_goal_segment(objp, powerup_type_t::POW_ENERGY, powerup_flags);
949 break;
950 case ESCORT_GOAL_ENERGYCEN:
951 {
952 const auto &&ef = exists_fuelcen_in_mine(objp, powerup_flags);
953 set_escort_goal_non_object(BuddyState);
954 if (ef.second != d_unique_buddy_state::Escort_goal_reachability::unreachable)
955 escort_go_to_goal(objp, robptr, aip, ef.first);
956 else if (ef.first == segment_none)
957 escort_goal_does_not_exist(ESCORT_GOAL_ENERGYCEN);
958 else
959 escort_goal_unreachable(ESCORT_GOAL_ENERGYCEN);
960 return;
961 }
962 case ESCORT_GOAL_SHIELD:
963 goal_seg = escort_get_goal_segment(objp, powerup_type_t::POW_SHIELD_BOOST, powerup_flags);
964 break;
965 case ESCORT_GOAL_POWERUP:
966 goal_seg = escort_get_goal_segment(objp, OBJ_POWERUP, std::nullopt, powerup_flags);
967 break;
968 case ESCORT_GOAL_ROBOT:
969 goal_seg = escort_get_goal_segment(objp, OBJ_ROBOT, std::nullopt, powerup_flags);
970 break;
971 case ESCORT_GOAL_HOSTAGE:
972 goal_seg = escort_get_goal_segment(objp, OBJ_HOSTAGE, std::nullopt, powerup_flags);
973 break;
974 case ESCORT_GOAL_PLAYER_SPEW:
975 {
976 const auto &&egi = exists_in_mine(objp, objp->segnum, std::nullopt, std::nullopt, ESCORT_GOAL_PLAYER_SPEW, powerup_flags);
977 BuddyState.Escort_goal_objidx = egi.first;
978 BuddyState.Escort_goal_reachable = egi.second;
979 if (egi.second != d_unique_buddy_state::Escort_goal_reachability::unreachable)
980 {
981 auto &o = *vcobjptr(egi.first);
982 goal_seg = o.segnum;
983 }
984 }
985 break;
986 case ESCORT_GOAL_BOSS: {
987 const auto boss_id = get_boss_id();
988 assert(boss_id != robot_id::None);
989 goal_seg = escort_get_goal_segment(objp, OBJ_ROBOT, underlying_value(boss_id), powerup_flags);
990 break;
991 }
992 default:
993 Int3(); // Oops, Illegal value in Escort_goal_object.
994 con_printf(CON_URGENT, "BUG: buddy goal is %.8x, resetting to SCRAM", Escort_goal_object);
995 BuddyState.Escort_goal_object = ESCORT_GOAL_SCRAM;
996 [[fallthrough]];
997 case ESCORT_GOAL_SCRAM:
998 set_escort_goal_non_object(BuddyState);
999 create_n_segment_path(objp, robptr, 16 + d_rand() * 16, segment_none);
1000 aip->path_length = polish_path(objp, &Point_segs[aip->hide_index], aip->path_length);
1001 ailp->mode = ai_mode::AIM_GOTO_OBJECT;
1002 say_escort_goal(Escort_goal_object);
1003 return;
1004 }
1005 }
1006 const auto Escort_goal_objidx = BuddyState.Escort_goal_objidx;
1007 if (BuddyState.Escort_goal_reachable == d_unique_buddy_state::Escort_goal_reachability::unreachable)
1008 {
1009 if (Escort_goal_objidx == object_none) {
1010 escort_goal_does_not_exist(Escort_goal_object);
1011 } else {
1012 escort_goal_unreachable(Escort_goal_object);
1013 }
1014 } else {
1015 escort_go_to_goal(objp, robptr, aip, goal_seg);
1016 ailp->mode = ai_mode::AIM_GOTO_OBJECT;
1017 say_escort_goal(Escort_goal_object);
1018 }
1019 }
1020
1021 // -----------------------------------------------------------------------------
1022 // Escort robot chooses goal object based on player's keys, location.
1023 // Returns goal object.
1024 static escort_goal_t escort_set_goal_object(const object &Buddy_objp, const player_flags pl_flags)
1025 {
1026 auto &Boss_teleport_segs = LevelSharedBossState.Teleport_segs;
1027 auto &BuddyState = LevelUniqueObjectState.BuddyState;
1028 auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState;
1029 auto &Objects = LevelUniqueObjectState.Objects;
1030 if (BuddyState.Escort_special_goal != ESCORT_GOAL_UNSPECIFIED)
1031 return ESCORT_GOAL_UNSPECIFIED;
1032
1033 auto &plr = get_player_controlling_guidebot(BuddyState, Players);
1034 if (plr.objnum == object_none)
1035 /* should never happen */
1036 return ESCORT_GOAL_UNSPECIFIED;
1037 auto &plrobj = *Objects.vcptr(plr.objnum);
1038 if (plrobj.type != OBJ_PLAYER)
1039 return ESCORT_GOAL_UNSPECIFIED;
1040
1041 const auto need_key_and_key_exists = [&Buddy_objp, pl_flags, start_search_seg = plrobj.segnum](const PLAYER_FLAG flag_key, const powerup_type_t powerup_key) {
1042 if (pl_flags & flag_key)
1043 /* Player already has this key, so no need to get it again.
1044 */
1045 return false;
1046 const auto &&e = exists_in_mine(Buddy_objp, start_search_seg, OBJ_POWERUP, underlying_value(powerup_key), -1, pl_flags);
1047 /* For compatibility with classic Descent 2, test only whether
1048 * the key exists, but ignore whether it can be reached by the
1049 * guide bot.
1050 */
1051 return e.first != object_none;
1052 };
1053 if (need_key_and_key_exists(PLAYER_FLAGS_BLUE_KEY, powerup_type_t::POW_KEY_BLUE))
1054 return ESCORT_GOAL_BLUE_KEY;
1055 else if (need_key_and_key_exists(PLAYER_FLAGS_GOLD_KEY, powerup_type_t::POW_KEY_GOLD))
1056 return ESCORT_GOAL_GOLD_KEY;
1057 else if (need_key_and_key_exists(PLAYER_FLAGS_RED_KEY, powerup_type_t::POW_KEY_RED))
1058 return ESCORT_GOAL_RED_KEY;
1059 else if (LevelUniqueControlCenterState.Control_center_destroyed == 0)
1060 {
1061 if (!Boss_teleport_segs.empty())
1062 return ESCORT_GOAL_BOSS;
1063 else
1064 return ESCORT_GOAL_CONTROLCEN;
1065 } else
1066 return ESCORT_GOAL_EXIT;
1067 }
1068
1069 // -----------------------------------------------------------------------------
1070 static const player *time_to_visit_player(const d_level_unique_object_state &LevelUniqueObjectState, const object &buddy_object)
1071 {
1072 auto &BuddyState = LevelUniqueObjectState.BuddyState;
1073 auto &plr = get_player_controlling_guidebot(BuddyState, Players);
1074 if (plr.objnum == object_none)
1075 /* should never happen */
1076 return nullptr;
1077 auto &Objects = LevelUniqueObjectState.Objects;
1078 auto &plrobj = *Objects.vcptr(plr.objnum);
1079 if (plrobj.type != OBJ_PLAYER)
1080 return nullptr;
1081 // Note: This one has highest priority because, even if already going towards player,
1082 // might be necessary to create a new path, as player can move.
1083 if (GameTime64 - BuddyState.Buddy_last_seen_player > MAX_ESCORT_TIME_AWAY)
1084 if (GameTime64 - BuddyState.Buddy_last_player_path_created > F1_0)
1085 return &plr;
1086
1087 auto &ais = buddy_object.ctype.ai_info;
1088 if (ais.ail.mode == ai_mode::AIM_GOTO_PLAYER)
1089 return nullptr;
1090
1091 if (ais.cur_path_index < ais.path_length / 2)
1092 return nullptr;
1093
1094 if (buddy_object.segnum == plrobj.segnum)
1095 return nullptr;
1096 return &plr;
1097 }
1098
1099 // -----------------------------------------------------------------------------
1100 static void bash_buddy_weapon_info(d_unique_buddy_state &BuddyState, fvmobjptridx &vmobjptridx, object &weapon_obj)
1101 {
1102 auto &plr = get_player_controlling_guidebot(BuddyState, Players);
1103 if (plr.objnum == object_none)
1104 /* should never happen */
1105 return;
1106 /* Buddy can still fire while player is dead, so skip check for
1107 * plrobj.type */
1108 auto &&plrobj = vmobjptridx(plr.objnum);
1109 auto &laser_info = weapon_obj.ctype.laser_info;
1110 laser_info.parent_num = plrobj;
1111 laser_info.parent_type = OBJ_PLAYER;
1112 laser_info.parent_signature = plrobj->signature;
1113 }
1114
1115 // -----------------------------------------------------------------------------
1116 static int maybe_buddy_fire_mega(const vmobjptridx_t objp, const vcobjptridx_t Buddy_objp)
1117 {
1118 auto &BuddyState = LevelUniqueObjectState.BuddyState;
1119 auto &Objects = LevelUniqueObjectState.Objects;
1120 const auto &&[dist, vec_to_robot] = vm_vec_normalize_quick_with_magnitude(vm_vec_build_sub(Buddy_objp->pos, objp->pos));
1121
1122 if (dist > F1_0*100)
1123 return 0;
1124
1125 const auto dot = vm_vec_build_dot(vec_to_robot, Buddy_objp->orient.fvec);
1126
1127 if (dot < F1_0/2)
1128 return 0;
1129
1130 if (!object_to_object_visibility(Buddy_objp, objp, FQ_TRANSWALL))
1131 return 0;
1132
1133 if (Weapon_info[weapon_id_type::MEGA_ID].render == weapon_info::render_type::laser) {
1134 con_puts(CON_VERBOSE, "Buddy can't fire mega (shareware)");
1135 buddy_message_str("CLICK!");
1136 return 0;
1137 }
1138
1139 buddy_message_str("GAHOOGA!");
1140
1141 auto &buddy = *Buddy_objp;
1142 const imobjptridx_t weapon_objnum = Laser_create_new_easy(LevelSharedRobotInfoState.Robot_info, buddy.orient.fvec, buddy.pos, objp, weapon_id_type::MEGA_ID, weapon_sound_flag::audible);
1143
1144 if (weapon_objnum != object_none)
1145 bash_buddy_weapon_info(BuddyState, Objects.vmptridx, weapon_objnum);
1146
1147 return 1;
1148 }
1149
1150 //-----------------------------------------------------------------------------
1151 static int maybe_buddy_fire_smart(const vmobjptridx_t objp, const vcobjptridx_t buddy_objp)
1152 {
1153 auto &BuddyState = LevelUniqueObjectState.BuddyState;
1154 auto &Objects = LevelUniqueObjectState.Objects;
1155 fix dist;
1156
1157 dist = vm_vec_dist_quick(buddy_objp->pos, objp->pos);
1158
1159 if (dist > F1_0*80)
1160 return 0;
1161
1162 if (!object_to_object_visibility(buddy_objp, objp, FQ_TRANSWALL))
1163 return 0;
1164
1165 buddy_message_str("WHAMMO!");
1166
1167 const imobjptridx_t weapon_objnum = Laser_create_new_easy(LevelSharedRobotInfoState.Robot_info, buddy_objp->orient.fvec, buddy_objp->pos, objp, weapon_id_type::SMART_ID, weapon_sound_flag::audible);
1168
1169 if (weapon_objnum != object_none)
1170 bash_buddy_weapon_info(BuddyState, Objects.vmptridx, weapon_objnum);
1171
1172 return 1;
1173 }
1174
1175 // -----------------------------------------------------------------------------
1176 static void do_buddy_dude_stuff(const vmobjptridx_t buddy_objp)
1177 {
1178 auto &Objects = LevelUniqueObjectState.Objects;
1179 auto &BuddyState = LevelUniqueObjectState.BuddyState;
1180 auto &vmobjptridx = Objects.vmptridx;
1181 if (!ok_for_buddy_to_talk())
1182 return;
1183
1184 if (BuddyState.Buddy_last_missile_time + F1_0*2 < GameTime64) {
1185 // See if a robot potentially in view cone
1186 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
1187 const std::ranges::subrange rh{vmobjptridx};
1188 range_for (const auto &&objp, rh)
1189 {
1190 if ((objp->type == OBJ_ROBOT) && !Robot_info[get_robot_id(objp)].companion)
1191 if (maybe_buddy_fire_mega(objp, buddy_objp)) {
1192 BuddyState.Buddy_last_missile_time = {GameTime64};
1193 return;
1194 }
1195 }
1196
1197 // See if a robot near enough that buddy should fire smart missile
1198 range_for (const auto &&objp, rh)
1199 {
1200 if ((objp->type == OBJ_ROBOT) && !Robot_info[get_robot_id(objp)].companion)
1201 if (maybe_buddy_fire_smart(objp, buddy_objp)) {
1202 BuddyState.Buddy_last_missile_time = {GameTime64};
1203 return;
1204 }
1205 }
1206 }
1207 }
1208
1209 static void escort_set_goal_toward_controlling_player(d_unique_buddy_state &BuddyState, fvcobjptr &vcobjptr, const vmobjptridx_t buddy_obj, const robot_info &robptr)
1210 {
1211 auto &aip = buddy_obj->ctype.ai_info;
1212 aip.path_length = polish_path(buddy_obj, &Point_segs[aip.hide_index], aip.path_length);
1213 if (aip.path_length < 3) {
1214 auto &plr = get_player_controlling_guidebot(BuddyState, Players);
1215 if (plr.objnum != object_none)
1216 {
1217 auto &plrobj = *vcobjptr(plr.objnum);
1218 if (plrobj.type == OBJ_PLAYER)
1219 create_n_segment_path(buddy_obj, robptr, 5, plrobj.segnum);
1220 }
1221 }
1222 aip.ail.mode = ai_mode::AIM_GOTO_OBJECT;
1223 }
1224
1225 }
1226
1227 // -----------------------------------------------------------------------------
1228 // Called every frame (or something).
1229 void do_escort_frame(const vmobjptridx_t objp, const robot_info &robptr, const object &plrobj, const player_visibility_state player_visibility)
1230 {
1231 auto &BuddyState = LevelUniqueObjectState.BuddyState;
1232 auto &Objects = LevelUniqueObjectState.Objects;
1233 auto &vcobjptr = Objects.vcptr;
1234 const auto dist_to_player = vm_vec_dist_quick(plrobj.pos, objp->pos);
1235 ai_static *aip = &objp->ctype.ai_info;
1236 ai_local *ailp = &objp->ctype.ai_info.ail;
1237
1238 auto &player_info = plrobj.ctype.player_info;
1239 if (player_is_visible(player_visibility))
1240 {
1241 BuddyState.Buddy_last_seen_player = {GameTime64};
1242 if (player_info.powerup_flags & PLAYER_FLAGS_HEADLIGHT_ON) // DAMN! MK, stupid bug, fixed 12/08/95, changed PLAYER_FLAGS_HEADLIGHT to PLAYER_FLAGS_HEADLIGHT_ON
1243 {
1244 const auto energy = player_info.energy;
1245 const auto ienergy = f2i(energy);
1246 if (ienergy < 40)
1247 if (ienergy & 4)
1248 buddy_message_str("Hey, your headlight's on!");
1249 }
1250 }
1251
1252 if (cheats.buddyangry)
1253 do_buddy_dude_stuff(objp);
1254
1255 {
1256 const auto buddy_sorry_time = BuddyState.Buddy_sorry_time;
1257 if (buddy_sorry_time + F1_0 > GameTime64)
1258 {
1259 BuddyState.Buddy_sorry_time = -F1_0*2;
1260 if (buddy_sorry_time < GameTime64 + F1_0*2)
1261 {
1262 buddy_message_ignore_time("Oops, sorry 'bout that...");
1263 }
1264 }
1265 }
1266
1267 // If buddy not allowed to talk, then he is locked in his room. Make him mostly do nothing unless you're nearby.
1268 if (!BuddyState.Buddy_allowed_to_talk)
1269 if (dist_to_player > F1_0*100)
1270 aip->SKIP_AI_COUNT = (F1_0/4)/FrameTime;
1271
1272 // ai_mode::AIM_WANDER has been co-opted for buddy behavior (didn't want to modify aistruct.h)
1273 // It means the object has been told to get lost and has come to the end of its path.
1274 // If the player is now visible, then create a path.
1275 if (ailp->mode == ai_mode::AIM_WANDER)
1276 if (player_is_visible(player_visibility))
1277 {
1278 create_n_segment_path(objp, robptr, 16 + d_rand() * 16, segment_none);
1279 aip->path_length = polish_path(objp, &Point_segs[aip->hide_index], aip->path_length);
1280 }
1281
1282 if (BuddyState.Escort_special_goal == ESCORT_GOAL_SCRAM) {
1283 auto &plr = get_player_controlling_guidebot(BuddyState, Players);
1284 if (plr.objnum == object_none)
1285 return;
1286 auto &plrobj = *Objects.vcptr(plr.objnum);
1287 if (plrobj.type != OBJ_PLAYER)
1288 return;
1289 if (player_is_visible(player_visibility))
1290 if (BuddyState.Escort_last_path_created + F1_0*3 < GameTime64) {
1291 BuddyState.Escort_last_path_created = {GameTime64};
1292 create_n_segment_path(objp, robptr, 10 + d_rand() * 16, plrobj.segnum);
1293 }
1294
1295 return;
1296 }
1297
1298 // Force checking for new goal every 5 seconds, and create new path, if necessary.
1299 if (BuddyState.Escort_last_path_created + (BuddyState.Escort_special_goal != ESCORT_GOAL_SCRAM ? (F1_0 * 5) : (F1_0 * 15)) < GameTime64)
1300 {
1301 BuddyState.Escort_goal_object = ESCORT_GOAL_UNSPECIFIED;
1302 BuddyState.Escort_last_path_created = {GameTime64};
1303 }
1304
1305 const player *guidebot_controller_player;
1306 if (BuddyState.Escort_special_goal != ESCORT_GOAL_SCRAM && (guidebot_controller_player = time_to_visit_player(LevelUniqueObjectState, objp)))
1307 {
1308 unsigned max_len;
1309 BuddyState.Buddy_last_player_path_created = {GameTime64};
1310 ailp->mode = ai_mode::AIM_GOTO_PLAYER;
1311 if (!player_is_visible(player_visibility))
1312 {
1313 if (BuddyState.Last_come_back_message_time + F1_0 < GameTime64)
1314 {
1315 BuddyState.Last_come_back_message_time = {GameTime64};
1316 auto &local_player = *Players.vcptr(Player_num);
1317 if (guidebot_controller_player == &local_player)
1318 buddy_message_str("Coming back to get you.");
1319 else
1320 buddy_message("Going back to get %s.", guidebot_controller_player->callsign.operator const char *());
1321 }
1322 }
1323 // No point in Buddy creating very long path if he's not allowed to talk. Really kills framerate.
1324 max_len = Max_escort_length;
1325 if (!BuddyState.Buddy_allowed_to_talk)
1326 max_len = 3;
1327 create_path_to_segment(objp, robptr, max_len, create_path_safety_flag::safe, Believed_player_seg); // MK!: Last parm used to be 1!
1328 aip->path_length = polish_path(objp, &Point_segs[aip->hide_index], aip->path_length);
1329 ailp->mode = ai_mode::AIM_GOTO_PLAYER;
1330 }
1331 else if (GameTime64 - BuddyState.Buddy_last_seen_player > MAX_ESCORT_TIME_AWAY)
1332 {
1333 // This is to prevent buddy from looking for a goal, which he will do because we only allow path creation once/second.
1334 return;
1335 } else if ((ailp->mode == ai_mode::AIM_GOTO_PLAYER) && (dist_to_player < MIN_ESCORT_DISTANCE)) {
1336 BuddyState.Escort_goal_object = escort_set_goal_object(objp, player_info.powerup_flags);
1337 ailp->mode = ai_mode::AIM_GOTO_OBJECT; // May look stupid to be before path creation, but ai_door_is_openable uses mode to determine what doors can be got through
1338 escort_create_path_to_goal(objp, robptr, player_info);
1339 escort_set_goal_toward_controlling_player(BuddyState, vcobjptr, objp, robptr);
1340 }
1341 else if (BuddyState.Escort_goal_object == ESCORT_GOAL_UNSPECIFIED)
1342 {
1343 if ((ailp->mode != ai_mode::AIM_GOTO_PLAYER) || (dist_to_player < MIN_ESCORT_DISTANCE)) {
1344 BuddyState.Escort_goal_object = escort_set_goal_object(objp, player_info.powerup_flags);
1345 ailp->mode = ai_mode::AIM_GOTO_OBJECT; // May look stupid to be before path creation, but ai_door_is_openable uses mode to determine what doors can be got through
1346 escort_create_path_to_goal(objp, robptr, player_info);
1347 escort_set_goal_toward_controlling_player(BuddyState, vcobjptr, objp, robptr);
1348 }
1349 }
1350 }
1351
1352 void invalidate_escort_goal(d_unique_buddy_state &BuddyState)
1353 {
1354 BuddyState.Escort_goal_object = ESCORT_GOAL_UNSPECIFIED;
1355 }
1356
1357 // -------------------------------------------------------------------------------------------------
1358 void do_snipe_frame(const vmobjptridx_t objp, const robot_info &robptr, const fix dist_to_player, const player_visibility_state player_visibility, const vms_vector &vec_to_player)
1359 {
1360 ai_local *ailp = &objp->ctype.ai_info.ail;
1361 fix connected_distance;
1362
1363 if (dist_to_player > F1_0*500)
1364 return;
1365
1366 switch (ailp->mode) {
1367 case ai_mode::AIM_SNIPE_WAIT:
1368 if ((dist_to_player > F1_0*50) && (ailp->next_action_time > 0))
1369 return;
1370
1371 ailp->next_action_time = SNIPE_WAIT_TIME;
1372
1373 connected_distance = find_connected_distance(objp->pos, vmsegptridx(objp->segnum), Believed_player_pos, vmsegptridx(Believed_player_seg), 30, wall_is_doorway_mask::fly);
1374 if (connected_distance < F1_0*500) {
1375 create_path_to_believed_player_segment(objp, robptr, 30, create_path_safety_flag::safe);
1376 ailp->mode = ai_mode::AIM_SNIPE_ATTACK;
1377 ailp->next_action_time = SNIPE_ATTACK_TIME; // have up to 10 seconds to find player.
1378 }
1379 break;
1380
1381 case ai_mode::AIM_SNIPE_RETREAT:
1382 case ai_mode::AIM_SNIPE_RETREAT_BACKWARDS:
1383 if (ailp->next_action_time < 0) {
1384 ailp->mode = ai_mode::AIM_SNIPE_WAIT;
1385 ailp->next_action_time = SNIPE_WAIT_TIME;
1386 }
1387 else if (player_visibility == player_visibility_state::no_line_of_sight || ailp->next_action_time > SNIPE_ABORT_RETREAT_TIME)
1388 {
1389 ai_follow_path(LevelSharedRobotInfoState.Robot_info, objp, player_visibility, &vec_to_player);
1390 ailp->mode = ai_mode::AIM_SNIPE_RETREAT_BACKWARDS;
1391 } else {
1392 ailp->mode = ai_mode::AIM_SNIPE_FIRE;
1393 ailp->next_action_time = SNIPE_FIRE_TIME/2;
1394 }
1395 break;
1396
1397 case ai_mode::AIM_SNIPE_ATTACK:
1398 if (ailp->next_action_time < 0) {
1399 ailp->mode = ai_mode::AIM_SNIPE_RETREAT;
1400 ailp->next_action_time = SNIPE_WAIT_TIME;
1401 } else {
1402 ai_follow_path(LevelSharedRobotInfoState.Robot_info, objp, player_visibility, &vec_to_player);
1403 if (player_is_visible(player_visibility))
1404 {
1405 ailp->mode = ai_mode::AIM_SNIPE_FIRE;
1406 ailp->next_action_time = SNIPE_FIRE_TIME;
1407 } else
1408 ailp->mode = ai_mode::AIM_SNIPE_ATTACK;
1409 }
1410 break;
1411
1412 case ai_mode::AIM_SNIPE_FIRE:
1413 if (ailp->next_action_time < 0) {
1414 ai_static *aip = &objp->ctype.ai_info;
1415 create_n_segment_path(objp, robptr, 10 + d_rand()/2048, ConsoleObject->segnum);
1416 aip->path_length = polish_path(objp, &Point_segs[aip->hide_index], aip->path_length);
1417 if (d_rand() < 8192)
1418 ailp->mode = ai_mode::AIM_SNIPE_RETREAT_BACKWARDS;
1419 else
1420 ailp->mode = ai_mode::AIM_SNIPE_RETREAT;
1421 ailp->next_action_time = SNIPE_RETREAT_TIME;
1422 } else {
1423 }
1424 break;
1425
1426 default:
1427 Int3(); // Oops, illegal mode for snipe behavior.
1428 ailp->mode = ai_mode::AIM_SNIPE_ATTACK;
1429 ailp->next_action_time = F1_0;
1430 break;
1431 }
1432
1433 }
1434
1435 static fix64 Re_init_thief_time = 0x3f000000;
1436
1437 // ----------------------------------------------------------------------
1438 void recreate_thief(const d_robot_info_array &Robot_info, const robot_id thief_id)
1439 {
1440 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
1441 auto &Vertices = LevelSharedVertexState.get_vertices();
1442 const auto segnum = choose_thief_recreation_segment(vcsegptr, LevelUniqueWallSubsystemState.Walls.vcptr, ConsoleObject->segnum);
1443 const auto &&segp = vmsegptridx(segnum);
1444 auto &vcvertptr = Vertices.vcptr;
1445 const auto center_point{compute_segment_center(vcvertptr, segp)};
1446
1447 const auto &&new_obj = create_morph_robot(Robot_info, segp, center_point, thief_id);
1448 if (new_obj == object_none)
1449 return;
1450 Re_init_thief_time = GameTime64 + F1_0*10; // In 10 seconds, re-initialize thief.
1451 }
1452
1453 // ----------------------------------------------------------------------------
1454
1455 constexpr enumerated_array<fix, NDL, Difficulty_level_type> Thief_wait_times = {{{
1456 F1_0*30, F1_0*25, F1_0*20, F1_0*15, F1_0*10
1457 }}};
1458
1459 // -------------------------------------------------------------------------------------------------
1460 void do_thief_frame(const vmobjptridx_t objp, const robot_info &robptr, const fix dist_to_player, const player_visibility_state player_visibility, const vms_vector &vec_to_player)
1461 {
1462 static constexpr fix THIEF_ATTACK_TIME{F1_0 * 10};
1463 const auto Difficulty_level = GameUniqueState.Difficulty_level;
1464 ai_local *ailp = &objp->ctype.ai_info.ail;
1465 fix connected_distance;
1466
1467 if ((Current_level_num < 0) && (Re_init_thief_time < GameTime64)) {
1468 if (Re_init_thief_time > GameTime64 - F1_0*2)
1469 init_thief_for_level();
1470 Re_init_thief_time = 0x3f000000;
1471 }
1472
1473 if ((dist_to_player > F1_0*500) && (ailp->next_action_time > 0))
1474 return;
1475
1476 if (Player_dead_state != player_dead_state::no)
1477 ailp->mode = ai_mode::AIM_THIEF_RETREAT;
1478
1479 switch (ailp->mode) {
1480 case ai_mode::AIM_THIEF_WAIT:
1481 if (ailp->player_awareness_type >= player_awareness_type_t::PA_PLAYER_COLLISION) {
1482 ailp->player_awareness_type = player_awareness_type_t::PA_NONE;
1483 create_path_to_believed_player_segment(objp, robptr, 30, create_path_safety_flag::safe);
1484 ailp->mode = ai_mode::AIM_THIEF_ATTACK;
1485 ailp->next_action_time = THIEF_ATTACK_TIME/2;
1486 return;
1487 }
1488 else if (player_is_visible(player_visibility))
1489 {
1490 create_n_segment_path(objp, robptr, 15, ConsoleObject->segnum);
1491 ailp->mode = ai_mode::AIM_THIEF_RETREAT;
1492 return;
1493 }
1494
1495 if ((dist_to_player > F1_0*50) && (ailp->next_action_time > 0))
1496 return;
1497
1498 ailp->next_action_time = Thief_wait_times[Difficulty_level]/2;
1499
1500 connected_distance = find_connected_distance(objp->pos, vmsegptridx(objp->segnum), Believed_player_pos, vmsegptridx(Believed_player_seg), 30, wall_is_doorway_mask::fly);
1501 if (connected_distance < F1_0*500) {
1502 create_path_to_believed_player_segment(objp, robptr, 30, create_path_safety_flag::safe);
1503 ailp->mode = ai_mode::AIM_THIEF_ATTACK;
1504 ailp->next_action_time = THIEF_ATTACK_TIME; // have up to 10 seconds to find player.
1505 }
1506
1507 break;
1508
1509 case ai_mode::AIM_THIEF_RETREAT:
1510 if (ailp->next_action_time < 0) {
1511 ailp->mode = ai_mode::AIM_THIEF_WAIT;
1512 ailp->next_action_time = Thief_wait_times[Difficulty_level];
1513 }
1514 else if (dist_to_player < F1_0 * 100 || player_is_visible(player_visibility) || ailp->player_awareness_type >= player_awareness_type_t::PA_PLAYER_COLLISION)
1515 {
1516 ai_follow_path(LevelSharedRobotInfoState.Robot_info, objp, player_visibility, &vec_to_player);
1517 if ((dist_to_player < F1_0*100) || (ailp->player_awareness_type >= player_awareness_type_t::PA_PLAYER_COLLISION)) {
1518 ai_static *aip = &objp->ctype.ai_info;
1519 if (((aip->cur_path_index <=1) && (aip->PATH_DIR == -1)) || ((aip->cur_path_index >= aip->path_length-1) && (aip->PATH_DIR == 1))) {
1520 ailp->player_awareness_type = player_awareness_type_t::PA_NONE;
1521 create_n_segment_path(objp, robptr, 10, ConsoleObject->segnum);
1522
1523 // If path is real short, try again, allowing to go through player's segment
1524 if (aip->path_length < 4) {
1525 create_n_segment_path(objp, robptr, 10, segment_none);
1526 } else if (objp->shields* 4 < robptr.strength) {
1527 // If robot really low on hits, will run through player with even longer path
1528 if (aip->path_length < 8) {
1529 create_n_segment_path(objp, robptr, 10, segment_none);
1530 }
1531 }
1532
1533 ailp->mode = ai_mode::AIM_THIEF_RETREAT;
1534 }
1535 } else
1536 ailp->mode = ai_mode::AIM_THIEF_RETREAT;
1537
1538 }
1539
1540 break;
1541
1542 // This means the thief goes from wherever he is to the player.
1543 // Note: When thief successfully steals something, his action time is forced negative and his mode is changed
1544 // to retreat to get him out of attack mode.
1545 case ai_mode::AIM_THIEF_ATTACK:
1546 if (ailp->player_awareness_type >= player_awareness_type_t::PA_PLAYER_COLLISION) {
1547 ailp->player_awareness_type = player_awareness_type_t::PA_NONE;
1548 if (d_rand() > 8192) {
1549 create_n_segment_path(objp, robptr, 10, ConsoleObject->segnum);
1550 ailp->next_action_time = Thief_wait_times[Difficulty_level]/2;
1551 ailp->mode = ai_mode::AIM_THIEF_RETREAT;
1552 }
1553 } else if (ailp->next_action_time < 0) {
1554 // This forces him to create a new path every second.
1555 ailp->next_action_time = F1_0;
1556 create_path_to_believed_player_segment(objp, robptr, 100, create_path_safety_flag::unsafe);
1557 ailp->mode = ai_mode::AIM_THIEF_ATTACK;
1558 } else {
1559 if (player_is_visible(player_visibility) && dist_to_player < F1_0*100)
1560 {
1561 // If the player is close to looking at the thief, thief shall run away.
1562 // No more stupid thief trying to sneak up on you when you're looking right at him!
1563 if (dist_to_player > F1_0*60) {
1564 fix dot = vm_vec_build_dot(vec_to_player, ConsoleObject->orient.fvec);
1565 if (dot < -F1_0/2) { // Looking at least towards thief, so thief will run!
1566 create_n_segment_path(objp, robptr, 10, ConsoleObject->segnum);
1567 ailp->next_action_time = Thief_wait_times[Difficulty_level]/2;
1568 ailp->mode = ai_mode::AIM_THIEF_RETREAT;
1569 }
1570 }
1571 ai_turn_towards_vector(vec_to_player, objp, F1_0/4);
1572 move_towards_player(LevelSharedRobotInfoState.Robot_info, objp, vec_to_player);
1573 } else {
1574 ai_static *aip = &objp->ctype.ai_info;
1575 // If path length == 0, then he will keep trying to create path, but he is probably stuck in his closet.
1576 if ((aip->path_length > 1) || ((d_tick_count & 0x0f) == 0)) {
1577 ai_follow_path(LevelSharedRobotInfoState.Robot_info, objp, player_visibility, &vec_to_player);
1578 ailp->mode = ai_mode::AIM_THIEF_ATTACK;
1579 }
1580 }
1581 }
1582 break;
1583
1584 default:
1585 ailp->mode = ai_mode::AIM_THIEF_ATTACK;
1586 ailp->next_action_time = F1_0;
1587 break;
1588 }
1589
1590 }
1591
1592 namespace {
1593
1594 // ----------------------------------------------------------------------------
1595 // Return true if this item (whose presence is indicated by Players[player_num].flags) gets stolen.
1596 static int maybe_steal_flag_item(object &playerobj, const PLAYER_FLAG flagval)
1597 {
1598 auto &ThiefUniqueState = LevelUniqueObjectState.ThiefState;
1599 auto &plr_flags = playerobj.ctype.player_info.powerup_flags;
1600 if (plr_flags & flagval)
1601 {
1602 if (d_rand() < THIEF_PROBABILITY) {
1603 powerup_type_t powerup_index;
1604 const char *msg;
1605 plr_flags &= (~flagval);
1606 switch (flagval) {
1607 case PLAYER_FLAGS_INVULNERABLE:
1608 powerup_index = powerup_type_t::POW_INVULNERABILITY;
1609 msg = "Invulnerability stolen!";
1610 break;
1611 case PLAYER_FLAGS_CLOAKED:
1612 powerup_index = powerup_type_t::POW_CLOAK;
1613 msg = "Cloak stolen!";
1614 break;
1615 case PLAYER_FLAGS_MAP_ALL:
1616 powerup_index = powerup_type_t::POW_FULL_MAP;
1617 msg = "Full map stolen!";
1618 break;
1619 case PLAYER_FLAGS_QUAD_LASERS:
1620 update_laser_weapon_info();
1621 powerup_index = powerup_type_t::POW_QUAD_FIRE;
1622 msg = "Quad lasers stolen!";
1623 break;
1624 case PLAYER_FLAGS_AFTERBURNER:
1625 powerup_index = powerup_type_t::POW_AFTERBURNER;
1626 msg = "Afterburner stolen!";
1627 break;
1628 case PLAYER_FLAGS_CONVERTER:
1629 powerup_index = powerup_type_t::POW_CONVERTER;
1630 msg = "Converter stolen!";
1631 break;
1632 case PLAYER_FLAG::HEADLIGHT_PRESENT_AND_ON:
1633 powerup_index = powerup_type_t::POW_HEADLIGHT;
1634 msg = "Headlight stolen!";
1635 break;
1636 default:
1637 assert(false);
1638 return 0;
1639 }
1640 ThiefUniqueState.Stolen_items[ThiefUniqueState.Stolen_item_index] = powerup_index;
1641 thief_message_str(msg);
1642 return 1;
1643 }
1644 }
1645
1646 return 0;
1647 }
1648
1649 // ----------------------------------------------------------------------------
1650 static int maybe_steal_secondary_weapon(object &playerobj, const secondary_weapon_index_t weapon_num)
1651 {
1652 auto &ThiefUniqueState = LevelUniqueObjectState.ThiefState;
1653 auto &player_info = playerobj.ctype.player_info;
1654 if (auto &secondary_ammo = player_info.secondary_ammo[weapon_num])
1655 if (d_rand() < THIEF_PROBABILITY) {
1656 if (weapon_index_is_player_bomb(weapon_num))
1657 {
1658 if (d_rand() > 8192) // Come in groups of 4, only add 1/4 of time.
1659 return 0;
1660 }
1661 // Smart mines and proxbombs don't get dropped because they only come in 4 packs.
1662 else
1663 ThiefUniqueState.Stolen_items[ThiefUniqueState.Stolen_item_index] = Secondary_weapon_to_powerup[weapon_num];
1664 thief_message("%s stolen!", SECONDARY_WEAPON_NAMES(weapon_num)); // Danger! Danger! Use of literal! Danger!
1665 if (-- secondary_ammo == 0)
1666 auto_select_secondary_weapon(player_info);
1667
1668 // -- compress_stolen_items();
1669 return 1;
1670 }
1671
1672 return 0;
1673 }
1674
1675 // ----------------------------------------------------------------------------
1676 static int maybe_steal_primary_weapon(object &playerobj, const primary_weapon_index_t weapon_num)
1677 {
1678 auto &ThiefUniqueState = LevelUniqueObjectState.ThiefState;
1679 auto &player_info = playerobj.ctype.player_info;
1680 bool is_energy_weapon = true;
1681 switch (static_cast<primary_weapon_index_t>(weapon_num))
1682 {
1683 case primary_weapon_index_t::LASER_INDEX:
1684 if (player_info.laser_level == laser_level::_1)
1685 return 0;
1686 break;
1687 case primary_weapon_index_t::VULCAN_INDEX:
1688 case primary_weapon_index_t::GAUSS_INDEX:
1689 if (!player_info.vulcan_ammo)
1690 return 0;
1691 is_energy_weapon = false;
1692 [[fallthrough]];
1693 default:
1694 if (!(player_info.primary_weapon_flags & HAS_PRIMARY_FLAG(weapon_num)))
1695 return 0;
1696 break;
1697 }
1698 if (is_energy_weapon)
1699 {
1700 if ((
1701 #if DXX_USE_MULTIPLAYER
1702 (Game_mode & GM_MULTI)
1703 ? Netgame.ThiefModifierFlags
1704 :
1705 #endif
1706 PlayerCfg.ThiefModifierFlags) & ThiefModifier::NoEnergyWeapons)
1707 return 0;
1708 }
1709 {
1710 if (d_rand() < THIEF_PROBABILITY) {
1711 powerup_type_t primary_weapon_powerup;
1712 if (weapon_num == primary_weapon_index_t::LASER_INDEX)
1713 {
1714 auto &laser_level = player_info.laser_level;
1715 primary_weapon_powerup = (laser_level > MAX_LASER_LEVEL)
1716 ? powerup_type_t::POW_SUPER_LASER
1717 : Primary_weapon_to_powerup[weapon_num];
1718 /* Laser levels are zero-based, so print the old
1719 * level, then decrement it. Decrementing first
1720 * would produce confusing output, particularly when
1721 * the user loses the final laser powerup and drops
1722 * to level 0 lasers.
1723 */
1724 const unsigned l = static_cast<unsigned>(laser_level);
1725 -- laser_level;
1726 thief_message("%s level decreased to %u!", PRIMARY_WEAPON_NAMES(weapon_num), l);
1727 }
1728 else
1729 {
1730 player_info.primary_weapon_flags &= ~HAS_PRIMARY_FLAG(weapon_num);
1731 primary_weapon_powerup = Primary_weapon_to_powerup[weapon_num];
1732
1733 thief_message("%s stolen!", PRIMARY_WEAPON_NAMES(weapon_num)); // Danger! Danger! Use of literal! Danger!
1734 auto_select_primary_weapon(player_info);
1735 }
1736 ThiefUniqueState.Stolen_items[ThiefUniqueState.Stolen_item_index] = primary_weapon_powerup;
1737 return 1;
1738 }
1739 }
1740
1741 return 0;
1742 }
1743
1744 // ----------------------------------------------------------------------------
1745 // Called for a thief-type robot.
1746 // If a item successfully stolen, returns true, else returns false.
1747 // If a wapon successfully stolen, do everything, removing it from player,
1748 // updating Stolen_items information, deselecting, etc.
1749 static int attempt_to_steal_item_3(object &thief, object &player_num)
1750 {
1751 (void)thief;
1752 // First, try to steal equipped items.
1753
1754 if (auto r = maybe_steal_flag_item(player_num, PLAYER_FLAGS_INVULNERABLE))
1755 return r;
1756
1757 // If primary weapon = laser, first try to rip away those nasty quad lasers!
1758 const auto Primary_weapon = player_num.ctype.player_info.Primary_weapon;
1759 if (Primary_weapon == primary_weapon_index_t::LASER_INDEX)
1760 if (auto r = maybe_steal_flag_item(player_num, PLAYER_FLAGS_QUAD_LASERS))
1761 return r;
1762
1763 // Makes it more likely to steal primary than secondary.
1764 range_for (const int i, xrange(2u))
1765 {
1766 (void)i;
1767 if (auto r = maybe_steal_primary_weapon(player_num, Primary_weapon))
1768 return r;
1769 }
1770
1771 if (auto r = maybe_steal_secondary_weapon(player_num, player_num.ctype.player_info.Secondary_weapon))
1772 return r;
1773
1774 // See what the player has and try to snag something.
1775 // Try best things first.
1776 if (auto r = maybe_steal_flag_item(player_num, PLAYER_FLAGS_INVULNERABLE))
1777 return r;
1778 if (auto r = maybe_steal_flag_item(player_num, PLAYER_FLAGS_CLOAKED))
1779 return r;
1780 if (auto r = maybe_steal_flag_item(player_num, PLAYER_FLAGS_QUAD_LASERS))
1781 return r;
1782 if (auto r = maybe_steal_flag_item(player_num, PLAYER_FLAGS_AFTERBURNER))
1783 return r;
1784 if (auto r = maybe_steal_flag_item(player_num, PLAYER_FLAGS_CONVERTER))
1785 return r;
1786 // -- if (maybe_steal_flag_item(player_num, PLAYER_FLAGS_AMMO_RACK)) // Can't steal because what if have too many items, say 15 homing missiles?
1787 // -- return 1;
1788 if (auto r = maybe_steal_flag_item(player_num, PLAYER_FLAG::HEADLIGHT_PRESENT_AND_ON))
1789 return r;
1790 if (auto r = maybe_steal_flag_item(player_num, PLAYER_FLAGS_MAP_ALL))
1791 return r;
1792
1793 for (int i=MAX_SECONDARY_WEAPONS-1; i>=0; i--) {
1794 if (auto r = maybe_steal_primary_weapon(player_num, static_cast<primary_weapon_index_t>(i)))
1795 return r;
1796 if (auto r = maybe_steal_secondary_weapon(player_num, static_cast<secondary_weapon_index_t>(i)))
1797 return r;
1798 }
1799
1800 return 0;
1801 }
1802
1803 // ----------------------------------------------------------------------------
1804 static int attempt_to_steal_item_2(object &thief, object &player_num)
1805 {
1806 auto &ThiefUniqueState = LevelUniqueObjectState.ThiefState;
1807 const auto rval = attempt_to_steal_item_3(thief, player_num);
1808 if (rval) {
1809 digi_play_sample_once(sound_effect::SOUND_WEAPON_STOLEN, F1_0);
1810 auto i = ThiefUniqueState.Stolen_item_index;
1811 if (d_rand() > 20000) // Occasionally, boost the value again
1812 ++i;
1813 constexpr auto size{std::tuple_size<decltype(ThiefUniqueState.Stolen_items)>::value};
1814 if (++ i >= size)
1815 i -= size;
1816 ThiefUniqueState.Stolen_item_index = i;
1817 }
1818 return rval;
1819 }
1820
1821 }
1822
1823 // ----------------------------------------------------------------------------
1824 // Called for a thief-type robot.
1825 // If a item successfully stolen, returns true, else returns false.
1826 // If a wapon successfully stolen, do everything, removing it from player,
1827 // updating Stolen_items information, deselecting, etc.
1828 void attempt_to_steal_item(const vmobjptridx_t thiefp, const robot_info &robptr, object &player_num)
1829 {
1830 int rval{0};
1831
1832 auto &thief = *thiefp;
1833 if (thief.ctype.ai_info.dying_start_time)
1834 return;
1835
1836 if (thief.ctype.ai_info.ail.mode == ai_mode::AIM_THIEF_ATTACK)
1837 {
1838 for (auto i = 4u;; i--)
1839 {
1840 rval += attempt_to_steal_item_2(thief, player_num);
1841 if (!i)
1842 break;
1843 if (rval && !(d_rand() < 11000)) { // about 1/3 of time, steal another item
1844 break;
1845 }
1846 }
1847 }
1848 create_n_segment_path(thiefp, robptr, 10, ConsoleObject->segnum);
1849 auto &ailp = thief.ctype.ai_info.ail;
1850 ailp.next_action_time = Thief_wait_times[GameUniqueState.Difficulty_level] / 2;
1851 ailp.mode = ai_mode::AIM_THIEF_RETREAT;
1852 if (rval) {
1853 PALETTE_FLASH_ADD(30, 15, -20);
1854 if (Game_mode & GM_NETWORK)
1855 multi_send_stolen_items();
1856 }
1857 }
1858
1859 // --------------------------------------------------------------------------------------------------------------
1860 // Indicate no items have been stolen.
1861 void init_thief_for_level(void)
1862 {
1863 auto &ThiefUniqueState = LevelUniqueObjectState.ThiefState;
1864 ThiefUniqueState.Stolen_item_index = 0;
1865 auto &Stolen_items = ThiefUniqueState.Stolen_items;
1866 Stolen_items.fill(ThiefUniqueState.stolen_item_type_none);
1867
1868 constexpr unsigned iterations{3};
1869 static_assert (std::tuple_size<decltype(ThiefUniqueState.Stolen_items)>::value >= iterations * 2, "Stolen_items too small"); // Oops! Loop below will overwrite memory!
1870 if (!(Game_mode & GM_MULTI))
1871 for (unsigned i = 0; i < iterations; i++)
1872 {
1873 Stolen_items[2*i] = powerup_type_t::POW_SHIELD_BOOST;
1874 Stolen_items[2*i+1] = powerup_type_t::POW_ENERGY;
1875 }
1876 }
1877
1878 // --------------------------------------------------------------------------------------------------------------
1879 void drop_stolen_items_local(d_level_unique_object_state &LevelUniqueObjectState, const d_level_shared_segment_state &LevelSharedSegmentState, d_level_unique_segment_state &LevelUniqueSegmentState, const d_vclip_array &Vclip, const vmsegptridx_t segp, const vms_vector &thief_velocity, const vms_vector &thief_position, d_thief_unique_state::stolen_item_storage &Stolen_items)
1880 {
1881 for (auto &i : Stolen_items)
1882 {
1883 if (i != d_thief_unique_state::stolen_item_type_none)
1884 drop_powerup(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, Vclip, std::exchange(i, d_thief_unique_state::stolen_item_type_none), thief_velocity, thief_position, segp, true);
1885 }
1886 }
1887
1888 // --------------------------------------------------------------------------------------------------------------
1889 namespace {
1890
1891 struct escort_menu_items
1892 {
1893 std::array<char, 300> msg;
1894 const unsigned border_x, border_y;
1895 escort_menu_items(int next_goal, const char *Buddy_messages_suppressed, grs_canvas &src, gr_string_size &);
1896 };
1897
1898 struct escort_menu : escort_menu_items, window
1899 {
1900 escort_menu(int next_goal, const char *Buddy_messages_suppressed, grs_canvas &src, gr_string_size = {});
1901 virtual window_event_result event_handler(const d_event &) override;
1902 static window_event_result event_key_command(const d_event &event);
1903 void show_escort_menu();
1904 };
1905
1906 escort_menu::escort_menu(int next_goal, const char *Buddy_messages_suppressed, grs_canvas &src, gr_string_size text_size) :
1907 escort_menu_items(next_goal, Buddy_messages_suppressed, src, text_size),
1908 window(src, ((src.cv_bitmap.bm_w - text_size.width) / 2) - border_x, ((src.cv_bitmap.bm_h - text_size.height) / 2) - border_y, text_size.width + (border_x * 2), text_size.height + (border_y * 2))
1909 {
1910 gr_set_fontcolor(w_canv, BM_XRGB(0, 28, 0), -1);
1911 }
1912
1913 window_event_result escort_menu::event_key_command(const d_event &event)
1914 {
1915 auto &BuddyState = LevelUniqueObjectState.BuddyState;
1916 switch (const auto key = event_key_get(event))
1917 {
1918 case KEY_0:
1919 case KEY_1:
1920 case KEY_2:
1921 case KEY_3:
1922 case KEY_4:
1923 case KEY_5:
1924 case KEY_6:
1925 case KEY_7:
1926 case KEY_8:
1927 case KEY_9:
1928 BuddyState.Looking_for_marker = game_marker_index::None;
1929 BuddyState.Last_buddy_key = -1;
1930 set_escort_special_goal(BuddyState, key);
1931 BuddyState.Last_buddy_key = -1;
1932 return window_event_result::close;
1933 case KEY_ESC:
1934 case KEY_ENTER:
1935 return window_event_result::close;
1936 case KEY_T: {
1937 const auto temp = std::exchange(BuddyState.Buddy_messages_suppressed, 0);
1938 buddy_message("Messages %s.", temp ? "enabled" : "suppressed");
1939 BuddyState.Buddy_messages_suppressed = ~temp;
1940 return window_event_result::close;
1941 }
1942
1943 default:
1944 break;
1945 }
1946 return window_event_result::ignored;
1947 }
1948
1949 window_event_result escort_menu::event_handler(const d_event &event)
1950 {
1951 switch (event.type)
1952 {
1953 case event_type::window_activated:
1954 game_flush_inputs(Controls);
1955 break;
1956 case event_type::key_command:
1957 return event_key_command(event);
1958 case event_type::idle:
1959 timer_delay2(50);
1960 break;
1961 case event_type::window_draw:
1962 show_escort_menu();
1963 break;
1964 case event_type::window_close:
1965 return window_event_result::ignored; // continue closing
1966 default:
1967 return window_event_result::ignored;
1968 }
1969 return window_event_result::handled;
1970 }
1971
1972 // -------------------------------------------------------------------------------
1973 // Show the Buddy menu!
1974 void escort_menu::show_escort_menu()
1975 {
1976 nm_draw_background(w_canv, 0, 0, w_canv.cv_bitmap.bm_w, w_canv.cv_bitmap.bm_h);
1977 gr_ustring(w_canv, *GAME_FONT, border_x, border_y, msg.data());
1978 }
1979
1980 }
1981
1982 #if DXX_USE_MULTIPLAYER
1983 unsigned check_warn_local_player_can_control_guidebot(fvcobjptr &vcobjptr, const d_unique_buddy_state &BuddyState, const netgame_info &Netgame)
1984 {
1985 if (!Netgame.AllowGuidebot || !(Game_mode & GM_MULTI_COOP))
1986 {
1987 HUD_init_message_literal(HM_DEFAULT, "Guide-Bot is not enabled!");
1988 return 0;
1989 }
1990 auto &plr = get_player_controlling_guidebot(BuddyState, Players);
1991 if (plr.objnum == object_none)
1992 return 0;
1993 auto &plrobj = *vcobjptr(plr.objnum);
1994 if (ConsoleObject != &plrobj)
1995 {
1996 HUD_init_message(HM_DEFAULT, "Guide-Bot is controlled by %s!", plr.callsign.operator const char *());
1997 return 0;
1998 }
1999 return 1;
2000 }
2001 #endif
2002
2003 void do_escort_menu(void)
2004 {
2005 auto &BuddyState = LevelUniqueObjectState.BuddyState;
2006 auto &Objects = LevelUniqueObjectState.Objects;
2007 auto &vcobjptr = Objects.vcptr;
2008 auto &vmobjptr = Objects.vmptr;
2009 auto &vmobjptridx = Objects.vmptridx;
2010 int next_goal;
2011
2012 #if DXX_USE_MULTIPLAYER
2013 if (Game_mode & GM_MULTI) {
2014 if (!check_warn_local_player_can_control_guidebot(vcobjptr, BuddyState, Netgame))
2015 return;
2016 }
2017 #endif
2018
2019 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
2020 const auto &&buddy = find_escort(vmobjptridx, Robot_info);
2021 if (buddy == object_none)
2022 {
2023 HUD_init_message_literal(HM_DEFAULT, "No Guide-Bot present in mine!");
2024 return;
2025 }
2026 // Needed here or we might not know buddy can talk when he can.
2027 if (!ok_for_buddy_to_talk()) {
2028 HUD_init_message(HM_DEFAULT, "%s has not been released.", static_cast<const char *>(PlayerCfg.GuidebotName));
2029 return;
2030 }
2031
2032 auto &plrobj = get_local_plrobj();
2033 // This prevents the buddy from coming back if you've told him to scram.
2034 // If we don't set next_goal, we get garbage there.
2035 if (BuddyState.Escort_special_goal == ESCORT_GOAL_SCRAM) {
2036 BuddyState.Escort_special_goal = ESCORT_GOAL_UNSPECIFIED; // Else setting next goal might fail.
2037 next_goal = escort_set_goal_object(buddy, plrobj.ctype.player_info.powerup_flags);
2038 BuddyState.Escort_special_goal = ESCORT_GOAL_SCRAM;
2039 } else {
2040 BuddyState.Escort_special_goal = ESCORT_GOAL_UNSPECIFIED; // Else setting next goal might fail.
2041 next_goal = escort_set_goal_object(buddy, plrobj.ctype.player_info.powerup_flags);
2042 }
2043
2044 const auto Buddy_messages_suppressed = BuddyState.Buddy_messages_suppressed
2045 ? "Enable"
2046 : "Suppress";
2047 auto wind = window_create<escort_menu>(next_goal, Buddy_messages_suppressed, grd_curscreen->sc_canvas);
2048 (void)wind;
2049 }
2050
2051 namespace {
2052
2053 escort_menu_items::escort_menu_items(const int next_goal, const char *Buddy_messages_suppressed, grs_canvas &src, gr_string_size &text_size) :
2054 border_x(get_border_x(src)), border_y(get_border_y(src))
2055 {
2056 std::array<char, 12> goal_str;
2057 const char *goal_txt;
2058 switch (next_goal) {
2059 default:
2060 case ESCORT_GOAL_UNSPECIFIED:
2061 Int3();
2062 goal_txt = "ERROR";
2063 break;
2064 case ESCORT_GOAL_BLUE_KEY:
2065 goal_txt = "blue key";
2066 break;
2067 case ESCORT_GOAL_GOLD_KEY:
2068 goal_txt = "yellow key";
2069 break;
2070 case ESCORT_GOAL_RED_KEY:
2071 goal_txt = "red key";
2072 break;
2073 case ESCORT_GOAL_CONTROLCEN:
2074 goal_txt = "reactor";
2075 break;
2076 case ESCORT_GOAL_BOSS:
2077 goal_txt = "boss";
2078 break;
2079 case ESCORT_GOAL_EXIT:
2080 goal_txt = "exit";
2081 break;
2082 case ESCORT_GOAL_MARKER1:
2083 case ESCORT_GOAL_MARKER2:
2084 case ESCORT_GOAL_MARKER3:
2085 case ESCORT_GOAL_MARKER4:
2086 case ESCORT_GOAL_MARKER5:
2087 case ESCORT_GOAL_MARKER6:
2088 case ESCORT_GOAL_MARKER7:
2089 case ESCORT_GOAL_MARKER8:
2090 case ESCORT_GOAL_MARKER9:
2091 goal_txt = goal_str.data();
2092 std::snprintf(goal_str.data(), goal_str.size(), "marker %i", next_goal - ESCORT_GOAL_MARKER1 + 1);
2093 break;
2094 }
2095
2096 snprintf(msg.data(), msg.size(), "Select Guide-Bot Command:\n\n\n"
2097 "0. Next Goal: %s" CC_LSPACING_S "3\n\n"
2098 "\x84. Find Energy Powerup" CC_LSPACING_S "3\n\n"
2099 "2. Find Energy Center" CC_LSPACING_S "3\n\n"
2100 "3. Find Shield Powerup" CC_LSPACING_S "3\n\n"
2101 "4. Find Any Powerup" CC_LSPACING_S "3\n\n"
2102 "5. Find a Robot" CC_LSPACING_S "3\n\n"
2103 "6. Find a Hostage" CC_LSPACING_S "3\n\n"
2104 "7. Stay Away From Me" CC_LSPACING_S "3\n\n"
2105 "8. Find My Powerups" CC_LSPACING_S "3\n\n"
2106 "9. Find the exit\n\n"
2107 "T. %s Messages"
2108 // -- "9. Find the exit" CC_LSPACING_S "3\n"
2109 , goal_txt, Buddy_messages_suppressed);
2110 text_size = gr_get_string_size(*GAME_FONT, msg.data());
2111 }
2112
2113 }
2114
2115 }