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