Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / tilemap.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (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, see <http://www.gnu.org/licenses/>.
16
17 #include <math.h>
18
19 #include "object/tilemap.hpp"
20 #include "scripting/squirrel_util.hpp"
21 #include "scripting/tilemap.hpp"
22 #include "supertux/main.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/tile_manager.hpp"
25 #include "supertux/tile_set.hpp"
26 #include "util/reader.hpp"
27
28 TileMap::TileMap(const TileSet *new_tileset) :
29   tileset(new_tileset), 
30   solid(false), 
31   speed_x(1), 
32   speed_y(1), 
33   width(0),
34   height(0), 
35   z_pos(0), 
36   x_offset(0), 
37   y_offset(0), 
38   movement(Vector(0,0)), 
39   drawing_effect(NO_EFFECT),
40   alpha(1.0), 
41   current_alpha(1.0),
42   remaining_fade_time(0),
43   draw_target(DrawingContext::NORMAL)
44 {
45 }
46
47 TileMap::TileMap(const Reader& reader) :
48   solid(false), 
49   speed_x(1), 
50   speed_y(1), 
51   width(-1),
52   height(-1), 
53   z_pos(0), 
54   x_offset(0),
55   y_offset(0),
56   movement(Vector(0,0)), 
57   drawing_effect(NO_EFFECT),
58   alpha(1.0), 
59   current_alpha(1.0), 
60   remaining_fade_time(0),
61   draw_target(DrawingContext::NORMAL)
62 {
63   tileset = current_tileset;
64   assert(tileset != NULL);
65
66   reader.get("name",   name);
67   reader.get("z-pos",  z_pos);
68   reader.get("solid",  solid);
69   reader.get("speed",  speed_x);
70   reader.get("speed-y", speed_y);
71   
72   if(solid && ((speed_x != 1) || (speed_y != 1))) {
73     log_warning << "Speed of solid tilemap is not 1. fixing" << std::endl;
74     speed_x = 1;
75     speed_y = 1;
76   }
77
78   const lisp::Lisp* pathLisp = reader.get_lisp("path");
79   if (pathLisp) {
80     path.reset(new Path());
81     path->read(*pathLisp);
82     walker.reset(new PathWalker(path.get(), /*running*/false));
83     Vector v = path->get_base();
84     set_x_offset(v.x);
85     set_y_offset(v.y);
86   }
87
88   std::string draw_target_s = "normal";
89   reader.get("draw-target", draw_target_s);
90   if (draw_target_s == "normal") draw_target = DrawingContext::NORMAL;
91   if (draw_target_s == "lightmap") draw_target = DrawingContext::LIGHTMAP;
92
93   if (reader.get("alpha", alpha)) {
94     current_alpha = alpha;
95   }
96
97   reader.get("width", width);
98   reader.get("height", height);
99   if(width < 0 || height < 0)
100     throw std::runtime_error("Invalid/No width/height specified in tilemap.");
101
102   if(!reader.get("tiles", tiles))
103     throw std::runtime_error("No tiles in tilemap.");
104
105   if(int(tiles.size()) != width*height) {
106     throw std::runtime_error("wrong number of tiles in tilemap.");
107   }
108
109   bool empty = true;
110
111   // make sure all tiles used on the tilemap are loaded and tilemap isn't empty
112   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i) {
113     if(*i != 0) {
114       empty = false;
115     }
116
117     tileset->get(*i);
118   }
119
120   if(empty)
121     log_info << "Tilemap '" << name << "', z-pos '" << z_pos << "' is empty." << std::endl;
122 }
123
124 TileMap::TileMap(const TileSet *new_tileset, std::string name, int z_pos,
125                  bool solid, size_t width, size_t height) :
126   tileset(new_tileset), 
127   solid(solid), 
128   speed_x(1), 
129   speed_y(1), 
130   width(0),
131   height(0), 
132   z_pos(z_pos), 
133   x_offset(0), 
134   y_offset(0), 
135   movement(Vector(0,0)),
136   drawing_effect(NO_EFFECT), 
137   alpha(1.0), 
138   current_alpha(1.0),
139   remaining_fade_time(0), 
140   draw_target(DrawingContext::NORMAL)
141 {
142   this->name = name;
143
144   resize(width, height);
145 }
146
147 TileMap::~TileMap()
148 {
149 }
150
151 void
152 TileMap::update(float elapsed_time)
153 {
154   // handle tilemap fading
155   if (current_alpha != alpha) {
156     remaining_fade_time = std::max(0.0f, remaining_fade_time - elapsed_time);
157     if (remaining_fade_time == 0.0f) {
158       current_alpha = alpha;
159     } else {
160       float amt = (alpha - current_alpha) / (remaining_fade_time / elapsed_time);
161       if (amt > 0) current_alpha = std::min(current_alpha + amt, alpha);
162       if (amt < 0) current_alpha = std::max(current_alpha + amt, alpha);
163     }
164     if ((alpha < 0.25) && (current_alpha < 0.25)) set_solid(false);
165     if ((alpha > 0.75) && (current_alpha > 0.75)) set_solid(true);
166   }
167
168   movement = Vector(0,0);
169   // if we have a path to follow, follow it
170   if (walker.get()) {
171     Vector v = walker->advance(elapsed_time);
172     movement = Vector(v.x-get_x_offset(), std::max(0.0f,v.y-get_y_offset()));
173     set_x_offset(v.x);
174     set_y_offset(v.y);
175   }
176 }
177
178 void
179 TileMap::draw(DrawingContext& context)
180 {
181   // skip draw if current opacity is set to 0.0
182   if (current_alpha == 0.0) return;
183
184   context.push_transform();
185   context.push_target();
186   context.set_target(draw_target);
187
188   if(drawing_effect != 0) context.set_drawing_effect(drawing_effect);
189   if(current_alpha != 1.0) context.set_alpha(current_alpha);
190
191   float trans_x = roundf(context.get_translation().x);
192   float trans_y = roundf(context.get_translation().y);
193   context.set_translation(Vector(int(trans_x * speed_x),
194                                  int(trans_y * speed_y)));
195
196   /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
197    * I have no idea why */
198   float start_x = int((roundf(context.get_translation().x) - roundf(x_offset)) / 32) * 32 + roundf(x_offset);
199   float start_y = int((roundf(context.get_translation().y) - roundf(y_offset)) / 32) * 32 + roundf(y_offset);
200   float end_x = std::min(start_x + SCREEN_WIDTH + 32, float(width * 32 + roundf(x_offset)));
201   float end_y = std::min(start_y + SCREEN_HEIGHT + 32, float(height * 32 + roundf(y_offset)));
202   int tsx = int((start_x - roundf(x_offset)) / 32); // tilestartindex x
203   int tsy = int((start_y - roundf(y_offset)) / 32); // tilestartindex y
204
205   Vector pos;
206   int tx, ty;
207   for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
208     for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
209       if ((tx < 0) || (ty < 0)) continue;
210       const Tile* tile = tileset->get(tiles[ty*width + tx]);
211       assert(tile != 0);
212       tile->draw(context, pos, z_pos);
213     }
214   }
215
216   context.pop_target();
217   context.pop_transform();
218 }
219
220 void
221 TileMap::goto_node(int node_no)
222 {
223   if (!walker.get()) return;
224   walker->goto_node(node_no);
225 }
226
227 void
228 TileMap::start_moving()
229 {
230   if (!walker.get()) return;
231   walker->start_moving();
232 }
233
234 void
235 TileMap::stop_moving()
236 {
237   if (!walker.get()) return;
238   walker->stop_moving();
239 }
240
241 void
242 TileMap::expose(HSQUIRRELVM vm, SQInteger table_idx)
243 {
244   if (name.empty()) return;
245   Scripting::TileMap* interface = new Scripting::TileMap(this);
246   expose_object(vm, table_idx, interface, name, true);
247 }
248
249 void
250 TileMap::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
251 {
252   if (name.empty()) return;
253   Scripting::unexpose_object(vm, table_idx, name);
254 }
255
256 void
257 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
258              int new_z_pos, bool newsolid)
259 {
260   if(int(newt.size()) != newwidth * newheight)
261     throw std::runtime_error("Wrong tilecount count.");
262
263   width  = newwidth;
264   height = newheight;
265
266   tiles.resize(newt.size());
267   tiles = newt;
268
269   z_pos  = new_z_pos;
270   solid  = newsolid;
271
272   // make sure all tiles are loaded
273   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
274     tileset->get(*i);
275 }
276
277 void
278 TileMap::resize(int new_width, int new_height, int fill_id)
279 {
280   if(new_width < width) {
281     // remap tiles for new width
282     for(int y = 0; y < height && y < new_height; ++y) {
283       for(int x = 0; x < new_width; ++x) {
284         tiles[y * new_width + x] = tiles[y * width + x];
285       }
286     }
287   }
288
289   tiles.resize(new_width * new_height, fill_id);
290
291   if(new_width > width) {
292     // remap tiles
293     for(int y = std::min(height, new_height)-1; y >= 0; --y) {
294       for(int x = new_width-1; x >= 0; --x) {
295         if(x >= width) {
296           tiles[y * new_width + x] = fill_id;
297           continue;
298         }
299
300         tiles[y * new_width + x] = tiles[y * width + x];
301       }
302     }
303   }
304
305   height = new_height;
306   width = new_width;
307 }
308
309 void
310 TileMap::set_solid(bool solid)
311 {
312   this->solid = solid;
313 }
314
315 uint32_t
316 TileMap::get_tile_id(int x, int y) const
317 {
318   if(x < 0 || x >= width || y < 0 || y >= height) {
319     //log_warning << "tile outside tilemap requested" << std::endl;
320     return 0;
321   }
322
323   return tiles[y*width + x];
324 }
325
326 const Tile*
327 TileMap::get_tile(int x, int y) const
328 {
329   uint32_t id = get_tile_id(x, y);
330   return tileset->get(id);
331 }
332
333 uint32_t
334 TileMap::get_tile_id_at(const Vector& pos) const
335 {
336   return get_tile_id(int(pos.x - x_offset)/32, int(pos.y - y_offset)/32);
337 }
338
339 const Tile*
340 TileMap::get_tile_at(const Vector& pos) const
341 {
342   uint32_t id = get_tile_id_at(pos);
343   return tileset->get(id);
344 }
345
346 void
347 TileMap::change(int x, int y, uint32_t newtile)
348 {
349   assert(x >= 0 && x < width && y >= 0 && y < height);
350   tiles[y*width + x] = newtile;
351 }
352
353 void
354 TileMap::change_at(const Vector& pos, uint32_t newtile)
355 {
356   change(int(pos.x - x_offset)/32, int(pos.y - y_offset)/32, newtile);
357 }
358
359 void
360 TileMap::change_all(uint32_t oldtile, uint32_t newtile)
361 {
362   for (size_t x = 0; x < get_width(); x++) {
363     for (size_t y = 0; y < get_height(); y++) {
364       if (get_tile_id(x,y) != oldtile)
365         continue;
366
367       change(x,y,newtile);
368     }
369   }
370 }
371
372 void
373 TileMap::fade(float alpha, float seconds)
374 {
375   this->alpha = alpha;
376   this->remaining_fade_time = seconds;
377 }
378
379 void 
380 TileMap::set_alpha(float alpha)
381 {
382   this->alpha = alpha;
383   this->current_alpha = alpha;
384   this->remaining_fade_time = 0;
385   if (current_alpha < 0.25) set_solid(false);
386   if (current_alpha > 0.75) set_solid(true);
387 }
388
389 float 
390 TileMap::get_alpha()
391 {
392   return this->current_alpha;
393 }
394   
395 IMPLEMENT_FACTORY(TileMap, "tilemap");
396
397 /* EOF */