Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added DoCargoRoute #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion Source/UnitEx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,17 @@ struct cmdMove // same for patrol, dock, dockEG, cargo route
// ...
};

// todo: cargoroute
struct cmdCargoRoute
{
char numUnits;
short unitId;
short numWayPoints;
long pts[3];
short mineWayPointIndex;
short smelterWayPointIndex;
short mineUnitId;
short smelterUnitId;
};
Comment on lines +68 to +78
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this has a built in assumption of exactly 3 waypoints. More accurate would be to split this struct into two, so that the first struct could accommodate a variable length array at the end.

struct cmdCargoRoute
{
	char numUnits;
	short unitId;
	short numWayPoints;
	long pts[1]; // Variable length array (maybe use max size instead of the typical 1?)
}
struct CargoRouteSpecificData { // Name this better
	short mineWayPointIndex;
	short smelterWayPointIndex;
	short mineUnitId;
	short smelterUnitId;
};

Another somewhat less robust alternative, would be to document the assumptions with default values:

struct cmdCargoRoute
{
	char numUnits;
	short unitId;
	short numWayPoints{3};
	long pts[3];
	short mineWayPointIndex{0};
	short smelterWayPointIndex{1};
	short mineUnitId;
	short smelterUnitId;
};

At any rate, it does meet the requirements for the typical case of a 3 waypoint ore haul route.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how I would do this, in practice. How would I "combine" the two structs into the packet's data buffer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically with casting and pointer arithmetic. I suppose that could potentially be easier if the array wasn't part of the struct.

At any rate, if you're building the struct specifically for that one method, it may be easier to just use a single struct, and perhaps default the values that have fixed assumptions.

Might be cleaner if the scope of the struct could be limited to the function that the assumptions are tied to.


struct cmdBuildWall
{
Expand Down Expand Up @@ -270,6 +280,44 @@ void UnitEx::DoAttack(LOCATION where)
ExtPlayer[OwnerID()].ProcessCommandPacket(&packet);
}

void UnitEx::DoCargoRoute(UnitEx mine, UnitEx smelter)
{
if (!isInited)
return;

if (!IsLive())
return;

CommandPacket packet;
cmdCargoRoute *data = (cmdCargoRoute*)packet.dataBuff;

packet.type = ctMoCargoRoute;
packet.dataLength = sizeof(cmdMove);
data->numUnits = 1;
data->unitId = unitID;
data->mineUnitId = mine.unitID;
data->smelterUnitId = smelter.unitID;
data->numWayPoints = 3;

int pxLoc = 0;
int x = mine.GetDockLocation().x * 32,
y = mine.GetDockLocation().y * 32;
pxLoc = (x & 0x07FFF) | (y & 0x03FFF) << 15;
data->pts[0] = data->pts[2] = pxLoc;

pxLoc = 0;
x = smelter.GetDockLocation().x * 32;
y = smelter.GetDockLocation().y * 32;
pxLoc = (x & 0x07FFF) | (y & 0x03FFF) << 15;
data->pts[1] = pxLoc;

data->mineWayPointIndex = 0;
data->smelterWayPointIndex = 1;


ExtPlayer[OwnerID()].ProcessCommandPacket(&packet);
}

void UnitEx::DoDeployMiner(LOCATION where)
{
if (!isInited) {
Expand Down
1 change: 1 addition & 0 deletions Source/UnitEx.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class UnitEx : public Unit
{
public:
void DoAttack(LOCATION where);
void DoCargoRoute(UnitEx mine, UnitEx smelter);
void DoDeployMiner(LOCATION where);
void DoDoze(MAP_RECT area);
void DoDock(LOCATION dockLocation);
Expand Down