Files
OpenRCT2-Web/src/openrct2/entity/Duck.cpp
T

386 lines
9.2 KiB
C++
Raw Normal View History

2017-03-11 13:34:54 +00:00
/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
2017-03-11 13:34:54 +00:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2017-03-11 13:34:54 +00:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2017-03-11 13:34:54 +00:00
*****************************************************************************/
#include "Duck.h"
2017-03-11 13:34:54 +00:00
2018-06-22 23:17:03 +02:00
#include "../Game.h"
#include "../GameState.h"
2025-02-13 10:51:46 -05:00
#include "../SpriteIds.h"
2025-02-17 07:24:03 -05:00
#include "../audio/Audio.h"
#include "../core/DataSerialiser.h"
#include "../entity/EntityList.h"
2021-11-27 15:27:41 +02:00
#include "../paint/Paint.h"
2022-01-29 05:29:08 -08:00
#include "../profiling/Profiling.h"
2024-10-15 22:02:31 +02:00
#include "../world/tile_element/SurfaceElement.h"
2021-11-24 17:19:52 +02:00
#include "EntityRegistry.h"
2017-03-11 13:34:54 +00:00
2018-11-21 23:16:04 +01:00
#include <iterator>
2018-06-22 23:17:03 +02:00
#include <limits>
using namespace OpenRCT2;
constexpr int32_t kDuckMaxStates = 5;
2017-03-11 13:34:54 +00:00
// clang-format off
static constexpr CoordsXY kDuckMoveOffset[] =
2017-03-11 13:34:54 +00:00
{
{ -1, 0 },
{ 0, 1 },
{ 1, 0 },
{ 0, -1 },
};
static constexpr uint8_t kDuckAnimationFlyToWater[] =
2017-03-11 13:34:54 +00:00
{
8, 9, 10, 11, 12, 13
};
2025-01-19 16:59:45 +01:00
static constexpr uint8_t kDuckAnimationSwim[] =
2017-03-11 13:34:54 +00:00
{
0
};
static constexpr uint8_t kDuckAnimationDrink[] =
2017-03-11 13:34:54 +00:00
{
1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0xFF
};
static constexpr uint8_t kDuckAnimationDoubleDrink[] =
2017-03-11 13:34:54 +00:00
{
4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 6,
6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 0, 0, 0, 0, 0xFF
};
static constexpr uint8_t kDuckAnimationFlyAway[] =
2017-03-11 13:34:54 +00:00
{
8, 9, 10, 11, 12, 13
};
// clang-format on
2017-03-11 13:34:54 +00:00
2025-01-19 16:59:45 +01:00
static constexpr const uint8_t* kDuckAnimations[] = {
kDuckAnimationFlyToWater, // DuckState::FlyToWater,
kDuckAnimationSwim, // DuckState::Swim,
kDuckAnimationDrink, // DuckState::Drink,
kDuckAnimationDoubleDrink, // DuckState::DoubleDrink,
kDuckAnimationFlyAway, // DuckState::FlyAway,
};
template<>
bool EntityBase::Is<Duck>() const
{
return Type == EntityType::Duck;
2017-03-11 13:58:31 +00:00
}
bool Duck::IsFlying()
{
return this->state == DuckState::FlyAway || this->state == DuckState::FlyToWater;
}
2020-01-19 15:50:01 +00:00
void Duck::Remove()
2017-03-11 13:58:31 +00:00
{
2019-10-15 16:49:00 +02:00
Invalidate();
2021-11-24 15:37:47 +02:00
EntityRemove(this);
2017-03-11 13:58:31 +00:00
}
2020-01-19 15:50:01 +00:00
void Duck::UpdateFlyToWater()
2017-03-11 13:34:54 +00:00
{
const auto currentTicks = getGameState().currentTicks;
if ((currentTicks & 3) != 0)
2018-06-22 23:17:03 +02:00
return;
2017-03-11 13:34:54 +00:00
2017-03-11 13:58:31 +00:00
frame++;
if (frame >= std::size(kDuckAnimationFlyToWater))
2017-03-11 13:34:54 +00:00
{
2017-03-11 13:58:31 +00:00
frame = 0;
2017-03-11 13:34:54 +00:00
}
2017-03-11 13:58:31 +00:00
Invalidate();
int32_t manhattanDistance = abs(target_x - x) + abs(target_y - y);
int32_t direction = Orientation >> 3;
auto destination = CoordsXYZ{ CoordsXY{ x, y } + kDuckMoveOffset[direction], 0 };
2020-01-13 21:03:57 -03:00
int32_t manhattanDistanceN = abs(target_x - destination.x) + abs(target_y - destination.y);
2017-03-11 13:34:54 +00:00
auto surfaceElement = MapGetSurfaceElementAt(CoordsXY{ target_x, target_y });
int32_t waterHeight = surfaceElement != nullptr ? surfaceElement->GetWaterHeight() : 0;
2017-03-11 13:34:54 +00:00
if (waterHeight == 0)
{
state = DuckState::FlyAway;
2017-03-11 13:58:31 +00:00
UpdateFlyAway();
2017-03-11 13:34:54 +00:00
}
else
{
2020-01-13 21:03:57 -03:00
destination.z = abs(z - waterHeight);
2017-03-11 13:34:54 +00:00
if (manhattanDistanceN <= manhattanDistance)
{
2020-01-13 21:03:57 -03:00
if (destination.z > manhattanDistanceN)
2017-03-11 13:34:54 +00:00
{
2020-01-13 21:03:57 -03:00
destination.z = z - 2;
2017-03-11 13:58:31 +00:00
if (waterHeight >= z)
2017-03-11 13:34:54 +00:00
{
2020-01-13 21:03:57 -03:00
destination.z += 4;
2017-03-11 13:34:54 +00:00
}
2017-03-11 13:58:31 +00:00
frame = 1;
2017-03-11 13:34:54 +00:00
}
else
{
2020-01-13 21:03:57 -03:00
destination.z = z;
2017-03-11 13:34:54 +00:00
}
2020-01-13 21:03:57 -03:00
MoveTo(destination);
2017-03-11 13:34:54 +00:00
}
else
{
2020-01-13 21:03:57 -03:00
if (destination.z > 4)
2017-03-11 13:34:54 +00:00
{
state = DuckState::FlyAway;
2017-03-11 13:58:31 +00:00
UpdateFlyAway();
2017-03-11 13:34:54 +00:00
}
else
{
state = DuckState::Swim;
2017-03-11 13:58:31 +00:00
frame = 0;
UpdateSwim();
2017-03-11 13:34:54 +00:00
}
}
}
}
2020-01-19 15:50:01 +00:00
void Duck::UpdateSwim()
2017-03-11 13:34:54 +00:00
{
const auto currentTicks = getGameState().currentTicks;
if (((currentTicks + Id.ToUnderlying()) & 3) != 0)
2018-06-22 23:17:03 +02:00
return;
2017-03-11 13:34:54 +00:00
2023-01-17 20:46:55 +01:00
uint32_t randomNumber = ScenarioRand();
2017-03-11 13:34:54 +00:00
if ((randomNumber & 0xFFFF) < 0x666)
{
if (randomNumber & 0x80000000)
{
state = DuckState::DoubleDrink;
frame = std::numeric_limits<uint16_t>::max();
2017-03-11 13:58:31 +00:00
UpdateDoubleDrink();
2017-03-11 13:34:54 +00:00
}
else
{
state = DuckState::Drink;
frame = std::numeric_limits<uint16_t>::max();
2017-03-11 13:58:31 +00:00
UpdateDrink();
2017-03-11 13:34:54 +00:00
}
}
else
{
int32_t currentMonth = GetDate().GetMonth();
2017-03-11 13:34:54 +00:00
if (currentMonth >= MONTH_SEPTEMBER && (randomNumber >> 16) < 218)
{
state = DuckState::FlyAway;
2017-03-11 13:58:31 +00:00
UpdateFlyAway();
2017-03-11 13:34:54 +00:00
}
else
{
2017-03-11 13:58:31 +00:00
Invalidate();
2022-10-12 06:35:20 +01:00
int16_t landZ = TileElementHeight({ x, y });
int16_t waterZ = TileElementWaterHeight({ x, y });
2017-03-11 13:34:54 +00:00
2017-03-11 13:58:31 +00:00
if (z < landZ || waterZ == 0)
2017-03-11 13:34:54 +00:00
{
state = DuckState::FlyAway;
2017-03-11 13:58:31 +00:00
UpdateFlyAway();
2017-03-11 13:34:54 +00:00
}
else
{
2017-03-11 13:58:31 +00:00
z = waterZ;
2023-01-17 20:46:55 +01:00
randomNumber = ScenarioRand();
2017-03-11 13:34:54 +00:00
if ((randomNumber & 0xFFFF) <= 0xAAA)
{
randomNumber >>= 16;
Orientation = randomNumber & 0x18;
2017-03-11 13:34:54 +00:00
}
int32_t direction = Orientation >> 3;
auto destination = CoordsXYZ{ CoordsXY{ x, y } + kDuckMoveOffset[direction], 0 };
2022-10-12 06:35:20 +01:00
landZ = TileElementHeight(destination);
waterZ = TileElementWaterHeight(destination);
2017-03-11 13:34:54 +00:00
if (z > landZ && z == waterZ)
2017-03-11 13:34:54 +00:00
{
2020-01-13 21:03:57 -03:00
destination.z = waterZ;
MoveTo(destination);
2017-03-11 13:58:31 +00:00
Invalidate();
2017-03-11 13:34:54 +00:00
}
}
}
}
}
2020-01-19 15:50:01 +00:00
void Duck::UpdateDrink()
2017-03-11 13:34:54 +00:00
{
2017-03-11 13:58:31 +00:00
frame++;
if (kDuckAnimationDrink[frame] == 0xFF)
2017-03-11 13:34:54 +00:00
{
state = DuckState::Swim;
2017-03-11 13:58:31 +00:00
frame = 0;
UpdateSwim();
2017-03-11 13:34:54 +00:00
}
else
{
2017-03-11 13:58:31 +00:00
Invalidate();
2017-03-11 13:34:54 +00:00
}
}
2020-01-19 15:50:01 +00:00
void Duck::UpdateDoubleDrink()
2017-03-11 13:34:54 +00:00
{
2017-03-11 13:58:31 +00:00
frame++;
if (kDuckAnimationDoubleDrink[frame] == 0xFF)
2017-03-11 13:34:54 +00:00
{
state = DuckState::Swim;
2017-03-11 13:58:31 +00:00
frame = 0;
UpdateSwim();
2017-03-11 13:34:54 +00:00
}
else
{
2017-03-11 13:58:31 +00:00
Invalidate();
2017-03-11 13:34:54 +00:00
}
}
2020-01-19 15:50:01 +00:00
void Duck::UpdateFlyAway()
2017-03-11 13:34:54 +00:00
{
if ((getGameState().currentTicks & 3) == 0)
2017-03-11 13:34:54 +00:00
{
2017-03-11 13:58:31 +00:00
frame++;
if (frame >= std::size(kDuckAnimationFlyAway))
2017-03-11 13:34:54 +00:00
{
2017-03-11 13:58:31 +00:00
frame = 0;
2017-03-11 13:34:54 +00:00
}
2017-03-11 13:58:31 +00:00
Invalidate();
int32_t direction = Orientation >> 3;
auto destination = CoordsXYZ{ x + (kDuckMoveOffset[direction].x * 2), y + (kDuckMoveOffset[direction].y * 2),
2020-01-13 21:03:57 -03:00
std::min<int32_t>(z + 2, 496) };
2022-10-12 06:35:20 +01:00
if (MapIsLocationValid(destination))
2017-03-11 13:34:54 +00:00
{
2020-01-13 21:03:57 -03:00
MoveTo(destination);
2017-03-11 13:34:54 +00:00
}
else
{
2017-03-11 13:58:31 +00:00
Remove();
2017-03-11 13:34:54 +00:00
}
}
}
2020-01-19 15:50:01 +00:00
uint32_t Duck::GetFrameImage(int32_t direction) const
2017-03-12 12:28:35 +00:00
{
uint32_t imageId = 0;
if (EnumValue(state) < kDuckMaxStates)
2017-03-12 12:28:35 +00:00
{
2017-05-15 02:28:43 -05:00
// TODO: Check frame is in range
uint8_t imageOffset = kDuckAnimations[EnumValue(state)][frame];
2017-03-12 12:28:35 +00:00
imageId = SPR_DUCK + (imageOffset * 4) + (direction / 8);
}
return imageId;
}
2021-05-12 07:56:32 +01:00
void Duck::Create(const CoordsXY& pos)
2017-03-11 13:34:54 +00:00
{
auto* duck = CreateEntity<Duck>();
if (duck == nullptr)
return;
CoordsXY targetPos = pos;
2023-01-17 20:46:55 +01:00
int32_t offsetXY = ScenarioRand() & 0x1E;
targetPos.x += offsetXY;
targetPos.y += offsetXY;
2023-04-07 19:47:26 +03:00
duck->SpriteData.Width = 9;
duck->SpriteData.HeightMin = 12;
duck->SpriteData.HeightMax = 9;
2020-08-01 11:29:54 +01:00
duck->target_x = targetPos.x;
duck->target_y = targetPos.y;
2023-01-17 20:46:55 +01:00
uint8_t direction = ScenarioRand() & 3;
switch (direction)
2017-03-11 13:34:54 +00:00
{
case 0:
2023-01-17 20:46:55 +01:00
targetPos.x = GetMapSizeMaxXY().x - (ScenarioRand() & 0x3F);
break;
case 1:
2023-01-17 20:46:55 +01:00
targetPos.y = ScenarioRand() & 0x3F;
break;
case 2:
2023-01-17 20:46:55 +01:00
targetPos.x = ScenarioRand() & 0x3F;
break;
case 3:
2023-01-17 20:46:55 +01:00
targetPos.y = GetMapSizeMaxXY().y - (ScenarioRand() & 0x3F);
break;
2017-03-12 12:28:35 +00:00
}
duck->Orientation = direction << 3;
2020-08-01 11:29:54 +01:00
duck->MoveTo({ targetPos.x, targetPos.y, 496 });
duck->state = Duck::DuckState::FlyToWater;
2020-08-01 11:29:54 +01:00
duck->frame = 0;
2017-03-11 13:34:54 +00:00
}
2018-02-01 18:49:14 +01:00
2020-07-10 14:16:03 +01:00
void Duck::Update()
2018-02-01 18:49:14 +01:00
{
switch (state)
2018-06-22 23:17:03 +02:00
{
case DuckState::FlyToWater:
2020-07-10 14:16:03 +01:00
UpdateFlyToWater();
2018-06-22 23:17:03 +02:00
break;
case DuckState::Swim:
2020-07-10 14:16:03 +01:00
UpdateSwim();
2018-06-22 23:17:03 +02:00
break;
case DuckState::Drink:
2020-07-10 14:16:03 +01:00
UpdateDrink();
2018-06-22 23:17:03 +02:00
break;
case DuckState::DoubleDrink:
2020-07-10 14:16:03 +01:00
UpdateDoubleDrink();
2018-06-22 23:17:03 +02:00
break;
case DuckState::FlyAway:
2020-07-10 14:16:03 +01:00
UpdateFlyAway();
2018-06-22 23:17:03 +02:00
break;
2018-02-01 18:49:14 +01:00
}
}
2021-05-12 07:56:32 +01:00
void Duck::Press()
2018-02-01 18:49:14 +01:00
{
2021-05-12 07:56:32 +01:00
OpenRCT2::Audio::Play3D(OpenRCT2::Audio::SoundId::Quack, { x, y, z });
2018-02-01 18:49:14 +01:00
}
2021-05-12 07:56:32 +01:00
void Duck::RemoveAll()
2018-02-01 18:49:14 +01:00
{
2021-03-16 11:18:06 +00:00
for (auto duck : EntityList<Duck>())
2018-02-01 18:49:14 +01:00
{
2020-06-09 20:45:17 +01:00
duck->Remove();
2018-02-01 18:49:14 +01:00
}
}
void Duck::Serialise(DataSerialiser& stream)
{
EntityBase::Serialise(stream);
stream << frame;
stream << target_x;
stream << target_y;
stream << state;
}
2021-11-27 15:20:17 +02:00
void Duck::Paint(PaintSession& session, int32_t imageDirection) const
2021-11-27 15:20:17 +02:00
{
2022-01-29 05:29:08 -08:00
PROFILED_FUNCTION();
RenderTarget& rt = session.DPI;
if (rt.zoom_level > ZoomLevel{ 1 })
2021-11-27 15:27:41 +02:00
return;
uint32_t imageId = GetFrameImage(imageDirection);
if (imageId != 0)
{
PaintAddImageAsParent(session, ImageId(imageId), { 0, 0, z }, { 1, 1, 0 });
2021-11-27 15:27:41 +02:00
}
2021-11-27 15:20:17 +02:00
}