It would be nice to be able to add or subtract LOCATION structures from each other using a standard operator function like + or -.
For example, you could place structures relative to each other like this:
Unit unit;
TethysGame::CreateUnit(unit, map_id::mapCommandCenter, LOCATION(20 + X_, 15 + Y_), 0, map_id::mapNone, UnitDirection::East);
TethysGame::CreateUnit(unit, map_id::mapAgridome, unit.Location() + LOCATION(7, 0), 0, map_id::mapNone, UnitDirection::East);
TethysGame::CreateUnit(unit, map_id::mapStandardLab, unit.Location() - LOCATION(-7, 0), 0, map_id::mapNone, UnitDirection::East);
I don't think the definition of the structure LOCATION can be modified, so the operator functions have to be declared globally instead of being declared as members of LOCATION. (I think the definition of LOCATION is being pulled directly from Outpost 2 code, but not sure).
I added the following declarations and definitions to a local copy of the SDK and tested it. I'm still new to C++, so please take it with a discerning eye.
OP2Helper.h Addition
LOCATION operator+ (LOCATION &loc1, LOCATION &loc2);
LOCATION operator- (LOCATION &loc1, LOCATION &loc2);
OP2Helper.cpp Addition
/*Global Operator Functions for structure LOCATION.
* This allows LOCATION loc = LOCATION(10, 10) + LOCATION(5, 0)
* Or LOCATION loc = LOCATION(10, 10) - LOCATION(5, 0)*/
LOCATION operator+ (LOCATION &loc1, LOCATION &loc2)
{
LOCATION newLoc;
newLoc.x = loc1.x + loc2.x;
newLoc.y = loc1.y + loc2.y;
return newLoc;
}
LOCATION operator- (LOCATION &loc1, LOCATION &loc2)
{
LOCATION newLoc;
newLoc.x = loc1.x - loc2.x;
newLoc.y = loc1.y - loc2.y;
return newLoc;
}
Attached files with modifications included.
That's a good idea.
I believe you can optimize that slightly. You should be able to change the return type to LOCATION&, and do an in-place construction of the LOCATION object at the return statement. The compiler should optimize that by passing in an address for the return value, and constructing it in-place on the caller's stack frame. That would avoid creating a new LOCATION object in the callee's stack frame, copying the object to the caller's stack frame, and then destructing the object from the callee's stack frame.
The result should look something like this:
LOCATION& operator+ (LOCATION &loc1, LOCATION &loc2)
{
return LOCATION(loc1.x + loc2.x, loc1.y + loc2.y);
}
I believe you are right in that OP2Helper is an appropriate place to add such an extension. I like how you made it a global operator overload rather than mucking about with a class exported from Outpost2.exe.