| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/main/aiterrain.cpp $ |
| 21 | * $Revision: 7 $ |
| 22 | * $Date: 2/04/98 6:09p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Terrain specific AI stuff |
| 26 | * |
| 27 | * $Log: /DescentIII/main/aiterrain.cpp $ |
| 28 | * |
| 29 | * 7 2/04/98 6:09p Matt |
| 30 | * Changed object room number to indicate a terrain cell via a flag. Got |
| 31 | * rid of the object flag which used to indicate terrain. |
| 32 | * |
| 33 | * 6 8/20/97 3:16p Chris |
| 34 | * Working on the edge of terrain problems |
| 35 | * |
| 36 | * 5 7/31/97 3:12p Chris |
| 37 | * Fixed a problem with the new iterator based radius checker |
| 38 | * |
| 39 | * 4 7/30/97 1:31p Chris |
| 40 | * Made helicopters slightly more interesting. |
| 41 | * |
| 42 | * 3 7/29/97 12:20p Chris |
| 43 | * Incremental improvements. Fixed a memory bug. |
| 44 | * |
| 45 | * 2 7/28/97 1:19p Chris |
| 46 | * Expanding the AI system |
| 47 | * |
| 48 | * $NoKeywords: $ |
| 49 | */ |
| 50 | |
| 51 | #include "vecmat.h" |
| 52 | #include "terrain.h" |
| 53 | #include "pstypes.h" |
| 54 | #include "findintersection.h" |
| 55 | #include "aiterrain.h" |
| 56 | #include <string.h> |
| 57 | |
| 58 | #define AI_MAX_SEGS_CHECKED 200 |
| 59 | |
| 60 | static int ai_num_segs_checked = 0; |
| 61 | static uint8_t ai_terrain_check_list[((TERRAIN_WIDTH * TERRAIN_DEPTH) >> 3) + 1]; |
| 62 | static int ai_segs_checked[AI_MAX_SEGS_CHECKED]; |
| 63 | #ifdef _DEBUG |
| 64 | static int ai_num_checks_since_init = 0; |
| 65 | #endif |
| 66 | |
| 67 | static float ai_rad; |
| 68 | static ground_information *ai_ground_info_ptr; |
| 69 | |
| 70 | void ait_Init() { |
| 71 | ai_num_segs_checked = 0; |
| 72 | memset(ai_terrain_check_list, 0, ((TERRAIN_WIDTH * TERRAIN_DEPTH) >> 3) + 1); |
| 73 | |
| 74 | #ifdef _DEBUG |
| 75 | ai_num_checks_since_init = 0; |
| 76 | #endif |
| 77 | } |
| 78 | |
| 79 | static void ait_terrain_clean() { |
| 80 | int i; |
| 81 | |
| 82 | assert(ai_num_segs_checked >= 0 && ai_num_segs_checked <= AI_MAX_SEGS_CHECKED); |
| 83 | |
| 84 | for (i = 0; i < ai_num_segs_checked; i++) { |
| 85 | ASSERT((CELLNUM(ai_segs_checked[i]) >= 0) && (CELLNUM(ai_segs_checked[i]) < TERRAIN_WIDTH * TERRAIN_DEPTH)); |
| 86 | ai_terrain_check_list[CELLNUM(ai_segs_checked[i]) >> 3] = 0; |
| 87 | } |
| 88 | |
| 89 | // #ifdef _DEBUG |
| 90 | // for(i = 0; i < ((TERRAIN_WIDTH*TERRAIN_DEPTH)>>3)+1; i++) |
| 91 | // { |
| 92 | // ASSERT(ai_terrain_check_list[i] == 0); |
| 93 | // } |
| 94 | // #endif |
| 95 | |
| 96 | ai_num_segs_checked = 0; |
| 97 | } |
| 98 | |
| 99 | static void ai_check_terrain_node(int cur_node, int f_check_local_nodes) { |
| 100 | int check_x, check_y; |
| 101 | int new_node; |
| 102 | int xcounter, ycounter; |
| 103 | int xstart, xend; |
| 104 | int ystart, yend; |
| 105 | int f_ignore_terrain_in_this_node = 0; |
| 106 | |
| 107 | // Chrishack -- Note |
| 108 | |
| 109 | ASSERT(cur_node >= 0 && cur_node < TERRAIN_WIDTH * TERRAIN_DEPTH); |
| 110 | ASSERT((ai_terrain_check_list[cur_node >> 3] & (0x00000001 << (cur_node % 8))) == 0); |
| 111 | |
| 112 | // Mark the current node as visited |
| 113 | ai_terrain_check_list[cur_node >> 3] |= 0x00000001 << (cur_node % 8); |
| 114 | ai_segs_checked[ai_num_segs_checked] = MAKE_ROOMNUM(cur_node); |
| 115 | ai_num_segs_checked++; |
| 116 | ASSERT(ai_num_segs_checked < AI_MAX_SEGS_CHECKED); |
| 117 | |
| 118 | if (Terrain_seg[cur_node].y > ai_ground_info_ptr->highest_y) |
| 119 | ai_ground_info_ptr->highest_y = Terrain_seg[cur_node].y; |
| 120 | |
| 121 | if (Terrain_seg[cur_node].y < ai_ground_info_ptr->lowest_y) |
| 122 | ai_ground_info_ptr->lowest_y = Terrain_seg[cur_node].y; |
| 123 | |
| 124 | // check local nodes for any collision type, but no recursion for them :) |
| 125 | if (f_check_local_nodes) { |
| 126 | int next_y_delta; |
| 127 | // Check worst-case collisions. This includes all nodes within a radius edge of the current node |
| 128 | check_x = ai_rad / TERRAIN_SIZE + 1; |
| 129 | check_y = ai_rad / TERRAIN_SIZE + 1; |
| 130 | |
| 131 | xstart = cur_node % TERRAIN_WIDTH - check_x; |
| 132 | xend = cur_node % TERRAIN_WIDTH + check_x; |
| 133 | ystart = cur_node / TERRAIN_WIDTH - check_y; |
| 134 | yend = cur_node / TERRAIN_WIDTH + check_y; |
| 135 | |
| 136 | if (xstart < 0) |
| 137 | xstart = 0; |
| 138 | if (xend >= TERRAIN_WIDTH) |
| 139 | xend = TERRAIN_WIDTH - 1; |
| 140 | if (ystart < 0) |
| 141 | ystart = 0; |
| 142 | if (yend >= TERRAIN_DEPTH) |
| 143 | yend = TERRAIN_DEPTH - 1; |
| 144 | |
| 145 | // This should be a faster interative why to do a square with center at original position |
| 146 | new_node = TERRAIN_WIDTH * ystart + xstart; |
| 147 | next_y_delta = TERRAIN_WIDTH - (xend - xstart) - 1; |
| 148 | |
| 149 | for (ycounter = ystart; ycounter <= yend; ycounter++) { |
| 150 | for (xcounter = xstart; xcounter <= xend; xcounter++) { |
| 151 | if ((ai_terrain_check_list[new_node >> 3] & (0x00000001 << (new_node % 8))) == 0) |
| 152 | ai_check_terrain_node(new_node, 0); |
| 153 | new_node += 1; |
| 154 | } |
| 155 | |
| 156 | new_node += next_y_delta; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | // Returns true if the new point is on the terrain and false if the path results in leaving the terrain |
| 164 | bool ait_GetGroundInfo(ground_information *ground_info, vector *p0, vector *p1, float rad, angle fov) { |
| 165 | int start_node, end_node; |
| 166 | int x1, x2, y1, y2, x, y, delta_y, delta_x, change_x, change_y, length, cur_node, error_term, i; |
| 167 | |
| 168 | // Clean up the last call. This will make the info available till the next call. Maybe |
| 169 | // useful at a later date. Plus, we have multiple exits so, this nice spot so that |
| 170 | // we need only one call. :) |
| 171 | ait_terrain_clean(); |
| 172 | |
| 173 | #ifdef _DEBUG |
| 174 | ai_num_checks_since_init++; |
| 175 | #endif |
| 176 | |
| 177 | ai_ground_info_ptr = ground_info; |
| 178 | ai_rad = rad; |
| 179 | |
| 180 | start_node = GetTerrainCellFromPos(p0); |
| 181 | end_node = GetTerrainCellFromPos(p1); |
| 182 | |
| 183 | if (start_node == -1) |
| 184 | return false; |
| 185 | |
| 186 | if (end_node == -1) { |
| 187 | float delta = 1.0; |
| 188 | vector movement = *p1 - *p0; |
| 189 | |
| 190 | if (p1->x < (0.5f * TERRAIN_SIZE)) { |
| 191 | delta = (p0->x - (0.5f * TERRAIN_SIZE)) / (-movement.x); |
| 192 | } else if (p1->x > (float)(TERRAIN_WIDTH * TERRAIN_SIZE) - (0.5f * TERRAIN_SIZE)) { |
| 193 | delta = ((float)(TERRAIN_WIDTH * TERRAIN_SIZE) - (0.5f * TERRAIN_SIZE) - p0->x) / (movement.x); |
| 194 | } |
| 195 | |
| 196 | if (p1->z < (0.5f * TERRAIN_SIZE)) { |
| 197 | if ((p0->z - (0.5f * TERRAIN_SIZE)) / (-movement.z) < delta) |
| 198 | delta = (p0->z - (0.5f * TERRAIN_SIZE)) / (-movement.z); |
| 199 | } else if (p1->z > (float)(TERRAIN_DEPTH * TERRAIN_SIZE) - (0.5f * TERRAIN_SIZE)) { |
| 200 | if (((float)(TERRAIN_WIDTH * TERRAIN_SIZE) - (0.5f * TERRAIN_SIZE) - p0->z) / (movement.z) < delta) |
| 201 | delta = ((float)(TERRAIN_WIDTH * TERRAIN_SIZE) - (0.5f * TERRAIN_SIZE) - p0->z) / (movement.z); |
| 202 | } |
| 203 | |
| 204 | *p1 = *p0 + delta * movement; |
| 205 | |
| 206 | end_node = GetTerrainCellFromPos(p1); |
| 207 | ASSERT(end_node != -1); |
| 208 | } |
| 209 | |
| 210 | ai_ground_info_ptr->highest_y = Terrain_seg[start_node].y; |
| 211 | ai_ground_info_ptr->lowest_y = Terrain_seg[start_node].y; |
| 212 | |
| 213 | // Determine the start end end nodes |
| 214 | x1 = start_node % TERRAIN_WIDTH; |
| 215 | y1 = start_node / TERRAIN_WIDTH; |
| 216 | |
| 217 | x2 = end_node % TERRAIN_WIDTH; |
| 218 | y2 = end_node / TERRAIN_WIDTH; |
| 219 | |
| 220 | x = x1; |
| 221 | y = y1; |
| 222 | |
| 223 | // How many nodes did I move? |
| 224 | delta_x = x2 - x1; |
| 225 | delta_y = y2 - y1; |
| 226 | |
| 227 | // check the current node for collsions (if we are done, return) |
| 228 | ASSERT((ai_terrain_check_list[start_node >> 3] & (0x00000001 << (start_node % 8))) == 0); |
| 229 | ai_check_terrain_node(start_node, 1); |
| 230 | if (delta_x == 0 && delta_y == 0) |
| 231 | return true; |
| 232 | |
| 233 | // Do a Breshenham line algorithm |
| 234 | if (delta_x < 0) { |
| 235 | change_x = -1; |
| 236 | delta_x = -delta_x; |
| 237 | |
| 238 | } else { |
| 239 | change_x = 1; |
| 240 | } |
| 241 | |
| 242 | if (delta_y < 0) { |
| 243 | change_y = -1; |
| 244 | delta_y = -delta_y; |
| 245 | |
| 246 | } else { |
| 247 | change_y = 1; |
| 248 | } |
| 249 | |
| 250 | error_term = 0; |
| 251 | i = 1; |
| 252 | |
| 253 | if (delta_x < delta_y) { |
| 254 | length = delta_y + 1; |
| 255 | |
| 256 | while (i < length) { |
| 257 | y += change_y; |
| 258 | error_term += delta_x; |
| 259 | |
| 260 | if (error_term >= delta_y) { |
| 261 | x += change_x; |
| 262 | error_term -= delta_y; |
| 263 | } |
| 264 | |
| 265 | if (y >= TERRAIN_DEPTH || y < 0 || x < 0 || x >= TERRAIN_WIDTH) { |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | // Check the current node for collisions |
| 270 | cur_node = y * TERRAIN_WIDTH + x; |
| 271 | if ((ai_terrain_check_list[cur_node >> 3] & (0x00000001 << (cur_node % 8))) == 0) |
| 272 | ai_check_terrain_node(cur_node, 1); |
| 273 | |
| 274 | i++; |
| 275 | } |
| 276 | |
| 277 | } else { |
| 278 | length = delta_x + 1; |
| 279 | |
| 280 | while (i < length) { |
| 281 | x += change_x; |
| 282 | error_term += delta_y; |
| 283 | |
| 284 | if (error_term >= delta_x) { |
| 285 | y += change_y; |
| 286 | error_term -= delta_x; |
| 287 | } |
| 288 | |
| 289 | if (y >= TERRAIN_DEPTH || y < 0 || x < 0 || x >= TERRAIN_WIDTH) { |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | // Check the current node for collisions |
| 294 | cur_node = y * TERRAIN_WIDTH + x; |
| 295 | if ((ai_terrain_check_list[cur_node >> 3] & (0x00000001 << (cur_node % 8))) == 0) |
| 296 | ai_check_terrain_node(cur_node, 1); |
| 297 | |
| 298 | i++; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | ASSERT(x == x2 && y == y2); |
| 303 | |
| 304 | return true; |
| 305 | } |