File: ballistic_missile.cpp

package info (click to toggle)
btanks 0.9.8083-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 43,616 kB
  • sloc: cpp: 46,425; ansic: 12,005; xml: 4,262; python: 313; sh: 13; makefile: 13
file content (168 lines) | stat: -rw-r--r-- 4,259 bytes parent folder | download | duplicates (5)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "object.h"
#include "registrar.h"
#include "mrt/random.h"
#include "config.h"
#include "world.h"
#include "math/unary.h"
#include "tmx/map.h"
#include "ai/targets.h"

class BallisticMissile : public Object {
public: 
	virtual Object * clone() const { return new BallisticMissile(*this); }
	
	BallisticMissile() : Object("ballistic-missile"), _fall(false), _launch(false), _reaction(true) {
		set_directions_number(16);
		piercing = true;
	}
	
	void on_spawn() {
		play("main", true);
		float duration = 5.0f, launch_duration = 512.0f / speed;
		_launch.set(launch_duration);
		_fall.set(duration - launch_duration);
		
		float reaction = 0.05f;
		mrt::randomize(reaction, reaction / 10);
		_reaction.set(reaction);
		
		set_direction(4);
		_direction = _velocity = v2<float>(0, -1);

		Object *target = spawn("ballistic-missile-target", "target");
		target_id = target->get_id();
		speed_backup = speed;
	}
	
	virtual const bool skip_rendering() const {
		float l = _launch.get(), f = _fall.get();
		return l >= 1 && f < 1;
	}
	
	void calculate(const float dt) {
		bool react = _reaction.tick(dt), falling = _fall.tick(dt), launch = !_launch.tick(dt);
		//LOG_DEBUG(("launch: %c, falling: %c", launch?'+':'-', falling?'+':'-'));
		if (launch) {
			_velocity = v2<float>(0, -1);
		} else if (!falling) {
			v2<float> pos = get_position();
			if (react) {
				Object *target = World->getObjectByID(target_id);
				if (target == NULL) {
					Object::emit("death", NULL); //just hide 
					return;
				}
				speed = target->speed * 1.3f;
				_velocity = get_relative_position(target) + v2<float>(0, -512);
				//LOG_DEBUG(("correcting: %g", _velocity.x));
			}
		} else { //falling
			if (speed != speed_backup) {
				speed = speed_backup;
				Object *target = World->getObjectByID(target_id);
				ttl = ((target != NULL)?get_relative_position(target).length():512.0f) / speed;
				set_direction(12);
			}
			_velocity = v2<float>(0, 1);
			/*
			
			v2<float> pos = get_center_position(), tpos;

			if (target != NULL)			
				tpos = target->get_center_position();
			
			if (target == NULL || tpos.y <= 0) {
				tpos = pos + v2<float>(0, 50);
			}
			
			_velocity = Map->distance(pos, tpos);
			if (_velocity.y <= 0) {
				if (animation == "nuke-missile") {
					spawn("nuke-explosion", "nuke-explosion");
				}
			
				emit("death", NULL);
				if (target) 
					target->emit("death", NULL);
			} else {
				if (math::abs(_velocity.x) * 5 > _velocity.y)
					_velocity.x = _velocity.y / 5;
			}
			*/
		}
	}
	void emit(const std::string &event, Object * emitter) {
		if (event == "death") {
			Object *target = World->getObjectByID(target_id);
			if (target != NULL)
				target->emit("death", NULL);
			if (animation == "nuke-missile") {
				spawn("nuke-explosion", "nuke-explosion");
			}
		}
		Object::emit(event, emitter);
	}

	virtual void serialize(mrt::Serializator &s) const {
		Object::serialize(s);
		s.add(_fall);
		s.add(_launch);
		s.add(_reaction);
		s.add(speed_backup);
		s.add(target_id);
	}

	virtual void deserialize(const mrt::Serializator &s) {
		Object::deserialize(s);
		s.get(_fall);
		s.get(_launch);
		s.get(_reaction);
		s.get(speed_backup);
		s.get(target_id);
	}
private: 
	Alarm _fall, _launch, _reaction;
	float speed_backup;
	int target_id;
};

class BallisticMissileTarget : public Object {
public: 
	virtual Object * clone() const { return new BallisticMissileTarget(*this); }
	
	BallisticMissileTarget() : Object("mark"), _reaction(true) {
		set_directions_number(1);
	}
	
	void on_spawn() { 
		GET_CONFIG_VALUE("objects.target.reaction-time", float, rt, 0.2f);
		mrt::randomize(rt, rt / 10);
		_reaction.set(rt);

		play("main", true);
	}
	void calculate(const float dt) {
		if (!_reaction.tick(dt))
			return;
		v2<float> pos, vel;
		if (get_nearest(ai::Targets->troops, speed * 5.0f, pos, vel, false)) {
			_velocity = pos;
			return;
		}
	}

	virtual void serialize(mrt::Serializator &s) const {
		s.add(_reaction);
		Object::serialize(s);
	}

	virtual void deserialize(const mrt::Serializator &s) {
		s.get(_reaction);
		Object::deserialize(s);
	}
private: 
	Alarm _reaction;
};

REGISTER_OBJECT("ballistic-missile", BallisticMissile, ());
REGISTER_OBJECT("ballistic-missile-target", BallisticMissileTarget, ());