summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels Martin Hansen <nielsm@indvikleren.dk>2020-01-04 17:31:52 +0100
committerNiels Martin Hansen <nielsm@indvikleren.dk>2020-01-04 19:07:13 +0100
commitf40162214909116507811be4a1f5af9ec9d3ef97 (patch)
tree7e06ca0ceda70bda1c9c3cd2ee091e879e0fdb1d
parentb524f1ae21487caf8ed460d8e20a51c3dd7e11a6 (diff)
downloadopenttd-f40162214909116507811be4a1f5af9ec9d3ef97.tar.xz
Feature: Script API to change town rating of companies
-rw-r--r--src/command.cpp2
-rw-r--r--src/command_type.h1
-rw-r--r--src/script/api/game/game_town.hpp.sq2
-rw-r--r--src/script/api/script_town.cpp27
-rw-r--r--src/script/api/script_town.hpp24
-rw-r--r--src/town_cmd.cpp29
6 files changed, 85 insertions, 0 deletions
diff --git a/src/command.cpp b/src/command.cpp
index dd65fa36e..ed4c457bc 100644
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -130,6 +130,7 @@ CommandProc CmdFoundTown;
CommandProc CmdRenameTown;
CommandProc CmdDoTownAction;
CommandProc CmdTownGrowthRate;
+CommandProc CmdTownRating;
CommandProc CmdTownCargoGoal;
CommandProc CmdTownSetText;
CommandProc CmdExpandTown;
@@ -295,6 +296,7 @@ static const Command _command_proc_table[] = {
DEF_CMD(CmdDoTownAction, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DO_TOWN_ACTION
DEF_CMD(CmdTownCargoGoal, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_CARGO_GOAL
DEF_CMD(CmdTownGrowthRate, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_GROWTH_RATE
+ DEF_CMD(CmdTownRating, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_RATING
DEF_CMD(CmdTownSetText, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_SET_TEXT
DEF_CMD(CmdExpandTown, CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_EXPAND_TOWN
DEF_CMD(CmdDeleteTown, CMD_OFFLINE, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DELETE_TOWN
diff --git a/src/command_type.h b/src/command_type.h
index 41f2360d8..d03639367 100644
--- a/src/command_type.h
+++ b/src/command_type.h
@@ -262,6 +262,7 @@ enum Commands {
CMD_DO_TOWN_ACTION, ///< do a action from the town detail window (like advertises or bribe)
CMD_TOWN_CARGO_GOAL, ///< set the goal of a cargo for a town
CMD_TOWN_GROWTH_RATE, ///< set the town growth rate
+ CMD_TOWN_RATING, ///< set rating of a company in a town
CMD_TOWN_SET_TEXT, ///< set the custom text of a town
CMD_EXPAND_TOWN, ///< expand a town
CMD_DELETE_TOWN, ///< delete a town
diff --git a/src/script/api/game/game_town.hpp.sq b/src/script/api/game/game_town.hpp.sq
index b97cb59d2..5cf394ca1 100644
--- a/src/script/api/game/game_town.hpp.sq
+++ b/src/script/api/game/game_town.hpp.sq
@@ -79,6 +79,8 @@ void SQGSTown_Register(Squirrel *engine)
SQGSTown.DefSQStaticMethod(engine, &ScriptTown::ExpandTown, "ExpandTown", 3, ".ii");
SQGSTown.DefSQStaticMethod(engine, &ScriptTown::FoundTown, "FoundTown", 6, ".iibi.");
SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetRating, "GetRating", 3, ".ii");
+ SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetDetailedRating, "GetDetailedRating", 3, ".ii");
+ SQGSTown.DefSQStaticMethod(engine, &ScriptTown::ChangeRating, "ChangeRating", 4, ".iii");
SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetAllowedNoise, "GetAllowedNoise", 2, ".i");
SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetRoadLayout, "GetRoadLayout", 2, ".i");
diff --git a/src/script/api/script_town.cpp b/src/script/api/script_town.cpp
index 1cb6e32bb..afe561456 100644
--- a/src/script/api/script_town.cpp
+++ b/src/script/api/script_town.cpp
@@ -336,6 +336,33 @@
}
}
+/* static */ int ScriptTown::GetDetailedRating(TownID town_id, ScriptCompany::CompanyID company_id)
+{
+ if (!IsValidTown(town_id)) return TOWN_RATING_INVALID;
+ ScriptCompany::CompanyID company = ScriptCompany::ResolveCompanyID(company_id);
+ if (company == ScriptCompany::COMPANY_INVALID) return TOWN_RATING_INVALID;
+
+ const Town *t = ::Town::Get(town_id);
+ return t->ratings[company];
+}
+
+/* static */ bool ScriptTown::ChangeRating(TownID town_id, ScriptCompany::CompanyID company_id, int delta)
+{
+ EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY);
+ EnforcePrecondition(false, IsValidTown(town_id));
+ ScriptCompany::CompanyID company = ScriptCompany::ResolveCompanyID(company_id);
+ EnforcePrecondition(false, company != ScriptCompany::COMPANY_INVALID);
+
+ const Town *t = ::Town::Get(town_id);
+ int16 new_rating = Clamp(t->ratings[company] + delta, RATING_MINIMUM, RATING_MAXIMUM);
+ if (new_rating == t->ratings[company]) return false;
+
+ uint16 p2 = 0;
+ memcpy(&p2, &new_rating, sizeof(p2));
+
+ return ScriptObject::DoCommand(0, town_id | (company_id << 16), p2, CMD_TOWN_RATING);
+}
+
/* static */ int ScriptTown::GetAllowedNoise(TownID town_id)
{
if (!IsValidTown(town_id)) return -1;
diff --git a/src/script/api/script_town.hpp b/src/script/api/script_town.hpp
index 71fb5a712..6ae7ee33f 100644
--- a/src/script/api/script_town.hpp
+++ b/src/script/api/script_town.hpp
@@ -424,6 +424,30 @@ public:
static TownRating GetRating(TownID town_id, ScriptCompany::CompanyID company_id);
/**
+ * Get the accurate rating of a company within a town.
+ * @param town_id The town to get the rating for.
+ * @param company_id The company to get the rating for.
+ * @pre IsValidTown(town_id).
+ * @pre ScriptCompany.ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID.
+ * @return The rating as a number between -1000 (worst) and 1000 (best).
+ * @api -ai
+ */
+ static int GetDetailedRating(TownID town_id, ScriptCompany::CompanyID company_id);
+
+ /**
+ * Change the rating of a company within a town.
+ * @param town_id The town to change the rating in.
+ * @param company_id The company to change the rating for.
+ * @param delta How much to change rating by (range -1000 to +1000).
+ * @return True if the rating was changed.
+ * @pre IsValidTown(town_id).
+ * @pre ScriptCompany.ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID.
+ * @pre no company mode in scope
+ * @api -ai
+ */
+ static bool ChangeRating(TownID town_id, ScriptCompany::CompanyID company_id, int delta);
+
+ /**
* Get the maximum level of noise that still can be added by airports
* before the town start to refuse building a new airport.
* @param town_id The town to get the allowed noise from.
diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp
index 7b1d5c90f..31e11ae45 100644
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -2805,6 +2805,35 @@ CommandCost CmdTownGrowthRate(TileIndex tile, DoCommandFlag flags, uint32 p1, ui
}
/**
+ * Change the rating of a company in a town
+ * @param tile Unused.
+ * @param flags Type of operation.
+ * @param p1 Bit 0..15 = Town ID to change, bit 16..23 = Company ID to change.
+ * @param p2 Bit 0..15 = New rating of company (signed int16).
+ * @param text Unused.
+ * @return Empty cost or an error.
+ */
+CommandCost CmdTownRating(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
+{
+ if (_current_company != OWNER_DEITY) return CMD_ERROR;
+
+ TownID town_id = (TownID)GB(p1, 0, 16);
+ Town *t = Town::GetIfValid(town_id);
+ if (t == nullptr) return CMD_ERROR;
+
+ CompanyID company_id = (CompanyID)GB(p1, 16, 8);
+ if (!Company::IsValidID(company_id)) return CMD_ERROR;
+
+ int16 new_rating = Clamp((int16)GB(p2, 0, 16), RATING_MINIMUM, RATING_MAXIMUM);
+ if (flags & DC_EXEC) {
+ t->ratings[company_id] = new_rating;
+ InvalidateWindowData(WC_TOWN_AUTHORITY, town_id);
+ }
+
+ return CommandCost();
+}
+
+/**
* Expand a town (scenario editor only).
* @param tile Unused.
* @param flags Type of operation.