Commit 54c8bd4b authored by louiz’'s avatar louiz’

Implement utils::ticks as an std::duration

fix #3034
parent 7f609494
......@@ -3,9 +3,6 @@
namespace utils
{
// The duration of a world tick, in microseconds
constexpr Duration tick_duration{15000us};
Time now()
{
return std::chrono::steady_clock::now();
......
......@@ -3,8 +3,6 @@
#include <chrono>
using namespace std::chrono_literals;
namespace utils
{
using namespace std::chrono_literals;
......@@ -13,11 +11,21 @@ namespace utils
// go backward during the game
using Time = std::chrono::time_point<std::chrono::steady_clock>;
// A duration is expressed in microseconds.
// A duration is expressed in nano.
using Duration = std::chrono::steady_clock::duration;
using FloatingSeconds = decltype(0.1s);
// This means that there are 50 ticks per seconds. We can use
// std::duration_cast to convert from std::chrono::seconds to utils::ticks
// for example. This way we can simply express a duration in seconds in
// the abilities/entities description, and convert that into a number of
// ticks in the lower-level code
using ticks = std::chrono::duration<int, std::ratio<1, 50>>;
// The duration of a world tick, in microseconds
constexpr Duration tick_duration = std::chrono::duration_cast<Duration>(ticks(1));
/**
* Returns the current time, from a steady clock
*/
......@@ -27,12 +35,12 @@ namespace utils
* Takes a duration, makes it “% tick_duration” and returns the number of
* tick_duration it contains.
* i.e. if tick_duration is 100 and the given duration is 245us, the
* e.g. if tick_duration is 100us and the given duration is 245us, the
* duration becomes 45 and the function returns 2: because the given
* duration contains 2 whole ticks, and we keep the remaining, to be
* consumed later on.
*/
unsigned long get_number_of_ticks(std::chrono::steady_clock::duration& duration);
unsigned long get_number_of_ticks(Duration& duration);
/**
* Convert a duration into a (float point) seconds.
......
......@@ -30,7 +30,7 @@ void Dash::cast(Entity* entity, World* world, const Position& pos, const bool qu
if (concentrate == nullptr)
{
// If none is found, that's weird, but do nothing
log_debug("Work is not a ConcentrateWork, doing nothing.");
log_warning("Work is not a ConcentrateWork, doing nothing.");
return;
}
// Otherwise, use its value for the DashWork
......@@ -57,7 +57,7 @@ void Dash::cast(Entity* entity, World* world, const Position& pos, const bool qu
world->callbacks->impact(entity, impacted_entity);
health->add(-concentrate_value);
};
auto work = std::make_unique<DashWork>(entity, world, pos, 35, this->max_distance, 50,
auto work = std::make_unique<DashWork>(entity, world, pos, 50, this->max_distance, 50,
on_impact, nullptr);
entity->set_work(std::move(work));
}
......
......@@ -34,15 +34,15 @@ std::unique_ptr<Entity> EntityFactory::make_entity(const EntityType type)
entity->add_component(std::make_unique<Vision>(700, false));
entity->add_component(std::make_unique<Location>(20, true));
entity->add_component(std::make_unique<Team>());
entity->add_component(std::make_unique<Mobility>(2.2_fix));
entity->add_component(std::make_unique<Mobility>(3.8_fix));
entity->add_component(std::make_unique<Acquisition>(200_fix));
auto abilities = std::make_unique<Abilities>(6u, 0u, 0u);
abilities->add(0, std::make_unique<Attack>(20u, 20u));
abilities->add(0, std::make_unique<Attack>(5u, 2u));
abilities->add(1, std::make_unique<Blink>());
abilities->add(2, std::make_unique<Phase>());
abilities->add(3, std::make_unique<Emp>());
abilities->add(4, std::make_unique<Dash>(400, 50));
abilities->add(4, std::make_unique<Dash>(500, 50));
abilities->add(5, std::make_unique<Concentrate>());
entity->add_component(std::move(abilities));
......@@ -50,7 +50,7 @@ std::unique_ptr<Entity> EntityFactory::make_entity(const EntityType type)
else if (type == 1)
{ // Some test projectile
entity->add_component(std::make_unique<Location>(2, false));
entity->add_component(std::make_unique<Mobility>(9));
entity->add_component(std::make_unique<Mobility>(11));
entity->add_component(std::make_unique<Team>());
}
else if (type == 2)
......
......@@ -81,7 +81,7 @@ TEST(Time, BasicTick)
utils::Duration dt = 1s;
auto ticks = utils::get_number_of_ticks(dt);
ASSERT_EQ(ticks, 100);
ASSERT_EQ(ticks, 50);
ASSERT_EQ(dt, 0s);
}
......@@ -89,9 +89,9 @@ TEST(Time, BasicTick2)
{
using namespace std::chrono_literals;
utils::Duration dt = 380000us;
utils::Duration dt = 500000us;
auto ticks = utils::get_number_of_ticks(dt);
ASSERT_EQ(ticks, 38);
ASSERT_EQ(ticks, 25);
ASSERT_EQ(dt, 0s);
}
......@@ -99,10 +99,10 @@ TEST(Time, Ticks)
{
using namespace std::chrono_literals;
utils::Duration dt = 385600us;
utils::Duration dt = 420000us;
auto ticks = utils::get_number_of_ticks(dt);
ASSERT_EQ(ticks, 38);
ASSERT_EQ(dt, 5600us);
ASSERT_EQ(ticks, 21);
ASSERT_EQ(dt, 0us);
}
TEST(Time, ConvertToFloatingSeconds)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment