Files
OpenRCT2-Web/test/tests/PlayTests.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

213 lines
7.3 KiB
C++
Raw Normal View History

2020-04-12 11:00:02 -03:00
/*****************************************************************************
* Copyright (c) 2014-2026 OpenRCT2 developers
2020-04-12 11:00:02 -03:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "TestData.h"
#include <gtest/gtest.h>
2022-07-29 18:45:10 +02:00
#include <memory>
2020-04-12 11:00:02 -03:00
#include <openrct2/Context.h>
#include <openrct2/Game.h>
#include <openrct2/GameState.h>
#include <openrct2/OpenRCT2.h>
#include <openrct2/ParkImporter.h>
2026-02-09 15:07:42 +01:00
#include <openrct2/actions/GameActionRunner.h>
#include <openrct2/actions/park/ParkMarketingAction.h>
#include <openrct2/actions/park/ParkSetEntranceFeeAction.h>
#include <openrct2/actions/park/ParkSetParameterAction.h>
#include <openrct2/actions/ride/RideSetPriceAction.h>
#include <openrct2/actions/ride/RideSetStatusAction.h>
2026-01-05 20:09:34 +01:00
#include <openrct2/drawing/Drawing.h>
2021-11-24 16:58:01 +02:00
#include <openrct2/entity/EntityRegistry.h>
2021-11-24 16:48:33 +02:00
#include <openrct2/entity/EntityTweener.h>
2021-11-25 23:47:24 +02:00
#include <openrct2/entity/Peep.h>
2020-04-12 11:00:02 -03:00
#include <openrct2/object/ObjectManager.h>
#include <openrct2/ride/Ride.h>
#include <openrct2/ride/RideManager.hpp>
2020-04-12 11:00:02 -03:00
#include <openrct2/world/MapAnimation.h>
#include <openrct2/world/Park.h>
#include <string>
using namespace OpenRCT2;
class PlayTests : public testing::Test
{
};
static std::unique_ptr<IContext> localStartGame(const std::string& parkPath)
{
gOpenRCT2Headless = true;
gOpenRCT2NoGraphics = true;
auto context = CreateContext();
if (!context->Initialise())
return {};
auto importer = ParkImporter::CreateS6(context->GetObjectRepository());
auto loadResult = importer->LoadSavedGame(parkPath.c_str(), false);
2021-09-13 19:24:42 +01:00
context->GetObjectManager().LoadObjects(loadResult.RequiredObjects);
MapAnimations::ClearAll();
// TODO: Have a separate GameState and exchange once loaded.
auto& gameState = getGameState();
importer->Import(gameState);
2020-04-12 11:00:02 -03:00
gameState.entities.resetEntitySpatialIndices();
2020-04-12 11:00:02 -03:00
ResetAllSpriteQuadrantPlacements();
LoadPalette();
EntityTweener::Get().Reset();
MapAnimations::MarkAllTiles();
FixInvalidVehicleSpriteSizes();
2020-04-12 11:00:02 -03:00
gGameSpeed = 1;
return context;
}
template<class Fn>
static bool updateUntil(int maxSteps, Fn&& fn)
2020-04-12 11:00:02 -03:00
{
while (maxSteps-- && !fn())
{
2024-03-26 15:08:17 +01:00
gameStateUpdateLogic();
2020-04-12 11:00:02 -03:00
}
return maxSteps > 0;
}
template<class GA, class... Args>
static void execute(Args&&... args)
2020-04-12 11:00:02 -03:00
{
GA ga(std::forward<Args>(args)...);
GameActions::Execute(&ga, getGameState());
2020-04-12 11:00:02 -03:00
}
TEST_F(PlayTests, NegativeMarketingCampaignDurationIsRejected)
{
auto context = localStartGame(TestData::GetParkPath("small_park_with_ferris_wheel.sv6"));
ASSERT_NE(context.get(), nullptr);
GameActions::ParkMarketingAction action(ADVERTISING_CAMPAIGN_PARK, 0, -1);
const auto result = GameActions::Query(&action, getGameState());
ASSERT_EQ(result.error, GameActions::Status::invalidParameters);
}
2020-04-12 11:00:02 -03:00
TEST_F(PlayTests, SecondGuestInQueueShouldNotRideIfNoFunds)
{
/* This test verifies that a guest, when second in queue, won't be forced to enter
* the ride if it has not enough money to pay for it.
* To simulate this scenario, two guests (a rich and a poor) are encouraged to enter
* the ride queue, and then the price is raised such that the second guest in line
* (the poor one) cannot pay. The poor guest should not enter the ride.
*/
std::string initStateFile = TestData::GetParkPath("small_park_with_ferris_wheel.sv6");
auto context = localStartGame(initStateFile);
ASSERT_NE(context.get(), nullptr);
auto& gameState = getGameState();
2020-04-12 11:00:02 -03:00
// Open park for free but charging for rides
execute<GameActions::ParkSetParameterAction>(GameActions::ParkParameter::open);
execute<GameActions::ParkSetEntranceFeeAction>(0);
gameState.park.flags.set(ParkFlag::unlockAllPrices);
2020-04-12 11:00:02 -03:00
// Find ferris wheel
auto rideManager = RideManager(gameState);
2020-04-12 11:00:02 -03:00
auto it = std::find_if(
rideManager.begin(), rideManager.end(), [](auto& ride) { return ride.type == RIDE_TYPE_FERRIS_WHEEL; });
ASSERT_NE(it, rideManager.end());
Ride& ferrisWheel = *it;
// Open it for free
2025-03-06 22:34:48 +01:00
execute<GameActions::RideSetStatusAction>(ferrisWheel.id, RideStatus::open);
2020-04-12 11:00:02 -03:00
execute<GameActions::RideSetPriceAction>(ferrisWheel.id, 0, true);
2021-01-10 18:23:35 +00:00
// Ignore intensity to stimulate peeps to queue into ferris wheel
gameState.cheats.ignoreRideIntensity = true;
2020-04-12 11:00:02 -03:00
// Insert a rich guest
auto richGuest = Park::GenerateGuest();
2026-05-30 17:21:37 +03:00
richGuest->cashInPocket = 3000;
2020-04-12 11:00:02 -03:00
// Wait for rich guest to get in queue
2026-08-17 23:42:30 +02:00
bool matched = updateUntil(1000, [&]() { return richGuest->state == PeepState::queuing; });
2020-04-12 11:00:02 -03:00
ASSERT_TRUE(matched);
// Insert poor guest
auto poorGuest = Park::GenerateGuest();
2026-05-30 17:21:37 +03:00
poorGuest->cashInPocket = 5;
2020-04-12 11:00:02 -03:00
// Wait for poor guest to get in queue
2026-08-17 23:42:30 +02:00
matched = updateUntil(1000, [&]() { return poorGuest->state == PeepState::queuing; });
2020-04-12 11:00:02 -03:00
ASSERT_TRUE(matched);
// Raise the price of the ride to a value poor guest can't pay
execute<GameActions::RideSetPriceAction>(ferrisWheel.id, 10, true);
// Verify that the poor guest goes back to walking without riding
// since it doesn't have enough money to pay for it
bool enteredTheRide = false;
2024-03-26 15:08:17 +01:00
matched = updateUntil(10000, [&]() {
2026-08-17 23:42:30 +02:00
enteredTheRide |= poorGuest->state == PeepState::onRide;
return poorGuest->state == PeepState::walking || enteredTheRide;
2020-04-12 11:00:02 -03:00
});
ASSERT_TRUE(matched);
ASSERT_FALSE(enteredTheRide);
}
TEST_F(PlayTests, CarRideWithOneCarOnlyAcceptsTwoGuests)
{
// This test verifies that a car ride with one car will accept at most two guests
std::string initStateFile = TestData::GetParkPath("small_park_car_ride_one_car.sv6");
auto context = localStartGame(initStateFile);
ASSERT_NE(context.get(), nullptr);
auto& gameState = getGameState();
2020-04-12 11:00:02 -03:00
// Open park for free but charging for rides
execute<GameActions::ParkSetParameterAction>(GameActions::ParkParameter::open);
execute<GameActions::ParkSetEntranceFeeAction>(0);
gameState.park.flags.set(ParkFlag::unlockAllPrices);
2020-04-12 11:00:02 -03:00
// Find car ride
auto rideManager = RideManager(gameState);
2020-04-12 11:00:02 -03:00
auto it = std::find_if(rideManager.begin(), rideManager.end(), [](auto& ride) { return ride.type == RIDE_TYPE_CAR_RIDE; });
ASSERT_NE(it, rideManager.end());
Ride& carRide = *it;
// Open it for free
2025-03-06 22:34:48 +01:00
execute<GameActions::RideSetStatusAction>(carRide.id, RideStatus::open);
2020-04-12 11:00:02 -03:00
execute<GameActions::RideSetPriceAction>(carRide.id, 0, true);
2021-01-10 18:23:35 +00:00
// Ignore intensity to stimulate peeps to queue into the ride
gameState.cheats.ignoreRideIntensity = true;
2020-04-12 11:00:02 -03:00
// Create some guests
std::vector<Peep*> guests;
for (int i = 0; i < 25; i++)
{
guests.push_back(Park::GenerateGuest());
2020-04-12 11:00:02 -03:00
}
// Wait until one of them is riding
2026-08-17 23:42:30 +02:00
auto guestIsOnRide = [](auto* g) { return g->state == PeepState::onRide; };
2024-03-26 15:08:17 +01:00
bool matched = updateUntil(10000, [&]() { return std::any_of(guests.begin(), guests.end(), guestIsOnRide); });
2020-04-12 11:00:02 -03:00
ASSERT_TRUE(matched);
// For the next few ticks at most two guests can be on the ride
for (int i = 0; i < 100; i++)
{
int numRiding = std::count_if(guests.begin(), guests.end(), guestIsOnRide);
ASSERT_LE(numRiding, 2);
2024-03-26 15:08:17 +01:00
gameStateUpdateLogic();
2020-04-12 11:00:02 -03:00
}
}