mirror of https://github.com/OpenTTD/OpenTTD
Compare commits
3 Commits
ce01615521
...
820e67d5b0
Author | SHA1 | Date |
---|---|---|
|
820e67d5b0 | |
|
7bb4940ebd | |
|
f9aef24325 |
|
@ -83,7 +83,7 @@ void AIInstance::Died()
|
||||||
|
|
||||||
void AIInstance::LoadDummyScript()
|
void AIInstance::LoadDummyScript()
|
||||||
{
|
{
|
||||||
ScriptAllocatorScope alloc_scope(this->engine);
|
ScriptAllocatorScope alloc_scope(this->engine.get());
|
||||||
Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
|
Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1024,7 +1024,8 @@ static void MakeLake(TileIndex tile, uint height_lake)
|
||||||
for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
|
for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
|
||||||
TileIndex t = tile + TileOffsByDiagDir(d);
|
TileIndex t = tile + TileOffsByDiagDir(d);
|
||||||
if (IsWaterTile(t)) {
|
if (IsWaterTile(t)) {
|
||||||
MakeRiverAndModifyDesertZoneAround(tile);
|
MakeRiver(tile, Random());
|
||||||
|
MarkTileDirtyByTile(tile);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1154,7 +1155,8 @@ static void RiverMakeWider(TileIndex tile, TileIndex origin_tile)
|
||||||
/* If the tile upstream isn't flat, don't bother. */
|
/* If the tile upstream isn't flat, don't bother. */
|
||||||
if (GetTileSlope(downstream_tile) != SLOPE_FLAT) return;
|
if (GetTileSlope(downstream_tile) != SLOPE_FLAT) return;
|
||||||
|
|
||||||
MakeRiverAndModifyDesertZoneAround(downstream_tile);
|
MakeRiver(downstream_tile, Random());
|
||||||
|
MarkTileDirtyByTile(downstream_tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If upstream is dry and flat, try making it a river tile. */
|
/* If upstream is dry and flat, try making it a river tile. */
|
||||||
|
@ -1162,13 +1164,15 @@ static void RiverMakeWider(TileIndex tile, TileIndex origin_tile)
|
||||||
/* If the tile upstream isn't flat, don't bother. */
|
/* If the tile upstream isn't flat, don't bother. */
|
||||||
if (GetTileSlope(upstream_tile) != SLOPE_FLAT) return;
|
if (GetTileSlope(upstream_tile) != SLOPE_FLAT) return;
|
||||||
|
|
||||||
MakeRiverAndModifyDesertZoneAround(upstream_tile);
|
MakeRiver(upstream_tile, Random());
|
||||||
|
MarkTileDirtyByTile(upstream_tile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If the tile slope matches the desired slope, add a river tile. */
|
/* If the tile slope matches the desired slope, add a river tile. */
|
||||||
if (cur_slope == desired_slope) {
|
if (cur_slope == desired_slope) {
|
||||||
MakeRiverAndModifyDesertZoneAround(tile);
|
MakeRiver(tile, Random());
|
||||||
|
MarkTileDirtyByTile(tile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1238,7 +1242,8 @@ protected:
|
||||||
for (PathNode *path = current.parent; path != nullptr; path = path->parent) {
|
for (PathNode *path = current.parent; path != nullptr; path = path->parent) {
|
||||||
TileIndex tile = path->GetTile();
|
TileIndex tile = path->GetTile();
|
||||||
if (!IsWaterTile(tile)) {
|
if (!IsWaterTile(tile)) {
|
||||||
MakeRiverAndModifyDesertZoneAround(tile);
|
MakeRiver(tile, Random());
|
||||||
|
MarkTileDirtyByTile(tile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1356,7 +1361,8 @@ static std::tuple<bool, bool> FlowRiver(TileIndex spring, TileIndex begin, uint
|
||||||
/* We only want a lake if the river is long enough. */
|
/* We only want a lake if the river is long enough. */
|
||||||
DistanceManhattan(spring, lake_centre) > min_river_length) {
|
DistanceManhattan(spring, lake_centre) > min_river_length) {
|
||||||
end = lake_centre;
|
end = lake_centre;
|
||||||
MakeRiverAndModifyDesertZoneAround(lake_centre);
|
MakeRiver(lake_centre, Random());
|
||||||
|
MarkTileDirtyByTile(lake_centre);
|
||||||
uint diameter = RandomRange(8) + 3;
|
uint diameter = RandomRange(8) + 3;
|
||||||
|
|
||||||
/* Run the loop twice, so artefacts from going circular in one direction get (mostly) hidden. */
|
/* Run the loop twice, so artefacts from going circular in one direction get (mostly) hidden. */
|
||||||
|
@ -1420,6 +1426,25 @@ static void CreateRivers()
|
||||||
/* Widening rivers may have left some tiles requiring to be watered. */
|
/* Widening rivers may have left some tiles requiring to be watered. */
|
||||||
ConvertGroundTilesIntoWaterTiles();
|
ConvertGroundTilesIntoWaterTiles();
|
||||||
|
|
||||||
|
/* Search for unwanted pockets of river artifacts to clear them of water. */
|
||||||
|
for (const auto tile : Map::Iterate()) {
|
||||||
|
if (!IsTileType(tile, MP_WATER) || !IsRiver(tile)) continue;
|
||||||
|
|
||||||
|
bool has_neighbour = false;
|
||||||
|
for (DiagDirection d = DIAGDIR_BEGIN; d != DIAGDIR_END; d++) {
|
||||||
|
TileIndex other_tile = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDiagDir(d));
|
||||||
|
if (other_tile == INVALID_TILE || !IsWaterTile(other_tile)) continue;
|
||||||
|
|
||||||
|
has_neighbour = true;
|
||||||
|
if (_settings_game.game_creation.landscape == LandscapeType::Tropic) ModifyDesertZoneAroundRiver(tile);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!has_neighbour) {
|
||||||
|
DoClearSquare(tile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Run tile loop to update the ground density. */
|
/* Run tile loop to update the ground density. */
|
||||||
for (uint i = 0; i != TILE_UPDATE_FREQUENCY; i++) {
|
for (uint i = 0; i != TILE_UPDATE_FREQUENCY; i++) {
|
||||||
if (i % 64 == 0) IncreaseGeneratingWorldProgress(GWP_RIVER);
|
if (i % 64 == 0) IncreaseGeneratingWorldProgress(GWP_RIVER);
|
||||||
|
|
|
@ -53,7 +53,7 @@ static ScriptStorage &GetStorage()
|
||||||
|
|
||||||
/* static */ ScriptInstance *ScriptObject::ActiveInstance::active = nullptr;
|
/* static */ ScriptInstance *ScriptObject::ActiveInstance::active = nullptr;
|
||||||
|
|
||||||
ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance &instance) : alc_scope(instance.engine)
|
ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance &instance) : alc_scope(instance.engine.get())
|
||||||
{
|
{
|
||||||
this->last_active = ScriptObject::ActiveInstance::active;
|
this->last_active = ScriptObject::ActiveInstance::active;
|
||||||
ScriptObject::ActiveInstance::active = &instance;
|
ScriptObject::ActiveInstance::active = &instance;
|
||||||
|
@ -230,8 +230,8 @@ ScriptObject::DisableDoCommandScope::DisableDoCommandScope()
|
||||||
|
|
||||||
/* static */ bool ScriptObject::CanSuspend()
|
/* static */ bool ScriptObject::CanSuspend()
|
||||||
{
|
{
|
||||||
Squirrel *squirrel = ScriptObject::GetActiveInstance().engine;
|
Squirrel &squirrel = *ScriptObject::GetActiveInstance().engine;
|
||||||
return GetStorage().allow_do_command && squirrel->CanSuspend();
|
return GetStorage().allow_do_command && squirrel.CanSuspend();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* static */ ScriptEventQueue &ScriptObject::GetEventQueue()
|
/* static */ ScriptEventQueue &ScriptObject::GetEventQueue()
|
||||||
|
|
|
@ -50,8 +50,8 @@ static void PrintFunc(bool error_msg, std::string_view message)
|
||||||
|
|
||||||
ScriptInstance::ScriptInstance(std::string_view api_name)
|
ScriptInstance::ScriptInstance(std::string_view api_name)
|
||||||
{
|
{
|
||||||
this->storage = new ScriptStorage();
|
this->storage = std::make_unique<ScriptStorage>();
|
||||||
this->engine = new Squirrel(api_name);
|
this->engine = std::make_unique<Squirrel>(api_name);
|
||||||
this->engine->SetPrintFunction(&PrintFunc);
|
this->engine->SetPrintFunction(&PrintFunc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,10 +59,10 @@ void ScriptInstance::Initialize(const std::string &main_script, const std::strin
|
||||||
{
|
{
|
||||||
ScriptObject::ActiveInstance active(*this);
|
ScriptObject::ActiveInstance active(*this);
|
||||||
|
|
||||||
this->controller = new ScriptController(company);
|
this->controller = std::make_unique<ScriptController>(company);
|
||||||
|
|
||||||
/* Register the API functions and classes */
|
/* Register the API functions and classes */
|
||||||
this->engine->SetGlobalPointer(this->engine);
|
this->engine->SetGlobalPointer(this->engine.get());
|
||||||
this->RegisterAPI();
|
this->RegisterAPI();
|
||||||
if (this->IsDead()) {
|
if (this->IsDead()) {
|
||||||
/* Failed to register API; a message has already been logged. */
|
/* Failed to register API; a message has already been logged. */
|
||||||
|
@ -81,12 +81,11 @@ void ScriptInstance::Initialize(const std::string &main_script, const std::strin
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create the main-class */
|
/* Create the main-class */
|
||||||
this->instance = new SQObject();
|
this->instance = std::make_unique<SQObject>();
|
||||||
if (!this->engine->CreateClassInstance(instance_name, this->controller, this->instance)) {
|
if (!this->engine->CreateClassInstance(instance_name, this->controller.get(), this->instance.get())) {
|
||||||
/* If CreateClassInstance has returned false instance has not been
|
/* If CreateClassInstance has returned false instance has not been
|
||||||
* registered with squirrel, so avoid trying to Release it by clearing it now */
|
* registered with squirrel, so avoid trying to Release it by clearing it now */
|
||||||
delete this->instance;
|
this->instance.reset();
|
||||||
this->instance = nullptr;
|
|
||||||
this->Died();
|
this->Died();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -158,11 +157,10 @@ ScriptInstance::~ScriptInstance()
|
||||||
ScriptObject::ActiveInstance active(*this);
|
ScriptObject::ActiveInstance active(*this);
|
||||||
this->in_shutdown = true;
|
this->in_shutdown = true;
|
||||||
|
|
||||||
if (instance != nullptr) this->engine->ReleaseObject(this->instance);
|
if (instance != nullptr) this->engine->ReleaseObject(this->instance.get());
|
||||||
if (engine != nullptr) delete this->engine;
|
|
||||||
delete this->storage;
|
/* Engine must be reset explicitly in scope of the active instance. */
|
||||||
delete this->controller;
|
this->engine.reset();
|
||||||
delete this->instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptInstance::Continue()
|
void ScriptInstance::Continue()
|
||||||
|
@ -179,11 +177,9 @@ void ScriptInstance::Died()
|
||||||
|
|
||||||
this->last_allocated_memory = this->GetAllocatedMemory(); // Update cache
|
this->last_allocated_memory = this->GetAllocatedMemory(); // Update cache
|
||||||
|
|
||||||
if (this->instance != nullptr) this->engine->ReleaseObject(this->instance);
|
if (this->instance != nullptr) this->engine->ReleaseObject(this->instance.get());
|
||||||
delete this->instance;
|
this->engine.reset();
|
||||||
delete this->engine;
|
this->instance.reset();
|
||||||
this->instance = nullptr;
|
|
||||||
this->engine = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptInstance::GameLoop()
|
void ScriptInstance::GameLoop()
|
||||||
|
|
|
@ -256,7 +256,7 @@ public:
|
||||||
void ReleaseSQObject(HSQOBJECT *obj);
|
void ReleaseSQObject(HSQOBJECT *obj);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
class Squirrel *engine = nullptr; ///< A wrapper around the squirrel vm.
|
std::unique_ptr<class Squirrel> engine; ///< A wrapper around the squirrel vm.
|
||||||
std::string api_version{}; ///< Current API used by this script.
|
std::string api_version{}; ///< Current API used by this script.
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -288,9 +288,9 @@ protected:
|
||||||
virtual void LoadDummyScript() = 0;
|
virtual void LoadDummyScript() = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class ScriptController *controller = nullptr; ///< The script main class.
|
std::unique_ptr<class ScriptStorage> storage; ///< Some global information for each running script.
|
||||||
class ScriptStorage *storage = nullptr; ///< Some global information for each running script.
|
std::unique_ptr<class ScriptController> controller; ///< The script main class.
|
||||||
SQObject *instance = nullptr; ///< Squirrel-pointer to the script main class.
|
std::unique_ptr<SQObject> instance; ///< Squirrel-pointer to the script main class.
|
||||||
|
|
||||||
bool is_started = false; ///< Is the scripts constructor executed?
|
bool is_started = false; ///< Is the scripts constructor executed?
|
||||||
bool is_dead = false; ///< True if the script has been stopped.
|
bool is_dead = false; ///< True if the script has been stopped.
|
||||||
|
|
|
@ -39,6 +39,7 @@ void MakeWaterKeepingClass(TileIndex tile, Owner o);
|
||||||
void CheckForDockingTile(TileIndex t);
|
void CheckForDockingTile(TileIndex t);
|
||||||
|
|
||||||
void MakeRiverAndModifyDesertZoneAround(TileIndex tile);
|
void MakeRiverAndModifyDesertZoneAround(TileIndex tile);
|
||||||
|
void ModifyDesertZoneAroundRiver(TileIndex tile);
|
||||||
static const uint RIVER_OFFSET_DESERT_DISTANCE = 5; ///< Circular tile search diameter to create non-desert around a river tile.
|
static const uint RIVER_OFFSET_DESERT_DISTANCE = 5; ///< Circular tile search diameter to create non-desert around a river tile.
|
||||||
|
|
||||||
bool IsWateredTile(TileIndex tile, Direction from);
|
bool IsWateredTile(TileIndex tile, Direction from);
|
||||||
|
|
|
@ -454,6 +454,20 @@ void MakeRiverAndModifyDesertZoneAround(TileIndex tile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove desert directly around a river tile.
|
||||||
|
* @param tile The tile to create non-desert around
|
||||||
|
*/
|
||||||
|
void ModifyDesertZoneAroundRiver(TileIndex tile)
|
||||||
|
{
|
||||||
|
assert(IsTileType(tile, MP_WATER) && IsRiver(tile));
|
||||||
|
|
||||||
|
/* Remove desert directly around the river tile. */
|
||||||
|
for (auto t : SpiralTileSequence(tile, RIVER_OFFSET_DESERT_DISTANCE)) {
|
||||||
|
if (GetTropicZone(t) == TROPICZONE_DESERT) SetTropicZone(t, TROPICZONE_NORMAL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build a piece of canal.
|
* Build a piece of canal.
|
||||||
* @param flags type of operation
|
* @param flags type of operation
|
||||||
|
|
Loading…
Reference in New Issue