| 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-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Editor object functions. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <stdarg.h> |
| 29 |
#include <math.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#include "inferno.h" |
| 33 |
#include "segment.h" |
| 34 |
#include "editor.h" |
| 35 |
#include "editor/esegment.h" |
| 36 |
#include "editor/eobject.h" |
| 37 |
|
| 38 |
#include "maths.h" |
| 39 |
#include "dxxerror.h" |
| 40 |
#include "kdefs.h" |
| 41 |
#include "object.h" |
| 42 |
#include "robot.h" |
| 43 |
#include "game.h" |
| 44 |
#include "bm.h" |
| 45 |
#include "3d.h" // For g3_point_to_vec |
| 46 |
#include "fvi.h" |
| 47 |
#include "vclip.h" |
| 48 |
|
| 49 |
#include "powerup.h" |
| 50 |
#include "hostage.h" |
| 51 |
#include "player.h" |
| 52 |
#include "gameseg.h" |
| 53 |
#include "cntrlcen.h" |
| 54 |
|
| 55 |
#include "d_construct.h" |
| 56 |
#include "d_levelstate.h" |
| 57 |
|
| 58 |
#define OBJ_SCALE (F1_0/2) |
| 59 |
#define OBJ_DEL_SIZE (F1_0/2) |
| 60 |
|
| 61 |
//returns the number of the first object in a segment, skipping the player |
| 62 |
static objnum_t get_first_object(fvcobjptr &vcobjptr, const unique_segment &seg) |
| 63 |
{ |
| 64 |
const auto id{seg.objects}; |
| 65 |
if (id == object_none) |
| 66 |
return object_none; |
| 67 |
auto &o = *vcobjptr(id); |
| 68 |
if (&o == ConsoleObject) |
| 69 |
return o.next; |
| 70 |
return id; |
| 71 |
} |
| 72 |
|
| 73 |
//returns the number of the next object in a segment, skipping the player |
| 74 |
static objnum_t get_next_object(const unique_segment &seg, objnum_t id) |
| 75 |
{ |
| 76 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 77 |
auto &vcobjptr = Objects.vcptr; |
| 78 |
auto &vmobjptr = Objects.vmptr; |
| 79 |
if (id == object_none) |
| 80 |
return get_first_object(vcobjptr, seg); |
| 81 |
for (auto o = vmobjptr(id);;) |
| 82 |
{ |
| 83 |
id = o->next; |
| 84 |
if (id == object_none) |
| 85 |
return get_first_object(vcobjptr, seg); |
| 86 |
o = vmobjptr(id); |
| 87 |
if (o != ConsoleObject) |
| 88 |
return id; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
//@@// ------------------------------------------------------------------------------------------------------ |
| 94 |
//@@// this should be called whenever the current segment may have changed |
| 95 |
//@@// If Cur_object_seg != Cursegp, then update various variables. |
| 96 |
//@@// this used to be called update_due_to_new_segment() |
| 97 |
//@@void ObjectUpdateCurrent(void) |
| 98 |
//@@{ |
| 99 |
//@@ if (Cur_object_seg != Cursegp) { |
| 100 |
//@@ Cur_object_seg = Cursegp; |
| 101 |
//@@ Cur_object_index = get_first_object(Cur_object_seg); |
| 102 |
//@@ Update_flags |= UF_WORLD_CHANGED; |
| 103 |
//@@ } |
| 104 |
//@@ |
| 105 |
//@@} |
| 106 |
|
| 107 |
namespace dsx { |
| 108 |
|
| 109 |
// ------------------------------------------------------------------------------------ |
| 110 |
int place_object(d_level_unique_object_state &LevelUniqueObjectState, const d_level_shared_polygon_model_state &LevelSharedPolygonModelState, const d_robot_info_array &Robot_info, const d_level_shared_segment_state &LevelSharedSegmentState, d_level_unique_segment_state &LevelUniqueSegmentState, const vmsegptridx_t segp, const vms_vector &object_pos, short object_type, const uint8_t object_id) |
| 111 |
{ |
| 112 |
auto seg_matrix{med_extract_matrix_from_segment(segp)}; |
| 113 |
|
| 114 |
imobjptridx_t objnum = object_none; |
| 115 |
auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models; |
| 116 |
switch (object_type) |
| 117 |
{ |
| 118 |
|
| 119 |
case OBJ_HOSTAGE: |
| 120 |
{ |
| 121 |
objnum = obj_create(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, OBJ_HOSTAGE, -1, |
| 122 |
segp,object_pos,&seg_matrix,HOSTAGE_SIZE, |
| 123 |
object::control_type::None, object::movement_type::None, render_type::RT_HOSTAGE); |
| 124 |
|
| 125 |
if ( objnum == object_none) |
| 126 |
return 0; |
| 127 |
|
| 128 |
const vmobjptridx_t obj = objnum; |
| 129 |
|
| 130 |
// Fill in obj->id and other hostage info |
| 131 |
obj->id = 0; |
| 132 |
|
| 133 |
obj->control_source = object::control_type::powerup; |
| 134 |
|
| 135 |
obj->rtype.vclip_info.vclip_num = Hostage_vclip_num[object_id]; |
| 136 |
obj->rtype.vclip_info.frametime = Vclip[obj->rtype.vclip_info.vclip_num].frame_time; |
| 137 |
obj->rtype.vclip_info.framenum = 0; |
| 138 |
break; |
| 139 |
} |
| 140 |
case OBJ_ROBOT: |
| 141 |
{ |
| 142 |
segnum_t hide_segment; |
| 143 |
if (Markedsegp) |
| 144 |
hide_segment = Markedsegp; |
| 145 |
else |
| 146 |
hide_segment = segment_none; |
| 147 |
|
| 148 |
const robot_id rid{object_id}; |
| 149 |
auto &ri = Robot_info[rid]; |
| 150 |
objnum = robot_create(Robot_info, rid, segp, object_pos, |
| 151 |
&seg_matrix, Polygon_models[ri.model_num].rad, |
| 152 |
ri.attack_type ? |
| 153 |
// robots which lunge forward to attack cannot have behavior type still. |
| 154 |
ai_behavior::AIB_NORMAL : |
| 155 |
ai_behavior::AIB_STILL, |
| 156 |
hide_segment); |
| 157 |
|
| 158 |
if ( objnum == object_none) |
| 159 |
return 0; |
| 160 |
|
| 161 |
const vmobjptridx_t obj = objnum; |
| 162 |
|
| 163 |
//Set polygon-object-specific data |
| 164 |
|
| 165 |
obj->rtype.pobj_info.model_num = ri.model_num; |
| 166 |
obj->rtype.pobj_info.subobj_flags = 0; |
| 167 |
|
| 168 |
//set Physics info |
| 169 |
|
| 170 |
obj->mtype.phys_info.mass = ri.mass; |
| 171 |
obj->mtype.phys_info.drag = ri.drag; |
| 172 |
|
| 173 |
obj->mtype.phys_info.flags |= (PF_LEVELLING); |
| 174 |
|
| 175 |
obj->shields = ri.strength; |
| 176 |
break; |
| 177 |
} |
| 178 |
case OBJ_POWERUP: |
| 179 |
{ |
| 180 |
objnum = obj_create(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, OBJ_POWERUP, object_id, |
| 181 |
segp, object_pos, &seg_matrix, Powerup_info[(powerup_type_t{object_id})].size, |
| 182 |
object::control_type::powerup, object::movement_type::None, render_type::RT_POWERUP); |
| 183 |
|
| 184 |
if ( objnum == object_none) |
| 185 |
return 0; |
| 186 |
|
| 187 |
const vmobjptridx_t obj = objnum; |
| 188 |
|
| 189 |
//set powerup-specific data |
| 190 |
|
| 191 |
obj->rtype.vclip_info.vclip_num = Powerup_info[get_powerup_id(obj)].vclip_num; |
| 192 |
obj->rtype.vclip_info.frametime = Vclip[obj->rtype.vclip_info.vclip_num].play_time/Vclip[obj->rtype.vclip_info.vclip_num].num_frames; |
| 193 |
obj->rtype.vclip_info.framenum = 0; |
| 194 |
|
| 195 |
if (get_powerup_id(obj) == powerup_type_t::POW_VULCAN_WEAPON) |
| 196 |
obj->ctype.powerup_info.count = VULCAN_WEAPON_AMMO_AMOUNT; |
| 197 |
else |
| 198 |
obj->ctype.powerup_info.count = 1; |
| 199 |
break; |
| 200 |
} |
| 201 |
case OBJ_CNTRLCEN: |
| 202 |
{ |
| 203 |
const auto model_num = |
| 204 |
#if DXX_BUILD_DESCENT == 1 |
| 205 |
ObjId[object_type]; |
| 206 |
#elif DXX_BUILD_DESCENT == 2 |
| 207 |
Reactors[object_id].model_num; |
| 208 |
#endif |
| 209 |
objnum = obj_create(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, OBJ_CNTRLCEN, object_id, segp, object_pos, |
| 210 |
&seg_matrix, Polygon_models[model_num].rad, |
| 211 |
object::control_type::cntrlcen, object::movement_type::None, render_type::RT_POLYOBJ); |
| 212 |
|
| 213 |
if ( objnum == object_none) |
| 214 |
return 0; |
| 215 |
|
| 216 |
const vmobjptridx_t obj = objnum; |
| 217 |
|
| 218 |
//Set polygon-object-specific data |
| 219 |
obj->shields = 0; // stored in Reactor_strength or calculated |
| 220 |
obj->rtype.pobj_info.model_num = model_num; |
| 221 |
obj->rtype.pobj_info.subobj_flags = 0; |
| 222 |
|
| 223 |
break; |
| 224 |
} |
| 225 |
case OBJ_PLAYER: { |
| 226 |
objnum = obj_create(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, OBJ_PLAYER, object_id, segp, object_pos, |
| 227 |
&seg_matrix, Polygon_models[Player_ship->model_num].rad, |
| 228 |
object::control_type::None, object::movement_type::physics, render_type::RT_POLYOBJ); |
| 229 |
|
| 230 |
if ( objnum == object_none) |
| 231 |
return 0; |
| 232 |
|
| 233 |
const vmobjptridx_t obj = objnum; |
| 234 |
|
| 235 |
//Set polygon-object-specific data |
| 236 |
|
| 237 |
obj->rtype.pobj_info.model_num = Player_ship->model_num; |
| 238 |
obj->rtype.pobj_info.subobj_flags = 0; |
| 239 |
//for (i=0;i<MAX_SUBMODELS;i++) |
| 240 |
// vm_angvec_zero(&obj->rtype.pobj_info.anim_angles[i]); |
| 241 |
|
| 242 |
//set Physics info |
| 243 |
|
| 244 |
obj->mtype.phys_info.velocity = {}; |
| 245 |
obj->mtype.phys_info.mass = Player_ship->mass; |
| 246 |
obj->mtype.phys_info.drag = Player_ship->drag; |
| 247 |
obj->mtype.phys_info.flags |= PF_TURNROLL | PF_LEVELLING | PF_WIGGLE; |
| 248 |
obj->shields = i2f(100); |
| 249 |
break; |
| 250 |
} |
| 251 |
default: |
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
Cur_object_index = objnum; |
| 256 |
//Cur_object_seg = Cursegp; |
| 257 |
|
| 258 |
Update_flags |= UF_WORLD_CHANGED; |
| 259 |
|
| 260 |
return 1; |
| 261 |
} |
| 262 |
|
| 263 |
// ------------------------------------------------------------------------------------------------------ |
| 264 |
// Count number of player objects, return value. |
| 265 |
static int compute_num_players(void) |
| 266 |
{ |
| 267 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 268 |
auto &vcobjptr = Objects.vcptr; |
| 269 |
int count = 0; |
| 270 |
|
| 271 |
for (auto &obj : vcobjptr) |
| 272 |
{ |
| 273 |
if (obj.type == OBJ_PLAYER) |
| 274 |
count++; |
| 275 |
} |
| 276 |
|
| 277 |
return count; |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
int ObjectMakeCoop(void) |
| 282 |
{ |
| 283 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 284 |
auto &vmobjptr = Objects.vmptr; |
| 285 |
Assert(Cur_object_index != object_none); |
| 286 |
Assert(Cur_object_index < MAX_OBJECTS); |
| 287 |
// Assert(Objects[Cur_object_index.type == OBJ_PLAYER); |
| 288 |
|
| 289 |
const auto &&objp = vmobjptr(Cur_object_index); |
| 290 |
if (objp->type == OBJ_PLAYER) |
| 291 |
{ |
| 292 |
objp->type = OBJ_COOP; |
| 293 |
editor_status("You just made a player object COOPERATIVE"); |
| 294 |
} else |
| 295 |
editor_status("This is not a player object"); |
| 296 |
|
| 297 |
return 1; |
| 298 |
} |
| 299 |
|
| 300 |
// ------------------------------------------------------------------------------------------------------ |
| 301 |
// Place current object at center of current segment. |
| 302 |
int ObjectPlaceObject(void) |
| 303 |
{ |
| 304 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 305 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 306 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 307 |
auto &vmobjptr = Objects.vmptr; |
| 308 |
int old_cur_object_index; |
| 309 |
if (Cur_object_type == OBJ_PLAYER) |
| 310 |
{ |
| 311 |
int num_players = compute_num_players(); |
| 312 |
Assert(num_players <= MAX_MULTI_PLAYERS); |
| 313 |
if (num_players > MAX_PLAYERS) |
| 314 |
editor_status("You just placed a cooperative player object"); |
| 315 |
if (num_players == MAX_MULTI_PLAYERS) { |
| 316 |
editor_status_fmt("Can't place player object. Already %i players.", MAX_MULTI_PLAYERS); |
| 317 |
return -1; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
//update_due_to_new_segment(); |
| 322 |
auto &vcvertptr = Vertices.vcptr; |
| 323 |
const auto cur_object_loc{compute_segment_center(vcvertptr, Cursegp)}; |
| 324 |
|
| 325 |
old_cur_object_index = Cur_object_index; |
| 326 |
const auto rval = place_object(LevelUniqueObjectState, LevelSharedPolygonModelState, LevelSharedRobotInfoState.Robot_info, LevelSharedSegmentState, LevelUniqueSegmentState, Cursegp, cur_object_loc, Cur_object_type, Cur_object_id); |
| 327 |
|
| 328 |
if (old_cur_object_index != Cur_object_index) |
| 329 |
vmobjptr(Cur_object_index)->rtype.pobj_info.tmap_override = -1; |
| 330 |
|
| 331 |
return rval; |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
// ------------------------------------------------------------------------------------------------------ |
| 336 |
// Place current object at center of current segment. |
| 337 |
int ObjectPlaceObjectTmap(void) |
| 338 |
{ |
| 339 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 340 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 341 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 342 |
int old_cur_object_index; |
| 343 |
//update_due_to_new_segment(); |
| 344 |
auto &vcvertptr = Vertices.vcptr; |
| 345 |
const auto cur_object_loc{compute_segment_center(vcvertptr, Cursegp)}; |
| 346 |
|
| 347 |
old_cur_object_index = Cur_object_index; |
| 348 |
const auto rval = place_object(LevelUniqueObjectState, LevelSharedPolygonModelState, LevelSharedRobotInfoState.Robot_info, LevelSharedSegmentState, LevelUniqueSegmentState, Cursegp, cur_object_loc, Cur_object_type, Cur_object_id); |
| 349 |
|
| 350 |
if (Cur_object_index != old_cur_object_index && Objects[Cur_object_index].render_type == render_type::RT_POLYOBJ) |
| 351 |
Objects[Cur_object_index].rtype.pobj_info.tmap_override = CurrentTexture; |
| 352 |
else |
| 353 |
editor_status("Unable to apply current texture map to this object."); |
| 354 |
|
| 355 |
return rval; |
| 356 |
} |
| 357 |
|
| 358 |
// ------------------------------------------------------------------------------------------------------ |
| 359 |
int ObjectSelectNextinSegment(void) |
| 360 |
{ |
| 361 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 362 |
//update_due_to_new_segment(); |
| 363 |
|
| 364 |
//Assert(Cur_object_seg == Cursegp); |
| 365 |
|
| 366 |
const unique_segment &objuseg = Cursegp; |
| 367 |
if (Cur_object_index == object_none) { |
| 368 |
Cur_object_index = objuseg.objects; |
| 369 |
} else { |
| 370 |
if (Objects[Cur_object_index].segnum != Cursegp) |
| 371 |
Cur_object_index = objuseg.objects; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
//Debug: make sure current object is in current segment |
| 376 |
objnum_t id; |
| 377 |
for (id = objuseg.objects; id != Cur_object_index && id != object_none; id = Objects[id].next) |
| 378 |
{ |
| 379 |
} |
| 380 |
Assert(id == Cur_object_index); //should have found object |
| 381 |
|
| 382 |
// Select the next object, wrapping back to start if we are at the end of the linked list for this segment. |
| 383 |
if (id != object_none) |
| 384 |
Cur_object_index = get_next_object(objuseg, Cur_object_index); |
| 385 |
|
| 386 |
Update_flags |= UF_WORLD_CHANGED; |
| 387 |
|
| 388 |
return 1; |
| 389 |
|
| 390 |
} |
| 391 |
|
| 392 |
//Moves to next object in the mine, skipping the player |
| 393 |
int ObjectSelectNextInMine() |
| 394 |
{ int i; |
| 395 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 396 |
auto &vcobjptr = Objects.vcptr; |
| 397 |
for (i=0;i<MAX_OBJECTS;i++) { |
| 398 |
Cur_object_index++; |
| 399 |
if (Cur_object_index>= MAX_OBJECTS ) Cur_object_index= 0; |
| 400 |
|
| 401 |
const auto &&objp = vcobjptr(Cur_object_index); |
| 402 |
if (objp->type != OBJ_NONE && objp != ConsoleObject) |
| 403 |
{ |
| 404 |
Cursegp = imsegptridx(objp->segnum); |
| 405 |
med_create_new_segment_from_cursegp(); |
| 406 |
//Cur_object_seg = Cursegp; |
| 407 |
return 1; |
| 408 |
} |
| 409 |
} |
| 410 |
Cur_object_index = object_none; |
| 411 |
|
| 412 |
Update_flags |= UF_WORLD_CHANGED; |
| 413 |
|
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
//Moves to next object in the mine, skipping the player |
| 418 |
int ObjectSelectPrevInMine() |
| 419 |
{ int i; |
| 420 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 421 |
auto &vcobjptr = Objects.vcptr; |
| 422 |
for (i=0;i<MAX_OBJECTS;i++) { |
| 423 |
if (!(Cur_object_index --)) |
| 424 |
Cur_object_index = MAX_OBJECTS-1; |
| 425 |
|
| 426 |
const auto &&objp = vcobjptr(Cur_object_index); |
| 427 |
if (objp->type != OBJ_NONE && objp != ConsoleObject) |
| 428 |
{ |
| 429 |
Cursegp = imsegptridx(objp->segnum); |
| 430 |
med_create_new_segment_from_cursegp(); |
| 431 |
//Cur_object_seg = Cursegp; |
| 432 |
return 1; |
| 433 |
} |
| 434 |
} |
| 435 |
Cur_object_index = object_none; |
| 436 |
|
| 437 |
Update_flags |= UF_WORLD_CHANGED; |
| 438 |
|
| 439 |
return 0; |
| 440 |
} |
| 441 |
|
| 442 |
// ------------------------------------------------------------------------------------------------------ |
| 443 |
// Delete current object, if it exists. |
| 444 |
// If it doesn't exist, reformat Matt's hard disk, even if he is in Boston. |
| 445 |
int ObjectDelete(void) |
| 446 |
{ |
| 447 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 448 |
auto &vmobjptridx = Objects.vmptridx; |
| 449 |
|
| 450 |
if (Cur_object_index != object_none) { |
| 451 |
auto delete_objnum = Cur_object_index; |
| 452 |
ObjectSelectNextinSegment(); |
| 453 |
|
| 454 |
obj_delete(LevelUniqueObjectState, Segments, vmobjptridx(delete_objnum)); |
| 455 |
|
| 456 |
if (delete_objnum == Cur_object_index) |
| 457 |
Cur_object_index = object_none; |
| 458 |
|
| 459 |
Update_flags |= UF_WORLD_CHANGED; |
| 460 |
} |
| 461 |
|
| 462 |
return 1; |
| 463 |
} |
| 464 |
|
| 465 |
// ----------------------------------------------------------------------------------------------------------------- |
| 466 |
// Object has moved to another segment, (or at least poked through). |
| 467 |
// If still in mine, that is legal, so relink into new segment. |
| 468 |
// Return value: 0 = in mine, 1 = not in mine |
| 469 |
static int move_object_within_mine(fvmobjptr &vmobjptr, segment_array &Segments, fvcvertptr &vcvertptr, const vmobjptridx_t obj, const vms_vector &newpos) |
| 470 |
{ |
| 471 |
for (const auto &&segp : Segments.vmptridx) |
| 472 |
{ |
| 473 |
if (get_seg_masks(vcvertptr, obj->pos, segp, 0).centermask == sidemask_t{}) |
| 474 |
{ |
| 475 |
fvi_info hit_info; |
| 476 |
|
| 477 |
// See if the radius pokes through any wall. |
| 478 |
const auto fate = find_vector_intersection(fvi_query{ |
| 479 |
obj->pos, |
| 480 |
newpos, |
| 481 |
fvi_query::unused_ignore_obj_list, |
| 482 |
fvi_query::unused_LevelUniqueObjectState, |
| 483 |
fvi_query::unused_Robot_info, |
| 484 |
0, |
| 485 |
object_none, |
| 486 |
}, obj->segnum, obj->size, hit_info); |
| 487 |
if (fate != fvi_hit_type::Wall) |
| 488 |
{ |
| 489 |
if (segp != obj->segnum) |
| 490 |
obj_relink(vmobjptr, Segments.vmptr, obj, segp); |
| 491 |
obj->pos = newpos; |
| 492 |
return 0; |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
return 1; |
| 497 |
} |
| 498 |
|
| 499 |
// Return 0 if object is in expected segment, else return 1 |
| 500 |
static int verify_object_seg(fvmobjptr &vmobjptr, segment_array &Segments, fvcvertptr &vcvertptr, const vmobjptridx_t objp, const vms_vector &newpos) |
| 501 |
{ |
| 502 |
const auto &&result = get_seg_masks(vcvertptr, newpos, Segments.vcptr(objp->segnum), objp->size); |
| 503 |
if (result.facemask == 0) |
| 504 |
return 0; |
| 505 |
else |
| 506 |
return move_object_within_mine(vmobjptr, Segments, vcvertptr, objp, newpos); |
| 507 |
} |
| 508 |
|
| 509 |
namespace { |
| 510 |
|
| 511 |
class extract_fvec_from_segment |
| 512 |
{ |
| 513 |
public: |
| 514 |
static vms_vector get(fvcvertptr &vcvertptr, const shared_segment &segp) |
| 515 |
{ |
| 516 |
return extract_forward_vector_from_segment(vcvertptr, segp); |
| 517 |
} |
| 518 |
}; |
| 519 |
|
| 520 |
class extract_rvec_from_segment |
| 521 |
{ |
| 522 |
public: |
| 523 |
static vms_vector get(fvcvertptr &vcvertptr, const shared_segment &segp) |
| 524 |
{ |
| 525 |
return extract_right_vector_from_segment(vcvertptr, segp); |
| 526 |
} |
| 527 |
}; |
| 528 |
|
| 529 |
class extract_uvec_from_segment |
| 530 |
{ |
| 531 |
public: |
| 532 |
static vms_vector get(fvcvertptr &vcvertptr, const shared_segment &segp) |
| 533 |
{ |
| 534 |
return extract_up_vector_from_segment(vcvertptr, segp); |
| 535 |
} |
| 536 |
}; |
| 537 |
|
| 538 |
static int ObjectMoveFailed() |
| 539 |
{ |
| 540 |
editor_status("No current object, cannot move."); |
| 541 |
return 1; |
| 542 |
} |
| 543 |
|
| 544 |
static int ObjectMovePos(const vmobjptridx_t obj, vms_vector &&vec, int scale) |
| 545 |
{ |
| 546 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 547 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 548 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 549 |
auto &vmobjptr = Objects.vmptr; |
| 550 |
vm_vec_normalize(vec); |
| 551 |
const auto &&newpos = vm_vec_build_add(obj->pos, vm_vec_scale(vec, scale)); |
| 552 |
auto &vcvertptr = Vertices.vcptr; |
| 553 |
if (!verify_object_seg(vmobjptr, Segments, vcvertptr, obj, newpos)) |
| 554 |
obj->pos = newpos; |
| 555 |
Update_flags |= UF_WORLD_CHANGED; |
| 556 |
return 1; |
| 557 |
} |
| 558 |
|
| 559 |
template <typename extract_type, int direction> |
| 560 |
static int ObjectMove() |
| 561 |
{ |
| 562 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 563 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 564 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 565 |
auto &vmobjptridx = Objects.vmptridx; |
| 566 |
const auto i{Cur_object_index}; |
| 567 |
if (i == object_none) |
| 568 |
return ObjectMoveFailed(); |
| 569 |
const auto &&obj = vmobjptridx(i); |
| 570 |
auto &vcvertptr = Vertices.vcptr; |
| 571 |
return ObjectMovePos(obj, extract_type::get(vcvertptr, vcsegptr(obj->segnum)), direction * OBJ_SCALE); |
| 572 |
} |
| 573 |
|
| 574 |
} |
| 575 |
|
| 576 |
// ------------------------------------------------------------------------------------------------------ |
| 577 |
int ObjectMoveForward(void) |
| 578 |
{ |
| 579 |
return ObjectMove<extract_fvec_from_segment, 1>(); |
| 580 |
} |
| 581 |
|
| 582 |
// ------------------------------------------------------------------------------------------------------ |
| 583 |
int ObjectMoveBack(void) |
| 584 |
{ |
| 585 |
return ObjectMove<extract_fvec_from_segment, -1>(); |
| 586 |
} |
| 587 |
|
| 588 |
// ------------------------------------------------------------------------------------------------------ |
| 589 |
int ObjectMoveLeft(void) |
| 590 |
{ |
| 591 |
return ObjectMove<extract_rvec_from_segment, -1>(); |
| 592 |
} |
| 593 |
|
| 594 |
// ------------------------------------------------------------------------------------------------------ |
| 595 |
int ObjectMoveRight(void) |
| 596 |
{ |
| 597 |
return ObjectMove<extract_rvec_from_segment, 1>(); |
| 598 |
} |
| 599 |
|
| 600 |
// ------------------------------------------------------------------------------------------------------ |
| 601 |
int ObjectSetDefault(void) |
| 602 |
{ |
| 603 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 604 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 605 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 606 |
auto &vmobjptr = Objects.vmptr; |
| 607 |
//update_due_to_new_segment(); |
| 608 |
|
| 609 |
if (Cur_object_index == object_none) { |
| 610 |
editor_status("No current object, cannot move."); |
| 611 |
return 1; |
| 612 |
} |
| 613 |
|
| 614 |
const auto &&objp = vmobjptr(Cur_object_index); |
| 615 |
auto &vcvertptr = Vertices.vcptr; |
| 616 |
objp->pos = compute_segment_center(vcvertptr, vcsegptr(objp->segnum)); |
| 617 |
|
| 618 |
Update_flags |= UF_WORLD_CHANGED; |
| 619 |
|
| 620 |
return 1; |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
// ------------------------------------------------------------------------------------------------------ |
| 625 |
int ObjectMoveUp(void) |
| 626 |
{ |
| 627 |
return ObjectMove<extract_uvec_from_segment, 1>(); |
| 628 |
} |
| 629 |
|
| 630 |
// ------------------------------------------------------------------------------------------------------ |
| 631 |
int ObjectMoveDown(void) |
| 632 |
{ |
| 633 |
return ObjectMove<extract_uvec_from_segment, -1>(); |
| 634 |
} |
| 635 |
|
| 636 |
// ------------------------------------------------------------------------------------------------------ |
| 637 |
|
| 638 |
static int rotate_object(const vmobjptridx_t obj, int p, int b, int h) |
| 639 |
{ |
| 640 |
vms_angvec ang; |
| 641 |
// vm_extract_angles_matrix( &ang,&obj->orient); |
| 642 |
|
| 643 |
// ang.p += p; |
| 644 |
// ang.b += b; |
| 645 |
// ang.h += h; |
| 646 |
|
| 647 |
ang.p = p; |
| 648 |
ang.b = b; |
| 649 |
ang.h = h; |
| 650 |
|
| 651 |
const auto &&rotmat{vm_angles_2_matrix(ang)}; |
| 652 |
obj->orient = vm_matrix_x_matrix(obj->orient, rotmat); |
| 653 |
|
| 654 |
Update_flags |= UF_WORLD_CHANGED; |
| 655 |
|
| 656 |
return 1; |
| 657 |
} |
| 658 |
|
| 659 |
static void reset_object(const vmobjptridx_t obj) |
| 660 |
{ |
| 661 |
reconstruct_at(obj->orient, med_extract_matrix_from_segment, *vcsegptr(obj->segnum)); |
| 662 |
} |
| 663 |
|
| 664 |
int ObjectResetObject() |
| 665 |
{ |
| 666 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 667 |
auto &vmobjptridx = Objects.vmptridx; |
| 668 |
reset_object(vmobjptridx(Cur_object_index)); |
| 669 |
|
| 670 |
Update_flags |= UF_WORLD_CHANGED; |
| 671 |
|
| 672 |
return 1; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
int ObjectFlipObject() |
| 677 |
{ |
| 678 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 679 |
auto &vmobjptr = Objects.vmptr; |
| 680 |
const auto m = &vmobjptr(Cur_object_index)->orient; |
| 681 |
|
| 682 |
vm_vec_negate(m->uvec); |
| 683 |
vm_vec_negate(m->rvec); |
| 684 |
|
| 685 |
Update_flags |= UF_WORLD_CHANGED; |
| 686 |
|
| 687 |
return 1; |
| 688 |
} |
| 689 |
|
| 690 |
template <int p, int b, int h> |
| 691 |
int ObjectChangeRotation() |
| 692 |
{ |
| 693 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 694 |
auto &vmobjptridx = Objects.vmptridx; |
| 695 |
return rotate_object(vmobjptridx(Cur_object_index), p, b, h); |
| 696 |
} |
| 697 |
|
| 698 |
template int ObjectDecreaseBank(); |
| 699 |
template int ObjectIncreaseBank(); |
| 700 |
template int ObjectDecreasePitch(); |
| 701 |
template int ObjectIncreasePitch(); |
| 702 |
template int ObjectDecreaseHeading(); |
| 703 |
template int ObjectIncreaseHeading(); |
| 704 |
|
| 705 |
template int ObjectDecreaseBankBig(); |
| 706 |
template int ObjectIncreaseBankBig(); |
| 707 |
template int ObjectDecreasePitchBig(); |
| 708 |
template int ObjectIncreasePitchBig(); |
| 709 |
template int ObjectDecreaseHeadingBig(); |
| 710 |
template int ObjectIncreaseHeadingBig(); |
| 711 |
|
| 712 |
// ----------------------------------------------------------------------------------------------------- |
| 713 |
// Move object around based on clicks in 2d screen. |
| 714 |
// Slide an object parallel to the 2d screen, to a point on a vector. |
| 715 |
// The vector is defined by a point on the 2d screen and the eye. |
| 716 |
|
| 717 |
// V = vector from eye to 2d screen point. |
| 718 |
// E = eye |
| 719 |
// F = forward vector from eye |
| 720 |
// O = 3-space location of object |
| 721 |
|
| 722 |
// D = depth of object given forward vector F |
| 723 |
// = (OE dot norm(F)) |
| 724 |
|
| 725 |
// Must solve intersection of: |
| 726 |
// E + tV ( equation of vector from eye through point on 2d screen) |
| 727 |
// Fs + D ( equation of plane parallel to 2d screen, at depth D) |
| 728 |
// = Fx(Ex + tVx) + Fy(Ey + tVy) + Fz(Ez + tVz) + D = 0 |
| 729 |
// |
| 730 |
// FxEx + FyEy + FzEz - D |
| 731 |
// t = - ---------------------- |
| 732 |
// VxFx + VyFy + VzFz |
| 733 |
|
| 734 |
static void move_object_to_position(const vmobjptridx_t objp, const vms_vector &newpos) |
| 735 |
{ |
| 736 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 737 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 738 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 739 |
auto &vmobjptr = Objects.vmptr; |
| 740 |
auto &vcvertptr = Vertices.vcptr; |
| 741 |
if (get_seg_masks(vcvertptr, newpos, vcsegptr(objp->segnum), objp->size).facemask == 0) |
| 742 |
{ |
| 743 |
objp->pos = newpos; |
| 744 |
} else { |
| 745 |
if (verify_object_seg(vmobjptr, Segments, vcvertptr, objp, newpos)) { |
| 746 |
fvi_info hit_info; |
| 747 |
|
| 748 |
auto temp_viewer_obj = *Viewer; |
| 749 |
auto viewer_segnum = find_object_seg(LevelSharedSegmentState, LevelUniqueSegmentState, *Viewer); |
| 750 |
temp_viewer_obj.segnum = viewer_segnum; |
| 751 |
|
| 752 |
// If the viewer is outside the mine, get him in the mine! |
| 753 |
if (viewer_segnum == segment_none) { |
| 754 |
editor_status("Unable to move object, viewer not in mine. Aborting"); |
| 755 |
return; |
| 756 |
#if 0 |
| 757 |
vms_vector last_outside_pos; |
| 758 |
// While outside mine, move towards object |
| 759 |
count = 0; |
| 760 |
while (viewer_segnum == segment_none) { |
| 761 |
vms_vector temp_vec; |
| 762 |
|
| 763 |
last_outside_pos = temp_viewer_obj.pos; |
| 764 |
|
| 765 |
vm_vec_avg(&temp_vec, &temp_viewer_obj.pos, newpos); |
| 766 |
temp_viewer_obj.pos = temp_vec; |
| 767 |
viewer_segnum = find_object_seg(&temp_viewer_obj); |
| 768 |
temp_viewer_obj.segnum = viewer_segnum; |
| 769 |
|
| 770 |
if (count > 5) { |
| 771 |
editor_status("Unable to move object, can't get viewer in mine. Aborting"); |
| 772 |
return; |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
count = 0; |
| 777 |
// While inside mine, move away from object. |
| 778 |
while (viewer_segnum != segment_none) { |
| 779 |
|
| 780 |
vms_vector temp_vec; |
| 781 |
|
| 782 |
vm_vec_avg(&temp_vec, &temp_viewer_obj.pos, &last_outside_pos); |
| 783 |
temp_viewer_obj.pos = temp_vec; |
| 784 |
update_object_seg(&temp_viewer_obj); |
| 785 |
viewer_segnum = find_object_seg(&temp_viewer_obj); |
| 786 |
temp_viewer_obj.segnum = viewer_segnum; |
| 787 |
|
| 788 |
if (count > 5) { |
| 789 |
editor_status("Unable to move object, can't get viewer back out of mine. Aborting"); |
| 790 |
return; |
| 791 |
} |
| 792 |
} |
| 793 |
#endif |
| 794 |
} |
| 795 |
|
| 796 |
const auto fate = find_vector_intersection(fvi_query{ |
| 797 |
temp_viewer_obj.pos, |
| 798 |
newpos, |
| 799 |
fvi_query::unused_ignore_obj_list, |
| 800 |
fvi_query::unused_LevelUniqueObjectState, |
| 801 |
fvi_query::unused_Robot_info, |
| 802 |
0, |
| 803 |
object_none, |
| 804 |
}, temp_viewer_obj.segnum, temp_viewer_obj.size, hit_info); |
| 805 |
if (fate == fvi_hit_type::Wall) |
| 806 |
{ |
| 807 |
objp->pos = hit_info.hit_pnt; |
| 808 |
const auto &&segp = find_object_seg(LevelSharedSegmentState, LevelUniqueSegmentState, objp); |
| 809 |
if (segp != segment_none) |
| 810 |
obj_relink(vmobjptr, vmsegptr, objp, segp); |
| 811 |
} else { |
| 812 |
editor_status("Attempted to move object out of mine. Object not moved."); |
| 813 |
} |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
Update_flags |= UF_WORLD_CHANGED; |
| 818 |
} |
| 819 |
|
| 820 |
static void move_object_to_vector(const vms_vector &vec_through_screen, fix delta_distance) |
| 821 |
{ |
| 822 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 823 |
auto &vmobjptridx = Objects.vmptridx; |
| 824 |
const auto &&objp = vmobjptridx(Cur_object_index); |
| 825 |
const auto result{vm_vec_scale_add(Viewer->pos, vec_through_screen, vm_vec_dist(Viewer->pos, objp->pos) + delta_distance)}; |
| 826 |
move_object_to_position(objp, result); |
| 827 |
} |
| 828 |
|
| 829 |
} |
| 830 |
|
| 831 |
static void move_object_to_mouse_click_delta(fix delta_distance) |
| 832 |
{ |
| 833 |
short xcrd,ycrd; |
| 834 |
|
| 835 |
if (Cur_object_index == object_none) { |
| 836 |
editor_status("Cur_object_index == -1, cannot move that peculiar object...aborting!"); |
| 837 |
return; |
| 838 |
} |
| 839 |
|
| 840 |
xcrd = GameViewBox->b1_drag_x1; |
| 841 |
ycrd = GameViewBox->b1_drag_y1; |
| 842 |
|
| 843 |
const auto vec_through_screen{med_point_2_vec(&_canv_editor_game, xcrd, ycrd)}; |
| 844 |
|
| 845 |
move_object_to_vector(vec_through_screen, delta_distance); |
| 846 |
|
| 847 |
} |
| 848 |
|
| 849 |
void move_object_to_mouse_click(void) |
| 850 |
{ |
| 851 |
move_object_to_mouse_click_delta(0); |
| 852 |
} |
| 853 |
|
| 854 |
namespace dsx { |
| 855 |
|
| 856 |
int ObjectMoveNearer(void) |
| 857 |
{ |
| 858 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 859 |
auto &vcobjptr = Objects.vcptr; |
| 860 |
if (Cur_object_index == object_none) { |
| 861 |
editor_status("Cur_object_index == -1, cannot move that peculiar object...aborting!"); |
| 862 |
return 1; |
| 863 |
} |
| 864 |
|
| 865 |
// move_object_to_mouse_click_delta(-4*F1_0); // Move four units closer to eye |
| 866 |
|
| 867 |
const auto &&result = vm_vec_normalized(vm_vec_build_sub(vcobjptr(Cur_object_index)->pos, Viewer->pos)); |
| 868 |
move_object_to_vector(result, -4*F1_0); |
| 869 |
|
| 870 |
return 1; |
| 871 |
} |
| 872 |
|
| 873 |
int ObjectMoveFurther(void) |
| 874 |
{ |
| 875 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 876 |
auto &vcobjptr = Objects.vcptr; |
| 877 |
if (Cur_object_index == object_none) { |
| 878 |
editor_status("Cur_object_index == -1, cannot move that peculiar object...aborting!"); |
| 879 |
return 1; |
| 880 |
} |
| 881 |
|
| 882 |
// move_object_to_mouse_click_delta(+4*F1_0); // Move four units further from eye |
| 883 |
|
| 884 |
const auto &&result = vm_vec_normalized(vm_vec_build_sub(vcobjptr(Cur_object_index)->pos, Viewer->pos)); |
| 885 |
move_object_to_vector(result, 4*F1_0); |
| 886 |
|
| 887 |
return 1; |
| 888 |
} |
| 889 |
|
| 890 |
} |