1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include "ai/trooper.h"
#include "config.h"
#include "mrt/random.h"
#include "mrt/serializator.h"
#include "object.h"
using namespace ai;
StupidTrooper::StupidTrooper(const std::string &object, const std::set<std::string> &targets) :
_object(object), _reaction(true), _target_dir(-1), _targets(targets) {}
void StupidTrooper::on_spawn() {
GET_CONFIG_VALUE("objects.ai-trooper.reaction-time", float, rt, 0.15f);
mrt::randomize(rt, rt / 10);
//LOG_DEBUG(("rt = %g", rt));
_reaction.set(rt);
}
void StupidTrooper::serialize(mrt::Serializator &s) const {
s.add(_object);
s.add(_reaction);
s.add(_target_dir);
}
void StupidTrooper::deserialize(const mrt::Serializator &s) {
s.get(_object);
s.get(_reaction);
s.get(_target_dir);
}
StupidTrooper::~StupidTrooper() {}
void StupidTrooper::calculate(Object *object, PlayerState &_state, v2<float> &_velocity, v2<float> &_direction, const float dt) {
int dirs = object->get_directions_number();
if (!_reaction.tick(dt)) {
return;
}
float range = object->getWeaponRange(_object);
_target_dir = object->get_target_position(_velocity, _targets, range);
if (_target_dir >= 0) {
//LOG_DEBUG(("target: %g %g %g", tp.x, tp.y, tp.length()));
/*
Way way;
if (find_path(tp, way)) {
set_way(way);
calculate_way_velocity();
}
*/
if (_velocity.length() >= 9) {
object->quantize_velocity();
_direction.fromDirection(object->get_direction(), dirs);
_state.fire = false;
} else {
_velocity.clear();
object->set_direction(_target_dir);
//LOG_DEBUG(("%d", _target_dir));
_direction.fromDirection(_target_dir, dirs);
_state.fire = true;
}
} else {
_velocity.clear();
_target_dir = -1;
onIdle();
_state.fire = false;
}
}
|