Skip to content

Commit

Permalink
Split off litter code to litter.cpp (#14799)
Browse files Browse the repository at this point in the history
* Split off litter code to litter.cpp

* Use PATH_CLEARANCE
  • Loading branch information
duncanspumpkin committed Jun 3, 2021
1 parent 7253b92 commit 0337878
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 123 deletions.
4 changes: 4 additions & 0 deletions OpenRCT2.xcodeproj/project.pbxproj
Expand Up @@ -766,6 +766,7 @@
F7D774AE1EC6741D00BE6EBC /* sequence in CopyFiles */ = {isa = PBXBuildFile; fileRef = D4EC48E51C2637710024B507 /* sequence */; };
E73A781EBC3C440A99D59821 /* EntityTweener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0EB0D1DF6CC74E4190301D84 /* EntityTweener.cpp */; };
F42186C5840D4196981ADD16 /* EntityTweener.h in Headers */ = {isa = PBXBuildFile; fileRef = 091352A950004312BAB18717 /* EntityTweener.h */; };
0746674FA0794ABF86E406A1 /* Litter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9D3DD6CD73F5421880280D9D /* Litter.cpp */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -1834,6 +1835,7 @@
F7D774841EC66CD700BE6EBC /* OpenRCT2-cli */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "OpenRCT2-cli"; sourceTree = BUILT_PRODUCTS_DIR; };
0EB0D1DF6CC74E4190301D84 /* EntityTweener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = EntityTweener.cpp; path = src/openrct2/world/EntityTweener.cpp; sourceTree = SOURCE_ROOT; };
091352A950004312BAB18717 /* EntityTweener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = EntityTweener.h; path = src/openrct2/world/EntityTweener.h; sourceTree = SOURCE_ROOT; };
9D3DD6CD73F5421880280D9D /* Litter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Litter.cpp; path = src/openrct2/world/Litter.cpp; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -3122,6 +3124,7 @@
4C7B54422007646A00A52E21 /* Water.h */,
0EB0D1DF6CC74E4190301D84 /* EntityTweener.cpp */,
091352A950004312BAB18717 /* EntityTweener.h */,
9D3DD6CD73F5421880280D9D /* Litter.cpp */,
);
path = world;
sourceTree = "<group>";
Expand Down Expand Up @@ -4269,6 +4272,7 @@
66A10FD1257F1E3000DD651A /* WaterRaiseAction.cpp in Sources */,
C688791820289B9B0084B384 /* MonorailCycles.cpp in Sources */,
E73A781EBC3C440A99D59821 /* EntityTweener.cpp in Sources */,
0746674FA0794ABF86E406A1 /* Litter.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/libopenrct2.vcxproj
Expand Up @@ -871,6 +871,7 @@
<ClCompile Include="world\Footpath.cpp" />
<ClCompile Include="world\Fountain.cpp" />
<ClCompile Include="world\LargeScenery.cpp" />
<ClCompile Include="world\Litter.cpp" />
<ClCompile Include="world\Map.cpp" />
<ClCompile Include="world\MapAnimation.cpp" />
<ClCompile Include="world\MapGen.cpp" />
Expand Down
127 changes: 127 additions & 0 deletions src/openrct2/world/Litter.cpp
@@ -0,0 +1,127 @@
#include "Litter.h"

#include "../Cheats.h"
#include "../localisation/StringIds.h"
#include "../scenario/Scenario.h"
#include "EntityList.h"
#include "Map.h"
#include "Sprite.h"

static bool isLocationLitterable(const CoordsXYZ& mapPos)
{
TileElement* tileElement;

if (!map_is_location_owned(mapPos))
return false;

tileElement = map_get_first_element_at(mapPos);
if (tileElement == nullptr)
return false;
do
{
if (tileElement->GetType() != TILE_ELEMENT_TYPE_PATH)
continue;

int32_t pathZ = tileElement->GetBaseZ();
if (pathZ < mapPos.z || pathZ >= mapPos.z + PATH_CLEARANCE)
continue;

return !tile_element_is_underground(tileElement);
} while (!(tileElement++)->IsLastForTile());
return false;
}

/**
*
* rct2: 0x0067375D
*/
void Litter::Create(const CoordsXYZD& litterPos, Type type)
{
if (gCheatsDisableLittering)
return;

auto offsetLitterPos = litterPos
+ CoordsXY{ CoordsDirectionDelta[litterPos.direction >> 3].x / 8,
CoordsDirectionDelta[litterPos.direction >> 3].y / 8 };

if (!isLocationLitterable(offsetLitterPos))
return;

if (GetEntityListCount(EntityType::Litter) >= 500)
{
Litter* newestLitter = nullptr;
uint32_t newestLitterCreationTick = 0;
for (auto litter : EntityList<Litter>())
{
if (newestLitterCreationTick <= litter->creationTick)
{
newestLitterCreationTick = litter->creationTick;
newestLitter = litter;
}
}

if (newestLitter != nullptr)
{
newestLitter->Invalidate();
sprite_remove(newestLitter);
}
}

Litter* litter = CreateEntity<Litter>();
if (litter == nullptr)
return;

litter->sprite_direction = offsetLitterPos.direction;
litter->sprite_width = 6;
litter->sprite_height_negative = 6;
litter->sprite_height_positive = 3;
litter->SubType = type;
litter->MoveTo(offsetLitterPos);
litter->creationTick = gScenarioTicks;
}

