Skip to content

Commit 95315d7

Browse files
committed
Optimizes dock_undock_objects() and dock_dead_undock_objects() methods by removing their dock pair check, because the same check is done in the remove_instance subroutines.
* Adds a warning for debug users when an unknown ship_state is passed to red_alert_delete. * Removes commented-out code * Doxy's ship_cleanup() *Conform to each file's style. *Extend ship_cleanup to better handle red-alert deletion. *Also treat cleanup_mode as a flagset, instead of an enum as it was previously.
1 parent 6577d53 commit 95315d7

File tree

5 files changed

+84
-66
lines changed

5 files changed

+84
-66
lines changed

code/missionui/redalert.cpp

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -781,43 +781,25 @@ void red_alert_store_wingman_status()
781781
void red_alert_delete_ship(int shipnum, int ship_state)
782782
{
783783
ship* shipp = &Ships[shipnum];
784-
if ( (shipp->wing_status_wing_index >= 0) && (shipp->wing_status_wing_pos >= 0) ) {
785-
if (ship_state == RED_ALERT_DESTROYED_SHIP_CLASS) {
786-
hud_set_wingman_status_dead(shipp->wing_status_wing_index, shipp->wing_status_wing_pos);
787-
} else if (ship_state == RED_ALERT_PLAYER_DEL_SHIP_CLASS) {
788-
hud_set_wingman_status_none(shipp->wing_status_wing_index, shipp->wing_status_wing_pos);
789-
} else {
790-
Error(LOCATION, "Red Alert: asked to delete ship (%s) with invalid ship state (%d)", shipp->ship_name, ship_state);
791-
}
792-
}
793784

794-
// If this ship is docked with anybody, undock them all before deleting
795-
dock_undock_all(&Objects[shipp->objnum]);
796-
dock_dead_undock_all(&Objects[shipp->objnum]);
797-
798-
int cleanup_mode; // See ship.h for cleanup mode defines (SHIP_VANISHED)
785+
int cleanup_mode = SHIP_REDALERT; // See ship.h for cleanup mode defines
799786
switch (ship_state) {
800787
case RED_ALERT_DESTROYED_SHIP_CLASS:
801-
cleanup_mode = SHIP_DESTROYED;
788+
cleanup_mode |= SHIP_DESTROYED;
802789
break;
803790
case RED_ALERT_PLAYER_DEL_SHIP_CLASS:
804-
cleanup_mode = SHIP_DEPARTED;
791+
cleanup_mode |= SHIP_DEPARTED;
805792
break;
806793
default:
807-
cleanup_mode = SHIP_VANISHED;
794+
#ifdef DEBUG
795+
Warning(LOCATION, "Red-alert: Unknown delete state, assuming vanished.");
796+
#endif
797+
cleanup_mode |= SHIP_VANISHED;
808798
break;
809799
}
810800

811801
Objects[shipp->objnum].flags.set(Object::Object_Flags::Should_be_dead);
812802
ship_cleanup(shipnum, cleanup_mode);
813-
/*
814-
ship_add_exited_ship( shipp, exit_flag );
815-
816-
obj_delete(shipp->objnum);
817-
if ( shipp->wingnum >= 0 ) {
818-
ship_wing_cleanup( SHIP_INDEX(shipp), &Wings[shipp->wingnum] );
819-
}
820-
*/
821803
}
822804

823805
// just mark the parse object as never going to arrive

code/object/deadobjectdock.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,17 @@ void dock_dead_undock_objects(object *objp1, object *objp2)
7373
Assert(objp1 != NULL);
7474
Assert(objp2 != NULL);
7575

76-
#ifndef NDEBUG
77-
if ((dead_dock_find_instance(objp1, objp2) == NULL) || (dead_dock_find_instance(objp2, objp1) == NULL))
78-
{
79-
Error(LOCATION, "Trying to undock an object that isn't docked!\n");
80-
}
81-
#endif
82-
8376
// remove objects from each others' dock lists
8477
dead_dock_remove_instance(objp1, objp2);
8578
dead_dock_remove_instance(objp2, objp1);
8679
}
8780

