| 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 | |
| 19 | |
| 20 | |
| 21 | #include "attach.h" |
| 22 | #include "terrain.h" |
| 23 | #include "multi.h" |
| 24 | #include "game.h" |
| 25 | #include "demofile.h" |
| 26 | #include "polymodel.h" |
| 27 | #include "objinfo.h" |
| 28 | |
| 29 | /* // Determines the necessary orientation and position of the moving object |
| 30 | f_use_uvec = true; |
| 31 | AttachPointPos(goal_obj, goal_ap, true, &goal_pos, true, &goal_fvec, &f_use_uvec, &goal_uvec); |
| 32 | goal_fvec *= -1.0f; |
| 33 | |
| 34 | // Determines the initial attach fvec of the moving object |
| 35 | AttachPointPos(start_obj, start_ap, false, NULL, true, &start_fvec); |
| 36 | |
| 37 | float dot = start_fvec * goal_fvec; |
| 38 | ASSERT(dot >= -1.1f && dot <= 1.1f); |
| 39 | |
| 40 | // Bounds checking to save us from floating point round-off |
| 41 | if(dot > 1.0f) |
| 42 | dot = 1.0f; |
| 43 | if(dot < -1.0f) |
| 44 | dot = -1.0f; |
| 45 | |
| 46 | // If the dot is 1.0 we are are already perfectly aligned! |
| 47 | if(dot != 1.0f) |
| 48 | { |
| 49 | // if it is -1.0, then we are perfectly unaligned |
| 50 | if(dot != -1.0f) |
| 51 | { |
| 52 | vm_CrossProduct(&rot_vec, &start_fvec, &goal_fvec); |
| 53 | vm_NormalizeVector(&rot_vec); |
| 54 | |
| 55 | // returns the angle between two normalized vectors and a forward vector |
| 56 | // rot_angle = (vm_DeltaAngVecNorm(&start_fvec, &goal_fvec, &rot_vec)/65535.0f) * (2.0f * PI); |
| 57 | rot_angle = acos(dot); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | vector random_vector; |
| 62 | float dot; |
| 63 | |
| 64 | do |
| 65 | { |
| 66 | vm_MakeRandomVector(&random_vector); |
| 67 | vm_NormalizeVector(&random_vector); |
| 68 | |
| 69 | dot = random_vector * goal_fvec; |
| 70 | } |
| 71 | while(dot >= 1.0f || dot <= -1.0f); |
| 72 | |
| 73 | vm_CrossProduct(&rot_vec, &random_vector, &goal_fvec); |
| 74 | vm_NormalizeVector(&rot_vec); |
| 75 | |
| 76 | rot_angle = PI; |
| 77 | } |
| 78 | |
| 79 | ConvertAxisAmountMatrix(&rot_vec, rot_angle, &rot_mat); |
| 80 | vm_TransposeMatrix(&rot_mat); |
| 81 | start_obj->orient *= rot_mat; |
| 82 | } |
| 83 | |
| 84 | // Determines the offset of the start object's ap position (now that it is correctly aligned |
| 85 | // with the fvec) |
| 86 | f_use_uvec = true; |
| 87 | vector temp; |
| 88 | AttachPointPos(start_obj, start_ap, true, &start_pos, true, &temp, &f_use_uvec, &start_uvec); |
| 89 | |
| 90 | start_obj->pos += (goal_pos - start_pos); |
| 91 | |
| 92 | // If we have UVEC's then align them! |
| 93 | if(f_use_uvec) |
| 94 | { |
| 95 | float dot = start_uvec * goal_uvec; |
| 96 | ASSERT(dot >= -1.1f && dot <= 1.1f); |
| 97 | |
| 98 | // Bounds checking to save us from floating point round-off |
| 99 | if(dot > 1.0f) |
| 100 | dot = 1.0f; |
| 101 | if(dot < -1.0f) |
| 102 | dot = -1.0f; |
| 103 | |
| 104 | // If the dot is 1.0 we are are already perfectly aligned! |
| 105 | if(dot != 1.0f) |
| 106 | { |
| 107 | // if it is -1.0, then we are perfectly unaligned |
| 108 | if(dot != -1.0f) |
| 109 | { |
| 110 | vm_CrossProduct(&rot_vec, &start_uvec, &goal_uvec); |
| 111 | vm_NormalizeVector(&rot_vec); |
| 112 | |
| 113 | // rot_angle = (vm_DeltaAngVecNorm(&start_uvec, &goal_uvec, &rot_vec)/65535.0f) * (2.0f * |
| 114 | PI); rot_angle = acos(dot); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | vector random_vector; |
| 119 | float dot; |
| 120 | |
| 121 | do |
| 122 | { |
| 123 | vm_MakeRandomVector(&random_vector); |
| 124 | vm_NormalizeVector(&random_vector); |
| 125 | |
| 126 | dot = random_vector * goal_uvec; |
| 127 | } |
| 128 | while(dot >= 1.0f || dot <= -1.0f); |
| 129 | |
| 130 | vm_CrossProduct(&rot_vec, &random_vector, &goal_uvec); |
| 131 | vm_NormalizeVector(&rot_vec); |
| 132 | |
| 133 | rot_angle = PI; |
| 134 | } |
| 135 | |
| 136 | ConvertAxisAmountMatrix(&rot_vec, rot_angle, &rot_mat); |
| 137 | vm_TransposeMatrix(&rot_mat); |
| 138 | start_obj->orient *= rot_mat; |
| 139 | } |
| 140 | } |
| 141 | */ |
| 142 | |
| 143 | // Finds the position of a attach point on an object |
| 144 | // The uvec is optional as most attaching objects don't need at complete orientation set (only an fvec) |
| 145 | static bool AttachPointPos(object *obj, char ap, bool f_compute_pos, vector *attach_pos, bool f_compute_fvec, |
| 146 | vector *attach_fvec, bool *f_computed_uvec = NULL, vector *attach_uvec = NULL) { |
| 147 | poly_model *pm; |
| 148 | vector pnt; |
| 149 | vector normal; |
| 150 | vector uvec; |
| 151 | matrix m; |
| 152 | int mn; // submodel number |
| 153 | float normalized_time[MAX_SUBOBJECTS]; |
| 154 | |
| 155 | pm = &Poly_models[obj->rtype.pobj_info.model_num]; |
| 156 | |
| 157 | if (ap < 0 || ap >= pm->n_attach) { |
| 158 | mprintf(0, "WARNING: No ap %d on object %d.\n", ap, OBJNUM(obj)); |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | SetNormalizedTimeObj(obj, normalized_time); |
| 163 | SetModelAnglesAndPos(pm, normalized_time); |
| 164 | |
| 165 | if (f_compute_pos) |
| 166 | pnt = pm->attach_slots[ap].pnt; |
| 167 | |
| 168 | if (f_compute_fvec) { |
| 169 | normal = pm->attach_slots[ap].norm; |
| 170 | // mprintf(0, "Original fvec is %f long.\n", vm_GetMagnitude(&normal)); |
| 171 | } |
| 172 | |
| 173 | if (pm->attach_slots[ap].f_uvec && attach_uvec && f_computed_uvec) { |
| 174 | *f_computed_uvec = true; |
| 175 | uvec = pm->attach_slots[ap].uvec; |
| 176 | |
| 177 | // mprintf(0, "Original uvec is %f long.\n", vm_GetMagnitude(&uvec)); |
| 178 | } else if (f_computed_uvec) { |
| 179 | *f_computed_uvec = false; |
| 180 | } |
| 181 | |
| 182 | mn = pm->attach_slots[ap].parent; |
| 183 | |
| 184 | // Instance up the tree for this gun |
| 185 | while (mn != -1) { |
| 186 | vector tpnt; |
| 187 | |
| 188 | vm_AnglesToMatrix(&m, pm->submodel[mn].angs.p, pm->submodel[mn].angs.h, pm->submodel[mn].angs.b); |
| 189 | vm_TransposeMatrix(&m); |
| 190 | |
| 191 | if (f_compute_pos) |
| 192 | tpnt = pnt * m; |
| 193 | |
| 194 | if (f_compute_fvec) |
| 195 | normal = normal * m; |
| 196 | |
| 197 | if (f_computed_uvec && *f_computed_uvec) { |
| 198 | uvec = uvec * m; |
| 199 | } |
| 200 | |
| 201 | if (f_compute_pos) |
| 202 | pnt = tpnt + pm->submodel[mn].offset + pm->submodel[mn].mod_pos; |
| 203 | |
| 204 | mn = pm->submodel[mn].parent; |
| 205 | } |
| 206 | |
| 207 | // now instance for the entire object |
| 208 | m = obj->orient; |
| 209 | vm_TransposeMatrix(&m); |
| 210 | |
| 211 | if (f_compute_pos) |
| 212 | *attach_pos = pnt * m; |
| 213 | |
| 214 | if (f_compute_fvec) { |
| 215 | *attach_fvec = normal * m; |
| 216 | // mprintf(0, "Rotated fvec is %f long.\n", vm_GetMagnitude(attach_fvec)); |
| 217 | } |
| 218 | |
| 219 | if (f_computed_uvec && *f_computed_uvec) { |
| 220 | *attach_uvec = uvec * m; |
| 221 | // mprintf(0, "Rotated uvec is %f long.\n", vm_GetMagnitude(attach_uvec)); |
| 222 | } |
| 223 | |
| 224 | if (f_compute_pos) |
| 225 | *attach_pos += obj->pos; |
| 226 | |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | static void ConvertAxisAmountMatrix(vector *n, float w, matrix *rotmat) { |
| 231 | float s; |
| 232 | float c; |
| 233 | float t; |
| 234 | |
| 235 | // float scale = *w/.0001f; |
| 236 | // float w_n = .0001f; |
| 237 | // vector s_result; |
| 238 | |
| 239 | if (w == 0.0f) { |
| 240 | matrix temp = IDENTITY_MATRIX; |
| 241 | |
| 242 | *rotmat = temp; |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | s = sin(w); |
| 247 | c = cos(w); |
| 248 | t = 1.0f - c; |
| 249 | |
| 250 | const float sx = s * n->x; |
| 251 | const float sy = s * n->y; |
| 252 | const float sz = s * n->z; |
| 253 | const float txy = t * n->x * n->y; |
| 254 | const float txz = t * n->x * n->z; |
| 255 | const float tyz = t * n->y * n->z; |
| 256 | const float txx = t * n->x * n->x; |
| 257 | const float tyy = t * n->y * n->y; |
| 258 | const float tzz = t * n->z * n->z; |
| 259 | |
| 260 | rotmat->rvec.x = txx + c; |
| 261 | rotmat->rvec.y = txy - sz; |
| 262 | rotmat->rvec.z = txz + sy; |
| 263 | rotmat->uvec.x = txy + sz; |
| 264 | rotmat->uvec.y = tyy + c; |
| 265 | rotmat->uvec.z = tyz - sx; |
| 266 | rotmat->fvec.x = txz - sy; |
| 267 | rotmat->fvec.y = tyz + sx; |
| 268 | rotmat->fvec.z = tzz + c; |
| 269 | } |
| 270 | |
| 271 | bool AttachDoPosOrientRad(object *parent, char p_ap, object *child, float rad_percent, vector *pos) { |
| 272 | vector attach_pos; |
| 273 | vector attach_fvec; |
| 274 | |
| 275 | if (AttachPointPos(parent, p_ap, true, &attach_pos, true, &attach_fvec)) { |
| 276 | vector child_ap_pos = child->pos - attach_fvec * (rad_percent * child->size); |
| 277 | |
| 278 | *pos = (child_ap_pos - attach_pos) + parent->pos; |
| 279 | return true; |
| 280 | } else { |
| 281 | return false; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | bool AttachDoPosOrient(object *parent, char parent_ap, object *child, char child_ap, bool f_parent, bool f_move_obj, |
| 286 | vector *pos, matrix *orient, bool f_dropping_off) { |
| 287 | vector goal_fvec; |
| 288 | vector goal_uvec; |
| 289 | vector goal_pos; |
| 290 | |
| 291 | bool f_use_uvec = true; |
| 292 | |
| 293 | vector start_fvec; |
| 294 | vector start_uvec; |
| 295 | vector start_pos; |
| 296 | |
| 297 | object *goal_obj; |
| 298 | object *start_obj; |
| 299 | |
| 300 | matrix rot_mat; |
| 301 | |
| 302 | char goal_ap; |
| 303 | char start_ap; |
| 304 | |
| 305 | if (f_parent) { |
| 306 | goal_obj = child; |
| 307 | goal_ap = child_ap; |
| 308 | |
| 309 | start_obj = parent; |
| 310 | start_ap = parent_ap; |
| 311 | } else { |
| 312 | goal_obj = parent; |
| 313 | goal_ap = parent_ap; |
| 314 | |
| 315 | start_obj = child; |
| 316 | start_ap = child_ap; |
| 317 | } |
| 318 | |
| 319 | vector saved_pos = start_obj->pos; |
| 320 | matrix saved_orient = start_obj->orient; |
| 321 | |
| 322 | f_use_uvec = true; |
| 323 | AttachPointPos(goal_obj, goal_ap, true, &goal_pos, true, &goal_fvec, &f_use_uvec, &goal_uvec); |
| 324 | goal_fvec *= -1.0f; |
| 325 | |
| 326 | f_use_uvec = true; |
| 327 | if (f_dropping_off) { |
| 328 | object *attached_obj = ObjGet(start_obj->attach_children[0]); |
| 329 | ASSERT(attached_obj); |
| 330 | AttachPointPos(attached_obj, start_ap, true, &start_pos, true, &start_fvec, &f_use_uvec, &start_uvec); |
| 331 | } else { |
| 332 | AttachPointPos(start_obj, start_ap, true, &start_pos, true, &start_fvec, &f_use_uvec, &start_uvec); |
| 333 | } |
| 334 | |
| 335 | vm_NormalizeVector(&start_fvec); |
| 336 | vm_NormalizeVector(&start_uvec); |
| 337 | vm_NormalizeVector(&goal_fvec); |
| 338 | vm_NormalizeVector(&goal_uvec); |
| 339 | |
| 340 | matrix start_m; |
| 341 | matrix goal_m; |
| 342 | |
| 343 | vm_VectorToMatrix(&start_m, &start_fvec, &start_uvec, NULL); |
| 344 | vm_VectorToMatrix(&goal_m, &goal_fvec, &goal_uvec, NULL); |
| 345 | |
| 346 | matrix sot; |
| 347 | matrix srt; |
| 348 | |
| 349 | sot = start_obj->orient; |
| 350 | vm_TransposeMatrix(&sot); |
| 351 | |
| 352 | srt = sot * start_m; |
| 353 | vm_TransposeMatrix(&srt); |
| 354 | |
| 355 | rot_mat = sot * goal_m * srt; |
| 356 | |
| 357 | start_obj->orient *= rot_mat; |
| 358 | vm_Orthogonalize(&start_obj->orient); |
| 359 | |
| 360 | if (f_dropping_off) { |
| 361 | object *attached_obj = ObjGet(start_obj->attach_children[0]); |
| 362 | ASSERT(attached_obj); |
| 363 | AttachPointPos(attached_obj, start_ap, true, &start_pos, false, NULL, NULL, NULL); |
| 364 | } else { |
| 365 | AttachPointPos(start_obj, start_ap, true, &start_pos, false, NULL, NULL, NULL); |
| 366 | } |
| 367 | start_obj->pos += (goal_pos - start_pos); |
| 368 | |
| 369 | if (pos) |
| 370 | *pos = start_obj->pos; |
| 371 | |
| 372 | if (orient) |
| 373 | *orient = start_obj->orient; |
| 374 | |
| 375 | if (f_move_obj) { |
| 376 | int room = goal_obj->roomnum; |
| 377 | if (room != -1) { |
| 378 | if (ROOMNUM_OUTSIDE(room)) { |
| 379 | room = GetTerrainRoomFromPos(&start_obj->pos); |
| 380 | if (room == -1) { |
| 381 | SetObjectDeadFlag(start_obj); |
| 382 | return false; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | ObjSetPos(start_obj, &start_obj->pos, room, &start_obj->orient, false); |
| 387 | |
| 388 | return true; |
| 389 | } else { |
| 390 | SetObjectDeadFlag(start_obj); |
| 391 | return false; |
| 392 | } |
| 393 | } else { |
| 394 | start_obj->pos = saved_pos; |
| 395 | start_obj->orient = saved_orient; |
| 396 | } |
| 397 | |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | // Adds and subtracts from mass from connections and disconnections |
| 402 | static void AttachPropagateMass(object *child, bool f_attach = true) { |
| 403 | object *new_parent; |
| 404 | |
| 405 | if (!child) |
| 406 | return; |
| 407 | |
| 408 | do { |
| 409 | new_parent = ObjGet(child->attach_parent_handle); |
| 410 | ASSERT(new_parent); |
| 411 | if (!new_parent) { |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | float mass; |
| 416 | |
| 417 | mass = Object_info[child->id].phys_info.mass; |
| 418 | |
| 419 | if (mass < 0.0f) |
| 420 | mass = 0.0f; |
| 421 | |
| 422 | ASSERT(new_parent->mtype.phys_info.mass >= 0.0f && mass >= 0.0f); |
| 423 | |
| 424 | if (f_attach) |
| 425 | new_parent->mtype.phys_info.mass += mass; |
| 426 | else |
| 427 | new_parent->mtype.phys_info.mass -= mass; |
| 428 | |
| 429 | if (new_parent->mtype.phys_info.mass < 0.0f) { |
| 430 | new_parent->mtype.phys_info.mass = 0.0f; |
| 431 | } |
| 432 | |
| 433 | child = new_parent; |
| 434 | |
| 435 | ASSERT(new_parent->mtype.phys_info.mass >= 0.0f); |
| 436 | } while (child->flags & OF_ATTACHED); |
| 437 | } |
| 438 | |
| 439 | static inline void ComputeUltimateAttachParent(object *child) { |
| 440 | object *cur_obj = child; |
| 441 | |
| 442 | do { |
| 443 | child->attach_ultimate_handle = cur_obj->attach_parent_handle; |
| 444 | cur_obj = ObjGet(child->attach_ultimate_handle); |
| 445 | ASSERT(cur_obj); |
| 446 | |
| 447 | } while (cur_obj->flags & OF_ATTACHED); |
| 448 | } |
| 449 | |
| 450 | static void ProprogateUltimateAttachParent(object *parent, int ultimate_handle) { |
| 451 | int i; |
| 452 | |
| 453 | ASSERT(parent); |
| 454 | ASSERT(ultimate_handle != OBJECT_HANDLE_NONE); |
| 455 | |
| 456 | poly_model *parent_pm = &Poly_models[parent->rtype.pobj_info.model_num]; |
| 457 | |
| 458 | if (!parent->attach_children) |
| 459 | return; |
| 460 | |
| 461 | for (i = 0; i < parent_pm->n_attach; i++) { |
| 462 | object *child; |
| 463 | |
| 464 | if ((child = ObjGet(parent->attach_children[i])) != NULL) { |
| 465 | child->attach_ultimate_handle = ultimate_handle; |
| 466 | ProprogateUltimateAttachParent(child, ultimate_handle); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | // Attaches 2 objects via attach points on each. The f_used_aligned allows for an aligned connection. |
| 472 | // NOTE: The child always moves to the parent |
| 473 | bool AttachObject(object *parent, char parent_ap, object *child, char child_ap, bool f_use_aligned) { |
| 474 | ASSERT(parent); |
| 475 | ASSERT(child); |
| 476 | |
| 477 | poly_model *parent_pm = &Poly_models[parent->rtype.pobj_info.model_num]; |
| 478 | poly_model *child_pm = &Poly_models[child->rtype.pobj_info.model_num]; |
| 479 | |
| 480 | if (parent_ap < 0 || parent_ap >= parent_pm->n_attach) { |
| 481 | mprintf(0, "ATTACH: Parent AP invalid\n"); |
| 482 | return false; |
| 483 | } |
| 484 | |
| 485 | if (child->flags & OF_ATTACHED) { |
| 486 | mprintf(0, "ATTACH: Child already attached to someone\n"); |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | if (child_ap >= 0 && child_ap < child_pm->n_attach) { |
| 491 | if (AttachDoPosOrient(parent, parent_ap, child, child_ap, false)) { |
| 492 | child->flags |= OF_ATTACHED; |
| 493 | child->attach_type = AT_ALIGNED; |
| 494 | child->attach_index = child_ap; |
| 495 | child->attach_parent_handle = parent->handle; |
| 496 | |
| 497 | parent->attach_children[parent_ap] = child->handle; |
| 498 | ComputeUltimateAttachParent(child); |
| 499 | ProprogateUltimateAttachParent(child, child->attach_ultimate_handle); |
| 500 | |
| 501 | AttachPropagateMass(child); |
| 502 | |
| 503 | AttachUpdateSubObjects(child); |
| 504 | if ((Game_mode & GM_MULTI) && (Netgame.local_role == LR_SERVER)) { |
| 505 | MultiSendAttach(parent, parent_ap, child, child_ap, f_use_aligned); |
| 506 | } |
| 507 | if (Demo_flags == DF_RECORDING) { |
| 508 | DemoWriteAttachObj(parent, parent_ap, child, child_ap, f_use_aligned); |
| 509 | } |
| 510 | |
| 511 | return true; |
| 512 | } |
| 513 | |
| 514 | mprintf(0, "ATTACH: AP attach failed\n"); |
| 515 | return false; |
| 516 | } |
| 517 | |
| 518 | mprintf(0, "ATTACH: Child AP (%d) - child only has %d attach points\n", child_ap, child_pm->n_attach); |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | // Attaches a child object to a parent object by a percent of the radius of the child. |
| 523 | // NOTE: The child always moves to the parent and not the reverse |
| 524 | bool AttachObject(object *parent, char parent_ap, object *child, float percent_rad) { |
| 525 | ASSERT(parent); |
| 526 | ASSERT(child); |
| 527 | |
| 528 | poly_model *parent_pm = &Poly_models[parent->rtype.pobj_info.model_num]; |
| 529 | poly_model *child_pm = &Poly_models[parent->rtype.pobj_info.model_num]; |
| 530 | |
| 531 | if (parent_ap < 0 || parent_ap >= parent_pm->n_attach) { |
| 532 | mprintf(0, "ATTACH: Parent AP invalid\n"); |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | if (child->flags & OF_ATTACHED) { |
| 537 | mprintf(0, "ATTACH: Child already attached to someone\n"); |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | if (percent_rad >= 0.0f) { |
| 542 | vector attach_pos; |
| 543 | vector attach_fvec; |
| 544 | |
| 545 | if (AttachPointPos(parent, parent_ap, true, &attach_pos, true, &attach_fvec)) { |
| 546 | child->flags |= OF_ATTACHED; |
| 547 | child->attach_type = AT_RAD; |
| 548 | child->attach_dist = percent_rad * child->size; |
| 549 | child->attach_parent_handle = parent->handle; |
| 550 | |
| 551 | parent->attach_children[parent_ap] = child->handle; |
| 552 | ComputeUltimateAttachParent(child); |
| 553 | ProprogateUltimateAttachParent(child, child->attach_ultimate_handle); |
| 554 | |
| 555 | AttachPropagateMass(child); |
| 556 | |
| 557 | vector new_child_pos = attach_pos + attach_fvec * child->attach_dist; |
| 558 | |
| 559 | int room = parent->roomnum; |
| 560 | if (ROOMNUM_OUTSIDE(room)) |
| 561 | room = GetTerrainRoomFromPos(&new_child_pos); |
| 562 | |
| 563 | ObjSetPos(child, &new_child_pos, room, NULL, false); |
| 564 | |
| 565 | AttachUpdateSubObjects(child); |
| 566 | if ((Game_mode & GM_MULTI) && (Netgame.local_role == LR_SERVER)) { |
| 567 | MultiSendAttachRad(parent, parent_ap, child, percent_rad); |
| 568 | } |
| 569 | if (Demo_flags == DF_RECORDING) { |
| 570 | DemoWriteAttachObjRad(parent, parent_ap, child, percent_rad); |
| 571 | } |
| 572 | |
| 573 | return true; |
| 574 | } |
| 575 | |
| 576 | mprintf(0, "ATTACH: RAD attach failed\n"); |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | mprintf(0, "ATTACH: Child percent rad is invalid\n"); |
| 581 | return false; |
| 582 | } |
| 583 | |
| 584 | // Updates the position of all the subobjects off the ultimate attach parent |
| 585 | void AttachUpdateSubObjects(object *parent) { |
| 586 | poly_model *parent_pm = &Poly_models[parent->rtype.pobj_info.model_num]; |
| 587 | int i; |
| 588 | |
| 589 | #ifdef _DEBUG |
| 590 | if (!Game_update_attach) |
| 591 | return; |
| 592 | #endif |
| 593 | |
| 594 | if (parent->attach_children == NULL) |
| 595 | return; |
| 596 | |
| 597 | for (i = 0; i < parent_pm->n_attach; i++) { |
| 598 | object *child; |
| 599 | |
| 600 | if ((child = ObjGet(parent->attach_children[i])) != NULL) { |
| 601 | ASSERT(child->flags & OF_ATTACHED); |
| 602 | |
| 603 | child->mtype.phys_info.velocity = parent->mtype.phys_info.velocity; |
| 604 | child->mtype.phys_info.rotvel = parent->mtype.phys_info.rotvel; |
| 605 | |
| 606 | switch (child->attach_type) { |
| 607 | case AT_UNALIGNED: |
| 608 | AttachDoPosOrient(parent, i, child, child->attach_index, false); |
| 609 | break; |
| 610 | case AT_ALIGNED: |
| 611 | AttachDoPosOrient(parent, i, child, child->attach_index, false); |
| 612 | break; |
| 613 | case AT_RAD: { |
| 614 | vector attach_pos; |
| 615 | vector attach_fvec; |
| 616 | |
| 617 | if (AttachPointPos(parent, i, true, &attach_pos, true, &attach_fvec)) { |
| 618 | vector new_child_pos = attach_pos + attach_fvec * child->attach_dist; |
| 619 | |
| 620 | int room = parent->roomnum; |
| 621 | if (ROOMNUM_OUTSIDE(room)) |
| 622 | room = GetTerrainRoomFromPos(&new_child_pos); |
| 623 | |
| 624 | ObjSetPos(child, &new_child_pos, room, NULL, false); |
| 625 | } else { |
| 626 | Int3(); // Get chris |
| 627 | } |
| 628 | } break; |
| 629 | default: |
| 630 | Int3(); // Non-supported attach type - Get Chris |
| 631 | } |
| 632 | |
| 633 | AttachUpdateSubObjects(child); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | // Unattaches an object from its parent |
| 639 | bool UnattachFromParent(object *child) { |
| 640 | if (!(child->flags & OF_ATTACHED)) |
| 641 | return false; |
| 642 | |
| 643 | AttachPropagateMass(child, false); |
| 644 | |
| 645 | object *parent = ObjGet(child->attach_parent_handle); |
| 646 | ASSERT(parent); |
| 647 | |
| 648 | poly_model *parent_pm = &Poly_models[parent->rtype.pobj_info.model_num]; |
| 649 | int i; |
| 650 | |
| 651 | for (i = 0; i < parent_pm->n_attach; i++) { |
| 652 | if (parent->attach_children[i] == child->handle) { |
| 653 | parent->attach_children[i] = OBJECT_HANDLE_NONE; |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | // ASSERT(i < parent_pm->n_attach); // Didn't find its parent? Get chris! |
| 659 | |
| 660 | child->flags &= ~OF_ATTACHED; |
| 661 | child->attach_parent_handle = OBJECT_HANDLE_NONE; |
| 662 | child->attach_ultimate_handle = OBJECT_HANDLE_NONE; |
| 663 | |
| 664 | ProprogateUltimateAttachParent(child, child->handle); |
| 665 | if ((Game_mode & GM_MULTI) && (Netgame.local_role == LR_SERVER)) { |
| 666 | MultiSendUnattach(child); |
| 667 | } |
| 668 | if (Demo_flags == DF_RECORDING) { |
| 669 | DemoWriteUnattachObj(child); |
| 670 | } |
| 671 | |
| 672 | return true; |
| 673 | } |
| 674 | |
| 675 | // Unattaches a child from an attach point |
| 676 | bool UnattachChild(object *parent, char parent_ap) { |
| 677 | object *child; |
| 678 | |
| 679 | if ((child = ObjGet(parent->attach_children[parent_ap])) != NULL) { |
| 680 | return UnattachFromParent(child); |
| 681 | } |
| 682 | |
| 683 | return false; |
| 684 | } |
| 685 | |
| 686 | // Unattaches all children from a parent object |
| 687 | bool UnattachChildren(object *parent) { |
| 688 | poly_model *parent_pm = &Poly_models[parent->rtype.pobj_info.model_num]; |
| 689 | int i; |
| 690 | |
| 691 | if (!parent->attach_children) |
| 692 | return false; |
| 693 | |
| 694 | for (i = 0; i < parent_pm->n_attach; i++) { |
| 695 | object *child; |
| 696 | |
| 697 | if ((child = ObjGet(parent->attach_children[i])) != NULL) { |
| 698 | UnattachFromParent(child); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return true; |
| 703 | } |