/**
*
* rct2: 0x006738E1
*/
void Litter::RemoveAt(const CoordsXYZ& litterPos)
{
std::vector<Litter*> removals;
for (auto litter : EntityTileList<Litter>(litterPos))
{
if (abs(litter->z - litterPos.z) <= 16)
{
if (abs(litter->x - litterPos.x) <= 8 && abs(litter->y - litterPos.y) <= 8)
{
removals.push_back(litter);
}
}
}
for (auto* litter : removals)
{
litter->Invalidate();
sprite_remove(litter);
}
}

static const rct_string_id litterNames[12] = {
STR_LITTER_VOMIT,
STR_LITTER_VOMIT,
STR_SHOP_ITEM_SINGULAR_EMPTY_CAN,
STR_SHOP_ITEM_SINGULAR_RUBBISH,
STR_SHOP_ITEM_SINGULAR_EMPTY_BURGER_BOX,
STR_SHOP_ITEM_SINGULAR_EMPTY_CUP,
STR_SHOP_ITEM_SINGULAR_EMPTY_BOX,
STR_SHOP_ITEM_SINGULAR_EMPTY_BOTTLE,
STR_SHOP_ITEM_SINGULAR_EMPTY_BOWL_RED,
STR_SHOP_ITEM_SINGULAR_EMPTY_DRINK_CARTON,
STR_SHOP_ITEM_SINGULAR_EMPTY_JUICE_CUP,
STR_SHOP_ITEM_SINGULAR_EMPTY_BOWL_BLUE,
};

rct_string_id Litter::GetName() const
{
if (EnumValue(SubType) >= sizeof(litterNames))
return STR_NONE;
return litterNames[EnumValue(SubType)];
}
123 changes: 0 additions & 123 deletions src/openrct2/world/Sprite.cpp
Expand Up @@ -9,18 +9,13 @@

#include "Sprite.h"

#include "../Cheats.h"
#include "../Game.h"
#include "../OpenRCT2.h"
#include "../audio/audio.h"
#include "../core/ChecksumStream.h"
#include "../core/Crypt.h"
#include "../core/DataSerialiser.h"
#include "../core/Guard.hpp"
#include "../core/MemoryStream.h"
#include "../interface/Viewport.h"
#include "../localisation/Date.h"
#include "../localisation/Localisation.h"
#include "../peep/Peep.h"
#include "../ride/Vehicle.h"
#include "../scenario/Scenario.h"
Expand Down Expand Up @@ -562,124 +557,6 @@ void sprite_remove(SpriteBase* sprite)
sprite_reset(sprite);
}

