1 #ifndef LEVEL_OBJECT_HPP_INCLUDED
2 #define LEVEL_OBJECT_HPP_INCLUDED
3 
4 #include <boost/shared_ptr.hpp>
5 #include <vector>
6 
7 #include "color_utils.hpp"
8 #include "draw_tile.hpp"
9 #include "formula_callable.hpp"
10 #include "raster.hpp"
11 #include "texture.hpp"
12 #include "variant.hpp"
13 
14 class level_object;
15 typedef boost::intrusive_ptr<level_object> level_object_ptr;
16 typedef boost::intrusive_ptr<const level_object> const_level_object_ptr;
17 
18 struct level_tile {
level_tilelevel_tile19 	level_tile() : object(NULL) {}
20 	bool is_solid(int x, int y) const;
21 	int x, y;
22 	int layer_from; //which zorder layer causes this tile to be built?
23 	int zorder;
24 	const level_object* object;
25 	bool face_right;
26 	bool draw_disabled;
27 };
28 
29 struct level_tile_zorder_comparer {
operator ()level_tile_zorder_comparer30 	bool operator()(const level_tile& a, const level_tile& b) const {
31 		return a.zorder < b.zorder;
32 	}
33 
operator ()level_tile_zorder_comparer34 	bool operator()(const level_tile& a, int b) const {
35 		return a.zorder < b;
36 	}
37 
operator ()level_tile_zorder_comparer38 	bool operator()(int a, const level_tile& b) const {
39 		return a < b.zorder;
40 	}
41 };
42 
43 struct level_tile_pos_comparer {
operator ()level_tile_pos_comparer44 	bool operator()(const level_tile& a, const level_tile& b) const {
45 		return a.y < b.y || a.y == b.y && a.x < b.x;
46 	}
47 
operator ()level_tile_pos_comparer48 	bool operator()(const level_tile& a, const std::pair<int, int>& b) const {
49 		return a.y < b.second || a.y == b.second && a.x < b.first;
50 	}
51 
operator ()level_tile_pos_comparer52 	bool operator()(const std::pair<int, int>& a, const level_tile& b) const {
53 		return a.second < b.y || a.second == b.y && a.first < b.x;
54 	}
55 };
56 
57 struct level_tile_zorder_pos_comparer {
operator ()level_tile_zorder_pos_comparer58 	bool operator()(const level_tile& a, const level_tile& b) const {
59 		return a.zorder < b.zorder || a.zorder == b.zorder && a.y < b.y || a.zorder == b.zorder && a.y == b.y && a.x < b.x;
60 	}
61 };
62 
63 struct level_tile_y_pos_comparer {
operator ()level_tile_y_pos_comparer64         bool operator()(const level_tile& a, int b) const {
65                 return a.y < b;
66         }
67 
operator ()level_tile_y_pos_comparer68         bool operator()(int a, const level_tile& b) const {
69                 return a < b.y;
70         }
71 
operator ()level_tile_y_pos_comparer72         bool operator()(const level_tile& a, const level_tile& b) const {
73                 return a.y < b.y;
74         }
75 };
76 
77 //utility which sets the palette for objects loaded within a scope
78 struct palette_scope {
79 	explicit palette_scope(const std::vector<std::string>& v);
80 	~palette_scope();
81 
82 	unsigned int original_value;
83 };
84 
85 class level_object : public game_logic::formula_callable {
86 public:
87 	static std::vector<const_level_object_ptr> all();
88 	static level_tile build_tile(variant node);
89 	static void write_compiled();
90 
91 	static void set_current_palette(unsigned int palette);
92 	explicit level_object(variant node, const char* id=NULL);
93 	~level_object();
94 
95 	int width() const;
96 	int height() const;
is_passthrough() const97 	bool is_passthrough() const { return passthrough_; }
98 	bool is_solid(int x, int y) const;
flipped() const99 	bool flipped() const { return flip_; }
has_solid() const100 	bool has_solid() const { return !solid_.empty(); }
all_solid() const101 	bool all_solid() const { return all_solid_; }
id() const102 	const std::string& id() const { return id_; }
info() const103 	const std::string& info() const { return info_; }
friction() const104 	int friction() const { return friction_; }
traction() const105 	int traction() const { return traction_; }
damage() const106 	int damage() const { return damage_; }
texture() const107 	const graphics::texture& texture() const { return t_; }
108 	static void queue_draw(graphics::blit_queue& q, const level_tile& t);
109 	static int calculate_tile_corners(tile_corner* result, const level_tile& t);
110 
is_opaque() const111 	bool is_opaque() const { return opaque_; }
112 	bool calculate_opaque() const;
113 	bool calculate_uses_alpha_channel() const;
114 	bool calculate_is_solid_color(graphics::color& col) const;
115 
116 	bool calculate_draw_area();
117 
solid_color() const118 	const graphics::color* solid_color() const { return solid_color_.get(); }
119 
120 	//write the compiled index of this object. buf MUST point to a buf
121 	//of at least 4 chars.
122 	void write_compiled_index(char* buf) const;
123 
124 	//reads an object from its compiled index. buf MUST point to a buf of
125 	//at least 3 chars.
126 	static const_level_object_ptr get_compiled(const char* buf);
127 
128 	//only used when compiling: notifies the object it is used at the
129 	//given zorder.
130 	level_object_ptr record_zorder(int zorder) const;
131 
132 private:
133 	variant get_value(const std::string& key) const;
134 
135 	std::string id_;
136 	std::string image_;
137 	std::string info_;
138 	graphics::texture t_;
139 	std::vector<int> tiles_;
140 	std::vector<bool> solid_;
141 	bool all_solid_;
142 	bool passthrough_;
143 	bool flip_;
144 	int damage_;
145 	int friction_;
146 	int traction_;
147 
148 	bool opaque_;
149 
150 	rect draw_area_;
151 
152 	boost::intrusive_ptr<graphics::color> solid_color_;
153 
154 	int tile_index_;
155 
156 	//only used when compiling: records all possible zorders for the object.
157 	mutable std::vector<int> zorders_;
158 
159 	unsigned int palettes_recognized_;
160 	unsigned int current_palettes_;
161 
162 	void set_palette(unsigned int palette);
163 
164 	void get_palettes_used(std::vector<int>& v) const;
165 };
166 
167 #endif
168