| 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 |
* AI path forming stuff. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <numeric> |
| 27 |
#include <random> |
| 28 |
#include <stdio.h> // for printf() |
| 29 |
#include <stdlib.h> // for d_rand() and qsort() |
| 30 |
#include <string.h> // for memset() |
| 31 |
|
| 32 |
#include "inferno.h" |
| 33 |
#include "console.h" |
| 34 |
#include "3d.h" |
| 35 |
|
| 36 |
#include "object.h" |
| 37 |
#include "dxxerror.h" |
| 38 |
#include "ai.h" |
| 39 |
#include "robot.h" |
| 40 |
#include "fvi.h" |
| 41 |
#include "gameseg.h" |
| 42 |
#include "physics.h" |
| 43 |
#include "wall.h" |
| 44 |
#if DXX_USE_EDITOR |
| 45 |
#include "editor/editor.h" |
| 46 |
#include "editor/esegment.h" |
| 47 |
#endif |
| 48 |
#include "player.h" |
| 49 |
#include "game.h" |
| 50 |
|
| 51 |
#include "compiler-range_for.h" |
| 52 |
#include "partial_range.h" |
| 53 |
#include "d_levelstate.h" |
| 54 |
|
| 55 |
namespace dcx { |
| 56 |
|
| 57 |
namespace { |
| 58 |
|
| 59 |
// Length in segments of avoidance path |
| 60 |
constexpr std::size_t AVOID_SEG_LENGTH{7}; |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
#ifdef NDEBUG |
| 67 |
#define PATH_VALIDATION 0 |
| 68 |
#else |
| 69 |
#define PATH_VALIDATION 1 |
| 70 |
#endif |
| 71 |
|
| 72 |
namespace dsx { |
| 73 |
namespace { |
| 74 |
static void ai_path_set_orient_and_vel(const d_robot_info_array &Robot_info, object &objp, const vms_vector &goal_point |
| 75 |
#if DXX_BUILD_DESCENT == 2 |
| 76 |
, player_visibility_state player_visibility, const vms_vector *vec_to_player |
| 77 |
#endif |
| 78 |
); |
| 79 |
static void maybe_ai_path_garbage_collect(void); |
| 80 |
static void ai_path_garbage_collect(void); |
| 81 |
#if PATH_VALIDATION |
| 82 |
static void validate_all_paths(); |
| 83 |
static int validate_path(int, point_seg* psegs, uint_fast32_t num_points); |
| 84 |
#endif |
| 85 |
|
| 86 |
// ----------------------------------------------------------------------------------------------------------- |
| 87 |
// Insert the point at the center of the side connecting two segments between the two points. |
| 88 |
// This is messy because we must insert into the list. The simplest (and not too slow) way to do this is to start |
| 89 |
// at the end of the list and go backwards. |
| 90 |
static uint_fast32_t insert_center_points(segment_array &segments, point_seg *psegs, uint_fast32_t count) |
| 91 |
{ |
| 92 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 93 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 94 |
auto &vcsegptr = segments.vcptr; |
| 95 |
auto &vcsegptridx = segments.vcptridx; |
| 96 |
if (count < 2) |
| 97 |
return count; |
| 98 |
uint_fast32_t last_point = count - 1; |
| 99 |
for (uint_fast32_t i = last_point; i; --i) |
| 100 |
{ |
| 101 |
psegs[2*i] = psegs[i]; |
| 102 |
const auto &&seg1 = vcsegptr(psegs[i-1].segnum); |
| 103 |
auto connect_side = find_connect_side(vcsegptridx(psegs[i].segnum), seg1); |
| 104 |
Assert(connect_side != side_none); // Impossible! These two segments must be connected, they were created by create_path_points (which was created by mk!) |
| 105 |
if (connect_side == side_none) // Try to blow past the assert, this should at least prevent a hang. |
| 106 |
connect_side = sidenum_t::WLEFT; |
| 107 |
auto &vcvertptr = Vertices.vcptr; |
| 108 |
const auto center_point{compute_center_point_on_side(vcvertptr, seg1, connect_side)}; |
| 109 |
auto new_point{vm_vec_build_sub(psegs[i-1].point, center_point)}; |
| 110 |
new_point.x /= 16; |
| 111 |
new_point.y /= 16; |
| 112 |
new_point.z /= 16; |
| 113 |
vm_vec_sub(psegs[2*i-1].point, center_point, new_point); |
| 114 |
#if DXX_BUILD_DESCENT == 2 |
| 115 |
const auto &&segp = segments.imptridx(psegs[2*i].segnum); |
| 116 |
const auto &&temp_segnum = find_point_seg(LevelSharedSegmentState, psegs[2*i-1].point, segp); |
| 117 |
if (temp_segnum == segment_none) { |
| 118 |
psegs[2*i-1].point = center_point; |
| 119 |
find_point_seg(LevelSharedSegmentState, psegs[2*i-1].point, segp); |
| 120 |
} |
| 121 |
#endif |
| 122 |
|
| 123 |
psegs[2*i-1].segnum = psegs[2*i].segnum; |
| 124 |
count++; |
| 125 |
} |
| 126 |
|
| 127 |
#if DXX_BUILD_DESCENT == 2 |
| 128 |
// Now, remove unnecessary center points. |
| 129 |
// A center point is unnecessary if it is close to the line between the two adjacent points. |
| 130 |
// MK, OPTIMIZE! Can get away with about half the math since every vector gets computed twice. |
| 131 |
for (uint_fast32_t i = 1; i < count - 1; i += 2) |
| 132 |
{ |
| 133 |
const auto temp1{vm_vec_build_sub(psegs[i].point, psegs[i - 1].point)}; |
| 134 |
const auto temp2{vm_vec_build_sub(psegs[i + 1].point, psegs[i].point)}; |
| 135 |
const auto dot{vm_vec_build_dot(temp1, temp2)}; |
| 136 |
if (dot * 9/8 > fixmul(vm_vec_mag(temp1), vm_vec_mag(temp2))) |
| 137 |
psegs[i].segnum = segment_none; |
| 138 |
} |
| 139 |
|
| 140 |
// Now, scan for points with segnum == -1 |
| 141 |
auto predicate = [](const point_seg &p) { return p.segnum == segment_none; }; |
| 142 |
count = std::distance(psegs, std::remove_if(psegs, psegs + count, predicate)); |
| 143 |
#endif |
| 144 |
return count; |
| 145 |
} |
| 146 |
|
| 147 |
#if DXX_BUILD_DESCENT == 2 |
| 148 |
// ----------------------------------------------------------------------------------------------------------- |
| 149 |
// Move points halfway to outside of segment. |
| 150 |
static void move_towards_outside(const d_level_shared_segment_state &LevelSharedSegmentState, point_seg *const psegs, const unsigned num_points, const vmobjptridx_t objp, const create_path_random_flag rand_flag) |
| 151 |
{ |
| 152 |
int i; |
| 153 |
std::array<point_seg, 200> new_psegs; |
| 154 |
assert(num_points < new_psegs.size()); |
| 155 |
|
| 156 |
auto &Segments = LevelSharedSegmentState.get_segments(); |
| 157 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 158 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 159 |
auto &vcvertptr = Vertices.vcptr; |
| 160 |
for (i = 1; i < num_points - 1; ++i) |
| 161 |
{ |
| 162 |
fix segment_size; |
| 163 |
segnum_t segnum; |
| 164 |
vms_vector e; |
| 165 |
int count; |
| 166 |
const auto &&temp_segnum = find_point_seg(LevelSharedSegmentState, psegs[i].point, Segments.vcptridx(psegs[i].segnum)); |
| 167 |
Assert(temp_segnum != segment_none); |
| 168 |
psegs[i].segnum = temp_segnum; |
| 169 |
segnum = psegs[i].segnum; |
| 170 |
|
| 171 |
// I don't think we can use quick version here and this is _very_ rarely called. --MK, 07/03/95 |
| 172 |
const auto a = vm_vec_normalized_quick(vm_vec_build_sub(psegs[i].point, psegs[i-1].point)); |
| 173 |
const auto b = vm_vec_normalized_quick(vm_vec_build_sub(psegs[i+1].point, psegs[i].point)); |
| 174 |
if (abs(vm_vec_build_dot(a, b)) > 3*F1_0/4 ) { |
| 175 |
if (abs(a.z) < F1_0/2) { |
| 176 |
if (rand_flag != create_path_random_flag::nonrandom) |
| 177 |
{ |
| 178 |
e.x = (d_rand()-16384)/2; |
| 179 |
e.y = (d_rand()-16384)/2; |
| 180 |
e.z = abs(e.x) + abs(e.y) + 1; |
| 181 |
vm_vec_normalize_quick(e); |
| 182 |
} else { |
| 183 |
e.x = 0; |
| 184 |
e.y = 0; |
| 185 |
e.z = F1_0; |
| 186 |
} |
| 187 |
} else { |
| 188 |
if (rand_flag != create_path_random_flag::nonrandom) |
| 189 |
{ |
| 190 |
e.y = (d_rand()-16384)/2; |
| 191 |
e.z = (d_rand()-16384)/2; |
| 192 |
e.x = abs(e.y) + abs(e.z) + 1; |
| 193 |
vm_vec_normalize_quick(e); |
| 194 |
} else { |
| 195 |
e.x = F1_0; |
| 196 |
e.y = 0; |
| 197 |
e.z = 0; |
| 198 |
} |
| 199 |
} |
| 200 |
} else { |
| 201 |
const auto d{vm_vec_cross(a, b)}; |
| 202 |
e = vm_vec_cross(vm_vec_build_sub(psegs[i + 1].point, psegs[i - 1].point), d); |
| 203 |
vm_vec_normalize_quick(e); |
| 204 |
} |
| 205 |
|
| 206 |
if (vm_vec_mag_quick(e) < F1_0/2) |
| 207 |
Int3(); |
| 208 |
|
| 209 |
const shared_segment &segp = *vcsegptr(segnum); |
| 210 |
segment_size = vm_vec_dist_quick(vcvertptr(segp.verts[segment_relative_vertnum::_0]), vcvertptr(segp.verts[segment_relative_vertnum::_6])); |
| 211 |
if (segment_size > F1_0*40) |
| 212 |
segment_size = F1_0*40; |
| 213 |
|
| 214 |
auto goal_pos{vm_vec_scale_add(psegs[i].point, e, segment_size / 4)}; |
| 215 |
|
| 216 |
count = 3; |
| 217 |
while (count) { |
| 218 |
fvi_info hit_data; |
| 219 |
auto &p0 = psegs[i].point; |
| 220 |
const auto hit_type = find_vector_intersection(fvi_query{ |
| 221 |
p0, |
| 222 |
goal_pos, |
| 223 |
fvi_query::unused_ignore_obj_list, |
| 224 |
fvi_query::unused_LevelUniqueObjectState, |
| 225 |
fvi_query::unused_Robot_info, |
| 226 |
0, |
| 227 |
objp, |
| 228 |
}, psegs[i].segnum, objp->size, hit_data); |
| 229 |
|
| 230 |
if (hit_type == fvi_hit_type::None) |
| 231 |
count = 0; |
| 232 |
else { |
| 233 |
if (count == 3 && hit_type == fvi_hit_type::BadP0) |
| 234 |
Int3(); |
| 235 |
goal_pos.x = (p0.x + hit_data.hit_pnt.x)/2; |
| 236 |
goal_pos.y = (p0.y + hit_data.hit_pnt.y)/2; |
| 237 |
goal_pos.z = (p0.z + hit_data.hit_pnt.z)/2; |
| 238 |
count--; |
| 239 |
if (count == 0) { // Couldn't move towards outside, that's ok, sometimes things can't be moved. |
| 240 |
goal_pos = psegs[i].point; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
// Only move towards outside if remained inside segment. |
| 246 |
const auto &&new_segnum = find_point_seg(LevelSharedSegmentState, goal_pos, Segments.vcptridx(psegs[i].segnum)); |
| 247 |
if (new_segnum == psegs[i].segnum) { |
| 248 |
new_psegs[i].point = goal_pos; |
| 249 |
new_psegs[i].segnum = new_segnum; |
| 250 |
} else { |
| 251 |
new_psegs[i].point = psegs[i].point; |
| 252 |
new_psegs[i].segnum = psegs[i].segnum; |
| 253 |
} |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
for (i = 1; i < num_points - 1; ++i) |
| 258 |
psegs[i] = new_psegs[i]; |
| 259 |
} |
| 260 |
#endif |
| 261 |
|
| 262 |
} |
| 263 |
|
| 264 |
// ----------------------------------------------------------------------------------------------------------- |
| 265 |
// Create a path from objp->pos to the center of end_seg. |
| 266 |
// Return a list of (segment_num, point_locations) at psegs |
| 267 |
// Return number of points in *num_points. |
| 268 |
// if max_depth == -1, then there is no maximum depth. |
| 269 |
// If unable to create path, return -1, else return 0. |
| 270 |
// If random_flag !0, then introduce randomness into path by looking at sides in random order. This means |
| 271 |
// that a path between two segments won't always be the same, unless it is unique. |
| 272 |
// If safety_flag is set, then additional points are added to "make sure" that points are reachable. I would |
| 273 |
// like to say that it ensures that the object can move between the points, but that would require knowing what |
| 274 |
// the object is (which isn't passed, right?) and making fvi calls (slow, right?). So, consider it the more_or_less_safe_flag. |
| 275 |
// If end_seg == -2, then end seg will never be found and this routine will drop out due to depth (probably called by create_n_segment_path). |
| 276 |
std::pair<create_path_result, unsigned> create_path_points(const vmobjptridx_t objp, const robot_info *const robptr, const vcsegidx_t start_seg, icsegidx_t end_seg, point_seg_array_t::iterator psegs, const unsigned max_depth, create_path_random_flag random_flag, const create_path_safety_flag safety_flag, icsegidx_t avoid_seg) |
| 277 |
{ |
| 278 |
#if DXX_BUILD_DESCENT == 2 |
| 279 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 280 |
auto &vmobjptr = Objects.vmptr; |
| 281 |
#endif |
| 282 |
segnum_t cur_seg; |
| 283 |
int qtail{0}, qhead = 0; |
| 284 |
int i; |
| 285 |
std::array<seg_seg, MAX_SEGMENTS> seg_queue; |
| 286 |
int cur_depth; |
| 287 |
point_seg_array_t::iterator original_psegs = psegs; |
| 288 |
unsigned l_num_points{0}; |
| 289 |
|
| 290 |
#if PATH_VALIDATION |
| 291 |
validate_all_paths(); |
| 292 |
#endif |
| 293 |
|
| 294 |
auto &obj = *objp; |
| 295 |
if (obj.type == OBJ_ROBOT && obj.ctype.ai_info.behavior == ai_behavior::AIB_RUN_FROM) |
| 296 |
{ |
| 297 |
random_flag = create_path_random_flag::random; |
| 298 |
avoid_seg = ConsoleObject->segnum; |
| 299 |
// Int3(); |
| 300 |
} |
| 301 |
|
| 302 |
visited_segment_bitarray_t visited; |
| 303 |
std::array<uint16_t, MAX_SEGMENTS> depth{}; |
| 304 |
|
| 305 |
// If there is a segment we're not allowed to visit, mark it. |
| 306 |
if (avoid_seg != segment_none) { |
| 307 |
Assert(avoid_seg <= Highest_segment_index); |
| 308 |
if ((start_seg != avoid_seg) && (end_seg != avoid_seg)) { |
| 309 |
visited[avoid_seg] = true; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
cur_seg = start_seg; |
| 314 |
visited[cur_seg] = true; |
| 315 |
cur_depth = 0; |
| 316 |
|
| 317 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 318 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 319 |
auto &vcvertptr = Vertices.vcptr; |
| 320 |
auto &Walls = LevelUniqueWallSubsystemState.Walls; |
| 321 |
auto &vcwallptr = Walls.vcptr; |
| 322 |
|
| 323 |
per_side_array<sidenum_t> side_traversal_translation; |
| 324 |
std::iota(side_traversal_translation.begin(), side_traversal_translation.end(), sidenum_t{}); |
| 325 |
#if DXX_BUILD_DESCENT == 1 |
| 326 |
/* Descent 1 can only shuffle once, before the loop begins. |
| 327 |
* Descent 2 can shuffle on every pass of the loop. |
| 328 |
*/ |
| 329 |
if (random_flag != create_path_random_flag::nonrandom) |
| 330 |
std::shuffle(side_traversal_translation.begin(), side_traversal_translation.end(), std::minstd_rand(d_rand())); |
| 331 |
#elif DXX_BUILD_DESCENT == 2 |
| 332 |
/* If shuffling is enabled, always shuffle on the first pass of the |
| 333 |
* loop. |
| 334 |
*/ |
| 335 |
unsigned shuffle_random_flag{0}; |
| 336 |
std::minstd_rand mrd((random_flag != create_path_random_flag::nonrandom) |
| 337 |
? d_rand() |
| 338 |
: std::minstd_rand::default_seed |
| 339 |
); |
| 340 |
std::uniform_int_distribution uid03(0, 3); |
| 341 |
#endif |
| 342 |
while (cur_seg != end_seg) { |
| 343 |
const cscusegment &&segp = vcsegptr(cur_seg); |
| 344 |
#if DXX_BUILD_DESCENT == 2 |
| 345 |
/* If path randomness is enabled, conditionally shuffle based on |
| 346 |
* the value of shuffle_random_flag. Since shuffle_random_flag |
| 347 |
* is initialized to 0 above, it is guaranteed to shuffle on the |
| 348 |
* first iteration of the while loop. This is done since |
| 349 |
* side_traversal_translation was initialized via std::iota, and |
| 350 |
* is therefore predictable. However, if path randomness is |
| 351 |
* enabled by the caller, side_traversal_translation should be |
| 352 |
* unpredictable, and therefore a shuffle is needed. |
| 353 |
* |
| 354 |
* Each subsequent iteration may or may not shuffle, depending |
| 355 |
* on the value from d_rand in the most recent loop. Regardless |
| 356 |
* of whether a pass shuffles, it will reload the value so that |
| 357 |
* the next pass may shuffle. This is necessary to avoid |
| 358 |
* pinning the value in no-shuffle mode after the first time |
| 359 |
* d_rand returns a no-shuffle value. |
| 360 |
*/ |
| 361 |
if (random_flag != create_path_random_flag::nonrandom && std::exchange(shuffle_random_flag, uid03(mrd)) == 0) |
| 362 |
{ |
| 363 |
std::shuffle(side_traversal_translation.begin(), side_traversal_translation.end(), mrd); |
| 364 |
} |
| 365 |
#endif |
| 366 |
|
| 367 |
for (const auto snum : side_traversal_translation) |
| 368 |
{ |
| 369 |
if (!IS_CHILD(segp.s.children[snum])) |
| 370 |
continue; |
| 371 |
#if DXX_BUILD_DESCENT == 1 |
| 372 |
#define AI_DOOR_OPENABLE_PLAYER_FLAGS |
| 373 |
#elif DXX_BUILD_DESCENT == 2 |
| 374 |
#define AI_DOOR_OPENABLE_PLAYER_FLAGS player_info.powerup_flags, |
| 375 |
auto &player_info = get_local_plrobj().ctype.player_info; |
| 376 |
#endif |
| 377 |
if ((WALL_IS_DOORWAY(GameBitmaps, Textures, vcwallptr, segp, snum) & WALL_IS_DOORWAY_FLAG::fly) || ai_door_is_openable(obj, robptr, AI_DOOR_OPENABLE_PLAYER_FLAGS segp, snum)) |
| 378 |
#undef AI_DOOR_OPENABLE_PLAYER_FLAGS |
| 379 |
{ |
| 380 |
const auto this_seg = segp.s.children[snum]; |
| 381 |
#if DXX_BUILD_DESCENT == 2 |
| 382 |
Assert(this_seg != segment_none); |
| 383 |
if (((cur_seg == avoid_seg) || (this_seg == avoid_seg)) && (ConsoleObject->segnum == avoid_seg)) { |
| 384 |
const auto center_point{compute_center_point_on_side(vcvertptr, segp, snum)}; |
| 385 |
fvi_info hit_data; |
| 386 |
|
| 387 |
const auto hit_type = find_vector_intersection(fvi_query{ |
| 388 |
obj.pos, |
| 389 |
center_point, |
| 390 |
fvi_query::unused_ignore_obj_list, |
| 391 |
fvi_query::unused_LevelUniqueObjectState, |
| 392 |
fvi_query::unused_Robot_info, |
| 393 |
0, |
| 394 |
objp, |
| 395 |
}, obj.segnum, obj.size, hit_data); |
| 396 |
if (hit_type != fvi_hit_type::None) |
| 397 |
{ |
| 398 |
goto dont_add; |
| 399 |
} |
| 400 |
} |
| 401 |
#endif |
| 402 |
|
| 403 |
if (!visited[this_seg]) { |
| 404 |
seg_queue[qtail].start = cur_seg; |
| 405 |
seg_queue[qtail].end = this_seg; |
| 406 |
visited[this_seg] = true; |
| 407 |
depth[qtail++] = cur_depth+1; |
| 408 |
if (depth[qtail-1] == max_depth) { |
| 409 |
end_seg = seg_queue[qtail-1].end; |
| 410 |
goto cpp_done1; |
| 411 |
} // end if (depth[... |
| 412 |
} // end if (!visited... |
| 413 |
} // if (WALL_IS_DOORWAY(... |
| 414 |
#if DXX_BUILD_DESCENT == 2 |
| 415 |
dont_add: ; |
| 416 |
#endif |
| 417 |
} |
| 418 |
|
| 419 |
if (qtail <= 0) |
| 420 |
break; |
| 421 |
|
| 422 |
if (qhead >= qtail) { |
| 423 |
// Couldn't get to goal, return a path as far as we got, which probably acceptable to the unparticular caller. |
| 424 |
end_seg = seg_queue[qtail-1].end; |
| 425 |
break; |
| 426 |
} |
| 427 |
|
| 428 |
cur_seg = seg_queue[qhead].end; |
| 429 |
cur_depth = depth[qhead]; |
| 430 |
qhead++; |
| 431 |
|
| 432 |
cpp_done1: ; |
| 433 |
} // while (cur_seg ... |
| 434 |
|
| 435 |
if (qtail > 0) |
| 436 |
{ |
| 437 |
// Set qtail to the segment which ends at the goal. |
| 438 |
while (seg_queue[--qtail].end != end_seg) |
| 439 |
if (qtail < 0) { |
| 440 |
return std::make_pair(create_path_result::early, l_num_points); |
| 441 |
} |
| 442 |
} |
| 443 |
else |
| 444 |
qtail = -1; |
| 445 |
|
| 446 |
#if DXX_BUILD_DESCENT == 1 |
| 447 |
#if DXX_USE_EDITOR |
| 448 |
Selected_segs.clear(); |
| 449 |
#endif |
| 450 |
#endif |
| 451 |
|
| 452 |
while (qtail >= 0) { |
| 453 |
segnum_t parent_seg, this_seg; |
| 454 |
|
| 455 |
this_seg = seg_queue[qtail].end; |
| 456 |
parent_seg = seg_queue[qtail].start; |
| 457 |
psegs->segnum = this_seg; |
| 458 |
psegs->point = compute_segment_center(vcvertptr, vcsegptr(this_seg)); |
| 459 |
psegs++; |
| 460 |
l_num_points++; |
| 461 |
#if DXX_BUILD_DESCENT == 1 |
| 462 |
#if DXX_USE_EDITOR |
| 463 |
Selected_segs.emplace_back(this_seg); |
| 464 |
#endif |
| 465 |
#endif |
| 466 |
|
| 467 |
if (parent_seg == start_seg) |
| 468 |
break; |
| 469 |
|
| 470 |
while (seg_queue[--qtail].end != parent_seg) |
| 471 |
Assert(qtail >= 0); |
| 472 |
} |
| 473 |
|
| 474 |
psegs->segnum = start_seg; |
| 475 |
psegs->point = compute_segment_center(vcvertptr, vcsegptr(start_seg)); |
| 476 |
psegs++; |
| 477 |
l_num_points++; |
| 478 |
|
| 479 |
#if PATH_VALIDATION |
| 480 |
validate_path(1, original_psegs, l_num_points); |
| 481 |
#endif |
| 482 |
|
| 483 |
// Now, reverse point_segs in place. |
| 484 |
for (i=0; i< l_num_points/2; i++) { |
| 485 |
point_seg temp_point_seg = *(original_psegs + i); |
| 486 |
*(original_psegs + i) = *(original_psegs + l_num_points - i - 1); |
| 487 |
*(original_psegs + l_num_points - i - 1) = temp_point_seg; |
| 488 |
} |
| 489 |
#if PATH_VALIDATION |
| 490 |
validate_path(2, original_psegs, l_num_points); |
| 491 |
#endif |
| 492 |
|
| 493 |
// Now, if safety_flag set, then insert the point at the center of the side connecting two segments |
| 494 |
// between the two points. This is messy because we must insert into the list. The simplest (and not too slow) |
| 495 |
// way to do this is to start at the end of the list and go backwards. |
| 496 |
if (safety_flag != create_path_safety_flag::unsafe) { |
| 497 |
if (psegs - Point_segs + l_num_points + 2 > MAX_POINT_SEGS) { |
| 498 |
// Ouch! Cannot insert center points in path. So return unsafe path. |
| 499 |
ai_reset_all_paths(); |
| 500 |
return std::make_pair(create_path_result::early, l_num_points); |
| 501 |
} else { |
| 502 |
l_num_points = insert_center_points(Segments, original_psegs, l_num_points); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
#if PATH_VALIDATION |
| 507 |
validate_path(3, original_psegs, l_num_points); |
| 508 |
#endif |
| 509 |
|
| 510 |
#if DXX_BUILD_DESCENT == 2 |
| 511 |
// -- MK, 10/30/95 -- This code causes apparent discontinuities in the path, moving a point |
| 512 |
// into a new segment. It is not necessarily bad, but it makes it hard to track down actual |
| 513 |
// discontinuity problems. |
| 514 |
auto &Robot_info = LevelSharedRobotInfoState.Robot_info; |
| 515 |
if (obj.type == OBJ_ROBOT) |
| 516 |
if (Robot_info[get_robot_id(obj)].companion) |
| 517 |
move_towards_outside(LevelSharedSegmentState, original_psegs, l_num_points, objp, create_path_random_flag::nonrandom); |
| 518 |
#endif |
| 519 |
|
| 520 |
#if PATH_VALIDATION |
| 521 |
validate_path(4, original_psegs, l_num_points); |
| 522 |
#endif |
| 523 |
return std::make_pair(create_path_result::finished, l_num_points); |
| 524 |
} |
| 525 |
|
| 526 |
#if DXX_BUILD_DESCENT == 2 |
| 527 |
// ------------------------------------------------------------------------------------------------------- |
| 528 |
// polish_path |
| 529 |
// Takes an existing path and makes it nicer. |
| 530 |
// Drops as many leading points as possible still maintaining direct accessibility |
| 531 |
// from current position to first point. |
| 532 |
// Will not shorten path to fewer than 3 points. |
| 533 |
// Returns number of points. |
| 534 |
// Starting position in psegs doesn't change. |
| 535 |
// Changed, MK, 10/18/95. I think this was causing robots to get hung up on walls. |
| 536 |
// Only drop up to the first three points. |
| 537 |
int polish_path(const vmobjptridx_t objp, point_seg *psegs, int num_points) |
| 538 |
{ |
| 539 |
auto &BuddyState = LevelUniqueObjectState.BuddyState; |
| 540 |
int i, first_point=0; |
| 541 |
|
| 542 |
if (num_points <= 4) |
| 543 |
return num_points; |
| 544 |
|
| 545 |
// Prevent the buddy from polishing his path twice in one tick, which can cause him to get hung up. Pretty ugly, huh? |
| 546 |
auto &Robot_info = LevelSharedRobotInfoState.Robot_info; |
| 547 |
auto &obj = *objp; |
| 548 |
if (Robot_info[get_robot_id(obj)].companion) |
| 549 |
{ |
| 550 |
if (d_tick_count == BuddyState.Last_buddy_polish_path_tick) |
| 551 |
return num_points; |
| 552 |
else |
| 553 |
BuddyState.Last_buddy_polish_path_tick = d_tick_count; |
| 554 |
} |
| 555 |
|
| 556 |
// -- MK: 10/18/95: for (i=0; i<num_points-3; i++) |
| 557 |
for (i=0; i<2; i++) { |
| 558 |
fvi_info hit_data; |
| 559 |
const auto hit_type = find_vector_intersection(fvi_query{ |
| 560 |
obj.pos, |
| 561 |
psegs[i].point, |
| 562 |
fvi_query::unused_ignore_obj_list, |
| 563 |
fvi_query::unused_LevelUniqueObjectState, |
| 564 |
fvi_query::unused_Robot_info, |
| 565 |
0, |
| 566 |
objp, |
| 567 |
}, obj.segnum, obj.size, hit_data); |
| 568 |
|
| 569 |
if (hit_type == fvi_hit_type::None) |
| 570 |
first_point = i+1; |
| 571 |
else |
| 572 |
break; |
| 573 |
} |
| 574 |
|
| 575 |
if (first_point) { |
| 576 |
// Scrunch down all the psegs. |
| 577 |
for (i=first_point; i<num_points; i++) |
| 578 |
psegs[i-first_point] = psegs[i]; |
| 579 |
} |
| 580 |
|
| 581 |
return num_points - first_point; |
| 582 |
} |
| 583 |
#endif |
| 584 |
|
| 585 |
#ifndef NDEBUG |
| 586 |
namespace { |
| 587 |
// ------------------------------------------------------------------------------------------------------- |
| 588 |
// Make sure that there are connections between all segments on path. |
| 589 |
// Note that if path has been optimized, connections may not be direct, so this function is useless, or worse. |
| 590 |
// Return true if valid, else return false. |
| 591 |
int validate_path(int, point_seg *psegs, uint_fast32_t num_points) |
| 592 |
{ |
| 593 |
#if PATH_VALIDATION |
| 594 |
auto curseg = psegs->segnum; |
| 595 |
if (curseg > Highest_segment_index) |
| 596 |
{ |
| 597 |
Int3(); // Contact Mike: Debug trap for elusive, nasty bug. |
| 598 |
return 0; |
| 599 |
} |
| 600 |
|
| 601 |
range_for (const auto &ps, unchecked_partial_range(psegs, 1u, num_points)) |
| 602 |
{ |
| 603 |
auto nextseg = ps.segnum; |
| 604 |
if (curseg != nextseg) { |
| 605 |
const shared_segment &csegp = vcsegptr(curseg); |
| 606 |
auto &children = csegp.children; |
| 607 |
if (std::find(children.begin(), children.end(), nextseg) == children.end()) |
| 608 |
{ |
| 609 |
// Assert(sidenum != MAX_SIDES_PER_SEGMENT); // Hey, created path is not contiguous, why!? |
| 610 |
Int3(); |
| 611 |
return 0; |
| 612 |
} |
| 613 |
curseg = nextseg; |
| 614 |
} |
| 615 |
} |
| 616 |
#endif |
| 617 |
return 1; |
| 618 |
|
| 619 |
} |
| 620 |
|
| 621 |
// ----------------------------------------------------------------------------------------------------------- |
| 622 |
void validate_all_paths(void) |
| 623 |
{ |
| 624 |
|
| 625 |
#if PATH_VALIDATION |
| 626 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 627 |
auto &vmobjptr = Objects.vmptr; |
| 628 |
for (auto &obj : vmobjptr) |
| 629 |
{ |
| 630 |
if (obj.type == OBJ_ROBOT) { |
| 631 |
auto &aip = obj.ctype.ai_info; |
| 632 |
if (obj.control_source == object::control_type::ai) |
| 633 |
{ |
| 634 |
if ((aip.hide_index != -1) && (aip.path_length > 0)) |
| 635 |
if (!validate_path(4, &Point_segs[aip.hide_index], aip.path_length)) |
| 636 |
{ |
| 637 |
Int3(); // This path is bogus! Who corrupted it! Danger! Danger! |
| 638 |
// Contact Mike, he caused this mess. |
| 639 |
aip.path_length=0; // This allows people to resume without harm... |
| 640 |
} |
| 641 |
} |
| 642 |
} |
| 643 |
} |
| 644 |
#endif |
| 645 |
} |
| 646 |
|
| 647 |
} |
| 648 |
#endif |
| 649 |
|
| 650 |
// ------------------------------------------------------------------------------------------------------- |
| 651 |
// Creates a path from the objects current segment (objp->segnum) to the specified segment for the object to |
| 652 |
// hide in Ai_local_info[objnum].goal_segment. |
| 653 |
// Sets objp->ctype.ai_info.hide_index, a pointer into Point_segs, the first point_seg of the path. |
| 654 |
// objp->ctype.ai_info.path_length, length of path |
| 655 |
// Point_segs_free_ptr global pointer into Point_segs array |
| 656 |
void create_path_to_segment(const vmobjptridx_t objp, const robot_info &robptr, const unsigned max_length, const create_path_safety_flag safety_flag, const icsegidx_t goal_segment) |
| 657 |
{ |
| 658 |
auto &obj = *objp; |
| 659 |
ai_static *const aip = &obj.ctype.ai_info; |
| 660 |
ai_local *const ailp = &obj.ctype.ai_info.ail; |
| 661 |
|
| 662 |
ailp->time_player_seen = {GameTime64}; // Prevent from resetting path quickly. |
| 663 |
ailp->goal_segment = goal_segment; |
| 664 |
|
| 665 |
segnum_t start_seg; |
| 666 |
start_seg = obj.segnum; |
| 667 |
const auto end_seg{goal_segment}; |
| 668 |
|
| 669 |
if (end_seg == segment_none) { |
| 670 |
; |
| 671 |
} else { |
| 672 |
aip->path_length = create_path_points(objp, &robptr, start_seg, end_seg, Point_segs_free_ptr, max_length, create_path_random_flag::random, safety_flag, segment_none).second; |
| 673 |
#if DXX_BUILD_DESCENT == 2 |
| 674 |
aip->path_length = polish_path(objp, Point_segs_free_ptr, aip->path_length); |
| 675 |
#endif |
| 676 |
aip->hide_index = Point_segs_free_ptr - Point_segs; |
| 677 |
aip->cur_path_index = 0; |
| 678 |
#if DXX_BUILD_DESCENT == 1 |
| 679 |
#ifndef NDEBUG |
| 680 |
validate_path(6, Point_segs_free_ptr, aip->path_length); |
| 681 |
#endif |
| 682 |
#endif |
| 683 |
Point_segs_free_ptr += aip->path_length; |
| 684 |
if (Point_segs_free_ptr - Point_segs + MAX_PATH_LENGTH*2 > MAX_POINT_SEGS) { |
| 685 |
//Int3(); // Contact Mike: This is stupid. Should call maybe_ai_garbage_collect before the add. |
| 686 |
ai_reset_all_paths(); |
| 687 |
return; |
| 688 |
} |
| 689 |
// Assert(Point_segs_free_ptr - Point_segs + MAX_PATH_LENGTH*2 < MAX_POINT_SEGS); |
| 690 |
aip->PATH_DIR = 1; // Initialize to moving forward. |
| 691 |
#if DXX_BUILD_DESCENT == 1 |
| 692 |
aip->SUBMODE = AISM_GOHIDE; // This forces immediate movement. |
| 693 |
#endif |
| 694 |
ailp->mode = ai_mode::AIM_FOLLOW_PATH; |
| 695 |
ailp->player_awareness_type = player_awareness_type_t::PA_NONE; // If robot too aware of player, will set mode to chase |
| 696 |
} |
| 697 |
maybe_ai_path_garbage_collect(); |
| 698 |
} |
| 699 |
|
| 700 |
// Change, 10/07/95: Used to create path to ConsoleObject->pos. Now creates path to Believed_player_pos. |
| 701 |
void create_path_to_believed_player_segment(const vmobjptridx_t objp, const robot_info &robptr, const unsigned max_length, const create_path_safety_flag safety_flag) |
| 702 |
{ |
| 703 |
#if DXX_BUILD_DESCENT == 1 |
| 704 |
const auto goal_segment = ConsoleObject->segnum; |
| 705 |
#elif DXX_BUILD_DESCENT == 2 |
| 706 |
const auto goal_segment{Believed_player_seg}; |
| 707 |
#endif |
| 708 |
create_path_to_segment(objp, robptr, max_length, safety_flag, goal_segment); |
| 709 |
} |
| 710 |
|
| 711 |
#if DXX_BUILD_DESCENT == 2 |
| 712 |
void create_path_to_guidebot_player_segment(const vmobjptridx_t objp, const robot_info &robptr, const unsigned max_length, const create_path_safety_flag safety_flag) |
| 713 |
{ |
| 714 |
auto &BuddyState = LevelUniqueObjectState.BuddyState; |
| 715 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 716 |
auto &plr = get_player_controlling_guidebot(BuddyState, Players); |
| 717 |
if (plr.objnum == object_none) |
| 718 |
return; |
| 719 |
auto &plrobj = *Objects.vcptr(plr.objnum); |
| 720 |
const auto goal_segment{plrobj.segnum}; |
| 721 |
create_path_to_segment(objp, robptr, max_length, safety_flag, goal_segment); |
| 722 |
} |
| 723 |
// ------------------------------------------------------------------------------------------------------- |
| 724 |
// Creates a path from the object's current segment (objp->segnum) to segment goalseg. |
| 725 |
void create_path_to_segment(const vmobjptridx_t objp, const robot_info &robptr, segnum_t goalseg, const unsigned max_length, const create_path_safety_flag safety_flag) |
| 726 |
{ |
| 727 |
ai_static *aip = &objp->ctype.ai_info; |
| 728 |
ai_local *ailp = &objp->ctype.ai_info.ail; |
| 729 |
|
| 730 |
ailp->time_player_seen = {GameTime64}; // Prevent from resetting path quickly. |
| 731 |
ailp->goal_segment = goalseg; |
| 732 |
|
| 733 |
segnum_t start_seg, end_seg; |
| 734 |
start_seg = objp->segnum; |
| 735 |
end_seg = ailp->goal_segment; |
| 736 |
|
| 737 |
if (end_seg == segment_none) { |
| 738 |
; |
| 739 |
} else { |
| 740 |
aip->path_length = create_path_points(objp, &robptr, start_seg, end_seg, Point_segs_free_ptr, max_length, create_path_random_flag::random, safety_flag, segment_none).second; |
| 741 |
aip->hide_index = Point_segs_free_ptr - Point_segs; |
| 742 |
aip->cur_path_index = 0; |
| 743 |
Point_segs_free_ptr += aip->path_length; |
| 744 |
if (Point_segs_free_ptr - Point_segs + MAX_PATH_LENGTH*2 > MAX_POINT_SEGS) { |
| 745 |
ai_reset_all_paths(); |
| 746 |
return; |
| 747 |
} |
| 748 |
|
| 749 |
aip->PATH_DIR = 1; // Initialize to moving forward. |
| 750 |
// -- UNUSED! aip->SUBMODE = AISM_GOHIDE; // This forces immediate movement. |
| 751 |
ailp->player_awareness_type = player_awareness_type_t::PA_NONE; // If robot too aware of player, will set mode to chase |
| 752 |
} |
| 753 |
|
| 754 |
maybe_ai_path_garbage_collect(); |
| 755 |
|
| 756 |
} |
| 757 |
#endif |
| 758 |
|
| 759 |
// ------------------------------------------------------------------------------------------------------- |
| 760 |
// Creates a path from the objects current segment (objp->segnum) to the specified segment for the object to |
| 761 |
// hide in Ai_local_info[objnum].goal_segment |
| 762 |
// Sets objp->ctype.ai_info.hide_index, a pointer into Point_segs, the first point_seg of the path. |
| 763 |
// objp->ctype.ai_info.path_length, length of path |
| 764 |
// Point_segs_free_ptr global pointer into Point_segs array |
| 765 |
void create_path_to_station(const vmobjptridx_t objp, const robot_info &robptr, int max_length) |
| 766 |
{ |
| 767 |
auto &obj = *objp; |
| 768 |
ai_static *const aip = &obj.ctype.ai_info; |
| 769 |
ai_local *const ailp = &obj.ctype.ai_info.ail; |
| 770 |
|
| 771 |
if (max_length == -1) |
| 772 |
max_length = MAX_DEPTH_TO_SEARCH_FOR_PLAYER; |
| 773 |
|
| 774 |
ailp->time_player_seen = {GameTime64}; // Prevent from resetting path quickly. |
| 775 |
|
| 776 |
segnum_t start_seg, end_seg; |
| 777 |
start_seg = obj.segnum; |
| 778 |
end_seg = aip->hide_segment; |
| 779 |
|
| 780 |
if (end_seg == segment_none) { |
| 781 |
; |
| 782 |
} else { |
| 783 |
aip->path_length = create_path_points(objp, &robptr, start_seg, end_seg, Point_segs_free_ptr, max_length, create_path_random_flag::random, create_path_safety_flag::safe, segment_none).second; |
| 784 |
#if DXX_BUILD_DESCENT == 2 |
| 785 |
aip->path_length = polish_path(objp, Point_segs_free_ptr, aip->path_length); |
| 786 |
#endif |
| 787 |
aip->hide_index = Point_segs_free_ptr - Point_segs; |
| 788 |
aip->cur_path_index = 0; |
| 789 |
#if DXX_BUILD_DESCENT == 1 |
| 790 |
#ifndef NDEBUG |
| 791 |
validate_path(7, Point_segs_free_ptr, aip->path_length); |
| 792 |
#endif |
| 793 |
#endif |
| 794 |
Point_segs_free_ptr += aip->path_length; |
| 795 |
if (Point_segs_free_ptr - Point_segs + MAX_PATH_LENGTH*2 > MAX_POINT_SEGS) { |
| 796 |
//Int3(); // Contact Mike: Stupid. |
| 797 |
ai_reset_all_paths(); |
| 798 |
return; |
| 799 |
} |
| 800 |
// Assert(Point_segs_free_ptr - Point_segs + MAX_PATH_LENGTH*2 < MAX_POINT_SEGS); |
| 801 |
aip->PATH_DIR = 1; // Initialize to moving forward. |
| 802 |
// aip->SUBMODE = AISM_GOHIDE; // This forces immediate movement. |
| 803 |
ailp->mode = ai_mode::AIM_FOLLOW_PATH; |
| 804 |
ailp->player_awareness_type = player_awareness_type_t::PA_NONE; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
maybe_ai_path_garbage_collect(); |
| 809 |
|
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
// ------------------------------------------------------------------------------------------------------- |
| 814 |
// Create a path of length path_length for an object, stuffing info in ai_info field. |
| 815 |
void create_n_segment_path(const vmobjptridx_t objp, const robot_info &robptr, unsigned path_length, const imsegidx_t avoid_seg) |
| 816 |
{ |
| 817 |
auto &obj = *objp; |
| 818 |
ai_static *const aip = &obj.ctype.ai_info; |
| 819 |
ai_local *const ailp = &obj.ctype.ai_info.ail; |
| 820 |
|
| 821 |
const auto &&cr0 = create_path_points(objp, &robptr, obj.segnum, segment_exit, Point_segs_free_ptr, path_length, create_path_random_flag::random, create_path_safety_flag::unsafe, avoid_seg); |
| 822 |
aip->path_length = cr0.second; |
| 823 |
if (cr0.first == create_path_result::early) |
| 824 |
{ |
| 825 |
Point_segs_free_ptr += aip->path_length; |
| 826 |
for (;;) |
| 827 |
{ |
| 828 |
const auto &&crf = create_path_points(objp, &robptr, obj.segnum, segment_exit, Point_segs_free_ptr, --path_length, create_path_random_flag::random, create_path_safety_flag::unsafe, segment_none); |
| 829 |
aip->path_length = crf.second; |
| 830 |
if (crf.first != create_path_result::early) |
| 831 |
break; |
| 832 |
} |
| 833 |
assert(path_length); |
| 834 |
} |
| 835 |
|
| 836 |
aip->hide_index = Point_segs_free_ptr - Point_segs; |
| 837 |
aip->cur_path_index = 0; |
| 838 |
#if PATH_VALIDATION |
| 839 |
validate_path(8, Point_segs_free_ptr, aip->path_length); |
| 840 |
#endif |
| 841 |
Point_segs_free_ptr += aip->path_length; |
| 842 |
if (Point_segs_free_ptr - Point_segs + MAX_PATH_LENGTH*2 > MAX_POINT_SEGS) { |
| 843 |
//Int3(); // Contact Mike: This is curious, though not deadly. /eip++;g |
| 844 |
ai_reset_all_paths(); |
| 845 |
} |
| 846 |
|
| 847 |
aip->PATH_DIR = 1; // Initialize to moving forward. |
| 848 |
#if DXX_BUILD_DESCENT == 1 |
| 849 |
aip->SUBMODE = -1; // Don't know what this means. |
| 850 |
#endif |
| 851 |
ailp->mode = ai_mode::AIM_FOLLOW_PATH; |
| 852 |
|
| 853 |
#if DXX_BUILD_DESCENT == 2 |
| 854 |
// If this robot is visible (player_visibility is not available) and it's running away, move towards outside with |
| 855 |
// randomness to prevent a stream of bots from going away down the center of a corridor. |
| 856 |
if (player_is_visible(ailp->previous_visibility)) |
| 857 |
{ |
| 858 |
if (aip->path_length) { |
| 859 |
int t_num_points = aip->path_length; |
| 860 |
move_towards_outside(LevelSharedSegmentState, &Point_segs[aip->hide_index], t_num_points, objp, create_path_random_flag::random); |
| 861 |
aip->path_length = t_num_points; |
| 862 |
} |
| 863 |
} |
| 864 |
#endif |
| 865 |
|
| 866 |
maybe_ai_path_garbage_collect(); |
| 867 |
|
| 868 |
} |
| 869 |
|
| 870 |
// ------------------------------------------------------------------------------------------------------- |
| 871 |
void create_n_segment_path_to_door(const vmobjptridx_t objp, const robot_info &robptr, const unsigned path_length) |
| 872 |
{ |
| 873 |
create_n_segment_path(objp, robptr, path_length, segment_none); |
| 874 |
} |
| 875 |
|
| 876 |
#define Int3_if(cond) if (!cond) Int3(); |
| 877 |
|
| 878 |
// -- too much work -- // ---------------------------------------------------------------------------------------------------------- |
| 879 |
// -- too much work -- // Return true if the object the companion wants to kill is reachable. |
| 880 |
// -- too much work -- int attack_kill_object(object *objp) |
| 881 |
// -- too much work -- { |
| 882 |
// -- too much work -- object *kill_objp; |
| 883 |
// -- too much work -- fvi_info hit_data; |
| 884 |
// -- too much work -- int fate; |
| 885 |
// -- too much work -- fvi_query fq; |
| 886 |
// -- too much work -- |
| 887 |
// -- too much work -- if (Escort_kill_object == -1) |
| 888 |
// -- too much work -- return 0; |
| 889 |
// -- too much work -- |
| 890 |
// -- too much work -- kill_objp = &Objects[Escort_kill_object]; |
| 891 |
// -- too much work -- |
| 892 |
// -- too much work -- fq.p0 = &objp->pos; |
| 893 |
// -- too much work -- fq.startseg = objp->segnum; |
| 894 |
// -- too much work -- fq.p1 = &kill_objp->pos; |
| 895 |
// -- too much work -- fq.rad = objp->size; |
| 896 |
// -- too much work -- fq.thisobjnum = objp-Objects; |
| 897 |
// -- too much work -- fq.ignore_obj_list = NULL; |
| 898 |
// -- too much work -- fq.flags = 0; |
| 899 |
// -- too much work -- |
| 900 |
// -- too much work -- fate = find_vector_intersection(&fq,&hit_data); |
| 901 |
// -- too much work -- |
| 902 |
// -- too much work -- if (fate == HIT_NONE) |
| 903 |
// -- too much work -- return 1; |
| 904 |
// -- too much work -- else |
| 905 |
// -- too much work -- return 0; |
| 906 |
// -- too much work -- } |
| 907 |
|
| 908 |
// ------------------------------------------------------------------------------------------------------- |
| 909 |
// Creates a path from the objects current segment (objp->segnum) to the specified segment for the object to |
| 910 |
// hide in Ai_local_info[objnum].goal_segment. |
| 911 |
// Sets objp->ctype.ai_info.hide_index, a pointer into Point_segs, the first point_seg of the path. |
| 912 |
// objp->ctype.ai_info.path_length, length of path |
| 913 |
// Point_segs_free_ptr global pointer into Point_segs array |
| 914 |
#if DXX_BUILD_DESCENT == 1 |
| 915 |
namespace { |
| 916 |
|
| 917 |
static void create_path(const vmobjptridx_t objp, const robot_info &robptr) |
| 918 |
{ |
| 919 |
auto &obj = *objp; |
| 920 |
ai_static *const aip = &obj.ctype.ai_info; |
| 921 |
|
| 922 |
const auto start_seg{obj.segnum}; |
| 923 |
const auto end_seg = obj.ctype.ai_info.ail.goal_segment; |
| 924 |
|
| 925 |
if (end_seg == segment_none) |
| 926 |
create_n_segment_path(objp, robptr, 3, segment_none); |
| 927 |
|
| 928 |
if (end_seg == segment_none) { |
| 929 |
; |
| 930 |
} else { |
| 931 |
aip->path_length = create_path_points(objp, &robptr, start_seg, end_seg, Point_segs_free_ptr, MAX_PATH_LENGTH, create_path_random_flag::nonrandom, create_path_safety_flag::unsafe, segment_none).second; |
| 932 |
aip->hide_index = Point_segs_free_ptr - Point_segs; |
| 933 |
aip->cur_path_index = 0; |
| 934 |
#ifndef NDEBUG |
| 935 |
validate_path(5, Point_segs_free_ptr, aip->path_length); |
| 936 |
#endif |
| 937 |
Point_segs_free_ptr += aip->path_length; |
| 938 |
if (Point_segs_free_ptr - Point_segs + MAX_PATH_LENGTH*2 > MAX_POINT_SEGS) { |
| 939 |
//Int3(); // Contact Mike: This is curious, though not deadly. /eip++;g |
| 940 |
ai_reset_all_paths(); |
| 941 |
} |
| 942 |
aip->PATH_DIR = 1; // Initialize to moving forward. |
| 943 |
aip->SUBMODE = AISM_HIDING; // Pretend we are hiding, so we sit here until bothered. |
| 944 |
} |
| 945 |
|
| 946 |
maybe_ai_path_garbage_collect(); |
| 947 |
} |
| 948 |
} |
| 949 |
#endif |
| 950 |
|
| 951 |
|
| 952 |
// ---------------------------------------------------------------------------------------------------------- |
| 953 |
// Optimization: If current velocity will take robot near goal, don't change velocity |
| 954 |
void ai_follow_path(const d_robot_info_array &Robot_info, const vmobjptridx_t objp, const player_visibility_state player_visibility, const vms_vector *const vec_to_player) |
| 955 |
{ |
| 956 |
auto &obj = *objp; |
| 957 |
ai_static *const aip = &obj.ctype.ai_info; |
| 958 |
|
| 959 |
vms_vector goal_point, new_goal_point; |
| 960 |
#if DXX_BUILD_DESCENT == 2 |
| 961 |
auto &BuddyState = LevelUniqueObjectState.BuddyState; |
| 962 |
#endif |
| 963 |
auto &robptr = Robot_info[get_robot_id(obj)]; |
| 964 |
int forced_break, original_dir, original_index; |
| 965 |
ai_local *const ailp = &obj.ctype.ai_info.ail; |
| 966 |
|
| 967 |
if ((aip->hide_index == -1) || (aip->path_length == 0)) |
| 968 |
{ |
| 969 |
if (ailp->mode == ai_mode::AIM_RUN_FROM_OBJECT) { |
| 970 |
create_n_segment_path(objp, robptr, 5, segment_none); |
| 971 |
//--Int3_if((aip->path_length != 0)); |
| 972 |
ailp->mode = ai_mode::AIM_RUN_FROM_OBJECT; |
| 973 |
} else { |
| 974 |
#if DXX_BUILD_DESCENT == 1 |
| 975 |
create_path(objp, robptr); |
| 976 |
#elif DXX_BUILD_DESCENT == 2 |
| 977 |
create_n_segment_path(objp, robptr, 5, segment_none); |
| 978 |
#endif |
| 979 |
//--Int3_if((aip->path_length != 0)); |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
if ((aip->hide_index + aip->path_length > Point_segs_free_ptr - Point_segs) && (aip->path_length>0)) { |
| 984 |
#if DXX_BUILD_DESCENT == 2 |
| 985 |
Int3(); // Contact Mike: Bad. Path goes into what is believed to be free space. |
| 986 |
// This is debugging code. Figure out why garbage collection |
| 987 |
// didn't compress this object's path information. |
| 988 |
ai_path_garbage_collect(); |
| 989 |
#endif |
| 990 |
ai_reset_all_paths(); |
| 991 |
} |
| 992 |
|
| 993 |
if (aip->path_length < 2) { |
| 994 |
#if DXX_BUILD_DESCENT == 1 |
| 995 |
if (ailp->mode == ai_mode::AIM_RUN_FROM_OBJECT) |
| 996 |
#elif DXX_BUILD_DESCENT == 2 |
| 997 |
if ((aip->behavior == ai_behavior::AIB_SNIPE) || (ailp->mode == ai_mode::AIM_RUN_FROM_OBJECT)) |
| 998 |
#endif |
| 999 |
{ |
| 1000 |
create_n_segment_path(objp, robptr, AVOID_SEG_LENGTH, ConsoleObject->segnum == obj.segnum ? segment_none : ConsoleObject->segnum); // Can't avoid segment player is in, robot is already in it! (That's what the -1 is for) |
| 1001 |
//--Int3_if((aip->path_length != 0)); |
| 1002 |
#if DXX_BUILD_DESCENT == 2 |
| 1003 |
if (aip->behavior == ai_behavior::AIB_SNIPE) { |
| 1004 |
if (robot_is_thief(robptr)) |
| 1005 |
ailp->mode = ai_mode::AIM_THIEF_ATTACK; // It gets bashed in create_n_segment_path |
| 1006 |
else |
| 1007 |
ailp->mode = ai_mode::AIM_SNIPE_FIRE; // It gets bashed in create_n_segment_path |
| 1008 |
} else |
| 1009 |
#endif |
| 1010 |
{ |
| 1011 |
ailp->mode = ai_mode::AIM_RUN_FROM_OBJECT; // It gets bashed in create_n_segment_path |
| 1012 |
} |
| 1013 |
} |
| 1014 |
#if DXX_BUILD_DESCENT == 1 |
| 1015 |
else { |
| 1016 |
ailp->mode = ai_mode::AIM_STILL; |
| 1017 |
} |
| 1018 |
return; |
| 1019 |
#elif DXX_BUILD_DESCENT == 2 |
| 1020 |
else if (robot_is_companion(robptr) == 0) { |
| 1021 |
ailp->mode = ai_mode::AIM_STILL; |
| 1022 |
aip->path_length = 0; |
| 1023 |
return; |
| 1024 |
} |
| 1025 |
#endif |
| 1026 |
} |
| 1027 |
|
| 1028 |
#if DXX_BUILD_DESCENT == 1 |
| 1029 |
Assert((aip->PATH_DIR == -1) || (aip->PATH_DIR == 1)); |
| 1030 |
|
| 1031 |
if ((aip->SUBMODE == AISM_HIDING) && (aip->behavior == ai_behavior::AIB_HIDE)) |
| 1032 |
return; |
| 1033 |
#endif |
| 1034 |
|
| 1035 |
goal_point = Point_segs[aip->hide_index + aip->cur_path_index].point; |
| 1036 |
auto dist_to_goal = vm_vec_dist_quick(goal_point, obj.pos); |
| 1037 |
|
| 1038 |
// If running from player, only run until can't be seen. |
| 1039 |
if (ailp->mode == ai_mode::AIM_RUN_FROM_OBJECT) { |
| 1040 |
if (player_visibility == player_visibility_state::no_line_of_sight && ailp->player_awareness_type == player_awareness_type_t::PA_NONE) |
| 1041 |
{ |
| 1042 |
fix vel_scale; |
| 1043 |
|
| 1044 |
vel_scale = F1_0 - FrameTime/2; |
| 1045 |
if (vel_scale < F1_0/2) |
| 1046 |
vel_scale = F1_0/2; |
| 1047 |
|
| 1048 |
vm_vec_scale(obj.mtype.phys_info.velocity, vel_scale); |
| 1049 |
|
| 1050 |
return; |
| 1051 |
} else |
| 1052 |
#if DXX_BUILD_DESCENT == 2 |
| 1053 |
if (!(d_tick_count ^ ((objp) & 0x07))) |
| 1054 |
#endif |
| 1055 |
{ // Done 1/8 ticks. |
| 1056 |
// If player on path (beyond point robot is now at), then create a new path. |
| 1057 |
point_seg *curpsp = &Point_segs[aip->hide_index]; |
| 1058 |
auto player_segnum = ConsoleObject->segnum; |
| 1059 |
int i; |
| 1060 |
|
| 1061 |
// This is probably being done every frame, which is wasteful. |
| 1062 |
for (i=aip->cur_path_index; i<aip->path_length; i++) { |
| 1063 |
if (curpsp[i].segnum == player_segnum) { |
| 1064 |
create_n_segment_path(objp, robptr, AVOID_SEG_LENGTH, player_segnum != obj.segnum ? player_segnum : segment_none); |
| 1065 |
#if DXX_BUILD_DESCENT == 1 |
| 1066 |
Assert(aip->path_length != 0); |
| 1067 |
#endif |
| 1068 |
ailp->mode = ai_mode::AIM_RUN_FROM_OBJECT; // It gets bashed in create_n_segment_path |
| 1069 |
break; |
| 1070 |
} |
| 1071 |
} |
| 1072 |
if (player_is_visible(player_visibility)) |
| 1073 |
{ |
| 1074 |
ailp->player_awareness_type = player_awareness_type_t::PA_NEARBY_ROBOT_FIRED; |
| 1075 |
ailp->player_awareness_time = F1_0; |
| 1076 |
} |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
if (aip->cur_path_index < 0) { |
| 1081 |
aip->cur_path_index = 0; |
| 1082 |
} else if (aip->cur_path_index >= aip->path_length) { |
| 1083 |
if (ailp->mode == ai_mode::AIM_RUN_FROM_OBJECT) { |
| 1084 |
create_n_segment_path(objp, robptr, AVOID_SEG_LENGTH, ConsoleObject->segnum); |
| 1085 |
ailp->mode = ai_mode::AIM_RUN_FROM_OBJECT; // It gets bashed in create_n_segment_path |
| 1086 |
#if DXX_BUILD_DESCENT == 2 |
| 1087 |
Assert(aip->path_length != 0); |
| 1088 |
#endif |
| 1089 |
} else { |
| 1090 |
aip->cur_path_index = aip->path_length-1; |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
goal_point = Point_segs[aip->hide_index + aip->cur_path_index].point; |
| 1095 |
|
| 1096 |
// If near goal, pick another goal point. |
| 1097 |
forced_break = 0; // Gets set for short paths. |
| 1098 |
original_dir = aip->PATH_DIR; |
| 1099 |
original_index = aip->cur_path_index; |
| 1100 |
const vm_distance threshold_distance{fixmul(vm_vec_mag_quick(obj.mtype.phys_info.velocity), FrameTime)*2 + F1_0*2}; |
| 1101 |
|
| 1102 |
#if DXX_BUILD_DESCENT == 2 |
| 1103 |
new_goal_point = Point_segs[aip->hide_index + aip->cur_path_index].point; |
| 1104 |
|
| 1105 |
//--Int3_if(((aip->cur_path_index >= 0) && (aip->cur_path_index < aip->path_length))); |
| 1106 |
#endif |
| 1107 |
|
| 1108 |
while ((dist_to_goal < threshold_distance) && !forced_break) { |
| 1109 |
|
| 1110 |
// Advance to next point on path. |
| 1111 |
aip->cur_path_index += aip->PATH_DIR; |
| 1112 |
|
| 1113 |
// See if next point wraps past end of path (in either direction), and if so, deal with it based on mode. |
| 1114 |
if ((aip->cur_path_index >= aip->path_length) || (aip->cur_path_index < 0)) { |
| 1115 |
#if DXX_BUILD_DESCENT == 2 |
| 1116 |
// Buddy bot. If he's in mode to get away from player and at end of line, |
| 1117 |
// if player visible, then make a new path, else just return. |
| 1118 |
if (robot_is_companion(robptr)) { |
| 1119 |
if (BuddyState.Escort_special_goal == ESCORT_GOAL_SCRAM) |
| 1120 |
{ |
| 1121 |
if (player_is_visible(player_visibility)) |
| 1122 |
{ |
| 1123 |
create_n_segment_path(objp, robptr, 16 + d_rand() * 16, segment_none); |
| 1124 |
aip->path_length = polish_path(objp, &Point_segs[aip->hide_index], aip->path_length); |
| 1125 |
Assert(aip->path_length != 0); |
| 1126 |
ailp->mode = ai_mode::AIM_WANDER; // Special buddy mode. |
| 1127 |
//--Int3_if(((aip->cur_path_index >= 0) && (aip->cur_path_index < aip->path_length))); |
| 1128 |
return; |
| 1129 |
} else { |
| 1130 |
ailp->mode = ai_mode::AIM_WANDER; // Special buddy mode. |
| 1131 |
obj.mtype.phys_info.velocity = {}; |
| 1132 |
obj.mtype.phys_info.rotvel = {}; |
| 1133 |
//!!Assert((aip->cur_path_index >= 0) && (aip->cur_path_index < aip->path_length)); |
| 1134 |
return; |
| 1135 |
} |
| 1136 |
} |
| 1137 |
} |
| 1138 |
#endif |
| 1139 |
|
| 1140 |
#if DXX_BUILD_DESCENT == 1 |
| 1141 |
if (ailp->mode == ai_mode::AIM_HIDE) { |
| 1142 |
ailp->mode = ai_mode::AIM_STILL; |
| 1143 |
return; // Stay here until bonked or hit by player. |
| 1144 |
} |
| 1145 |
#elif DXX_BUILD_DESCENT == 2 |
| 1146 |
if (aip->behavior == ai_behavior::AIB_FOLLOW) { |
| 1147 |
create_n_segment_path(objp, robptr, 10, ConsoleObject->segnum); |
| 1148 |
//--Int3_if(((aip->cur_path_index >= 0) && (aip->cur_path_index < aip->path_length))); |
| 1149 |
} |
| 1150 |
#endif |
| 1151 |
else if (aip->behavior == ai_behavior::AIB_STATION) { |
| 1152 |
create_path_to_station(objp, robptr, 15); |
| 1153 |
if ((aip->hide_segment != Point_segs[aip->hide_index+aip->path_length-1].segnum) |
| 1154 |
#if DXX_BUILD_DESCENT == 2 |
| 1155 |
|| (aip->path_length == 0) |
| 1156 |
#endif |
| 1157 |
) { |
| 1158 |
ailp->mode = ai_mode::AIM_STILL; |
| 1159 |
} |
| 1160 |
return; |
| 1161 |
} else if (ailp->mode == ai_mode::AIM_FOLLOW_PATH |
| 1162 |
#if DXX_BUILD_DESCENT == 1 |
| 1163 |
&& (aip->behavior != ai_behavior::AIB_FOLLOW_PATH) |
| 1164 |
#endif |
| 1165 |
) { |
| 1166 |
create_path_to_believed_player_segment(objp, robptr, 10, create_path_safety_flag::safe); |
| 1167 |
#if DXX_BUILD_DESCENT == 2 |
| 1168 |
if (aip->hide_segment != Point_segs[aip->hide_index+aip->path_length-1].segnum) { |
| 1169 |
ailp->mode = ai_mode::AIM_STILL; |
| 1170 |
return; |
| 1171 |
} |
| 1172 |
#endif |
| 1173 |
} else if (ailp->mode == ai_mode::AIM_RUN_FROM_OBJECT) { |
| 1174 |
create_n_segment_path(objp, robptr, AVOID_SEG_LENGTH, ConsoleObject->segnum); |
| 1175 |
ailp->mode = ai_mode::AIM_RUN_FROM_OBJECT; // It gets bashed in create_n_segment_path |
| 1176 |
#if DXX_BUILD_DESCENT == 2 |
| 1177 |
if (aip->path_length < 1) { |
| 1178 |
create_n_segment_path(objp, robptr, AVOID_SEG_LENGTH, ConsoleObject->segnum); |
| 1179 |
ailp->mode = ai_mode::AIM_RUN_FROM_OBJECT; // It gets bashed in create_n_segment_path |
| 1180 |
if (aip->path_length < 1) { |
| 1181 |
aip->behavior = ai_behavior::AIB_NORMAL; |
| 1182 |
ailp->mode = ai_mode::AIM_STILL; |
| 1183 |
return; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
//--Int3_if(((aip->cur_path_index >= 0) && (aip->cur_path_index < aip->path_length))); |
| 1187 |
#endif |
| 1188 |
} else { |
| 1189 |
// Reached end of the line. First see if opposite end point is reachable, and if so, go there. |
| 1190 |
// If not, turn around. |
| 1191 |
int opposite_end_index; |
| 1192 |
fvi_info hit_data; |
| 1193 |
|
| 1194 |
// See which end we're nearer and look at the opposite end point. |
| 1195 |
if (abs(aip->cur_path_index - aip->path_length) < aip->cur_path_index) { |
| 1196 |
// Nearer to far end (ie, index not 0), so try to reach 0. |
| 1197 |
opposite_end_index = 0; |
| 1198 |
} else { |
| 1199 |
// Nearer to 0 end, so try to reach far end. |
| 1200 |
opposite_end_index = aip->path_length-1; |
| 1201 |
} |
| 1202 |
|
| 1203 |
const auto fate = find_vector_intersection(fvi_query{ |
| 1204 |
obj.pos, |
| 1205 |
Point_segs[aip->hide_index + opposite_end_index].point, |
| 1206 |
fvi_query::unused_ignore_obj_list, |
| 1207 |
fvi_query::unused_LevelUniqueObjectState, |
| 1208 |
fvi_query::unused_Robot_info, |
| 1209 |
0, //what about trans walls??? |
| 1210 |
objp, |
| 1211 |
}, obj.segnum, obj.size, hit_data); |
| 1212 |
if (fate != fvi_hit_type::Wall) |
| 1213 |
{ |
| 1214 |
// We can be circular! Do it! |
| 1215 |
// Path direction is unchanged. |
| 1216 |
aip->cur_path_index = opposite_end_index; |
| 1217 |
} else { |
| 1218 |
aip->PATH_DIR = -aip->PATH_DIR; |
| 1219 |
#if DXX_BUILD_DESCENT == 2 |
| 1220 |
aip->cur_path_index += aip->PATH_DIR; |
| 1221 |
#endif |
| 1222 |
} |
| 1223 |
} |
| 1224 |
break; |
| 1225 |
} else { |
| 1226 |
new_goal_point = Point_segs[aip->hide_index + aip->cur_path_index].point; |
| 1227 |
goal_point = new_goal_point; |
| 1228 |
dist_to_goal = vm_vec_dist_quick(goal_point, obj.pos); |
| 1229 |
} |
| 1230 |
|
| 1231 |
// If went all the way around to original point, in same direction, then get out of here! |
| 1232 |
if ((aip->cur_path_index == original_index) && (aip->PATH_DIR == original_dir)) { |
| 1233 |
create_path_to_believed_player_segment(objp, robptr, 3, create_path_safety_flag::safe); |
| 1234 |
forced_break = 1; |
| 1235 |
} |
| 1236 |
} // end while |
| 1237 |
|
| 1238 |
// Set velocity (objp->mtype.phys_info.velocity) and orientation (objp->orient) for this object. |
| 1239 |
ai_path_set_orient_and_vel(Robot_info, objp, goal_point |
| 1240 |
#if DXX_BUILD_DESCENT == 2 |
| 1241 |
, player_visibility, vec_to_player |
| 1242 |
#endif |
| 1243 |
); |
| 1244 |
} |
| 1245 |
|
| 1246 |
} |
| 1247 |
|
| 1248 |
namespace { |
| 1249 |
|
| 1250 |
int Last_tick_garbage_collected; |
| 1251 |
|
| 1252 |
struct obj_path { |
| 1253 |
short path_start; |
| 1254 |
objnum_t objnum; |
| 1255 |
}; |
| 1256 |
|
| 1257 |
static int path_index_compare(const void *const v1, const void *const v2) |
| 1258 |
{ |
| 1259 |
const auto i1 = reinterpret_cast<const obj_path *>(v1); |
| 1260 |
const auto i2 = reinterpret_cast<const obj_path *>(v2); |
| 1261 |
if (i1->path_start < i2->path_start) |
| 1262 |
return -1; |
| 1263 |
else if (i1->path_start == i2->path_start) |
| 1264 |
return 0; |
| 1265 |
else |
| 1266 |
return 1; |
| 1267 |
} |
| 1268 |
|
| 1269 |
} |
| 1270 |
|
| 1271 |
namespace dsx { |
| 1272 |
namespace { |
| 1273 |
|
| 1274 |
// ---------------------------------------------------------------------------------------------------------- |
| 1275 |
// Set orientation matrix and velocity for objp based on its desire to get to a point. |
| 1276 |
void ai_path_set_orient_and_vel(const d_robot_info_array &Robot_info, object &objp, const vms_vector &goal_point |
| 1277 |
#if DXX_BUILD_DESCENT == 2 |
| 1278 |
, const player_visibility_state player_visibility, const vms_vector *const vec_to_player |
| 1279 |
#endif |
| 1280 |
) |
| 1281 |
{ |
| 1282 |
vms_vector cur_vel = objp.mtype.phys_info.velocity; |
| 1283 |
vms_vector cur_pos = objp.pos; |
| 1284 |
fix speed_scale; |
| 1285 |
fix dot; |
| 1286 |
auto &robptr = Robot_info[get_robot_id(objp)]; |
| 1287 |
fix max_speed; |
| 1288 |
|
| 1289 |
// If evading player, use highest difficulty level speed, plus something based on diff level |
| 1290 |
const auto Difficulty_level = GameUniqueState.Difficulty_level; |
| 1291 |
max_speed = robptr.max_speed[Difficulty_level]; |
| 1292 |
ai_local *ailp = &objp.ctype.ai_info.ail; |
| 1293 |
if (ailp->mode == ai_mode::AIM_RUN_FROM_OBJECT |
| 1294 |
#if DXX_BUILD_DESCENT == 2 |
| 1295 |
|| objp.ctype.ai_info.behavior == ai_behavior::AIB_SNIPE |
| 1296 |
#endif |
| 1297 |
) |
| 1298 |
max_speed = max_speed*3/2; |
| 1299 |
|
| 1300 |
auto norm_vec_to_goal = vm_vec_normalized_quick(vm_vec_build_sub(goal_point, cur_pos)); |
| 1301 |
auto norm_cur_vel = vm_vec_normalized_quick(cur_vel); |
| 1302 |
const auto norm_fvec = vm_vec_normalized_quick(objp.orient.fvec); |
| 1303 |
|
| 1304 |
dot = vm_vec_build_dot(norm_vec_to_goal, norm_fvec); |
| 1305 |
|
| 1306 |
// If very close to facing opposite desired vector, perturb vector |
| 1307 |
if (dot < -15*F1_0/16) { |
| 1308 |
norm_cur_vel = norm_vec_to_goal; |
| 1309 |
} else { |
| 1310 |
norm_cur_vel.x += norm_vec_to_goal.x/2/(static_cast<float>(DESIGNATED_GAME_FRAMETIME)/FrameTime); |
| 1311 |
norm_cur_vel.y += norm_vec_to_goal.y/2/(static_cast<float>(DESIGNATED_GAME_FRAMETIME)/FrameTime); |
| 1312 |
norm_cur_vel.z += norm_vec_to_goal.z/2/(static_cast<float>(DESIGNATED_GAME_FRAMETIME)/FrameTime); |
| 1313 |
} |
| 1314 |
|
| 1315 |
vm_vec_normalize_quick(norm_cur_vel); |
| 1316 |
|
| 1317 |
// Set speed based on this robot type's maximum allowed speed and how hard it is turning. |
| 1318 |
// How hard it is turning is based on the dot product of (vector to goal) and (current velocity vector) |
| 1319 |
// Note that since 3*F1_0/4 is added to dot product, it is possible for the robot to back up. |
| 1320 |
|
| 1321 |
// Set speed and orientation. |
| 1322 |
if (dot < 0) |
| 1323 |
dot /= -4; |
| 1324 |
|
| 1325 |
#if DXX_BUILD_DESCENT == 2 |
| 1326 |
// If in snipe mode, can move fast even if not facing that direction. |
| 1327 |
if (objp.ctype.ai_info.behavior == ai_behavior::AIB_SNIPE) |
| 1328 |
if (dot < F1_0/2) |
| 1329 |
dot = (dot + F1_0)/2; |
| 1330 |
#endif |
| 1331 |
|
| 1332 |
speed_scale = fixmul(max_speed, dot); |
| 1333 |
vm_vec_scale(norm_cur_vel, speed_scale); |
| 1334 |
objp.mtype.phys_info.velocity = norm_cur_vel; |
| 1335 |
|
| 1336 |
fix rate; |
| 1337 |
if (ailp->mode == ai_mode::AIM_RUN_FROM_OBJECT |
| 1338 |
#if DXX_BUILD_DESCENT == 2 |
| 1339 |
|| robot_is_companion(robptr) == 1 || objp.ctype.ai_info.behavior == ai_behavior::AIB_SNIPE |
| 1340 |
#endif |
| 1341 |
) { |
| 1342 |
#if DXX_BUILD_DESCENT == 2 |
| 1343 |
if (ailp->mode == ai_mode::AIM_SNIPE_RETREAT_BACKWARDS) { |
| 1344 |
if (player_is_visible(player_visibility) && vec_to_player) |
| 1345 |
norm_vec_to_goal = *vec_to_player; |
| 1346 |
else |
| 1347 |
vm_vec_negate(norm_vec_to_goal); |
| 1348 |
} |
| 1349 |
#endif |
| 1350 |
rate = robptr.turn_time[Difficulty_level_type::_4] / 2; |
| 1351 |
} else |
| 1352 |
rate = robptr.turn_time[Difficulty_level]; |
| 1353 |
ai_turn_towards_vector(norm_vec_to_goal, objp, rate); |
| 1354 |
} |
| 1355 |
|
| 1356 |
// ---------------------------------------------------------------------------------------------------------- |
| 1357 |
// Garbage colledion -- Free all unused records in Point_segs and compress all paths. |
| 1358 |
void ai_path_garbage_collect() |
| 1359 |
{ |
| 1360 |
int free_path_index{0}; |
| 1361 |
int num_path_objects{0}; |
| 1362 |
int objind; |
| 1363 |
obj_path object_list[MAX_OBJECTS]; |
| 1364 |
|
| 1365 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 1366 |
auto &vcobjptridx = Objects.vcptridx; |
| 1367 |
auto &vmobjptridx = Objects.vmptridx; |
| 1368 |
|
| 1369 |
Last_tick_garbage_collected = d_tick_count; |
| 1370 |
|
| 1371 |
#if PATH_VALIDATION |
| 1372 |
validate_all_paths(); |
| 1373 |
#endif |
| 1374 |
// Create a list of objects which have paths of length 1 or more. |
| 1375 |
range_for (const auto &&objp, vcobjptridx) |
| 1376 |
{ |
| 1377 |
if (objp->type == OBJ_ROBOT && (objp->control_source == object::control_type::ai |
| 1378 |
#if DXX_BUILD_DESCENT == 2 |
| 1379 |
|| objp->control_source == object::control_type::morph |
| 1380 |
#endif |
| 1381 |
)) { |
| 1382 |
const auto &aip = objp->ctype.ai_info; |
| 1383 |
if (aip.path_length) { |
| 1384 |
object_list[num_path_objects].path_start = aip.hide_index; |
| 1385 |
object_list[num_path_objects++].objnum = objp; |
| 1386 |
} |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
qsort(object_list, num_path_objects, sizeof(object_list[0]), |
| 1391 |
path_index_compare); |
| 1392 |
|
| 1393 |
for (objind=0; objind < num_path_objects; objind++) { |
| 1394 |
ai_static *aip; |
| 1395 |
int i; |
| 1396 |
int old_index; |
| 1397 |
|
| 1398 |
auto objnum = object_list[objind].objnum; |
| 1399 |
object &objp = vmobjptridx(objnum); |
| 1400 |
aip = &objp.ctype.ai_info; |
| 1401 |
old_index = aip->hide_index; |
| 1402 |
|
| 1403 |
aip->hide_index = free_path_index; |
| 1404 |
for (i=0; i<aip->path_length; i++) |
| 1405 |
Point_segs[free_path_index++] = Point_segs[old_index++]; |
| 1406 |
} |
| 1407 |
|
| 1408 |
Point_segs_free_ptr = Point_segs.begin() + free_path_index; |
| 1409 |
|
| 1410 |
#ifndef NDEBUG |
| 1411 |
{ |
| 1412 |
auto &vcobjptr = Objects.vcptr; |
| 1413 |
for (auto &obj : vcobjptr) |
| 1414 |
{ |
| 1415 |
const auto &aip = obj.ctype.ai_info; |
| 1416 |
|
| 1417 |
if (obj.type == OBJ_ROBOT && obj.control_source == object::control_type::ai) |
| 1418 |
if ((aip.hide_index + aip.path_length > Point_segs_free_ptr - Point_segs) && (aip.path_length>0)) |
| 1419 |
Int3(); // Contact Mike: Debug trap for nasty, elusive bug. |
| 1420 |
} |
| 1421 |
|
| 1422 |
validate_all_paths(); |
| 1423 |
} |
| 1424 |
#endif |
| 1425 |
} |
| 1426 |
|
| 1427 |
// ----------------------------------------------------------------------------- |
| 1428 |
// Do garbage collection if not been done for awhile, or things getting really critical. |
| 1429 |
void maybe_ai_path_garbage_collect(void) |
| 1430 |
{ |
| 1431 |
if (Point_segs_free_ptr - Point_segs > MAX_POINT_SEGS - MAX_PATH_LENGTH) { |
| 1432 |
if (Last_tick_garbage_collected+1 >= d_tick_count) { |
| 1433 |
// This is kind of bad. Garbage collected last frame or this frame. |
| 1434 |
// Just destroy all paths. Too bad for the robots. They are memory wasteful. |
| 1435 |
ai_reset_all_paths(); |
| 1436 |
} else { |
| 1437 |
// We are really close to full, but didn't just garbage collect, so maybe this is recoverable. |
| 1438 |
ai_path_garbage_collect(); |
| 1439 |
} |
| 1440 |
} else if (Point_segs_free_ptr - Point_segs > 3*MAX_POINT_SEGS/4) { |
| 1441 |
if (Last_tick_garbage_collected + 16 < d_tick_count) { |
| 1442 |
ai_path_garbage_collect(); |
| 1443 |
} |
| 1444 |
} else if (Point_segs_free_ptr - Point_segs > MAX_POINT_SEGS/2) { |
| 1445 |
if (Last_tick_garbage_collected + 256 < d_tick_count) { |
| 1446 |
ai_path_garbage_collect(); |
| 1447 |
} |
| 1448 |
} |
| 1449 |
} |
| 1450 |
} |
| 1451 |
|
| 1452 |
// ----------------------------------------------------------------------------- |
| 1453 |
// Reset all paths. Do garbage collection. |
| 1454 |
// Should be called at the start of each level. |
| 1455 |
void ai_reset_all_paths(void) |
| 1456 |
{ |
| 1457 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 1458 |
auto &vmobjptr = Objects.vmptr; |
| 1459 |
for (auto &obj : vmobjptr) |
| 1460 |
{ |
| 1461 |
if (obj.type == OBJ_ROBOT && obj.control_source == object::control_type::ai) |
| 1462 |
{ |
| 1463 |
obj.ctype.ai_info.hide_index = -1; |
| 1464 |
obj.ctype.ai_info.path_length = 0; |
| 1465 |
} |
| 1466 |
} |
| 1467 |
|
| 1468 |
ai_path_garbage_collect(); |
| 1469 |
} |
| 1470 |
|
| 1471 |
// --------------------------------------------------------------------------------------------------------- |
| 1472 |
// Probably called because a robot bashed a wall, getting a bunch of retries. |
| 1473 |
// Try to resume path. |
| 1474 |
void attempt_to_resume_path(const d_robot_info_array &Robot_info, const vmobjptridx_t objp) |
| 1475 |
{ |
| 1476 |
ai_static *aip = &objp->ctype.ai_info; |
| 1477 |
int new_path_index; |
| 1478 |
auto &robptr = Robot_info[get_robot_id(objp)]; |
| 1479 |
|
| 1480 |
if (aip->behavior == ai_behavior::AIB_STATION |
| 1481 |
#if DXX_BUILD_DESCENT == 2 |
| 1482 |
&& robptr.companion != 1 |
| 1483 |
#endif |
| 1484 |
) |
| 1485 |
if (d_rand() > 8192) { |
| 1486 |
ai_local *ailp = &objp->ctype.ai_info.ail; |
| 1487 |
|
| 1488 |
aip->hide_segment = objp->segnum; |
| 1489 |
ailp->mode = ai_mode::AIM_STILL; |
| 1490 |
} |
| 1491 |
|
| 1492 |
new_path_index = aip->cur_path_index - aip->PATH_DIR; |
| 1493 |
|
| 1494 |
if ((new_path_index >= 0) && (new_path_index < aip->path_length)) { |
| 1495 |
aip->cur_path_index = new_path_index; |
| 1496 |
} else { |
| 1497 |
// At end of line and have nowhere to go. |
| 1498 |
move_towards_segment_center(robptr, LevelSharedSegmentState, objp); |
| 1499 |
create_path_to_station(objp, robptr, 15); |
| 1500 |
} |
| 1501 |
} |
| 1502 |
|
| 1503 |
// ---------------------------------------------------------------------------------------------------------- |
| 1504 |
// DEBUG FUNCTIONS FOLLOW |
| 1505 |
// ---------------------------------------------------------------------------------------------------------- |
| 1506 |
|
| 1507 |
#if DXX_USE_EDITOR |
| 1508 |
namespace { |
| 1509 |
|
| 1510 |
dxx_compiler_attribute_used |
| 1511 |
static void test_create_path_many(fvmobjptridx &vmobjptridx, const d_level_shared_segment_state &LevelSharedSegmentState, const unsigned long seed) |
| 1512 |
{ |
| 1513 |
auto &SSegments = LevelSharedSegmentState.get_segments(); |
| 1514 |
std::minstd_rand mrd{seed}; |
| 1515 |
std::uniform_int_distribution<std::underlying_type_t<segnum_t>> uid{0u, SSegments.get_count()}; |
| 1516 |
for (const auto i : constant_xrange<std::size_t, 0, 1000u>{}) |
| 1517 |
{ |
| 1518 |
(void)i; |
| 1519 |
const vcsegidx_t start_segment{segnum_t{uid(mrd)}}; |
| 1520 |
const vcsegidx_t end_segment{segnum_t{uid(mrd)}}; |
| 1521 |
std::array<point_seg, 200> point_segs; |
| 1522 |
create_path_points(vmobjptridx(object_first), create_path_unused_robot_info, start_segment, end_segment, point_segs.begin(), MAX_PATH_LENGTH, create_path_random_flag::nonrandom, create_path_safety_flag::unsafe, segment_none); |
| 1523 |
} |
| 1524 |
} |
| 1525 |
|
| 1526 |
dxx_compiler_attribute_used |
| 1527 |
static void test_create_path(fvmobjptridx &vmobjptridx) |
| 1528 |
{ |
| 1529 |
std::array<point_seg, 200> point_segs; |
| 1530 |
|
| 1531 |
create_path_points(vmobjptridx(object_first), create_path_unused_robot_info, Cursegp, Markedsegp, point_segs.begin(), MAX_PATH_LENGTH, create_path_random_flag::nonrandom, create_path_safety_flag::unsafe, segment_none); |
| 1532 |
} |
| 1533 |
|
| 1534 |
// For all segments in mine, create paths to all segments in mine, print results. |
| 1535 |
dxx_compiler_attribute_used |
| 1536 |
static void test_create_all_paths(fvmobjptridx &vmobjptridx, fvcsegptridx &vcsegptridx) |
| 1537 |
{ |
| 1538 |
Point_segs_free_ptr = Point_segs.begin(); |
| 1539 |
|
| 1540 |
range_for (const auto &&segp0, vcsegptridx) |
| 1541 |
{ |
| 1542 |
const shared_segment &sseg0 = segp0; |
| 1543 |
if (sseg0.segnum != segment_none) |
| 1544 |
{ |
| 1545 |
for (const auto &&segp1 : partial_range(vcsegptridx, segp0.get_unchecked_index(), vcsegptridx.count())) |
| 1546 |
{ |
| 1547 |
const shared_segment &sseg1 = segp1; |
| 1548 |
if (sseg1.segnum != segment_none) |
| 1549 |
{ |
| 1550 |
create_path_points(vmobjptridx(object_first), create_path_unused_robot_info, segp0, segp1, Point_segs_free_ptr, MAX_PATH_LENGTH, create_path_random_flag::nonrandom, create_path_safety_flag::unsafe, segment_none); |
| 1551 |
} |
| 1552 |
} |
| 1553 |
} |
| 1554 |
} |
| 1555 |
} |
| 1556 |
|
| 1557 |
short Player_path_length{0}; |
| 1558 |
int Player_hide_index=-1; |
| 1559 |
int Player_cur_path_index{0}; |
| 1560 |
int Player_following_path_flag{0}; |
| 1561 |
|
| 1562 |
// ------------------------------------------------------------------------------------------------------------------ |
| 1563 |
// Set orientation matrix and velocity for objp based on its desire to get to a point. |
| 1564 |
static void player_path_set_orient_and_vel(object &objp, const vms_vector &goal_point) |
| 1565 |
{ |
| 1566 |
const auto &cur_vel = objp.mtype.phys_info.velocity; |
| 1567 |
const auto &cur_pos = objp.pos; |
| 1568 |
fix speed_scale; |
| 1569 |
fix dot; |
| 1570 |
|
| 1571 |
const fix max_speed = F1_0*50; |
| 1572 |
|
| 1573 |
const auto norm_vec_to_goal = vm_vec_normalized_quick(vm_vec_build_sub(goal_point, cur_pos)); |
| 1574 |
auto norm_cur_vel = vm_vec_normalized_quick(cur_vel); |
| 1575 |
const auto &&norm_fvec = vm_vec_normalized_quick(objp.orient.fvec); |
| 1576 |
|
| 1577 |
dot = vm_vec_build_dot(norm_vec_to_goal, norm_fvec); |
| 1578 |
|
| 1579 |
// If very close to facing opposite desired vector, perturb vector |
| 1580 |
if (dot < -15*F1_0/16) { |
| 1581 |
norm_cur_vel = norm_vec_to_goal; |
| 1582 |
} else { |
| 1583 |
norm_cur_vel.x += norm_vec_to_goal.x/2/(static_cast<float>(DESIGNATED_GAME_FRAMETIME)/FrameTime); |
| 1584 |
norm_cur_vel.y += norm_vec_to_goal.y/2/(static_cast<float>(DESIGNATED_GAME_FRAMETIME)/FrameTime); |
| 1585 |
norm_cur_vel.z += norm_vec_to_goal.z/2/(static_cast<float>(DESIGNATED_GAME_FRAMETIME)/FrameTime); |
| 1586 |
} |
| 1587 |
|
| 1588 |
vm_vec_normalize_quick(norm_cur_vel); |
| 1589 |
|
| 1590 |
// Set speed based on this robot type's maximum allowed speed and how hard it is turning. |
| 1591 |
// How hard it is turning is based on the dot product of (vector to goal) and (current velocity vector) |
| 1592 |
// Note that since 3*F1_0/4 is added to dot product, it is possible for the robot to back up. |
| 1593 |
|
| 1594 |
// Set speed and orientation. |
| 1595 |
if (dot < 0) |
| 1596 |
dot /= 4; |
| 1597 |
|
| 1598 |
speed_scale = fixmul(max_speed, dot); |
| 1599 |
vm_vec_scale(norm_cur_vel, speed_scale); |
| 1600 |
objp.mtype.phys_info.velocity = norm_cur_vel; |
| 1601 |
physics_turn_towards_vector(norm_vec_to_goal, objp, F1_0); |
| 1602 |
} |
| 1603 |
|
| 1604 |
} |
| 1605 |
|
| 1606 |
// ---------------------------------------------------------------------------------------------------------- |
| 1607 |
// Optimization: If current velocity will take robot near goal, don't change velocity |
| 1608 |
void player_follow_path(object &objp) |
| 1609 |
{ |
| 1610 |
vms_vector goal_point; |
| 1611 |
int count, forced_break, original_index; |
| 1612 |
int goal_seg; |
| 1613 |
|
| 1614 |
if (!Player_following_path_flag) |
| 1615 |
return; |
| 1616 |
|
| 1617 |
if (Player_hide_index == -1) |
| 1618 |
return; |
| 1619 |
|
| 1620 |
if (Player_path_length < 2) |
| 1621 |
return; |
| 1622 |
|
| 1623 |
goal_point = Point_segs[Player_hide_index + Player_cur_path_index].point; |
| 1624 |
goal_seg = Point_segs[Player_hide_index + Player_cur_path_index].segnum; |
| 1625 |
Assert((goal_seg >= 0) && (goal_seg <= Highest_segment_index)); |
| 1626 |
(void)goal_seg; |
| 1627 |
auto dist_to_goal = vm_vec_dist_quick(goal_point, objp.pos); |
| 1628 |
|
| 1629 |
if (Player_cur_path_index < 0) |
| 1630 |
Player_cur_path_index = 0; |
| 1631 |
else if (Player_cur_path_index >= Player_path_length) |
| 1632 |
Player_cur_path_index = Player_path_length-1; |
| 1633 |
|
| 1634 |
goal_point = Point_segs[Player_hide_index + Player_cur_path_index].point; |
| 1635 |
|
| 1636 |
count=0; |
| 1637 |
|
| 1638 |
// If near goal, pick another goal point. |
| 1639 |
forced_break = 0; // Gets set for short paths. |
| 1640 |
//original_dir = 1; |
| 1641 |
original_index = Player_cur_path_index; |
| 1642 |
const vm_distance threshold_distance{fixmul(vm_vec_mag_quick(objp.mtype.phys_info.velocity), FrameTime)*2 + F1_0*2}; |
| 1643 |
|
| 1644 |
while ((dist_to_goal < threshold_distance) && !forced_break) { |
| 1645 |
|
| 1646 |
// ----- Debug stuff ----- |
| 1647 |
if (count++ > 20) { |
| 1648 |
break; |
| 1649 |
} |
| 1650 |
|
| 1651 |
// Advance to next point on path. |
| 1652 |
Player_cur_path_index += 1; |
| 1653 |
|
| 1654 |
// See if next point wraps past end of path (in either direction), and if so, deal with it based on mode. |
| 1655 |
if ((Player_cur_path_index >= Player_path_length) || (Player_cur_path_index < 0)) { |
| 1656 |
Player_following_path_flag = 0; |
| 1657 |
forced_break = 1; |
| 1658 |
} |
| 1659 |
|
| 1660 |
// If went all the way around to original point, in same direction, then get out of here! |
| 1661 |
if (Player_cur_path_index == original_index) { |
| 1662 |
Player_following_path_flag = 0; |
| 1663 |
forced_break = 1; |
| 1664 |
} |
| 1665 |
|
| 1666 |
goal_point = Point_segs[Player_hide_index + Player_cur_path_index].point; |
| 1667 |
dist_to_goal = vm_vec_dist_quick(goal_point, objp.pos); |
| 1668 |
|
| 1669 |
} // end while |
| 1670 |
|
| 1671 |
// Set velocity (objp->mtype.phys_info.velocity) and orientation (objp->orient) for this object. |
| 1672 |
player_path_set_orient_and_vel(objp, goal_point); |
| 1673 |
} |
| 1674 |
|
| 1675 |
namespace { |
| 1676 |
|
| 1677 |
// ------------------------------------------------------------------------------------------------------------------ |
| 1678 |
// Create path for player from current segment to goal segment. |
| 1679 |
static void create_player_path_to_segment(fvmobjptridx &vmobjptridx, segnum_t segnum) |
| 1680 |
{ |
| 1681 |
const auto objp = vmobjptridx(ConsoleObject); |
| 1682 |
Player_hide_index=-1; |
| 1683 |
Player_cur_path_index=0; |
| 1684 |
Player_following_path_flag=0; |
| 1685 |
|
| 1686 |
auto &&cr = create_path_points(objp, create_path_unused_robot_info, objp->segnum, segnum, Point_segs_free_ptr, 100, create_path_random_flag::nonrandom, create_path_safety_flag::unsafe, segment_none); |
| 1687 |
Player_path_length = cr.second; |
| 1688 |
if (cr.first == create_path_result::early) |
| 1689 |
con_printf(CON_DEBUG,"Unable to form path of length %i for myself", 100); |
| 1690 |
|
| 1691 |
Player_following_path_flag = 1; |
| 1692 |
|
| 1693 |
Player_hide_index = Point_segs_free_ptr - Point_segs; |
| 1694 |
Player_cur_path_index = 0; |
| 1695 |
Point_segs_free_ptr += Player_path_length; |
| 1696 |
if (Point_segs_free_ptr - Point_segs + MAX_PATH_LENGTH*2 > MAX_POINT_SEGS) { |
| 1697 |
//Int3(); // Contact Mike: This is curious, though not deadly. /eip++;g |
| 1698 |
ai_reset_all_paths(); |
| 1699 |
} |
| 1700 |
|
| 1701 |
} |
| 1702 |
|
| 1703 |
} |
| 1704 |
segnum_t Player_goal_segment = segment_none; |
| 1705 |
|
| 1706 |
void check_create_player_path(void) |
| 1707 |
{ |
| 1708 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 1709 |
auto &vmobjptridx = Objects.vmptridx; |
| 1710 |
if (Player_goal_segment != segment_none) |
| 1711 |
create_player_path_to_segment(vmobjptridx, Player_goal_segment); |
| 1712 |
|
| 1713 |
Player_goal_segment = segment_none; |
| 1714 |
} |
| 1715 |
|
| 1716 |
#endif |
| 1717 |
|
| 1718 |
} |
| 1719 |
|
| 1720 |
// ---------------------------------------------------------------------------------------------------------- |
| 1721 |
// DEBUG FUNCTIONS ENDED |
| 1722 |
// ---------------------------------------------------------------------------------------------------------- |
| 1723 |
|