static bool litter_can_be_at(const CoordsXYZ& mapPos)
{
TileElement* tileElement;

if (!map_is_location_owned(mapPos))
return false;

tileElement = map_get_first_element_at(mapPos);
if (tileElement == nullptr)
return false;
do
{
if (tileElement->GetType() != TILE_ELEMENT_TYPE_PATH)
continue;

int32_t pathZ = tileElement->GetBaseZ();
if (pathZ < mapPos.z || pathZ >= mapPos.z + 32)
continue;

return !tile_element_is_underground(tileElement);
} while (!(tileElement++)->IsLastForTile());
return false;
}

/**
*
* rct2: 0x0067375D
*/
void Litter::Create(const CoordsXYZD& litterPos, Type type)
{
if (gCheatsDisableLittering)
return;

auto offsetLitterPos = litterPos
+ CoordsXY{ CoordsDirectionDelta[litterPos.direction >> 3].x / 8,
CoordsDirectionDelta[litterPos.direction >> 3].y / 8 };

if (!litter_can_be_at(offsetLitterPos))
return;

if (GetEntityListCount(EntityType::Litter) >= 500)
{
Litter* newestLitter = nullptr;
uint32_t newestLitterCreationTick = 0;
for (auto litter : EntityList<Litter>())
{
if (newestLitterCreationTick <= litter->creationTick)
{
newestLitterCreationTick = litter->creationTick;
newestLitter = litter;
}
}

if (newestLitter != nullptr)
{
newestLitter->Invalidate();
sprite_remove(newestLitter);
}
}

Litter* litter = CreateEntity<Litter>();
if (litter == nullptr)
return;

litter->sprite_direction = offsetLitterPos.direction;
litter->sprite_width = 6;
litter->sprite_height_negative = 6;
litter->sprite_height_positive = 3;
litter->SubType = type;
litter->MoveTo(offsetLitterPos);
litter->creationTick = gScenarioTicks;
}

/**
*
* rct2: 0x006738E1
*/
void Litter::RemoveAt(const CoordsXYZ& litterPos)
{
std::vector<Litter*> removals;
for (auto litter : EntityTileList<Litter>(litterPos))
{
if (abs(litter->z - litterPos.z) <= 16)
{
if (abs(litter->x - litterPos.x) <= 8 && abs(litter->y - litterPos.y) <= 8)
{
removals.push_back(litter);
}
}
}
for (auto* litter : removals)
{
litter->Invalidate();
sprite_remove(litter);
}
}

static const rct_string_id litterNames[12] = {
STR_LITTER_VOMIT,
STR_LITTER_VOMIT,
STR_SHOP_ITEM_SINGULAR_EMPTY_CAN,
STR_SHOP_ITEM_SINGULAR_RUBBISH,
STR_SHOP_ITEM_SINGULAR_EMPTY_BURGER_BOX,
STR_SHOP_ITEM_SINGULAR_EMPTY_CUP,
STR_SHOP_ITEM_SINGULAR_EMPTY_BOX,
STR_SHOP_ITEM_SINGULAR_EMPTY_BOTTLE,
STR_SHOP_ITEM_SINGULAR_EMPTY_BOWL_RED,
STR_SHOP_ITEM_SINGULAR_EMPTY_DRINK_CARTON,
STR_SHOP_ITEM_SINGULAR_EMPTY_JUICE_CUP,
STR_SHOP_ITEM_SINGULAR_EMPTY_BOWL_BLUE,
};

rct_string_id Litter::GetName() const
{
if (EnumValue(SubType) >= sizeof(litterNames))
return STR_NONE;
return litterNames[EnumValue(SubType)];
}
/**
* Loops through all sprites, finds floating objects and removes them.
* Returns the amount of removed objects as feedback.
Expand Down

0 comments on commit 0337878

Please sign in to comment.