Skip to content

Commit

Permalink
Merge pull request #8421 from IntelOrca/8405-use-cangrow
Browse files Browse the repository at this point in the history
Use new canGrow property on surface objects
  • Loading branch information
IntelOrca committed Dec 13, 2018
2 parents 88ff417 + ab53c69 commit f8d9127
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 61 deletions.
18 changes: 6 additions & 12 deletions src/openrct2/Cheats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,17 @@ int32_t day_spinner_value = 1;
static void cheat_set_grass_length(int32_t length)
{
int32_t x, y;
TileElement* tileElement;

for (y = 0; y < MAXIMUM_MAP_SIZE_TECHNICAL; y++)
{
for (x = 0; x < MAXIMUM_MAP_SIZE_TECHNICAL; x++)
{
tileElement = map_get_surface_element_at(x, y);
if (!(tileElement->AsSurface()->GetOwnership() & OWNERSHIP_OWNED))
continue;

if (tileElement->AsSurface()->GetSurfaceStyle() != TERRAIN_GRASS)
continue;

if (tileElement->AsSurface()->GetWaterHeight() > 0)
continue;

tileElement->AsSurface()->SetGrassLength(length);
auto surfaceElement = map_get_surface_element_at(x, y)->AsSurface();
if (surfaceElement != nullptr && (surfaceElement->GetOwnership() & OWNERSHIP_OWNED)
&& surfaceElement->GetWaterHeight() == 0 && surfaceElement->CanGrassGrow())
{
surfaceElement->SetGrassLength(length);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/openrct2/object/TerrainSurfaceObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ void TerrainSurfaceObject::ReadJson(IReadObjectContext* context, const json_t* r
Flags = ObjectJsonHelpers::GetFlags<TERRAIN_SURFACE_FLAGS>(
properties,
{ { "smoothWithSelf", TERRAIN_SURFACE_FLAGS::SMOOTH_WITH_SELF },
{ "smoothWithOther", TERRAIN_SURFACE_FLAGS::SMOOTH_WITH_OTHER } });
{ "smoothWithOther", TERRAIN_SURFACE_FLAGS::SMOOTH_WITH_OTHER },
{ "canGrow", TERRAIN_SURFACE_FLAGS::CAN_GROW } });

auto jDefault = json_object_get(root, "default");
if (json_is_object(jDefault))
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/TerrainSurfaceObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum TERRAIN_SURFACE_FLAGS
NONE = 0,
SMOOTH_WITH_SELF = 1 << 0,
SMOOTH_WITH_OTHER = 1 << 1,
CAN_GROW = 1 << 2,
};

class TerrainSurfaceObject final : public Object
Expand Down
67 changes: 32 additions & 35 deletions src/openrct2/peep/Staff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "../management/Finance.h"
#include "../network/network.h"
#include "../object/ObjectList.h"
#include "../object/ObjectManager.h"
#include "../object/TerrainSurfaceObject.h"
#include "../paint/tile_element/Paint.TileElement.h"
#include "../ride/RideData.h"
#include "../ride/Station.h"
Expand Down Expand Up @@ -990,18 +992,17 @@ static uint8_t staff_handyman_direction_to_uncut_grass(rct_peep* peep, uint8_t v
if (chosenTile.x > 0x1FFF || chosenTile.y > 0x1FFF)
continue;

TileElement* tileElement = map_get_surface_element_at(chosenTile);

if (tileElement->AsSurface()->GetSurfaceStyle() != TERRAIN_GRASS)
continue;

if (abs(tileElement->base_height - peep->next_z) > 2)
continue;

if ((tileElement->AsSurface()->GetGrassLength() & 0x7) < GRASS_LENGTH_CLEAR_1)
continue;

return chosenDirection;
auto surfaceElement = map_get_surface_element_at(chosenTile)->AsSurface();
if (surfaceElement != nullptr)
{
if (std::abs(surfaceElement->base_height - peep->next_z) <= 2)
{
if (surfaceElement->CanGrassGrow() && (surfaceElement->GetGrassLength() & 0x7) >= GRASS_LENGTH_CLEAR_1)
{
return chosenDirection;
}
}
}
}
return 0xFF;
}
Expand Down Expand Up @@ -1674,15 +1675,11 @@ void rct_peep::UpdateMowing()
if (var_37 != 7)
continue;

TileElement* tile_element = map_get_first_element_at(next_x / 32, next_y / 32);

for (; (tile_element->GetType() != TILE_ELEMENT_TYPE_SURFACE); tile_element++)
;

if (tile_element->AsSurface()->GetSurfaceStyle() == TERRAIN_GRASS)
auto surfaceElement = map_get_surface_element_at(next_x / 32, next_y / 32)->AsSurface();
if (surfaceElement != nullptr && surfaceElement->CanGrassGrow())
{
tile_element->AsSurface()->SetGrassLength(GRASS_LENGTH_MOWED);
map_invalidate_tile_zoom0(next_x, next_y, tile_element->base_height * 8, tile_element->base_height * 8 + 16);
surfaceElement->SetGrassLength(GRASS_LENGTH_MOWED);
map_invalidate_tile_zoom0(next_x, next_y, surfaceElement->base_height * 8, surfaceElement->base_height * 8 + 16);
}
staff_lawns_mown++;
window_invalidate_flags |= PEEP_INVALIDATE_STAFF_STATS;
Expand Down Expand Up @@ -2264,21 +2261,21 @@ static int32_t peep_update_patrolling_find_grass(rct_peep* peep)
if (!(peep->GetNextIsSurface()))
return 0;

TileElement* tile_element = map_get_surface_element_at({ peep->next_x, peep->next_y });

if ((tile_element->AsSurface()->GetSurfaceStyle()) != TERRAIN_GRASS)
return 0;

if ((tile_element->AsSurface()->GetGrassLength() & 0x7) < GRASS_LENGTH_CLEAR_1)
return 0;

peep->SetState(PEEP_STATE_MOWING);
peep->var_37 = 0;
// Original code used .y for both x and y. Changed to .x to make more sense (both x and y are 28)
peep->destination_x = peep->next_x + _MowingWaypoints[0].x;
peep->destination_y = peep->next_y + _MowingWaypoints[0].y;
peep->destination_tolerance = 3;
return 1;
auto surfaceElement = map_get_surface_element_at({ peep->next_x, peep->next_y })->AsSurface();
if (surfaceElement != nullptr && surfaceElement->CanGrassGrow())
{
if ((surfaceElement->GetGrassLength() & 0x7) >= GRASS_LENGTH_CLEAR_1)
{
peep->SetState(PEEP_STATE_MOWING);
peep->var_37 = 0;
// Original code used .y for both x and y. Changed to .x to make more sense (both x and y are 28)
peep->destination_x = peep->next_x + _MowingWaypoints[0].x;
peep->destination_y = peep->next_y + _MowingWaypoints[0].y;
peep->destination_tolerance = 3;
return 1;
}
}
return 0;
}

