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