From 2da8e18200b4ce8971a1c93e3fa1eb8da8c30082 Mon Sep 17 00:00:00 2001 From: Ted John Date: Tue, 11 Dec 2018 23:00:59 +0000 Subject: [PATCH 1/2] Read the canGrow flag from surface objects --- src/openrct2/object/TerrainSurfaceObject.cpp | 3 ++- src/openrct2/object/TerrainSurfaceObject.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/openrct2/object/TerrainSurfaceObject.cpp b/src/openrct2/object/TerrainSurfaceObject.cpp index a71ccba970..a5dec79c7e 100644 --- a/src/openrct2/object/TerrainSurfaceObject.cpp +++ b/src/openrct2/object/TerrainSurfaceObject.cpp @@ -81,7 +81,8 @@ void TerrainSurfaceObject::ReadJson(IReadObjectContext* context, const json_t* r Flags = ObjectJsonHelpers::GetFlags( 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)) diff --git a/src/openrct2/object/TerrainSurfaceObject.h b/src/openrct2/object/TerrainSurfaceObject.h index cfe05c0f73..71f2fff106 100644 --- a/src/openrct2/object/TerrainSurfaceObject.h +++ b/src/openrct2/object/TerrainSurfaceObject.h @@ -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 From ab53c69c3c396fa94192085c0c5f7ff32254c74c Mon Sep 17 00:00:00 2001 From: Ted John Date: Tue, 11 Dec 2018 23:31:06 +0000 Subject: [PATCH 2/2] Use new CanGrow method on surface element which uses object property --- src/openrct2/Cheats.cpp | 18 +++------ src/openrct2/peep/Staff.cpp | 67 +++++++++++++++----------------- src/openrct2/world/Map.cpp | 23 +++++------ src/openrct2/world/Surface.cpp | 23 ++++++++++- src/openrct2/world/TileElement.h | 1 + 5 files changed, 72 insertions(+), 60 deletions(-) diff --git a/src/openrct2/Cheats.cpp b/src/openrct2/Cheats.cpp index f65a315822..4849051c80 100644 --- a/src/openrct2/Cheats.cpp +++ b/src/openrct2/Cheats.cpp @@ -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); + } } } diff --git a/src/openrct2/peep/Staff.cpp b/src/openrct2/peep/Staff.cpp index 7bba343967..8c35c01428 100644 --- a/src/openrct2/peep/Staff.cpp +++ b/src/openrct2/peep/Staff.cpp @@ -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" @@ -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; } @@ -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; @@ -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; } /** diff --git a/src/openrct2/world/Map.cpp b/src/openrct2/world/Map.cpp index ded1c443cb..cd96440f91 100644 --- a/src/openrct2/world/Map.cpp +++ b/src/openrct2/world/Map.cpp @@ -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) { @@ -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)); @@ -1315,7 +1319,7 @@ static money32 map_change_surface_style( if (edgeStyle != 0xFF) { - uint8_t currentEdge = tileElement->AsSurface()->GetEdgeStyle(); + uint8_t currentEdge = surfaceElement->GetEdgeStyle(); if (edgeStyle != currentEdge) { @@ -1323,7 +1327,7 @@ static money32 map_change_surface_style( if (flags & GAME_COMMAND_FLAG_APPLY) { - tileElement->AsSurface()->SetEdgeStyle(edgeStyle); + surfaceElement->SetEdgeStyle(edgeStyle); map_invalidate_tile_full(x, y); } } @@ -1331,13 +1335,10 @@ static money32 map_change_surface_style( 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); } } } diff --git a/src/openrct2/world/Surface.cpp b/src/openrct2/world/Surface.cpp index 01ef70ed09..cbb34346ab 100644 --- a/src/openrct2/world/Surface.cpp +++ b/src/openrct2/world/Surface.cpp @@ -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" @@ -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(obj); + if (surfaceObject->Flags & TERRAIN_SURFACE_FLAGS::CAN_GROW) + { + return true; + } + } + return false; +} + uint8_t SurfaceElement::GetGrassLength() const { return grass_length; @@ -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; @@ -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; -} \ No newline at end of file +} diff --git a/src/openrct2/world/TileElement.h b/src/openrct2/world/TileElement.h index a477acf96f..ca6f223ff8 100644 --- a/src/openrct2/world/TileElement.h +++ b/src/openrct2/world/TileElement.h @@ -143,6 +143,7 @@ public: 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);