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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
feat(gameclient): take military box selection and a save fix from PR 7
Olcay Seyganbce291976985
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a7c726437..d4b3a0b6b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -128,6 +128,7 @@ found and fixed — EA's own, not port damage.**- A reservation lapses within half a second if the shot never arrives.- Ctrl+Q now takes infantry only, not every armed thing you own.- A box drawn with Ctrl held takes those units back out of your selection instead of replacing it, so a group is trimmed by dragging over the corner of it rather than picked again from scratch. Held with Alt the box keeps only what can shoot: drag across your base and you get the tanks, not the dozers and supply trucks standing among them. Both are drag-only, and a single click still means exactly what it always did.+- A box dragged over your army and your economy together takes the army. The dozers, workers, supply trucks and hackers standing among the tanks stay where they are, and ambulances and troop carriers go with the soldiers they look after. A box with nobody military in it takes the workers the way it always did. Legacy keeps the old box.- The three buttons each do one thing now. Left selects, and only selects. Right gives orders, and nothing it does moves the camera. The camera is on the middle button, and it scrolls the way the right button used to: the spot you pressed on stays put, and the further you pull away from it the faster the map runs. Hold Ctrl and the same drag turns the view instead. The wheel zooms toward whatever the cursor is over and the screen edges still scroll. The classic scheme, where left both selected and commanded and right scrolled and deselected, is gone rather than switched off, and the two checkboxes that used to choose between them are out of the options screen.- Drag the right button and you draw a line: the selection spreads itself evenly along it, in the order the units already stand, so a column arrives as a firing line instead of a queue. It is the line you actually drew, curves and all, however far you keep drawing it: a long sweep used to give up partway through and finish as one straight run to the cursor, and now it holds its shape to the end. Letting go over the sidebar finishes the order rather than leaving the line stuck to the cursor. Draw the line with the left button while attack move is armed and the whole line is an attack move: artillery spread across a front, each gun firing on what walks into it, instead of one clump on one spot. The left button, because that is what attack move is aimed with; with F armed a right drag draws nothing. Armed with attack instead, every unit fires on its own station along the line, which is a barrage laid across a ridge in one drag. This is what the right button freed up. `FormationDrag = no` in Options.ini turns it off if you would rather a slipped click did nothing.- Selected units draw a thread to wherever they are going, with a marker on the spot. A group sent to one spot draws each unit's thread to the ground that unit will actually stand on, where every line used to run into the same point. Every order draws one: move, attack move, attack, force fire, ground fire, enter, dock, repair, heal, capture, hack, guard, a waypoint path. Green for a move, pink for an attack-move, red for anything that shoots, so a group told three different things reads as three different colours. A move wears a dot and anything that shoots wears a ring, so the shape tells them apart for somebody who cannot tell the red from the green. The marker sits on the middle of the spot and grows with your screen; it used to be a tinted copy of the mouse pointer, hung off to one side of the destination and covering whatever stood beside it. It is not a flash at the moment of the order: the line is asked of the unit every frame, so it lasts until the unit gets there, changes its mind or leaves your selection, and it shows on the formation line while you are still drawing it. A unit that has walked off the edge of the screen still draws its line, which is how you see where the half of your army you cannot see is headed. The animated ring the ground used to sprout wherever you clicked is gone: it flashed for a second and told you nothing the marker sitting on the destination does not, and that marker stays for as long as the order does. A marker that has just appeared slides the last few pixels up out of the bottom right and fades in as it lands, over about an eighth of a second, so a fresh order catches your eye without anything flashing. Aircraft draw their thread from the click, not from the moment they leave the runway. A plane parked on its airfield answers an order by starting its takeoff and puts the order itself aside until the wheels are up, so the taxi and the climb, which is exactly the stretch where you are wondering whether the strike went out at all, used to show nothing.@@ -155,6 +156,7 @@ found and fixed — EA's own, not port damage.**- Loading a save remembers what your guards were guarding.- A turret loaded from a save is still tracking what it was tracking, and a worker loaded mid-job carries on with it, instead of both snapping back to their default state.- A unit that was driving somewhere when you saved is still driving there when you load. One flag was written to the save twice and another not at all, so every loaded unit came back claiming to stand still, and everything that asks - a bike waiting for its rider, a mob following its leader, an ability that breaks if you move - believed it.+- A game saved after losing a supply center loads again. When a truck looked for somewhere to drop its cash and found a center that had been destroyed, it took the center off one list and counted it off the other, so the save recorded the wrong number of supply docks and could not be read back.- Your units decide the same way on both machines whether to chase what they spotted on their own. That answer was read from uninitialised memory, so the same order could send one player's tank after a target and leave the other's standing.- A shell already in the air finishes its flight after a load instead of going off in the launcher's face.- An aircraft carrier remembers the order it was given, and each runway its own ramp.diff --git a/GeneralsMD/Code/GameEngine/Include/Common/ResourceGatheringManager.h b/GeneralsMD/Code/GameEngine/Include/Common/ResourceGatheringManager.hindex 210aae5ae..55b5dcec4 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/ResourceGatheringManager.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/ResourceGatheringManager.h@@ -58,6 +58,8 @@ public:protected:+ Object *findBestSupplyCenterInList(Object *queryObject);+// snapshot methodsvirtual void crc( Xfer *xfer );virtual void xfer( Xfer *xfer );@@ -73,4 +75,4 @@ private:};-#endif\ No newline at end of file+#endifdiff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/SelectionPriority.h b/GeneralsMD/Code/GameEngine/Include/GameClient/SelectionPriority.hnew file mode 100644index 000000000..8e974d141--- /dev/null+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/SelectionPriority.h@@ -0,0 +1,41 @@+/* Command & Conquer Generals Zero Hour - GPL-3.0-or-later */+#ifndef SELECTION_PRIORITY_H+#define SELECTION_PRIORITY_H++#include "Common/KindOf.h"++// Keep military support alongside combat units, but leave the economy behind.+template <typename Unit>+inline Bool isMilitaryBoxSelectionUnit(const Unit *unit)+{+ return unit && !unit->isKindOf(KINDOF_STRUCTURE) &&+ !unit->isKindOf(KINDOF_DOZER) && !unit->isKindOf(KINDOF_HARVESTER) &&+ !unit->isKindOf(KINDOF_MONEY_HACKER) && !unit->isKindOf(KINDOF_CASH_GENERATOR) &&+ (unit->isKindOf(KINDOF_CAN_ATTACK) || unit->isKindOf(KINDOF_INFANTRY) ||+ unit->isKindOf(KINDOF_VEHICLE) || unit->isKindOf(KINDOF_AIRCRAFT));+}++// The predicate includes eligibility: an enemy or unselectable soldier must not+// prevent a box containing only our workers from selecting those workers.+template <typename Selection, typename Predicate>+inline void prioritizeMilitaryBoxSelection(Selection& selection, Predicate isMilitary)+{+ typename Selection::iterator it = selection.begin();+ for (; it != selection.end(); ++it)+ {+ if (isMilitary(*it))+ break;+ }+ if (it == selection.end())+ return;++ for (it = selection.begin(); it != selection.end();)+ {+ if (isMilitary(*it))+ ++it;+ else+ it = selection.erase(it);+ }+}++#endifdiff --git a/GeneralsMD/Code/GameEngine/Source/Common/RTS/ResourceGatheringManager.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RTS/ResourceGatheringManager.cppindex 1eee4168b..c375a2f51 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/RTS/ResourceGatheringManager.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/RTS/ResourceGatheringManager.cpp@@ -199,8 +199,6 @@ Object *ResourceGatheringManager::findBestSupplyWarehouse( Object *queryObject )Object *ResourceGatheringManager::findBestSupplyCenter( Object *queryObject ){- Object *bestCenter = NULL;-if( ( queryObject == NULL ) || ( queryObject->getAI() == NULL ) )return NULL;@@ -224,6 +222,12 @@ Object *ResourceGatheringManager::findBestSupplyCenter( Object *queryObject )}//Otherwise, search for a good one.+ return findBestSupplyCenterInList(queryObject);+}++Object *ResourceGatheringManager::findBestSupplyCenterInList(Object *queryObject)+{+ Object *bestCenter = NULL;Real bestCost = FLT_MAX;objectIDListIterator iterator = m_supplyCenters.begin();@@ -234,7 +238,7 @@ Object *ResourceGatheringManager::findBestSupplyCenter( Object *queryObject )if( currentCenter == NULL ){- iterator = m_supplyWarehouses.erase( iterator );+ iterator = m_supplyCenters.erase( iterator );}else{@@ -288,4 +292,3 @@ void ResourceGatheringManager::loadPostProcess( void ){} // end loadPostProcess-diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cppindex 658bb5f80..54eb0987e 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp@@ -53,6 +53,7 @@#include "GameClient/GameWindowManager.h"#include "GameClient/Keyboard.h"#include "GameClient/SelectionInfo.h"+#include "GameClient/SelectionPriority.h"#include "GameClient/SelectionXlat.h"#include "GameClient/TerrainVisual.h"@@ -214,6 +215,15 @@ Bool CanSelectDrawable( const Drawable *draw, Bool dragSelecting )} // end canSelect+//-----------------------------------------------------------------------------+static Bool isSelectableMilitaryInBox(Drawable *draw)+{+ if (!CanSelectDrawable(draw, TRUE))+ return FALSE;+ const Object *obj = draw->getObject();+ return obj->getContainedBy() == NULL && isMilitaryBoxSelectionUnit(obj);+}+//-----------------------------------------------------------------------------static Bool canSelectWrapper( Drawable *draw, void *userData ){@@ -747,6 +757,12 @@ GameMessageDisposition SelectionTranslator::translateGameMessage(const GameMessa}}+ // A box with an army in it takes the army and leaves the dozers and trucks standing among+ // it; a box of workers alone still takes the workers. Drag-only, like the filters above,+ // and not under Legacy, which is the 2003 box.+ if (!isPoint && !TheGlobalData->isLegacyInput())+ prioritizeMilitaryBoxSelection(drawablesThatWillSelect, isSelectableMilitaryInBox);+SelectionInfo si;if (contextCommandForNewSelection(currentList, &drawablesThatWillSelect, &si, isPoint)){diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 665b78b2e..9b44094d4 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -12048,3 +12048,5 @@ TEST(camera_preferences_default_to_a_finite_map_margin)#include "test_camera_behavior.inc"#include "test_production_input.inc"#include "test_minimap_input.inc"+#include "test_selection_priority.inc"+#include "test_supply_center_save.inc"diff --git a/GeneralsMD/Code/Tests/test_selection_priority.inc b/GeneralsMD/Code/Tests/test_selection_priority.incnew file mode 100644index 000000000..0cfc1a330--- /dev/null+++ b/GeneralsMD/Code/Tests/test_selection_priority.inc@@ -0,0 +1,92 @@+#include "GameClient/SelectionPriority.h"++namespace+{+struct SelectionPriorityUnit+{+ KindOfMaskType kinds;+ Bool eligible;+ SelectionPriorityUnit(KindOfType first, KindOfType second, Bool canSelect = TRUE)+ : kinds(KindOfMaskType::kInit, first, second), eligible(canSelect) {}+ Bool isKindOf(KindOfType kind) const { return kinds.test(kind); }+};++Bool selectionPriorityMilitary(const SelectionPriorityUnit *unit)+{+ return unit && unit->eligible && isMilitaryBoxSelectionUnit(unit);+}+}++TEST(selection_priority_mixed_box_selects_all_military_in_original_order)+{+ SelectionPriorityUnit dozer(KINDOF_VEHICLE, KINDOF_DOZER);+ SelectionPriorityUnit ranger(KINDOF_INFANTRY, KINDOF_CAN_ATTACK);+ SelectionPriorityUnit truck(KINDOF_VEHICLE, KINDOF_HARVESTER);+ SelectionPriorityUnit tank(KINDOF_VEHICLE, KINDOF_CAN_ATTACK);+ SelectionPriorityUnit ambulance(KINDOF_VEHICLE, KINDOF_TRANSPORT);+ std::list<SelectionPriorityUnit *> selection;+ selection.push_back(&dozer);+ selection.push_back(&ranger);+ selection.push_back(&truck);+ selection.push_back(&tank);+ selection.push_back(&ambulance);+ prioritizeMilitaryBoxSelection(selection, selectionPriorityMilitary);+ CHECK(selection.size() == 3);+ CHECK(selection.front() == &ranger);+ selection.pop_front();+ CHECK(selection.front() == &tank);+ CHECK(selection.back() == &ambulance);+}++TEST(selection_priority_economy_only_box_preserves_every_candidate)+{+ SelectionPriorityUnit dozer(KINDOF_VEHICLE, KINDOF_DOZER);+ SelectionPriorityUnit truck(KINDOF_VEHICLE, KINDOF_HARVESTER);+ SelectionPriorityUnit worker(KINDOF_INFANTRY, KINDOF_DOZER);+ SelectionPriorityUnit chinook(KINDOF_AIRCRAFT, KINDOF_HARVESTER);+ SelectionPriorityUnit hacker(KINDOF_INFANTRY, KINDOF_MONEY_HACKER);+ hacker.kinds.set(KINDOF_CAN_ATTACK);+ std::list<SelectionPriorityUnit *> selection;+ selection.push_back(&dozer);+ selection.push_back(&truck);+ selection.push_back(&worker);+ selection.push_back(&chinook);+ selection.push_back(&hacker);+ const std::list<SelectionPriorityUnit *> before = selection;+ prioritizeMilitaryBoxSelection(selection, selectionPriorityMilitary);+ CHECK(selection == before);+}++TEST(selection_priority_ineligible_military_does_not_suppress_workers)+{+ SelectionPriorityUnit worker(KINDOF_INFANTRY, KINDOF_DOZER);+ // Foreign, dead, hidden or contained military fail the caller's eligibility test.+ SelectionPriorityUnit unavailableTank(KINDOF_VEHICLE, KINDOF_CAN_ATTACK, FALSE);+ SelectionPriorityUnit turret(KINDOF_STRUCTURE, KINDOF_CAN_ATTACK);+ std::list<SelectionPriorityUnit *> selection;+ selection.push_back(&worker);+ selection.push_back(&unavailableTank);+ selection.push_back(&turret);+ const std::list<SelectionPriorityUnit *> before = selection;+ prioritizeMilitaryBoxSelection(selection, selectionPriorityMilitary);+ CHECK(selection == before);+ SelectionPriorityUnit ranger(KINDOF_INFANTRY, KINDOF_CAN_ATTACK);+ selection.push_back(&ranger);+ prioritizeMilitaryBoxSelection(selection, selectionPriorityMilitary);+ CHECK(selection.size() == 1);+ CHECK(selection.front() == &ranger);+}++TEST(selection_priority_empty_box_and_military_support)+{+ std::list<SelectionPriorityUnit *> empty;+ prioritizeMilitaryBoxSelection(empty, selectionPriorityMilitary);+ CHECK(empty.empty());+ SelectionPriorityUnit lotus(KINDOF_INFANTRY, KINDOF_CAN_ATTACK);+ SelectionPriorityUnit aircraft(KINDOF_AIRCRAFT, KINDOF_VEHICLE);+ SelectionPriorityUnit cashUnit(KINDOF_INFANTRY, KINDOF_CASH_GENERATOR);+ CHECK(isMilitaryBoxSelectionUnit(&lotus));+ CHECK(isMilitaryBoxSelectionUnit(&aircraft));+ CHECK(!isMilitaryBoxSelectionUnit(&cashUnit));+ CHECK(!isMilitaryBoxSelectionUnit<SelectionPriorityUnit>(NULL));+}diff --git a/GeneralsMD/Code/Tests/test_supply_center_save.inc b/GeneralsMD/Code/Tests/test_supply_center_save.incnew file mode 100644index 000000000..6ffa6b509--- /dev/null+++ b/GeneralsMD/Code/Tests/test_supply_center_save.inc@@ -0,0 +1,88 @@+#include "Common/ResourceGatheringManager.h"+#include "Common/XferLoad.h"+#include "Common/XferSave.h"++namespace+{+class SupplyCenterSaveManager : public ResourceGatheringManager+{+public:+ void transfer(Xfer *xfer) { ResourceGatheringManager::xfer(xfer); }+ Object *search() { return findBestSupplyCenterInList(NULL); }+};++struct SupplyCenterSaveLogic+{+ GameLogic *saved;+ SupplyCenterSaveLogic() : saved(TheGameLogic)+ {+ // GameLogic destruction owns shared globals; keep this empty lookup alive with boot scaffolding.+ static GameLogic *emptyWorld = NEW GameLogic;+ TheGameLogic = emptyWorld;+ }+ ~SupplyCenterSaveLogic() { TheGameLogic = saved; }+};+}++TEST(supply_center_cleanup_keeps_saved_counts_and_lists_consistent)+{+ CHECK(bootOnce());+ SupplyCenterSaveLogic world;+ const char *path = "test_supply_center_save_tmp.bin";+ // Exercise both ordinary warehouses and the empty-list underflow case.+ for (Int warehouseCount = 0; warehouseCount <= 2; warehouseCount += 2)+ {+ std::list<ObjectID> warehouses;+ for (Int i = 0; i < warehouseCount; ++i)+ warehouses.push_back((ObjectID)(11 + i));+ std::list<ObjectID> destroyedCenters;+ destroyedCenters.push_back((ObjectID)91);+ destroyedCenters.push_back((ObjectID)92);+ destroyedCenters.push_back((ObjectID)93);++ XferSave write;+ write.open(AsciiString(path));+ XferVersion version = 1;+ write.xferVersion(&version, 1);+ write.xferSTLObjectIDList(&warehouses);+ write.xferSTLObjectIDList(&destroyedCenters);+ write.close();++ SupplyCenterSaveManager manager;+ XferLoad read;+ read.open(AsciiString(path));+ manager.transfer(&read);+ read.close();+ CHECK(manager.search() == NULL);+ write.open(AsciiString(path));+ manager.transfer(&write);+ write.close();++ // The old erase removed center nodes but changed the warehouse count instead.+ // Inspect the wire counts before attempting a reload so that underflow fails cleanly.+ FILE *fp = fopen(path, "rb");+ CHECK(fp != NULL);+ if (!fp) return;+ UnsignedByte bytes[64] = {};+ const size_t length = fread(bytes, 1, sizeof(bytes), fp);+ fclose(fp);+ const size_t centerHeader = 4 + warehouseCount * sizeof(ObjectID);+ CHECK(length == centerHeader + 3);+ const Bool countsValid = bytes[2] == warehouseCount && bytes[3] == 0 &&+ bytes[centerHeader + 1] == 0 && bytes[centerHeader + 2] == 0;+ CHECK(countsValid);+ if (countsValid)+ {+ std::list<ObjectID> restoredWarehouses;+ std::list<ObjectID> restoredCenters;+ read.open(AsciiString(path));+ read.xferVersion(&version, 1);+ read.xferSTLObjectIDList(&restoredWarehouses);+ read.xferSTLObjectIDList(&restoredCenters);+ read.close();+ CHECK(restoredWarehouses == warehouses);+ CHECK(restoredCenters.empty());+ }+ remove(path);+ }+}