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(gameengine): place a group move by a flood from the clicked cell
Olcay Seyganb7fc76d598ca
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e3b229599..063ab5c9a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -107,7 +107,7 @@ found and fixed — EA's own, not port damage.**- A unit turns onto whatever is actually shooting it, then resumes the advance.- Otherwise it picks the worst thing in range: anything armed before anything that is not, then the more dangerous of the two, and worth fades with distance so it is never the far one it walks the field for. A group ordered through a base no longer stops for the first dozer it passes while the artillery beside it keeps firing.- Ctrl on the attack-move click no longer shells the ground instead.-- A big selection arrives as a crowd. Two hundred units told to attack-move used to spread along a line and three hundred told to move mostly stood still: the group tried to hold its shape all the way in. Past forty units everyone now heads for the spot you clicked and takes the nearest free ground to it. Below forty the group keeps its spacing on the way in, because that is what stops a dozen tanks arriving in single file. A waypoint queued with alt is the exception and now keeps every unit's own spot: only the last point of a queued route gets moved to free ground, so sending a dozen tanks through the same intermediate cell left eleven of them unable to reach it, and the group walked to the first waypoint and stopped there.+- A big selection arrives as a crowd. Two hundred units told to attack-move used to spread along a line and three hundred told to move mostly stood still: the group tried to hold its shape all the way in. Past forty units everyone now heads for the spot you clicked and takes the nearest free ground to it. That ground is handed out from the spot outward, ring by ring, and the edge of a cliff ends it: click on the lip of a ridge and the group fills the top, where it used to send half its tanks down to the foot and back round by the ramp to stand there. Whoever gets there first takes the middle and the slow ones take the edge, and the pile grows back toward the way the group came, so the last tank onto a bridge stops behind the others instead of driving through all of them to the far end. Below forty the group keeps its spacing on the way in, because that is what stops a dozen tanks arriving in single file. A waypoint queued with alt is the exception and now keeps every unit's own spot: only the last point of a queued route gets moved to free ground, so sending a dozen tanks through the same intermediate cell left eleven of them unable to reach it, and the group walked to the first waypoint and stopped there.- Troop Crawlers wait for their squad to climb back in, up to ten seconds.- One man who cannot get back aboard no longer parks the Crawler for the rest of the attack move.- Their squad stays out until the area is clear, instead of piling back in after every kill.@@ -127,7 +127,7 @@ found and fixed — EA's own, not port damage.**- 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.- 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, and the marker on the spot is the game's own cursor. Every order that has a cursor gets that cursor: 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. 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.+- Selected units draw a thread to wherever they are going, and the marker on the spot is the game's own cursor. 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 that has a cursor gets that cursor: 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. 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.- A unit standing guard draws its marker on the ground it is guarding. A guard order clears the unit's goal on the way in, so every guard marker used to sit in the bottom left corner of the map with a thread running the whole way across it, whatever the unit was actually watching.- A is attack and F is attack move, each arming the next order click. A click on the ground with A held down is a shot at that ground, on your own tank a shot at your own tank, and the mode drops again the moment the order goes out, so nothing stays armed behind your back. The click keeps your group, too: an attack click that was not a drag used to run the same deselect an ordinary left click runs, so the order arrived a moment after the units it was meant for had been dropped. Both orders are aimed with the left button, the way a special power is, and a right click puts the key down again without giving any order. Holding ctrl no longer forces fire: A is the one way to shoot the ground, and ctrl on a move is only the shared pace. The two keys turn each other off, and the letters they took were sitting on grid commands that moved.- G is guard, armed the same way and aimed with the same left button. A left click posts the selection on that spot: they hold it, shoot what walks into range and do not chase it off the post you put them on. The cursor turns into the targeting cross while G is armed, so you can see the key is down before you click, and a right click puts it down again. Drag the left button with G armed (the right one draws nothing while G is down) and they post along the line you draw, one unit per station, which is how a hill or a bridge approach gets covered in one gesture instead of a click per tank. Guard has its own colour, blue, on the line while you draw it and on the thread and marker afterwards, so a group told to hold ground reads apart from one told to walk there. Hold position, which posts every unit on the spot it is already standing on, has moved to H.diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.hindex 47ad7f4a5..ab430d975 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h@@ -1045,9 +1045,13 @@ public:void cleanOpenAndClosedLists(void);// Adjusts the destination to a spot near dest that is not occupied by other units.- Bool adjustDestination(Object *obj, const LocomotorSet& locomotorSet,+ Bool adjustDestination(Object *obj, const LocomotorSet& locomotorSet,Coord3D *dest, const Coord3D *groupDest=NULL);+ // Gives each ground member its own reserved goal, spreading outward from dest with the soonest+ // arrivals nearest it. goals[k] belongs to members[k]; a member never placed keeps dest itself.+ void floodGroupGoals(const Coord3D *dest, const std::vector<Object *>& members, std::vector<Coord3D>& goals);+// Adjusts the destination to a spot near dest for landing that is not occupied by other units.Bool adjustToLandingDestination(Object *obj, Coord3D *dest);diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cppindex e927b047d..5ee56215b 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp@@ -83,6 +83,7 @@#include "GameNetwork/NetworkInterface.h"#include "GameLogic/AIGuard.h"+#include "GameLogic/AIPathfind.h"#include "GameLogic/AIStateMachine.h"#include "GameLogic/Module/AIUpdate.h"#include "GameLogic/Module/JetAIUpdate.h"@@ -3224,10 +3225,16 @@ void InGameUI::updateOrderHints( void )}// a goal object outranks the goal position: a unit chasing something is headed wherever that- // thing is standing now, not where it stood when the order was given+ // thing is standing now, not where it stood when the order was given. Without one the end of+ // the unit's own path is where it is really going: a group sent to one spot is spread over the+ // free cells round it, and the order's point is the same for every member. A unit still+ // waiting for its path has only the order's point to showObject *goalObj = ai->getGoalObject();+ Path *path = ai->getPath();if( goalObj )hint.to = *goalObj->getPosition();+ else if( path )+ hint.to = *path->getLastNode()->getPosition();elsehint.to = *ai->getGoalPosition();}diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cppindex c64adf2f2..eb74d2c3c 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp@@ -1738,10 +1738,10 @@ void AIGroup::groupMoveToPosition( const Coord3D *p_posIn, Bool addWaypoint, Com// lands strung out on an arc): two hundred units asked to attack-move drew a line instead of// arriving, and with three hundred most of them never got a workable goal cell at all.//- // So the point itself is the goal for every member, and each unit's own move state takes the- // nearest free cell to it (Pathfinder::adjustDestination spirals out from there, reserving as- // it goes, and the orders are issued nearest-first). They pile onto the spot instead of- // holding a shape. Explicit formations - the ones the player asked for by name - still keep+ // So the point itself is the goal for every member. Pathfinder::floodGroupGoals hands out the+ // free cells round it, the middle to whoever gets there first and the edge to the slow, and stops+ // at a cliff; a member it cannot place falls back to its move state's own spiral. They pile onto+ // the spot instead of holding a shape. Explicit formations - the ones the player asked for by name - still keep// theirs, and the AI still moves its teams the old way.//// A queued waypoint (alt-click) is the exception, and it has to be: the whole thing rests on the@@ -2011,6 +2011,23 @@ void AIGroup::groupMoveToPosition( const Coord3D *p_posIn, Bool addWaypoint, Com}}+ /* A player's gather takes every ground member's goal from one flood out of the clicked cell, so a+ click on the lip of a cliff keeps the group on top of it (Pathfinder::floodGroupGoals). The+ members go in iterator order, which is the order the loop below walks, so it reads them back+ with a cursor. */+ std::vector<Object *> floodMembers;+ std::vector<Coord3D> floodGoals;+ if (gatherOnPoint)+ {+ for (Object *o = iter->first(); o; o = iter->next())+ {+ if (o->getAIUpdateInterface()->isDoingGroundMovement())+ floodMembers.push_back( o );+ }+ TheAI->pathfinder()->floodGroupGoals( &goalPos, floodMembers, floodGoals );+ }+ Int floodCursor = 0;+// Works better if you let the near units get the first paths... jba.// Move the ones nearest the goal first. Reduces collision problems later.Object *theUnit;@@ -2032,7 +2049,11 @@ void AIGroup::groupMoveToPosition( const Coord3D *p_posIn, Bool addWaypoint, ComfirstUnit = false;}if (gatherOnPoint)- dest = goalPos; // the clicked spot; the move state finds the nearest free cell to it+ {+ dest = goalPos; // airborne members keep the clicked spot and adjust it in their move state+ if (floodCursor < (Int)floodMembers.size() && floodMembers[floodCursor] == theUnit)+ dest = floodGoals[floodCursor++];+ }elsecomputeIndividualDestination( &dest, &goalPos, theUnit, ¢er, isFormation );diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cppindex 4ac402260..f6eda8e78 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp@@ -6844,6 +6844,180 @@ Bool Pathfinder::adjustDestination(Object *obj, const LocomotorSet& locomotorSetreturn false;}+/** A member of a flooded group: how soon it gets somewhere, and which way it is coming from. */+struct FloodMember+{+ Object* obj;+ Int index; ///< its place in the members handed to floodGroupGoals+ Real speed;+ Real arrival; ///< seconds to the click, or to the cell being handed out+ Real approachX; ///< unit vector from the click toward the member+ Real approachY;+ Real approachLength;+};++/** Soonest first. The member's place in the group breaks a tie, so every machine in a network game+ queues two members at the same distance in the same order. */+static Bool floodMemberArrivesFirst( const FloodMember& a, const FloodMember& b )+{+ if (a.arrival != b.arrival)+ return a.arrival < b.arrival;+ return a.index < b.index;+}++/**+ * Hand a whole group its goals from one flood out of the clicked cell.+ *+ * The spiral above runs once per unit and accepts any free cell a path reaches, so a click on the lip+ * of a cliff put half a group at its foot and sent it round by the ramp. Here the clicked cell opens+ * its eight neighbours, the cells opened hand themselves out and open theirs, until nobody is left+ * without a goal. Only ground a unit could stand on opens anything: a cliff, water or a structure ends+ * the flood where it is, so the group stays on the side that was clicked. The clicked cell always+ * opens its neighbours, which is why a click on the face of a cliff reaches both sides.+ *+ * Members queue by how soon they reach the click, straight distance over speed, and each cell goes to+ * whichever of the first few in that queue would reach the cell itself soonest. The click and the+ * cells round it therefore fill with the first to arrive and the slow ones take the edge.+ *+ * A member is never handed a cell on the far side of the click from where it is coming. That cell is+ * ground it would reach only by driving through everybody who got there first, and on a bridge it was+ * the last tank of a column forcing its way down the whole column to stand at the other end. A group+ * arriving from one side piles up on that side of the click; one arriving from all round fills all+ * round it.+ *+ * Every goal is reserved as it is handed out, so the move state's own adjustDestination finds the cell+ * already its own and keeps it. A member still without a goal when the flood runs dry keeps the click+ * and gets the spiral there, as before.+ */+void Pathfinder::floodGroupGoals(const Coord3D *dest, const std::vector<Object *>& members, std::vector<Coord3D>& goals)+{+ PathProfile pfProfile( PF_ADJUST );+ enum { MIN_FLOOD_CELLS = 400, FLOOD_CELLS_PER_MEMBER = 32 };+ // ponytail: a cell is offered to the few soonest members only, so a mixed group whose soonest+ // members all fail on one cell leaves it empty; offer it to everyone if that shows up as gaps.+ enum { MAX_MEMBERS_TRIED_PER_CELL = 8 };+ const Real PAST_CLICK_ALLOWANCE = PATHFIND_CELL_SIZE_F; // how far past the click a member may still stop+ const Real MIN_MEMBER_SPEED = 1.0f; // a member with no speed yet still queues, behind everybody++ const Int memberCount = (Int)members.size();+ goals.assign( memberCount, *dest );+ if (memberCount == 0)+ return; // a selection with nothing on the ground, all aircraft++ const PathfindLayerEnum layer = TheTerrainLogic->getLayerForDestination( dest );+ ICoord2D start;+ worldToCell( dest, &start );+ if (getCell( layer, start.x, start.y ) == NULL)+ return;++ const Player *owner = members[0]->getControllingPlayer();+ const Bool isHuman = !(owner && owner->getPlayerType() == PLAYER_COMPUTER);++ std::vector<FloodMember> arrivals( memberCount );+ for (Int k = 0; k < memberCount; k++)+ {+ FloodMember& arrival = arrivals[k];+ arrival.obj = members[k];+ arrival.index = k;+ arrival.speed = MAX( members[k]->getAIUpdateInterface()->getCurLocomotorSpeed(), MIN_MEMBER_SPEED );+ arrival.approachX = members[k]->getPosition()->x - dest->x;+ arrival.approachY = members[k]->getPosition()->y - dest->y;+ arrival.approachLength = sqrt( arrival.approachX*arrival.approachX + arrival.approachY*arrival.approachY );+ arrival.arrival = arrival.approachLength / arrival.speed;+ if (arrival.approachLength > PAST_CLICK_ALLOWANCE)+ {+ arrival.approachX /= arrival.approachLength;+ arrival.approachY /= arrival.approachLength;+ }+ }+ std::sort( arrivals.begin(), arrivals.end(), floodMemberArrivesFirst );++ const Int extentWidth = m_extent.hi.x - m_extent.lo.x + 1;+ const Int extentHeight = m_extent.hi.y - m_extent.lo.y + 1;+ std::vector<bool> isOpened( extentWidth * extentHeight, false );+ std::vector<bool> isPlaced( memberCount, false );+ std::vector<FloodMember> candidates;+ std::vector<ICoord2D> wave;+ wave.push_back( start );+ isOpened[ (start.x - m_extent.lo.x) + (start.y - m_extent.lo.y) * extentWidth ] = true;++ Int unplacedCount = memberCount;+ const Int cellLimit = MIN_FLOOD_CELLS + FLOOD_CELLS_PER_MEMBER * memberCount;++ for (Int head = 0; head < (Int)wave.size() && head < cellLimit && unplacedCount > 0; head++)+ {+ const ICoord2D cell = wave[head];+ const PathfindCell::CellType type = getCell( layer, cell.x, cell.y )->getType();+ const Bool isStandable = type == PathfindCell::CELL_CLEAR || type == PathfindCell::CELL_RUBBLE;++ if (isStandable)+ {+ const Real cellX = ((Real)cell.x + 0.5f) * PATHFIND_CELL_SIZE_F;+ const Real cellY = ((Real)cell.y + 0.5f) * PATHFIND_CELL_SIZE_F;+ const Real offsetX = cellX - dest->x;+ const Real offsetY = cellY - dest->y;++ candidates.clear();+ for (Int a = 0; a < memberCount && (Int)candidates.size() < MAX_MEMBERS_TRIED_PER_CELL; a++)+ {+ if (isPlaced[arrivals[a].index])+ continue;+ FloodMember candidate = arrivals[a];+ if (candidate.approachLength > PAST_CLICK_ALLOWANCE)+ {+ const Real depthPastClick = -(offsetX*candidate.approachX + offsetY*candidate.approachY);+ if (depthPastClick > PAST_CLICK_ALLOWANCE)+ continue;+ }+ const Real dx = candidate.obj->getPosition()->x - cellX;+ const Real dy = candidate.obj->getPosition()->y - cellY;+ candidate.arrival = sqrt( dx*dx + dy*dy ) / candidate.speed;+ candidates.push_back( candidate );+ }+ std::sort( candidates.begin(), candidates.end(), floodMemberArrivesFirst );++ for (size_t c = 0; c < candidates.size(); c++)+ {+ Object *member = candidates[c].obj;+ Int iRadius;+ Bool center;+ getRadiusAndCenter( member, iRadius, center );+ Coord3D goal = *dest;+ if (!checkForAdjust( member, member->getAIUpdateInterface()->getLocomotorSet(), isHuman,+ cell.x, cell.y, layer, iRadius, center, &goal, NULL ))+ continue;++ if (memberCount == 1 && head == 0)+ goal = *dest; // a lone unit stands where it was clicked, as adjustDestination lets it+ updateGoal( member, &goal, layer );+ goals[candidates[c].index] = goal;+ isPlaced[candidates[c].index] = true;+ unplacedCount--;+ break;+ }+ }++ if (!isStandable && head > 0)+ continue;+ for (Int dy = -1; dy <= 1; dy++)+ {+ for (Int dx = -1; dx <= 1; dx++)+ {+ ICoord2D next;+ next.x = cell.x + dx;+ next.y = cell.y + dy;+ if (getCell( layer, next.x, next.y ) == NULL)+ continue;+ const Int openedIndex = (next.x - m_extent.lo.x) + (next.y - m_extent.lo.y) * extentWidth;+ if (isOpened[openedIndex])+ continue;+ isOpened[openedIndex] = true;+ wave.push_back( next );+ }+ }+ }+}+Bool Pathfinder::checkForTarget(const Object *obj, Int cellX, Int cellY, const Weapon *weapon,const Object *victim, const Coord3D *victimPos,Int iRadius, Bool center,Coord3D *dest)