Skip to content

Commit

Permalink
Bug with moving too early to building with BUILD command (#43)
Browse files Browse the repository at this point in the history
* Build orders move too early
* New buildings phase
  • Loading branch information
artyomtrityak authored Oct 5, 2019
1 parent a4b0afb commit 027cf83
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
1 change: 1 addition & 0 deletions game.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ class Game
void RunUnitProduce(ARegion *, Unit *);
void Run1BuildOrder(ARegion *, Object *, Unit *);
void RunBuildShipOrder(ARegion *, Object *, Unit *);
void AddNewBuildings(ARegion *);
void RunBuildHelpers(ARegion *);
int ShipConstruction(ARegion *, Unit *, Unit *, int, int, int);
void CreateShip(ARegion *, Unit *, int);
Expand Down
45 changes: 45 additions & 0 deletions monthorders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,50 @@ void Game::RunBuildShipOrder(ARegion * r,Object * obj,Unit * u)
u->monthorders = 0;
}

void Game::AddNewBuildings(ARegion *r)
{
int i;
forlist((&r->objects)) {
Object *obj = (Object *) elem;
forlist ((&obj->units)) {
Unit *u = (Unit *) elem;
if (u->monthorders) {
if (u->monthorders->type == O_BUILD) {
BuildOrder *o = (BuildOrder *)u->monthorders;

// If BUILD order was marked for creating new building
// in parse phase, it is time to create one now.
if (o->new_building != -1) {
for (i = 1; i < 100; i++) {
if (!r->GetObject(i)) {
break;
}
}
if (i < 100) {
Object * obj = new Object(r);
obj->type = o->new_building;
obj->incomplete = ObjectDefs[obj->type].cost;
obj->num = i;
obj->SetName(new AString("Building"));
u->build = obj->num;
r->objects.Add(obj);

// This moves unit to a new building.
// This unit might be processed again but from new object.
u->MoveUnit(obj);
// This why we need to unset new_building so it will not
// try to create new object again.
o->new_building = -1;
} else {
u->Error("BUILD: The region is full.");
}
}
}
}
}
}
}

void Game::RunBuildHelpers(ARegion *r)
{
forlist((&r->objects)) {
Expand Down Expand Up @@ -996,6 +1040,7 @@ void Game::RunMonthOrders()
ARegion * r = (ARegion *) elem;
RunIdleOrders(r);
RunStudyOrders(r);
AddNewBuildings(r);
RunBuildHelpers(r);
RunProduceOrders(r);
}
Expand Down
1 change: 1 addition & 0 deletions orders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ AttackOrder::~AttackOrder()
BuildOrder::BuildOrder()
{
type = O_BUILD;
new_building = -1;
}

BuildOrder::~BuildOrder()
Expand Down
1 change: 1 addition & 0 deletions orders.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ class BuildOrder : public Order {
~BuildOrder();

UnitId * target;
int new_building;
int needtocomplete;
};

Expand Down
19 changes: 2 additions & 17 deletions parseorders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@ void Game::ProcessBuildOrder(Unit *unit, AString *o, OrdersCheck *pCheck)
{
AString * token = o->gettoken();
BuildOrder * order = new BuildOrder;
int maxbuild, i;
int maxbuild;

// 'incomplete' for ships:
maxbuild = 0;
Expand Down Expand Up @@ -1615,22 +1615,7 @@ void Game::ProcessBuildOrder(Unit *unit, AString *o, OrdersCheck *pCheck)
ParseError(pCheck, unit, 0, "BUILD: Can't build that.");
return;
}
for (i = 1; i < 100; i++)
if (!reg->GetObject(i))
break;
if (i < 100) {
Object * obj = new Object(reg);
obj->type = ot;
obj->incomplete = ObjectDefs[obj->type].cost;
obj->num = i;
obj->SetName(new AString("Building"));
unit->build = obj->num;
unit->object->region->objects.Add(obj);
unit->MoveUnit(obj);
} else {
unit->Error("BUILD: The region is full.");
return;
}
order->new_building = ot;
}
}
order->target = NULL; // Not helping anyone...
Expand Down

0 comments on commit 027cf83

Please sign in to comment.