File: raider_heli.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 (159 lines) | stat: -rw-r--r-- 4,362 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

/* Battle Tanks Game
 * Copyright (C) 2006-2009 Battle Tanks team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/* 
 * Additional rights can be granted beyond the GNU General Public License 
 * on the terms provided in the Exception. If you modify this file, 
 * you may extend this exception to your version of the file, 
 * but you are not obligated to do so. If you do not wish to provide this
 * exception without modification, you must delete this exception statement
 * from your version and license this file solely under the GPL without exception. 
*/
#include "heli.h"
#include "config.h"
#include "registrar.h"
#include "player_manager.h"
#include "player_slot.h"

class RaiderHeli : public Heli {
public:
	virtual Object * clone() const { return new RaiderHeli(*this); }
	RaiderHeli() : Heli("helicopter"), _player(-1), _leaving(false), _toggle(true), _moving_time(0) {}
	
	virtual void on_spawn();
	void calculate(const float dt);
	virtual void serialize(mrt::Serializator &s) const {
		Heli::serialize(s);
		s.add(_player);
		s.add(_leaving);
		s.add(_toggle);
		s.add(_moving_time);
	}
	virtual void deserialize(const mrt::Serializator &s) {
		Heli::deserialize(s);
		s.get(_player);
		s.get(_leaving);
		s.get(_toggle);
		s.get(_moving_time);
	}

private: 
	int _player;
	Alarm _leaving, _toggle;
	float _moving_time;
};


void RaiderHeli::on_spawn() {
	Heli::on_spawn();
	_player = -1;
/*
*/
}

#include "tmx/map.h"

void RaiderHeli::calculate(const float dt) {
	if (_player == -1) {
	//deferred initialization
		int players = 0;
		int i, n = PlayerManager->get_slots_count();
		for(i = 0; i < n; ++i) {
			const PlayerSlot &slot = PlayerManager->get_slot(i);
			if (slot.empty())
				continue;
			++players;
		}

		if (players == 0) {
			LOG_DEBUG(("no players... "));
			emit("death", NULL);
			return;
		}
		LOG_DEBUG(("setting up %d players", players));
		float attack = 20.0f;
		//Config->get("objects." + registered_name + ".attack-duration", attack, 30.0f);
		_toggle.set(attack);
		_leaving.set(attack * players * 2);
		_player = 0;
	}

	if (!_variants.has("no-escape") && _leaving.tick(dt)) {
		v2<int> pos, map_size = Map->get_size();
		get_position(pos);
		pos += (size * 0.9).convert<int>();
		if (pos.x > map_size.x || pos.y > map_size.y) {
			LOG_DEBUG(("escaped"));
			Object::emit("death", NULL);
		}
		_velocity = v2<float>(4, 3);
		
		goto done;
	}

	{	
	//main ai
		PlayerSlot &slot = PlayerManager->get_slot(_player);
		Object *player = slot.getObject();
		if (player == NULL || _toggle.tick(dt)) {
			(++_player) %= PlayerManager->get_slots_count();
			//LOG_DEBUG(("changing player to %d", _player));
			return;
		}
		//LOG_DEBUG(("attacking player %d", _player));
		
		v2<float> pos, vel;
		pos = get_relative_position(player);
		player->get_velocity(vel);
		vel.normalize();
		
		float est = pos.length() / speed;
		
		v2<float> dir = pos  + speed * vel * est - v2<float>(0, 128); //bomb correction
		if (dir.length() > 64) {
			dir.normalize();
			_velocity.normalize();
			_velocity = _velocity * 2 + dir * 3;
			_velocity.normalize();
		} else {
			_velocity = _direction;
			//LOG_DEBUG(("idle velocity %g %g", _velocity.x, _velocity.y));
		}
	}

done: 
	//common part
	GET_CONFIG_VALUE("engine.mass-acceleration-divisor", float, ac_div, 1000.0f);
	if (!_velocity.is0()) {
		_moving_time += dt;
	} else {
		_moving_time = 0;
	}

	const float ac_t = mass / ac_div * 0.8;
	_state.alt_fire = _moving_time >= ac_t;

	calculate_way_velocity();

	GET_CONFIG_VALUE("objects.helicopter.rotation-time", float, rt, 0.2f);
	limit_rotation(dt, rt, false, false);	
	update_state_from_velocity();
}

REGISTER_OBJECT("raider-helicopter", RaiderHeli, ());