GS: Charge 5000 opcodes to call ScriptTown::FoundTown

This call is very expensive and calling it in a loop when it fails
can consume all available CPU time

Add a further 50000 charge if a town name cannot be generated
This commit is contained in:
Jonathan G Rennison
2020-04-15 17:04:32 +01:00
parent ab99c16601
commit 3f68c73b09
3 changed files with 14 additions and 0 deletions

View File

@@ -95,6 +95,11 @@ ScriptController::~ScriptController()
return ScriptObject::GetActiveInstance()->GetOpsTillSuspend();
}
/* static */ void ScriptController::DecreaseOps(int amount)
{
Squirrel::DecreaseOps(ScriptObject::GetActiveInstance()->engine->GetVM(), amount);
}
/* static */ int ScriptController::GetSetting(const char *name)
{
return ScriptObject::GetActiveInstance()->GetSetting(name);

View File

@@ -126,6 +126,11 @@ public:
*/
static int GetOpsTillSuspend();
/**
* Decrease the number of operations the script can execute before being suspended.
*/
static void DecreaseOps(int amount);
/**
* Get the value of one of your settings you set via info.nut.
* @param name The name of the setting.

View File

@@ -11,6 +11,7 @@
#include "script_town.hpp"
#include "script_map.hpp"
#include "script_error.hpp"
#include "script_controller.hpp"
#include "../../town.h"
#include "../../townname_func.h"
#include "../../string_func.h"
@@ -280,6 +281,8 @@
/* static */ bool ScriptTown::FoundTown(TileIndex tile, TownSize size, bool city, RoadLayout layout, Text *name)
{
ScriptController::DecreaseOps(5000);
CCountedPtr<Text> counter(name);
EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY || _settings_game.economy.found_town != TF_FORBIDDEN);
@@ -301,6 +304,7 @@
}
uint32 townnameparts;
if (!GenerateTownName(&townnameparts)) {
ScriptController::DecreaseOps(50000);
ScriptObject::SetLastError(ScriptError::ERR_NAME_IS_NOT_UNIQUE);
return false;
}