(svn r23620) -Add: ScriptTown::SetCargoGoal and ScriptTown::SetGrowthRate (GameScript only)

This commit is contained in:
truebrain
2011-12-19 20:59:19 +00:00
parent c7c1deaf41
commit afcf07ac8a
9 changed files with 124 additions and 4 deletions

View File

@@ -762,7 +762,7 @@ static void TownTickHandler(Town *t)
int i = t->grow_counter - 1;
if (i < 0) {
if (GrowTown(t)) {
i = t->growth_rate;
i = t->growth_rate & (~TOWN_GROW_RATE_CUSTOM);
} else {
i = 0;
}
@@ -2434,6 +2434,70 @@ const CargoSpec *FindFirstCargoWithTownEffect(TownEffect effect)
return NULL;
}
static void UpdateTownGrowRate(Town *t);
/**
* Change the cargo goal of a town.
* @param tile Unused.
* @param flags Type of operation.
* @param p1 various bitstuffed elements
* - p1 = (bit 0 - 15) - Town ID to cargo game of.
* - p1 = (bit 16 - 23) - TownEffect to change the game of.
* @param p2 The new goal value.
* @param text Unused.
* @return Empty cost or an error.
*/
CommandCost CmdTownCargoGoal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
if (_current_company != OWNER_DEITY) return CMD_ERROR;
TownEffect te = (TownEffect)GB(p1, 16, 8);
if (te < TE_BEGIN || te > TE_END) return CMD_ERROR;
uint16 index = GB(p1, 0, 16);
Town *t = Town::GetIfValid(index);
if (t == NULL) return CMD_ERROR;
/* Validate if there is a cargo which is the requested TownEffect */
const CargoSpec *cargo = FindFirstCargoWithTownEffect(te);
if (cargo == NULL) return CMD_ERROR;
if (flags & DC_EXEC) {
t->goal[te] = p2;
UpdateTownGrowRate(t);
InvalidateWindowData(WC_TOWN_VIEW, index);
}
return CommandCost();
}
/**
* Change the growth rate of the town.
* @param tile Unused.
* @param flags Type of operation.
* @param p1 Town ID to cargo game of.
* @param p2 Amount of days between growth.
* @param text Unused.
* @return Empty cost or an error.
*/
CommandCost CmdTownGrowthRate(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
if (_current_company != OWNER_DEITY) return CMD_ERROR;
if ((p2 & TOWN_GROW_RATE_CUSTOM) != 0) return CMD_ERROR;
if (GB(p2, 16, 16) != 0) return CMD_ERROR;
Town *t = Town::GetIfValid(p1);
if (t == NULL) return CMD_ERROR;
if (flags & DC_EXEC) {
t->growth_rate = (p2 == 0) ? 0 : p2 | TOWN_GROW_RATE_CUSTOM;
UpdateTownGrowRate(t);
InvalidateWindowData(WC_TOWN_VIEW, p1);
}
return CommandCost();
}
/**
* Expand a town (scenario editor only).
* @param tile Unused.
@@ -2883,6 +2947,12 @@ static void UpdateTownGrowRate(Town *t)
}
}
if ((t->growth_rate & TOWN_GROW_RATE_CUSTOM) != 0) {
SetBit(t->flags, TOWN_IS_FUNDED);
SetWindowDirty(WC_TOWN_VIEW, t->index);
return;
}
/**
* Towns are processed every TOWN_GROWTH_TICKS ticks, and this is the
* number of times towns are processed before a new building is built.
@@ -2907,7 +2977,6 @@ static void UpdateTownGrowRate(Town *t)
if (t->fund_buildings_months != 0) {
m = _grow_count_values[0][min(n, 5)];
t->fund_buildings_months--;
} else {
m = _grow_count_values[1][min(n, 5)];
if (n == 0 && !Chance16(1, 12)) return;
@@ -2933,6 +3002,7 @@ static void UpdateTownAmounts(Town *t)
{
for (CargoID i = 0; i < NUM_CARGO; i++) t->supplied[i].NewMonth();
for (int i = TE_BEGIN; i < TE_END; i++) t->received[i].NewMonth();
if (t->fund_buildings_months != 0) t->fund_buildings_months--;
SetWindowDirty(WC_TOWN_VIEW, t->index);
}