Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

feat(gameengine): open factory doors before the unit is done

Olcay Seygan0307d38d545c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 82bcf34df..99c78e136 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -230,6 +230,7 @@ found and fixed — EA's own, not port damage.**
- A bounty pays what the percentage says. Rounding up a fraction that was already a whole number paid a dollar extra on every kill.
- A supply upgrade pays for what arrives. The bonus was a flat sum handed over on arrival, so a truck turning up with one box collected the same as a full load - and a driver dropping off little and often earned several times what the upgrade is worth.
- Taking over a defeated ally's base no longer starts his research again from scratch. A player upgrade is bought once for the whole player, and the buildings you inherit were left paying for a second copy of what you already had.
+- A tank drives out the moment it is paid for. The factory doors only began opening once the vehicle was already finished, and it stood inside waiting for them, so every single unit cost its build time plus the whole door animation on top - on every tank, all game. The doors now start moving during the last seconds of the build and are open on the frame the unit is done. And they stay open while there is another vehicle behind him, however long that takes: a factory working through a queue keeps its doors up and shuts them when the queue runs out, instead of dropping and hauling them back for every unit. That was worth watching in its own right - there is no artwork for a door that changes its mind partway, so a door caught mid-close and pulled back open snapped wide in a single frame.
## One crate, one collector
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/ProductionUpdate.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/ProductionUpdate.h
index 0bb6c3770..34a0b5176 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/ProductionUpdate.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/ProductionUpdate.h
@@ -248,7 +248,8 @@ protected:
void addToProductionQueue( ProductionEntry *production ); ///< add to *END* of production queue list
void removeFromProductionQueue( ProductionEntry *production ); ///< remove production from the queue list
- void updateDoors(); ///< update the door behavior
+ void updateDoors( Bool moreUnitsComing ); ///< update the door behavior
+ void beginOpeningDoor( ExitDoorType exitDoor ); ///< start (or hold) a door on its way open
struct DoorInfo
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ProductionUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ProductionUpdate.cpp
index f8d3c342c..ba4d625c0 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ProductionUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ProductionUpdate.cpp
@@ -544,10 +544,22 @@ void ProductionUpdate::cancelAllUnitsOfType( const ThingTemplate *unitType)
} // end cancelAllUnitsOfType
+//-------------------------------------------------------------------------------------------------
+/** A door stays open while the next unit is still on its way out of the same building. Shutting
+ * it and hauling it straight back up for the vehicle behind is wasted motion, and worse, there
+ * is no artwork for a door that changes its mind partway - a door pulled back open mid-close
+ * snaps wide in a single frame. So the queue closes the doors, not the clock. */
+//-------------------------------------------------------------------------------------------------
+Bool Production_shouldCloseDoorNow( UnsignedInt framesWaitingOpen, UnsignedInt doorWaitOpenTime,
+ Bool holdOpen, Bool moreUnitsComing )
+{
+ return framesWaitingOpen > doorWaitOpenTime && !holdOpen && !moreUnitsComing;
+}
+
//-------------------------------------------------------------------------------------------------
/** Update the door behavior */
//-------------------------------------------------------------------------------------------------
-void ProductionUpdate::updateDoors()
+void ProductionUpdate::updateDoors( Bool moreUnitsComing )
{
const ProductionUpdateModuleData *d = getProductionUpdateModuleData();
@@ -582,7 +594,8 @@ void ProductionUpdate::updateDoors()
else if( m_doors[i].m_doorWaitOpenFrame )
{
- if( now - m_doors[i].m_doorWaitOpenFrame > d->m_doorWaitOpenTime && !m_doors[i].m_holdOpen )
+ if( Production_shouldCloseDoorNow( now - m_doors[i].m_doorWaitOpenFrame, d->m_doorWaitOpenTime,
+ m_doors[i].m_holdOpen, moreUnitsComing ) )
{
// set our frame marker for closing
@@ -619,6 +632,60 @@ void ProductionUpdate::updateDoors()
}
}
+//-------------------------------------------------------------------------------------------------
+/** Put a door on its way open, whatever state it is in right now. A closed door starts opening,
+ * an open one is held open, and one that had started to close is popped back to waiting open. */
+//-------------------------------------------------------------------------------------------------
+void ProductionUpdate::beginOpeningDoor( ExitDoorType exitDoor )
+{
+ DoorInfo *door = &m_doors[ exitDoor ];
+ UnsignedInt now = TheGameLogic->getFrame();
+
+ // if the door is closed, open it
+ if( door->m_doorOpenedFrame == 0 && door->m_doorWaitOpenFrame == 0 && door->m_doorClosedFrame == 0 )
+ {
+
+ door->m_doorOpenedFrame = now;
+ m_setFlags.set( theOpeningFlags[exitDoor] );
+ m_flagsDirty = TRUE;
+
+ } // end if
+ // if the door is waiting-open, keep it there
+ else if( door->m_doorWaitOpenFrame != 0 )
+ {
+
+ door->m_doorWaitOpenFrame = now;
+
+ } // end else if
+ // if the door is closing, for now, pop it to waiting open
+ else if( door->m_doorClosedFrame != 0 )
+ {
+
+ door->m_doorWaitOpenFrame = now;
+
+ m_clearFlags.set( theOpeningFlags[exitDoor], true );
+ m_clearFlags.set( theClosingFlags[exitDoor], true );
+ m_setFlags.set( theOpeningFlags[exitDoor], false );
+ m_setFlags.set( theClosingFlags[exitDoor], false );
+
+ m_setFlags.set( theWaitingOpenFlags[exitDoor] );
+ m_flagsDirty = TRUE;
+
+ } // end else
+
+} // end beginOpeningDoor
+
+//-------------------------------------------------------------------------------------------------
+/** The door animation used to start only once the unit was already finished, so every unit paid
+ * the whole opening time on top of its build time. Start the door this far from the end of the
+ * build instead, and it finishes opening on the frame the unit is done. updateDoors() leaves
+ * the opening state one frame *after* the time is up, hence the +1. */
+//-------------------------------------------------------------------------------------------------
+Bool Production_shouldOpenDoorEarly( Int framesRemaining, UnsignedInt doorOpeningTime )
+{
+ return framesRemaining > 0 && framesRemaining <= (Int)doorOpeningTime + 1;
+}
+
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
UpdateSleepTime ProductionUpdate::update( void )
@@ -629,9 +696,10 @@ UpdateSleepTime ProductionUpdate::update( void )
UnsignedInt now = TheGameLogic->getFrame();
Object *us = getObject();
- // update the door behaviors
+ // update the door behaviors. a unit still at the head of the queue is a unit still due out
+ // of a door, so the doors stay open for him instead of closing between the two.
if( d->m_numDoorAnimations > 0 )
- updateDoors();
+ updateDoors( production != NULL && production->getProductionType() == PRODUCTION_UNIT );
//
// if we're in the construction complete state ... we need to check frame
@@ -735,6 +803,44 @@ UpdateSleepTime ProductionUpdate::update( void )
INT_TO_REAL( totalProductionFrames ) *
100.0f;
+ //
+ // get the door moving before the unit is finished, so that the opening animation runs out
+ // during the tail of the build instead of being charged on top of it
+ //
+ if( production->m_type == PRODUCTION_UNIT &&
+ d->m_numDoorAnimations > 0 &&
+ Production_shouldOpenDoorEarly( totalProductionFrames - production->m_framesUnderConstruction,
+ d->m_doorOpeningTime ) )
+ {
+ ExitInterface *exitInterface = us->getObjectExitInterface();
+ if( exitInterface )
+ {
+ ExitDoorType exitDoor = production->getExitDoor();
+ if( exitDoor == DOOR_NONE_AVAILABLE )
+ exitDoor = exitInterface->reserveDoorForExit( production->m_objectToProduce, NULL );
+
+ //
+ // hold the reservation until we build, so the door we opened is the door we come out
+ // of; removeFromProductionQueue releases it again if this entry is cancelled first.
+ // anything else (no door needed, none free) is left for the finished-production path
+ // to ask for again, exactly as it always did.
+ //
+ if( exitDoor >= DOOR_1 && exitDoor < DOOR_COUNT_MAX )
+ {
+ production->setExitDoor( exitDoor );
+
+ //
+ // only a shut door is started here. one already opening needs nothing, and one
+ // still closing is left to finish - there is no animation for reversing a door
+ // partway, so pulling it back open snaps it wide in a single frame.
+ //
+ DoorInfo *door = &m_doors[ exitDoor ];
+ if( door->m_doorOpenedFrame == 0 && door->m_doorWaitOpenFrame == 0 && door->m_doorClosedFrame == 0 )
+ beginOpeningDoor( exitDoor );
+ }
+ }
+ }
+
// if we've reached 100% or more we're done, tada!
if( production->m_percentComplete >= 100.0f )
{
@@ -780,37 +886,7 @@ UpdateSleepTime ProductionUpdate::update( void )
if( d->m_numDoorAnimations > 0 && door != NULL )
{
- // if the door is closed, open it
- if( door->m_doorOpenedFrame == 0 && door->m_doorWaitOpenFrame == 0 && door->m_doorClosedFrame == 0 )
- {
-
- door->m_doorOpenedFrame = now;
- m_setFlags.set( theOpeningFlags[exitDoor] );
- m_flagsDirty = TRUE;
-
- } // end if
- // if the door is waiting-open, keep it there
- else if( door->m_doorWaitOpenFrame != 0 )
- {
-
- door->m_doorWaitOpenFrame = now;
-
- } // end else if
- // if the door is closing, for now, pop it to waiting open
- else if( door->m_doorClosedFrame != 0 )
- {
-
- door->m_doorWaitOpenFrame = now;
-
- m_clearFlags.set( theOpeningFlags[exitDoor], true );
- m_clearFlags.set( theClosingFlags[exitDoor], true );
- m_setFlags.set( theOpeningFlags[exitDoor], false );
- m_setFlags.set( theClosingFlags[exitDoor], false );
-
- m_setFlags.set( theWaitingOpenFlags[exitDoor] );
- m_flagsDirty = TRUE;
-
- } // end else
+ beginOpeningDoor( exitDoor );
} // end if, has door animation
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index c54236f7f..0f4ec98da 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -7508,3 +7508,58 @@ TEST(the_pool_survives_being_started_and_stopped_repeatedly)
CHECK_EQ( 1, s_jobVisits[ i ] );
}
}
+
+/* ProductionUpdate.cpp: a producer with a door animation only started opening it once the unit
+ was already finished, and then held the unit inside until the animation ran out - so every
+ tank cost its build time plus the whole door opening time. The door now starts early enough
+ to finish opening on the frame the unit is done. */
+extern Bool Production_shouldOpenDoorEarly( Int framesRemaining, UnsignedInt doorOpeningTime );
+
+TEST(door_starts_opening_before_the_unit_is_finished)
+{
+ /* a 60 frame door: updateDoors() only leaves the opening state on the frame *after* the time
+ is up, so the door has to start 61 frames out to be open when the unit lands. */
+ CHECK( Production_shouldOpenDoorEarly( 61, 60 ) );
+ CHECK( Production_shouldOpenDoorEarly( 1, 60 ) );
+
+ /* one frame earlier than that and the door would be sitting open, waiting on the build. */
+ CHECK( !Production_shouldOpenDoorEarly( 62, 60 ) );
+ CHECK( !Production_shouldOpenDoorEarly( 600, 60 ) );
+
+ /* the unit is done: the finished-production path owns the door from here, not this one. */
+ CHECK( !Production_shouldOpenDoorEarly( 0, 60 ) );
+ CHECK( !Production_shouldOpenDoorEarly( -5, 60 ) );
+
+ /* a producer whose door opens instantly still gets it on the last frame, and never before. */
+ CHECK( Production_shouldOpenDoorEarly( 1, 0 ) );
+ CHECK( !Production_shouldOpenDoorEarly( 2, 0 ) );
+}
+
+/* ProductionUpdate.cpp: the door ran on a stopwatch - it shut a fixed time after a unit came out,
+ whatever was behind him. A factory working through a queue therefore closed and hauled its
+ doors straight back up for every vehicle, and there is no artwork for a door that changes its
+ mind partway, so a door caught mid-close snapped wide in a single frame. The queue closes the
+ doors now: they stay open while a unit is still due, and shut when the queue runs out. */
+extern Bool Production_shouldCloseDoorNow( UnsignedInt framesWaitingOpen, UnsignedInt doorWaitOpenTime,
+ Bool holdOpen, Bool moreUnitsComing );
+
+TEST(a_door_stays_open_while_another_unit_is_still_coming)
+{
+ /* nothing left to build and the door has stood open its full time: shut it. */
+ CHECK( Production_shouldCloseDoorNow( 31, 30, false, false ) );
+
+ /* another vehicle behind this one - the door stays up however long it has been open. */
+ CHECK( !Production_shouldCloseDoorNow( 31, 30, false, true ) );
+ CHECK( !Production_shouldCloseDoorNow( 100000, 30, false, true ) );
+
+ /* the queue emptying is what closes it, so the same door shuts the moment nothing is due. */
+ CHECK( Production_shouldCloseDoorNow( 100000, 30, false, false ) );
+
+ /* it still owes its open time to the man walking out: an empty queue does not slam it. */
+ CHECK( !Production_shouldCloseDoorNow( 30, 30, false, false ) );
+ CHECK( !Production_shouldCloseDoorNow( 0, 30, false, false ) );
+
+ /* somebody holding the door open by hand still wins over both. */
+ CHECK( !Production_shouldCloseDoorNow( 31, 30, true, false ) );
+ CHECK( !Production_shouldCloseDoorNow( 31, 30, true, true ) );
+}