Feature: Script API to change town rating of companies

This commit is contained in:
Niels Martin Hansen
2020-01-04 17:31:52 +01:00
parent b524f1ae21
commit f401622149
6 changed files with 85 additions and 0 deletions

View File

@@ -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");

View File

@@ -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;

View File

@@ -423,6 +423,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.