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

381 lines
9.1 KiB
C++
Raw Normal View History

2017-03-11 13:34:54 +00:00
/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 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 "../audio/audio.h"
#include "../core/DataSerialiser.h"
2018-01-06 18:32:25 +01:00
#include "../localisation/Date.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"
2018-01-02 17:58:43 +00:00
#include "../scenario/Scenario.h"
2018-06-22 23:17:03 +02:00
#include "../sprites.h"
2018-05-01 16:33:16 +02:00
#include "../world/Surface.h"
2021-11-24 17:19:52 +02:00
#include "EntityRegistry.h"
2017-03-11 13:34:54 +00:00
2018-08-12 12:50:40 +01:00
#include <algorithm>
2018-11-21 23:16:04 +01:00
#include <iterator>
2018-06-22 23:17:03 +02:00
#include <limits>
constexpr const int32_t DUCK_MAX_STATES = 5;
2017-03-11 13:34:54 +00:00
// clang-format off
2019-12-12 09:58:27 +00:00
static constexpr const CoordsXY DuckMoveOffset[] =
2017-03-11 13:34:54 +00:00
{
{ -1, 0 },
{ 0, 1 },
{ 1, 0 },
{ 0, -1 },
};
static constexpr const uint8_t DuckAnimationFlyToWater[] =
2017-03-11 13:34:54 +00:00
{
8, 9, 10, 11, 12, 13
};
static constexpr const uint8_t DuckAnimationSwim[] =
2017-03-11 13:34:54 +00:00
{
0
};
static constexpr const uint8_t DuckAnimationDrink[] =
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 const uint8_t DuckAnimationDoubleDrink[] =
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 const uint8_t DuckAnimationFlyAway[] =
2017-03-11 13:34:54 +00:00
{
8, 9, 10, 11, 12, 13
};
static constexpr const uint8_t * DuckAnimations[] =
2017-03-11 13:34:54 +00:00
{
DuckAnimationFlyToWater, // FLY_TO_WATER
DuckAnimationSwim, // SWIM
DuckAnimationDrink, // DRINK
DuckAnimationDoubleDrink, // DOUBLE_DRINK
DuckAnimationFlyAway, // FLY_AWAY
};
// clang-format on
2017-03-11 13:34:54 +00:00
2021-08-25 17:42:09 +03:00
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
{
2018-06-22 23:17:03 +02:00
if ((gCurrentTicks & 3) != 0)
return;
2017-03-11 13:34:54 +00:00
2017-03-11 13:58:31 +00:00
frame++;
2018-11-21 23:16:04 +01:00
if (frame >= std::size(DuckAnimationFlyToWater))
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 = sprite_direction >> 3;
2020-01-13 21:03:57 -03:00
auto destination = CoordsXYZ{ CoordsXY{ x, y } + DuckMoveOffset[direction], 0 };
int32_t manhattanDistanceN = abs(target_x - destination.x) + abs(target_y - destination.y);
2017-03-11 13:34:54 +00:00
auto surfaceElement = map_get_surface_element_at(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
{
2018-06-22 23:17:03 +02:00
if (((gCurrentTicks + sprite_index) & 3) != 0)
return;
2017-03-11 13:34:54 +00:00
uint32_t randomNumber = scenario_rand();
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 = date_get_month(gDateMonthsElapsed);
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();
int16_t landZ = tile_element_height({ x, y });
int16_t waterZ = tile_element_water_height({ 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;
2017-03-11 13:34:54 +00:00
randomNumber = scenario_rand();
if ((randomNumber & 0xFFFF) <= 0xAAA)
{
randomNumber >>= 16;
2017-03-11 13:58:31 +00:00
sprite_direction = randomNumber & 0x18;
2017-03-11 13:34:54 +00:00
}
int32_t direction = sprite_direction >> 3;
2020-01-13 21:03:57 -03:00
auto destination = CoordsXYZ{ CoordsXY{ x, y } + DuckMoveOffset[direction], 0 };
landZ = tile_element_height(destination);
waterZ = tile_element_water_height(destination);
2017-03-11 13:34:54 +00:00
2017-03-11 13:58:31 +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 (DuckAnimationDrink[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 (DuckAnimationDoubleDrink[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 ((gCurrentTicks & 3) == 0)
{
2017-03-11 13:58:31 +00:00
frame++;
2018-11-21 23:16:04 +01:00
if (frame >= std::size(DuckAnimationFlyAway))
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 = sprite_direction >> 3;
2020-01-13 21:03:57 -03:00
auto destination = CoordsXYZ{ x + (DuckMoveOffset[direction].x * 2), y + (DuckMoveOffset[direction].y * 2),
std::min<int32_t>(z + 2, 496) };
if (map_is_location_valid(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) < DUCK_MAX_STATES)
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 = DuckAnimations[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;
int32_t offsetXY = scenario_rand() & 0x1E;
targetPos.x += offsetXY;
targetPos.y += offsetXY;
2020-08-01 11:29:54 +01:00
duck->sprite_width = 9;
duck->sprite_height_negative = 12;
duck->sprite_height_positive = 9;
duck->target_x = targetPos.x;
duck->target_y = targetPos.y;
uint8_t direction = scenario_rand() & 3;
switch (direction)
2017-03-11 13:34:54 +00:00
{
case 0:
targetPos.x = 8191 - (scenario_rand() & 0x3F);
break;
case 1:
targetPos.y = scenario_rand() & 0x3F;
break;
case 2:
targetPos.x = scenario_rand() & 0x3F;
break;
case 3:
targetPos.y = 8191 - (scenario_rand() & 0x3F);
break;
2017-03-12 12:28:35 +00:00
}
2020-08-01 11:29:54 +01:00
duck->sprite_direction = direction << 3;
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
2021-12-19 15:48:00 +01:00
void Duck::Paint(paint_session& session, int32_t imageDirection) const
2021-11-27 15:20:17 +02:00
{
2022-01-29 05:29:08 -08:00
PROFILED_FUNCTION();
2021-12-19 15:48:00 +01:00
rct_drawpixelinfo& dpi = session.DPI;
2021-09-29 20:04:56 +02:00
if (dpi.zoom_level > ZoomLevel{ 1 })
2021-11-27 15:27:41 +02:00
return;
uint32_t imageId = GetFrameImage(imageDirection);
if (imageId != 0)
{
PaintAddImageAsParent(session, imageId, { 0, 0, z }, { 1, 1, 0 });
}
2021-11-27 15:20:17 +02:00
}