2018-11-14 18:17:25 +01:00
|
|
|
#include "Dispatcher.hpp"
|
|
|
|
#include "Globals.hpp"
|
|
|
|
#include "Aircraft.hpp"
|
|
|
|
|
|
|
|
using namespace irr;
|
2018-11-15 15:37:22 +01:00
|
|
|
using namespace irr::core;
|
2018-11-14 18:17:25 +01:00
|
|
|
|
|
|
|
Dispatcher::Dispatcher()
|
|
|
|
: active(false)
|
2018-11-15 18:17:22 +01:00
|
|
|
, aircraftsRemaining(0)
|
|
|
|
, points(0)
|
2018-11-14 18:17:25 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Dispatcher::~Dispatcher()
|
|
|
|
{
|
|
|
|
aircrafts.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dispatcher::start()
|
|
|
|
{
|
2018-11-19 01:07:50 +01:00
|
|
|
lastDispatchMS = Globals::getDevice()->getTimer()->getRealTime() - 40000;
|
2018-11-14 18:17:25 +01:00
|
|
|
active = true;
|
2018-11-15 18:17:22 +01:00
|
|
|
aircraftsRemaining = 5;
|
|
|
|
points = 0;
|
2018-11-14 18:17:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Dispatcher::execute()
|
|
|
|
{
|
|
|
|
u32 curTime = Globals::getDevice()->getTimer()->getRealTime();
|
|
|
|
|
2018-11-15 18:17:22 +01:00
|
|
|
if (active && (curTime - lastDispatchMS) > 45000)
|
2018-11-14 18:17:25 +01:00
|
|
|
{
|
2018-11-15 18:17:22 +01:00
|
|
|
if (aircraftsRemaining > 0)
|
|
|
|
{
|
|
|
|
dispatchAircraft();
|
|
|
|
aircraftsRemaining--;
|
|
|
|
lastDispatchMS = curTime;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
2018-11-14 18:17:25 +01:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:37:22 +01:00
|
|
|
// loopdidoo & cleanup
|
2018-11-14 18:17:25 +01:00
|
|
|
auto a = aircrafts.begin();
|
|
|
|
while (a < aircrafts.end())
|
|
|
|
{
|
2018-11-15 15:37:22 +01:00
|
|
|
(*a)->update(curTime);
|
2018-11-14 18:17:25 +01:00
|
|
|
if ((*a)->isTerminated())
|
|
|
|
{
|
|
|
|
a = aircrafts.erase(a);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dispatcher::stop()
|
|
|
|
{
|
2018-11-15 18:17:22 +01:00
|
|
|
aircrafts.clear();
|
2018-11-14 18:17:25 +01:00
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Dispatcher::Aircrafts &Dispatcher::getAircrafts() const
|
|
|
|
{
|
|
|
|
return aircrafts;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Dispatcher::evalShot(const core::line3df& l)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
2021-08-05 16:00:19 +02:00
|
|
|
for (auto& a : aircrafts)
|
2018-11-14 18:17:25 +01:00
|
|
|
{
|
2018-11-15 18:17:22 +01:00
|
|
|
if (a->isGood() && a->evalShot(l))
|
2018-11-14 18:17:25 +01:00
|
|
|
{
|
2018-11-15 18:17:22 +01:00
|
|
|
points += 1000 + a->getPosition().getDistanceFrom(vector3df(0.f, 0.f, 0.f));
|
2018-11-14 18:17:25 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-11-15 15:37:22 +01:00
|
|
|
|
2018-11-15 18:17:22 +01:00
|
|
|
u32 Dispatcher::getAircraftsRemaining() const
|
|
|
|
{
|
|
|
|
return aircraftsRemaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 Dispatcher::getPoints() const
|
|
|
|
{
|
|
|
|
return points;
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:37:22 +01:00
|
|
|
void Dispatcher::dispatchAircraft()
|
|
|
|
{
|
|
|
|
IRandomizer *rand = Globals::getDevice()->getRandomizer();
|
|
|
|
// rand dir vector around 0/0
|
|
|
|
vector2df dVec(1.f, 0.f);
|
|
|
|
dVec.rotateBy(rand->frand() * 360.f, vector2df(0.f, 0.f));
|
|
|
|
dVec.normalize();
|
|
|
|
vector2df nVec(dVec.Y, -dVec.X);
|
|
|
|
f32 d = rand->frand() * 500.f;
|
|
|
|
f32 alt = 50.f + rand->frand() * 500.f;
|
|
|
|
|
|
|
|
// start/end 2km off our pos, displaced by d * nVec to the side
|
|
|
|
vector2df start = dVec * -2000.f + nVec * d;
|
|
|
|
vector2df end = dVec * 2000.f + nVec * d;
|
|
|
|
|
|
|
|
line3df l;
|
|
|
|
l.start = vector3df(start.X, alt, start.Y);
|
|
|
|
l.end = vector3df(end.X, alt, end.Y);
|
2021-08-05 16:00:19 +02:00
|
|
|
aircrafts.push_back(std::make_unique<Aircraft>(l, 30000 + static_cast<u32>(rand->frand() * 15000.f)));
|
2018-11-15 15:37:22 +01:00
|
|
|
}
|