88-
void dock_dead_undock_all(object *objp) {
81+
void dock_dead_undock_all(object *objp)
82+
{
8983
Assert(objp != NULL);
9084

91-
while (object_is_dead_docked(objp)) {
85+
while (object_is_dead_docked(objp))
86+
{
9287
object* dockee = dock_get_first_dead_docked_object(objp);
9388

9489
dock_dead_undock_objects(objp, dockee);
@@ -149,6 +144,16 @@ void dead_dock_remove_instance(object *objp, object *other_objp)
149144
// delete it
150145
vm_free(ptr);
151146
}
147+
else
148+
{
149+
// Raise an error if we're in a release build.
150+
// Raise a warning if we're in a debug build, the user may be doing something silly
151+
#ifndef NDEBUG
152+
Error(LOCATION, "Tried to undock an object that isn't docked!\n");
153+
#else
154+
Warning(LOCATION, "Tried to undock an object that isn't docked! Proceed with caution!\n");
155+
#endif
156+
}
152157
}
153158

154159
// just free the list without worrying about undocking anything

code/object/objectdock.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -662,22 +662,17 @@ void dock_undock_objects(object *objp1, object *objp2)
662662
Assert(objp1 != NULL);
663663
Assert(objp2 != NULL);
664664

665-
#ifndef NDEBUG
666-
if ((dock_find_instance(objp1, objp2) == NULL) || (dock_find_instance(objp2, objp1) == NULL))
667-
{
668-
Error(LOCATION, "Trying to undock an object that isn't docked!\n");
669-
}
670-
#endif
671-
672665
// remove objects from each others' dock lists
673666
dock_remove_instance(objp1, objp2);
674667
dock_remove_instance(objp2, objp1);
675668
}
676669

677-
void dock_undock_all(object *objp) {
670+
void dock_undock_all(object *objp)
671+
{
678672
Assert(objp != NULL);
679673

680-
while (object_is_docked(objp)) {
674+
while (object_is_docked(objp))
675+
{
681676
object* dockee = dock_get_first_docked_object(objp);
682677

683678
dock_undock_objects(objp, dockee);
@@ -771,6 +766,16 @@ void dock_remove_instance(object *objp, object *other_objp)
771766
// delete it
772767
vm_free(ptr);
773768
}
769+
else
770+
{
771+
// Raise an error if we're in a release build.
772+
// Raise a warning if we're in a debug build, the user may be doing something silly
773+
#ifndef NDEBUG
774+
Error(LOCATION, "Tried to undock an object that isn't docked!\n");
775+
#else
776+
Warning(LOCATION, "Tried to undock an object that isn't docked! Proceed with caution!\n");
777+
#endif
778+
}
774779
}
775780

776781
// just free the list without worrying about undocking anything

code/ship/ship.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7389,22 +7389,29 @@ void ship_destroy_instantly(object *ship_objp, int shipnum)
73897389
void ship_cleanup(int shipnum, int cleanup_mode)
73907390
{
73917391
Assert(shipnum >= 0 && shipnum < MAX_SHIPS);
7392-
Assert(cleanup_mode == SHIP_DESTROYED || cleanup_mode == SHIP_DEPARTED || cleanup_mode == SHIP_VANISHED);
7392+
Assert(cleanup_mode & (SHIP_DESTROYED | SHIP_DEPARTED | SHIP_VANISHED));
73937393
Assert(Objects[Ships[shipnum].objnum].type == OBJ_SHIP);
73947394
Assert(Objects[Ships[shipnum].objnum].flags[Object::Object_Flags::Should_be_dead]);
73957395

73967396
ship *shipp = &Ships[shipnum];
73977397
object *objp = &Objects[shipp->objnum];
73987398

73997399
// add the information to the exited ship list
7400-
if (cleanup_mode == SHIP_DESTROYED) {
7401-
ship_add_exited_ship(shipp, Ship::Exit_Flags::Destroyed);
7400+
if (!(cleanup_mode & SHIP_REDALERT)) {
7401+
if (cleanup_mode & SHIP_DESTROYED) {
7402+
ship_add_exited_ship(shipp, Ship::Exit_Flags::Destroyed);
7403+
} else {
7404+
// (cleanup_mode & (SHIP_DEPARTED | SHIP_VANISHED)
7405+
ship_add_exited_ship(shipp, Ship::Exit_Flags::Departed);
7406+
}
74027407
} else {
7403-
ship_add_exited_ship(shipp, Ship::Exit_Flags::Departed);
7408+
// (cleanup_mode & SHIP_REDALERT)
7409+
// Ship was removed in previous mission. Mark as "player deleted" for this mission
7410+
ship_add_exited_ship(shipp, Ship::Exit_Flags::Player_deleted);
74047411
}
74057412

74067413
// record kill?
7407-
if (cleanup_mode == SHIP_DESTROYED) {
7414+
if (!(cleanup_mode & SHIP_REDALERT) && (cleanup_mode & SHIP_DESTROYED)) {
74087415
// determine if we need to count this ship as a kill in counting number of kills per ship type
74097416
// look at the ignore flag for the ship (if not in a wing), or the ignore flag for the wing
74107417
// (if the ship is in a wing), and add to the kill count if the flags are not set
@@ -7417,8 +7424,8 @@ void ship_cleanup(int shipnum, int cleanup_mode)
74177424
}
74187425

74197426
// add mission log entry?
7420-
// (vanished ships have no log, and destroyed ships are logged in ship_hit_kill)
7421-
if (cleanup_mode == SHIP_DEPARTED) {
7427+
// (vanished ships and red-alert deleted ships have no log, and destroyed ships are logged in ship_hit_kill)
7428+
if (!(cleanup_mode & SHIP_REDALERT) && (cleanup_mode & SHIP_DEPARTED)) {
74227429
// see if this ship departed within the radius of a jump node -- if so, put the node name into
74237430
// the secondary mission log field
74247431
CJumpNode *jnp = jumpnode_get_which_in(objp);
@@ -7430,20 +7437,27 @@ void ship_cleanup(int shipnum, int cleanup_mode)
74307437

74317438
#ifndef NDEBUG
74327439
// add a debug log entry
7433-
if (cleanup_mode == SHIP_DESTROYED) {
7434-
nprintf(("Alan", "SHIP DESTROYED: %s\n", shipp->ship_name));
7435-
} else if (cleanup_mode == SHIP_DEPARTED) {
7436-
nprintf(("Alan", "SHIP DEPARTED: %s\n", shipp->ship_name));
7440+
if (cleanup_mode & SHIP_DESTROYED) {
7441+
nprintf(("Alan", "SHIP DESTROYED: %s", shipp->ship_name));
7442+
} else if (cleanup_mode & SHIP_DEPARTED) {
7443+
nprintf(("Alan", "SHIP DEPARTED: %s", shipp->ship_name));
74377444
} else {
7438-
nprintf(("Alan", "SHIP VANISHED: %s\n", shipp->ship_name));
7445+
nprintf(("Alan", "SHIP VANISHED: %s", shipp->ship_name));
7446+
}
7447+
7448+
// if it was removed via red_alert_delete_ship, log it as such
7449+
if (cleanup_mode & SHIP_REDALERT) {
7450+
nprintf(("Alan", "(RED-ALERT)\n"));
7451+
} else {
7452+
nprintf(("Alan", "\n"));
74397453
}
74407454
#endif
74417455

74427456
// update wingman status gauge
74437457
if ( (shipp->wing_status_wing_index >= 0) && (shipp->wing_status_wing_pos >= 0) ) {
7444-
if (cleanup_mode == SHIP_DESTROYED) {
7458+
if (cleanup_mode & SHIP_DESTROYED) {
74457459
hud_set_wingman_status_dead(shipp->wing_status_wing_index, shipp->wing_status_wing_pos);
7446-
} else if (cleanup_mode == SHIP_DEPARTED) {
7460+
} else if ((cleanup_mode & SHIP_DEPARTED) && !(cleanup_mode & SHIP_REDALERT)){
74477461
hud_set_wingman_status_departed(shipp->wing_status_wing_index, shipp->wing_status_wing_pos);
74487462
} else {
74497463
hud_set_wingman_status_none(shipp->wing_status_wing_index, shipp->wing_status_wing_pos);
@@ -7454,9 +7468,9 @@ void ship_cleanup(int shipnum, int cleanup_mode)
74547468
if ( shipp->wingnum != -1 ) {
74557469
wing *wingp = &Wings[shipp->wingnum];
74567470

7457-
if (cleanup_mode == SHIP_DESTROYED) {
7471+
if (cleanup_mode & SHIP_DESTROYED) {
74587472
wingp->total_destroyed++;
7459-
} else if (cleanup_mode == SHIP_DEPARTED) {
7473+
} else if (cleanup_mode & SHIP_DEPARTED) {
74607474
wingp->total_departed++;
74617475
} else {
74627476
wingp->total_vanished++;
@@ -7467,7 +7481,7 @@ void ship_cleanup(int shipnum, int cleanup_mode)
74677481

74687482
// Note, this call to ai_ship_destroy must come after ship_wing_cleanup for guarded wings to
74697483
// properly note the destruction of a ship in their wing.
7470-
if (cleanup_mode == SHIP_DESTROYED) {
7484+
if (cleanup_mode & SHIP_DESTROYED) {
74717485
ai_ship_destroy(shipnum, Ship::Exit_Flags::Destroyed); // Do AI stuff for destruction of ship.
74727486
} else {
74737487
ai_ship_destroy(shipnum, Ship::Exit_Flags::Departed); // should still do AI cleanup after ship has departed
@@ -7476,11 +7490,9 @@ void ship_cleanup(int shipnum, int cleanup_mode)
74767490
// Goober5000 - lastly, clear out the dead-docked list, per Mantis #2294
74777491
// (for exploding ships, this list should have already been cleared by now, via
74787492
// do_dying_undock_physics, except in the case of the destroy-instantly sexp)
7479-
while (object_is_dead_docked(objp))
7480-
{
7481-
object *docked_objp = dock_get_first_dead_docked_object(objp);
7482-
dock_dead_undock_objects(objp, docked_objp);
7483-
}
7493+
// z64555 - Also clear out the docked list, since a red-alert-carry ship could be docked with somebody
7494+
dock_undock_all(objp);
7495+
dock_dead_undock_all(objp);
74847496
}
74857497

74867498
/**

code/ship/ship.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,8 +1376,22 @@ extern int ship_get_num_ships();
13761376
#define SHIP_DEPARTED_WARP (1<<2)
13771377
#define SHIP_DEPARTED_BAY (1<<3)
13781378
#define SHIP_DEPARTED ( SHIP_DEPARTED_BAY | SHIP_DEPARTED_WARP )
1379-
// Goober5000
1379+
#define SHIP_REDALERT (1<<4)
1380+
1381+
/**
1382+
* @brief Deletes and de-inits a ship.
1383+
*
1384+
* @param[in] shipnum Index of this ship in Ships[]
1385+
* @param[in] cleanup_mode Flags describing how this ship is to be removed. See SHIP_VANISHED, SHIP_DESTROYED, etc.
1386+
*
1387+
* @details This is the deconstructor of a ship, it does all the necassary processes to remove the ship from the Ships
1388+
* array, and frees the slot for use by others. De-init of its Objects[] slot is handled elsewhere.
1389+
*
1390+
* @author Goober5000
1391+
*/
13801392
extern void ship_cleanup(int shipnum, int cleanup_mode);
1393+
1394+
// Goober5000
13811395
extern void ship_destroy_instantly(object *ship_obj, int shipnum);
13821396
extern void ship_actually_depart(int shipnum, int method = SHIP_DEPARTED_WARP);
13831397

0 commit comments

Comments
 (0)