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 "config.h"
31 
32 class PoisonCloud : public Object {
33 public:
PoisonCloud()34 	PoisonCloud() : Object("poison"), _damage(true) { pierceable = true; }
clone() const35 	virtual Object * clone() const { return new PoisonCloud(*this); }
36 	virtual void on_spawn();
37 	virtual void tick(const float dt);
38 	virtual void emit(const std::string &event, Object * emitter = NULL);
39 
serialize(mrt::Serializator & s) const40 	virtual void serialize(mrt::Serializator &s) const {
41 		Object::serialize(s);
42 		s.add(_damaged_objects);
43 		s.add(_damage);
44 	}
45 
deserialize(const mrt::Serializator & s)46 	virtual void deserialize(const mrt::Serializator &s) {
47 		Object::deserialize(s);
48 		s.get(_damaged_objects);
49 		s.get(_damage);
50 	}
51 
52 private:
53 	std::set<int> _damaged_objects;
54 	Alarm _damage;
55 };
56 
tick(const float dt)57 void PoisonCloud::tick(const float dt) {
58 	Object::tick(dt);
59 	if (_damage.tick(dt)) {
60 		_damaged_objects.clear();
61 	}
62 }
63 
on_spawn()64 void PoisonCloud::on_spawn() {
65 	float di;
66 	Config->get("objects." + registered_name + ".damage-interval", di, 1);
67 	_damage.set(di);
68 
69 	if (registered_name.substr(0, 7) != "static-")
70 		play("start", false);
71 
72 	play("main", true);
73 	disown();
74 }
75 
emit(const std::string & event,Object * emitter)76 void PoisonCloud::emit(const std::string &event, Object * emitter) {
77 	if (event == "collision") {
78 		if (emitter == NULL)
79 			return;
80 
81 		const std::string &ec = emitter->classname;
82 		if (ec != "trooper" && ec != "civilian" &&
83 			ec != "kamikaze" && ec != "watchtower" &&
84 			ec != "monster" && ec != "cannon" &&
85 			emitter->registered_name != "machinegunner")
86 			return;
87 
88 		const int id = emitter->get_id();
89 		if (_damaged_objects.find(id) != _damaged_objects.end())
90 			return; //damage was already added for this object.
91 
92 		_damaged_objects.insert(id);
93 		if (emitter->get_variants().has("poison-resistant")) {
94 			return;
95 		}
96 		emitter->add_damage(this, max_hp);
97 	} else Object::emit(event, emitter);
98 }
99 
100 REGISTER_OBJECT("smoke-cloud", PoisonCloud, ());
101 REGISTER_OBJECT("static-smoke-cloud", PoisonCloud, ());
102