1 
2 /* Battle Tanks Game
3  * Copyright (C) 2006-2009 Battle Tanks team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19 
20 /*
21  * Additional rights can be granted beyond the GNU General Public License
22  * on the terms provided in the Exception. If you modify this file,
23  * you may extend this exception to your version of the file,
24  * but you are not obligated to do so. If you do not wish to provide this
25  * exception without modification, you must delete this exception statement
26  * from your version and license this file solely under the GPL without exception.
27 */
28 
29 #include "old_school.h"
30 #include <set>
31 #include "world.h"
32 #include "object.h"
33 #include "mrt/random.h"
34 #include "tmx/map.h"
35 
OldSchool()36 ai::OldSchool::OldSchool() : trottle(0) {}
37 
on_spawn(const Object * object)38 void ai::OldSchool::on_spawn(const Object *object) {
39 	trottle = 0;
40 }
41 
calculateV(v2<float> & velocity,Object * object)42 void ai::OldSchool::calculateV(v2<float> &velocity, Object *object) {
43 	if (object->is_driven())
44 		return;
45 	velocity.clear();
46 	++trottle;
47 	if (trottle < 10) {
48 		return;
49 	} else {
50 		trottle = 0;
51 	}
52 
53 	//LOG_DEBUG(("[%d: %s]old school calculate", object->get_id(), object->animation.c_str()));
54 	int dirs = object->get_directions_number();
55 	int dirs0 = dirs;
56 
57 	int action = mrt::random(3);
58 	if (dirs == 1)
59 		dirs = 8;
60 
61 	if (action != 1 && dirs0 > 1) {
62 		int dir = mrt::random(dirs);
63 		object->set_direction(dir);
64 		velocity.clear();
65 	} else if (action == 1) {
66 		int dir = mrt::random(dirs);
67 		v2<int> pos;
68 		object->get_center_position(pos);
69 		v2<int> tile_size = Map->getPathTileSize();
70 
71 		const Matrix<int> &matrix = Map->get_impassability_matrix(object->get_z());
72 
73 		v2<float> delta;
74 		delta.fromDirection(dir, dirs);
75 
76 		v2<int> dpos = (delta * tile_size.convert<float>()).convert<int>();
77 		pos += dpos;
78 		v2<int> pos2 = pos + dpos;
79 
80 		pos /= tile_size;
81 		pos2 /= tile_size;
82 		if (matrix.get(pos.y, pos.x) != -1 && matrix.get(pos2.y, pos2.x) != -1) {
83 			Way way;
84 			way.push_back(pos2 * tile_size + tile_size / 2);
85 			object->set_way(way);
86 		}
87 	}
88 }
89 
90 
serialize(mrt::Serializator & s) const91 void ai::OldSchool::serialize(mrt::Serializator &s) const {
92 	s.add(trottle);
93 }
94 
deserialize(const mrt::Serializator & s)95 void ai::OldSchool::deserialize(const mrt::Serializator &s) {
96 	s.get(trottle);
97 }
98