/**
Expand Down
23 changes: 12 additions & 11 deletions src/openrct2/world/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1284,11 +1284,15 @@ static money32 map_change_surface_style(
continue;
}

TileElement* tileElement = map_get_surface_element_at({ x, y });
auto surfaceElement = map_get_surface_element_at({ x, y })->AsSurface();
if (surfaceElement == nullptr)
{
continue;
}

if (surfaceStyle != 0xFF)
{
uint8_t cur_terrain = tileElement->AsSurface()->GetSurfaceStyle();
uint8_t cur_terrain = surfaceElement->GetSurfaceStyle();

if (surfaceStyle != cur_terrain)
{
Expand All @@ -1305,7 +1309,7 @@ static money32 map_change_surface_style(

if (flags & GAME_COMMAND_FLAG_APPLY)
{
tileElement->AsSurface()->SetSurfaceStyle(surfaceStyle);
surfaceElement->SetSurfaceStyle(surfaceStyle);

map_invalidate_tile_full(x, y);
footpath_remove_litter(x, y, tile_element_height(x, y));
Expand All @@ -1315,29 +1319,26 @@ static money32 map_change_surface_style(

if (edgeStyle != 0xFF)
{
uint8_t currentEdge = tileElement->AsSurface()->GetEdgeStyle();
uint8_t currentEdge = surfaceElement->GetEdgeStyle();

if (edgeStyle != currentEdge)
{
edgeCost += 100;

if (flags & GAME_COMMAND_FLAG_APPLY)
{
tileElement->AsSurface()->SetEdgeStyle(edgeStyle);
surfaceElement->SetEdgeStyle(edgeStyle);
map_invalidate_tile_full(x, y);
}
}
}

if (flags & GAME_COMMAND_FLAG_APPLY)
{
if (tileElement->AsSurface()->GetSurfaceStyle() == TERRAIN_GRASS)
if (surfaceElement->CanGrassGrow() && (surfaceElement->GetGrassLength() & 7) != GRASS_LENGTH_CLEAR_0)
{
if ((tileElement->AsSurface()->GetGrassLength() & 7) != GRASS_LENGTH_CLEAR_0)
{
tileElement->AsSurface()->SetGrassLength(GRASS_LENGTH_CLEAR_0);
map_invalidate_tile_full(x, y);
}
surfaceElement->SetGrassLength(GRASS_LENGTH_CLEAR_0);
map_invalidate_tile_full(x, y);
}
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/openrct2/world/Surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#include "Surface.h"

#include "../Context.h"
#include "../object/ObjectManager.h"
#include "../object/TerrainSurfaceObject.h"
#include "../scenario/Scenario.h"
#include "Location.hpp"
#include "Map.h"
Expand Down Expand Up @@ -67,6 +70,22 @@ void SurfaceElement::SetWaterHeight(uint32_t newWaterHeight)
terrain |= newWaterHeight;
}

bool SurfaceElement::CanGrassGrow() const
{
auto surfaceStyle = GetSurfaceStyle();
auto& objMgr = OpenRCT2::GetContext()->GetObjectManager();
auto obj = objMgr.GetLoadedObject(OBJECT_TYPE_TERRAIN_SURFACE, surfaceStyle);
if (obj != nullptr)
{
auto surfaceObject = static_cast<TerrainSurfaceObject*>(obj);
if (surfaceObject->Flags & TERRAIN_SURFACE_FLAGS::CAN_GROW)
{
return true;
}
}
return false;
}

uint8_t SurfaceElement::GetGrassLength() const
{
return grass_length;
Expand Down Expand Up @@ -108,7 +127,7 @@ void SurfaceElement::SetGrassLengthAndInvalidate(uint8_t length, CoordsXY coords
void SurfaceElement::UpdateGrassLength(CoordsXY coords)
{
// Check if tile is grass
if (GetSurfaceStyle() != TERRAIN_GRASS)
if (!CanGrassGrow())
return;

uint8_t grassLengthTmp = grass_length & 7;
Expand Down Expand Up @@ -226,4 +245,4 @@ void SurfaceElement::SetHasTrackThatNeedsWater(bool on)
type &= ~SURFACE_ELEMENT_HAS_TRACK_THAT_NEEDS_WATER;
if (on)
type |= SURFACE_ELEMENT_HAS_TRACK_THAT_NEEDS_WATER;
}
}
1 change: 1 addition & 0 deletions src/openrct2/world/TileElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ struct SurfaceElement : TileElementBase
uint32_t GetEdgeStyle() const;
void SetEdgeStyle(uint32_t newStyle);

bool CanGrassGrow() const;
uint8_t GetGrassLength() const;
void SetGrassLength(uint8_t newLength);
void SetGrassLengthAndInvalidate(uint8_t newLength, CoordsXY coords);
Expand Down

0 comments on commit f8d9127

Please sign in to comment.