| 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/objinfo.cpp $ |
| 21 | * $Revision: 64 $ |
| 22 | * $Date: 4/19/00 5:07p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Code to handle objects in the big list of robots,powerups,etc. |
| 26 | * |
| 27 | * $NoKeywords: $ |
| 28 | */ |
| 29 | |
| 30 | #include "pstypes.h" |
| 31 | #include "pserror.h" |
| 32 | #include "object.h" |
| 33 | #include "objinfo.h" |
| 34 | #include "3d.h" |
| 35 | #include <stdlib.h> |
| 36 | #include <stdio.h> |
| 37 | #include <stdarg.h> |
| 38 | #include <string.h> |
| 39 | #include "polymodel.h" |
| 40 | #include "robotfire.h" |
| 41 | #include "AIMain.h" |
| 42 | #include "sounds.h" |
| 43 | #include "stringtable.h" |
| 44 | #include "mem.h" |
| 45 | |
| 46 | // The array with information for robots, powerups, buildings, etc. |
| 47 | object_info Object_info[MAX_OBJECT_IDS]; |
| 48 | |
| 49 | // The number of ids of each type in the list |
| 50 | int Num_object_ids[MAX_OBJECT_TYPES]; |
| 51 | |
| 52 | // Objects that must be remapped so we can reference by index |
| 53 | // If you change this array then you must change the defines in the header file |
| 54 | // #ifdef _WIN32 |
| 55 | // char *Static_object_names[]={TBL_GENERIC("GuideBot"),TBL_GENERIC("ChaffChunk"),TBL_GENERIC("GuideBotRed")}; |
| 56 | // #else |
| 57 | const char *Static_object_names[] = {TBL_GENERIC("GuideBot"), TBL_GENERIC("ChaffChunk"),TBL_GENERIC("GuideBotRed")}; |
| 58 | // #endif |
| 59 | |
| 60 | #define NUM_STATIC_OBJECTS (sizeof(Static_object_names) / sizeof(*Static_object_names)) |
| 61 | |
| 62 | #ifdef EDITOR |
| 63 | const char * const Movement_class_names[] = {"Standing", "Flying", "Rolling", "Walking", "Jumping"}; |
| 64 | const char * const Anim_state_names[] = {"Alert", |
| 65 | "Death", |
| 66 | "Fire Missile 1", |
| 67 | "Missile Recoil 1", |
| 68 | "Fire Missile 2", |
| 69 | "Missile Recoil 2", |
| 70 | "Melee 1", |
| 71 | "Melee Recoil 1", |
| 72 | "Melee 2", |
| 73 | "Melee Recoil 2", |
| 74 | "Idle", |
| 75 | "Quirk", |
| 76 | "Flinch", |
| 77 | "Taunt", |
| 78 | "To Standing Idle", |
| 79 | "To Flying Idle", |
| 80 | "To Rolling Idle", |
| 81 | ";To Walking Idle", |
| 82 | "To Jumping Idle", |
| 83 | "Goto standing", |
| 84 | "Goto flying", |
| 85 | "Goto rolling", |
| 86 | "Goto walking", |
| 87 | "Goto jumping"}; |
| 88 | #endif |
| 89 | |
| 90 | // Sets all objects to unused |
| 91 | void InitObjectInfo() { |
| 92 | int i; |
| 93 | |
| 94 | for (i = 0; i < MAX_OBJECT_IDS; i++) { |
| 95 | Object_info[i].type = OBJ_NONE; |
| 96 | Object_info[i].name[0] = 0; |
| 97 | // Object_info[i].script_name[0] = 0; |
| 98 | Object_info[i].script_name_override[0] = 0; |
| 99 | Object_info[i].multi_allowed = 1; |
| 100 | Object_info[i].module_name[0] = 0; |
| 101 | } |
| 102 | |
| 103 | for (i = 0; i < MAX_OBJECT_TYPES; i++) |
| 104 | Num_object_ids[i] = 0; |
| 105 | |
| 106 | atexit(FreeObjectInfo); |
| 107 | } |
| 108 | |
| 109 | #define DEFAULT_OBJECT_SIZE 4.0f |
| 110 | #define DEFAULT_OBJECT_MASS 1.0f |
| 111 | #define DEFAULT_OBJECT_DRAG 0.1f |
| 112 | #define DEFAULT_OBJECT_ROTDRAG 0.01f |
| 113 | |
| 114 | // Allocs a object for use, returns -1 if error, else index on success |
| 115 | int AllocObjectID(int type, bool f_anim, bool f_weapons, bool f_ai) { |
| 116 | int j, k; |
| 117 | |
| 118 | for (int i = NUM_STATIC_OBJECTS; i < MAX_OBJECT_IDS; i++) { |
| 119 | if (Object_info[i].type == OBJ_NONE) { |
| 120 | mprintf(1, "%d ", type); |
| 121 | memset(&Object_info[i], 0, sizeof(*Object_info)); |
| 122 | |
| 123 | extern void AISetDefault(t_ai_info * ai_info_ptr); |
| 124 | if (f_ai) { |
| 125 | Object_info[i].ai_info = (t_ai_info *)mem_malloc(sizeof(t_ai_info)); |
| 126 | memset(Object_info[i].ai_info, 0, sizeof(t_ai_info)); |
| 127 | AISetDefault(Object_info[i].ai_info); |
| 128 | } |
| 129 | // Make sure the weapon battery info is cleared for a new object |
| 130 | if (f_weapons) { |
| 131 | Object_info[i].static_wb = (otype_wb_info *)mem_malloc(sizeof(otype_wb_info) * MAX_WBS_PER_OBJ); |
| 132 | memset(Object_info[i].static_wb, 0, sizeof(otype_wb_info) * MAX_WBS_PER_OBJ); |
| 133 | WBClearInfo(Object_info[i].static_wb); |
| 134 | } |
| 135 | |
| 136 | if (f_anim) { |
| 137 | Object_info[i].anim = (anim_elem *)mem_malloc(sizeof(anim_elem) * NUM_MOVEMENT_CLASSES); |
| 138 | memset(Object_info[i].anim, 0, sizeof(anim_elem) * NUM_MOVEMENT_CLASSES); |
| 139 | for (j = 0; j < NUM_MOVEMENT_CLASSES; j++) |
| 140 | for (k = 0; k < NUM_ANIMS_PER_CLASS; k++) { |
| 141 | Object_info[i].anim[j].elem[k].spc = 1.0f; |
| 142 | Object_info[i].anim[j].elem[k].anim_sound_index = SOUND_NONE_INDEX; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | Object_info[i].type = type; |
| 147 | Object_info[i].size = DEFAULT_OBJECT_SIZE; |
| 148 | Object_info[i].phys_info.mass = DEFAULT_OBJECT_MASS; |
| 149 | Object_info[i].phys_info.drag = DEFAULT_OBJECT_DRAG; |
| 150 | Object_info[i].phys_info.rotdrag = DEFAULT_OBJECT_ROTDRAG; |
| 151 | |
| 152 | Object_info[i].phys_info.flags = PF_BOUNCE; |
| 153 | Object_info[i].phys_info.num_bounces = -1; |
| 154 | Object_info[i].phys_info.coeff_restitution = 1.0; |
| 155 | Object_info[i].phys_info.hit_die_dot = -1; //-1 mean doesn't apply |
| 156 | |
| 157 | Object_info[i].med_render_handle = -1; |
| 158 | Object_info[i].lo_render_handle = -1; |
| 159 | Object_info[i].med_lod_distance = DEFAULT_MED_LOD_DISTANCE; |
| 160 | Object_info[i].lo_lod_distance = DEFAULT_LO_LOD_DISTANCE; |
| 161 | Object_info[i].respawn_scalar = 1.0; |
| 162 | |
| 163 | if (type == OBJ_CLUTTER || type == OBJ_ROBOT) { |
| 164 | Object_info[i].med_lod_distance *= 10; |
| 165 | Object_info[i].lo_lod_distance *= 10; |
| 166 | } |
| 167 | |
| 168 | // Object_info[i].script_name[0] = 0; |
| 169 | Object_info[i].icon_name[0] = 0; |
| 170 | Object_info[i].description = NULL; |
| 171 | Object_info[i].flags |= OIF_INVEN_SELECTABLE; |
| 172 | |
| 173 | // init spew types |
| 174 | for (j = 0; j < MAX_DSPEW_TYPES; j++) { |
| 175 | Object_info[i].dspew[j] = -1; |
| 176 | Object_info[i].dspew_number[j] = 0; |
| 177 | } |
| 178 | |
| 179 | // init ammo count |
| 180 | Object_info[i].ammo_count = 0; |
| 181 | Object_info[i].multi_allowed = 1; |
| 182 | Num_object_ids[type]++; |
| 183 | return i; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | Int3(); // No slots free! |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | // Frees object index n |
| 192 | void FreeObjectID(int n) { |
| 193 | ASSERT(Object_info[n].type != OBJ_NONE); |
| 194 | |
| 195 | Num_object_ids[Object_info[n].type]--; |
| 196 | Object_info[n].type = OBJ_NONE; |
| 197 | Object_info[n].name[0] = 0; |
| 198 | Object_info[n].icon_name[0] = 0; |
| 199 | Object_info[n].script_name_override[0] = 0; |
| 200 | Object_info[n].module_name[0] = 0; |
| 201 | if (Object_info[n].description) { |
| 202 | mem_free(Object_info[n].description); |
| 203 | Object_info[n].description = NULL; |
| 204 | } |
| 205 | if (Object_info[n].anim) // DAJ LEAK FIX |
| 206 | { |
| 207 | mem_free(Object_info[n].anim); |
| 208 | Object_info[n].anim = NULL; |
| 209 | } |
| 210 | if (Object_info[n].ai_info) // DAJ LEAK FIX |
| 211 | { |
| 212 | mem_free(Object_info[n].ai_info); |
| 213 | Object_info[n].ai_info = NULL; |
| 214 | } |
| 215 | if (Object_info[n].description) // DAJ LEAK FIX |
| 216 | { |
| 217 | mem_free(Object_info[n].description); |
| 218 | Object_info[n].description = NULL; |
| 219 | } |
| 220 | if (Object_info[n].static_wb) // DAJ LEAK FIX |
| 221 | { |
| 222 | mem_free(Object_info[n].static_wb); |
| 223 | Object_info[n].static_wb = NULL; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Frees up all the Objects and any memory allocated |
| 228 | void FreeObjectInfo(void) { |
| 229 | for (int i = 0; i < MAX_OBJECT_IDS; i++) { |
| 230 | if (Object_info[i].type != OBJ_NONE) |
| 231 | FreeObjectID(i); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Find an object with the given type. Returns -1 if none found. |
| 236 | int GetObjectID(int type) { |
| 237 | int i; |
| 238 | |
| 239 | for (i = 0; i < MAX_OBJECT_IDS; i++) |
| 240 | if (Object_info[i].type == type) |
| 241 | return i; |
| 242 | |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | #ifdef EDITOR |
| 247 | |
| 248 | // Gets next object from n of the same type as n |
| 249 | int GetNextObjectID(int n) { |
| 250 | int i; |
| 251 | int type = Object_info[n].type; |
| 252 | |
| 253 | ASSERT(n >= 0 && n < MAX_OBJECT_IDS); |
| 254 | |
| 255 | if (Num_object_ids[type] == 0) |
| 256 | return -1; |
| 257 | |
| 258 | for (i = n + 1; i < MAX_OBJECT_IDS; i++) |
| 259 | if (Object_info[i].type == Object_info[n].type) |
| 260 | return i; |
| 261 | |
| 262 | for (i = 0; i <= n; i++) |
| 263 | if (Object_info[i].type == Object_info[n].type) |
| 264 | return i; |
| 265 | |
| 266 | Int3(); // shouldn't be here |
| 267 | return n; |
| 268 | } |
| 269 | |
| 270 | // Gets previous object from n that has the same type |
| 271 | int GetPrevObjectID(int n) { |
| 272 | int i; |
| 273 | int type = Object_info[n].type; |
| 274 | |
| 275 | ASSERT(n >= 0 && n < MAX_OBJECT_IDS); |
| 276 | |
| 277 | if (Num_object_ids[type] == 0) |
| 278 | return -1; |
| 279 | |
| 280 | for (i = n - 1; i >= 0; i--) { |
| 281 | if (Object_info[i].type == Object_info[n].type) |
| 282 | return i; |
| 283 | } |
| 284 | |
| 285 | for (i = MAX_OBJECT_IDS - 1; i >= n; i--) { |
| 286 | if (Object_info[i].type == Object_info[n].type) |
| 287 | return i; |
| 288 | } |
| 289 | |
| 290 | Int3(); // shouldn't get here |
| 291 | return n; |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | // Searches thru all object ids for a specific name |
| 296 | // Returns the found id, or -1 if not found |
| 297 | int FindObjectIDName(const char *name) { |
| 298 | int i; |
| 299 | |
| 300 | ASSERT(name != NULL); |
| 301 | |
| 302 | for (i = 0; i < MAX_OBJECT_IDS; i++) |
| 303 | if ((Object_info[i].type != OBJ_NONE) && !stricmp(name, Object_info[i].name)) |
| 304 | return i; |
| 305 | |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | // Given an object handle, returns an index to that object's model |
| 310 | int GetObjectImage(int handle) { |
| 311 | if (Object_info[handle].type == OBJ_NONE) |
| 312 | return -1; |
| 313 | else |
| 314 | return (Object_info[handle].render_handle); |
| 315 | } |
| 316 | |
| 317 | // Given an object, renders the representation of this object |
| 318 | void DrawObject(object *obj) { |
| 319 | ASSERT(Object_info[obj->id].type != OBJ_NONE); |
| 320 | |
| 321 | if (obj->render_type == RT_POLYOBJ) |
| 322 | DrawPolygonModel(&obj->pos, &obj->orient, Object_info[obj->id].render_handle, NULL, 0, 1.0, 1.0, 1.0, |
| 323 | obj->rtype.pobj_info.subobj_flags); |
| 324 | else |
| 325 | Int3(); |
| 326 | } |
| 327 | |
| 328 | // Go through the object list and update an ID which has changed |
| 329 | void RemapObjects(int old_id, int new_id) { |
| 330 | int i; |
| 331 | |
| 332 | for (i = 0; i < MAX_OBJECTS; i++) |
| 333 | if ((Objects[i].type == Object_info[new_id].type) && (Objects[i].id == old_id)) |
| 334 | Objects[i].id = new_id; |
| 335 | |
| 336 | for (i = 0; i < MAX_OBJECT_IDS; i++) { |
| 337 | if (Object_info[i].type != OBJ_NONE) { |
| 338 | int j; |
| 339 | |
| 340 | for (j = 0; j < MAX_DSPEW_TYPES; j++) |
| 341 | if (Object_info[i].dspew[j] == old_id) |
| 342 | Object_info[i].dspew[j] = new_id; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Remap all the static object ids (objects that must have a specific index) into their assigned slots. |
| 348 | void RemapStaticIDs() { |
| 349 | bool f_anim = false; |
| 350 | bool f_weapons = false; |
| 351 | bool f_ai = false; |
| 352 | |
| 353 | for (int i = 0; i < NUM_STATIC_OBJECTS; i++) { |
| 354 | |
| 355 | int cur_index = FindObjectIDName(IGNORE_TABLE(Static_object_names[i])); |
| 356 | |
| 357 | if (cur_index == -1) { |
| 358 | // The object we are trying to find is not loaded or doesn't exist |
| 359 | Error("Cannot find object <%s>", Static_object_names[i]); |
| 360 | continue; |
| 361 | } |
| 362 | |
| 363 | if (cur_index == i) |
| 364 | continue; // hurray, we're already matched up! |
| 365 | |
| 366 | // If our slot is already used, move the ID somewhere else |
| 367 | if (Object_info[i].type != OBJ_NONE) { |
| 368 | // int new_index = AllocObjectID(Object_info[i].type, Object_info[i].flags); |
| 369 | if (Object_info[i].anim) |
| 370 | f_anim = true; |
| 371 | if (Object_info[i].static_wb) |
| 372 | f_weapons = true; |
| 373 | if (Object_info[i].ai_info) |
| 374 | f_ai = true; |
| 375 | int new_index = AllocObjectID(Object_info[i].type, f_anim, f_weapons, f_ai); |
| 376 | if (new_index >= 0) { // DAJ -1FIX |
| 377 | Object_info[new_index] = Object_info[i]; |
| 378 | RemapObjects(i, new_index); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | // Copy static ID into new slot |
| 383 | Object_info[i] = Object_info[cur_index]; |
| 384 | |
| 385 | // Free up the old slot |
| 386 | Object_info[cur_index].type = OBJ_NONE; |
| 387 | |
| 388 | // Remap the objects that used this ID |
| 389 | RemapObjects(cur_index, i); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | // Changes all existing objects model_nums to a new model num |
| 394 | void ChangeOldModelsForObjects(int old_handle, int new_handle) { |
| 395 | int i; |
| 396 | |
| 397 | for (i = 0; i <= Highest_object_index; i++) { |
| 398 | if (Objects[i].render_type == RT_POLYOBJ && Objects[i].rtype.pobj_info.model_num == old_handle) { |
| 399 | Objects[i].rtype.pobj_info.model_num = new_handle; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | void RemapObjects() { RemapStaticIDs(); }; |