1 /* Battle Tanks Game
2  * Copyright (C) 2006-2009 Battle Tanks team
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18 
19 /*
20  * Additional rights can be granted beyond the GNU General Public License
21  * on the terms provided in the Exception. If you modify this file,
22  * you may extend this exception to your version of the file,
23  * but you are not obligated to do so. If you do not wish to provide this
24  * exception without modification, you must delete this exception statement
25  * from your version and license this file solely under the GPL without exception.
26 */
27 
28 #include "object.h"
29 #include "registrar.h"
30 #include "team.h"
31 #include "player_manager.h"
32 #include "world.h"
33 
34 class CTFFlag : public Object {
35 public:
tick(const float dt)36 	void tick(const float dt) {
37 		Object::tick(dt);
38 	}
39 
emit(const std::string & event,Object * emitter)40 	void emit(const std::string &event, Object * emitter) {
41 		if (event == "collision") {
42 			//add flag handling here.
43 			if (emitter == NULL || !emitter->get_variants().has("player")) {
44 				return;
45 			}
46 
47 			Team::ID team = Team::get_team(this);
48 			assert(team != Team::None);
49 
50 			PlayerSlot *slot = PlayerManager->get_slot_by_id(emitter->get_id());
51 			if (slot == NULL) {
52 				return;
53 			}
54 
55 			int base_id = get_summoner();
56 			Object *base = World->getObjectByID(base_id);
57 			if (slot->team == team) {
58 				if (base != NULL) {
59 					v2<float> dpos = get_relative_position(base);
60 					if (dpos.quick_length() > size.x * size.y / 4) {
61 						set_zbox(base->get_z());
62 						World->teleport(this, base->get_center_position());
63 						base->remove_effect("abandoned");
64 					} else {
65 						//my flag is close to the base. if i have foreign flag, frag it
66 						if (emitter->has("#ctf-flag")) {
67 							Object *flag = emitter->drop("#ctf-flag");
68 							++slot->frags;
69 							PlayerManager->action(*slot, "ctf");
70 							Object *base = World->getObjectByID(flag->get_summoner());
71 							if (base != NULL) {
72 								set_zbox(base->get_z());
73 								World->teleport(flag, base->get_center_position());
74 								base->remove_effect("abandoned");
75 							} else {
76 								LOG_WARN(("could not find base for the flag %s", flag->animation.c_str()));
77 							}
78 						}
79 					}
80 				} else {
81 					LOG_WARN(("could not find base %d", base_id));
82 				}
83 			} else {
84 				if (base != NULL)
85 					base->add_effect("abandoned", -1);
86 				if (!emitter->has("#ctf-flag"))  //just for the future expansion
87 					emitter->pick("#ctf-flag", this);
88 			}
89 		} else Object::emit(event, emitter);
90 	}
91 
serialize(mrt::Serializator & s) const92 	virtual void serialize(mrt::Serializator &s) const {
93 		Object::serialize(s);
94 	}
95 
deserialize(const mrt::Serializator & s)96 	virtual void deserialize(const mrt::Serializator &s) {
97 		Object::deserialize(s);
98 	}
99 
CTFFlag()100 	CTFFlag() : Object("ctf-flag") {
101 		impassability = -1;
102 		hp = -1;
103 		set_directions_number(1);
104 		pierceable = true;
105 	}
106 
clone() const107 	virtual Object * clone() const { return new CTFFlag(*this); }
108 
on_spawn()109 	void on_spawn() {
110 		play("main", true);
111 	}
112 
113 private:
114 };
115 
116 REGISTER_OBJECT("ctf-flag", CTFFlag, ());
117