more small adjustments from Tron
[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 <cmath>
27
28 #include "tilemap.hpp"
29 #include "video/drawing_context.hpp"
30 #include "level.hpp"
31 #include "tile.hpp"
32 #include "resources.hpp"
33 #include "tile_manager.hpp"
34 #include "lisp/lisp.hpp"
35 #include "lisp/writer.hpp"
36 #include "object_factory.hpp"
37 #include "main.hpp"
38 #include "log.hpp"
39 #include "scripting/tilemap.hpp"
40 #include "scripting/squirrel_util.hpp"
41
42 TileMap::TileMap()
43   : solid(false), speed(1), width(0), height(0), z_pos(0), x_offset(0), y_offset(0),
44     drawing_effect(NO_EFFECT), alpha(1.0), current_alpha(1.0), remaining_fade_time(0),
45     draw_target(DrawingContext::NORMAL)
46 {
47   tilemanager = tile_manager;
48 }
49
50 TileMap::TileMap(const lisp::Lisp& reader, TileManager* new_tile_manager)
51   : solid(false), speed(1), width(-1), height(-1), z_pos(0),
52     x_offset(0), y_offset(0),
53     drawing_effect(NO_EFFECT), alpha(1.0), current_alpha(1.0),
54     remaining_fade_time(0),
55     draw_target(DrawingContext::NORMAL)
56 {
57   tilemanager = new_tile_manager;
58   if(tilemanager == 0)
59     tilemanager = tile_manager;
60
61   reader.get("name", name);
62   reader.get("z-pos", z_pos);
63   reader.get("solid", solid);
64   reader.get("speed", speed);
65
66   if(solid && speed != 1) {
67     log_warning << "Speed of solid tilemap is not 1. fixing" << std::endl;
68     speed = 1;
69   }
70
71   const lisp::Lisp* pathLisp = reader.get_lisp("path");
72   if (pathLisp) {
73     path.reset(new Path());
74     path->read(*pathLisp);
75     walker.reset(new PathWalker(path.get(), /*running*/false));
76     Vector v = path->get_base();
77     set_x_offset(v.x);
78     set_y_offset(v.y);
79   }
80   
81   std::string draw_target_s = "normal";
82   reader.get("draw-target", draw_target_s);
83   if (draw_target_s == "normal") draw_target = DrawingContext::NORMAL;
84   if (draw_target_s == "lightmap") draw_target = DrawingContext::LIGHTMAP;
85
86   reader.get("width", width);
87   reader.get("height", height);
88   if(width < 0 || height < 0)
89     throw std::runtime_error("Invalid/No width/height specified in tilemap.");
90
91   if(!reader.get_vector("tiles", tiles))
92     throw std::runtime_error("No tiles in tilemap.");
93
94   if(int(tiles.size()) != width*height) {
95     throw std::runtime_error("wrong number of tiles in tilemap.");
96   }
97
98   // make sure all tiles are loaded
99   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
100     tilemanager->get(*i);
101 }
102
103 TileMap::TileMap(std::string name, int z_pos, bool solid, size_t width, size_t height)
104   : solid(solid), speed(1), width(0), height(0), z_pos(z_pos),
105     x_offset(0), y_offset(0), drawing_effect(NO_EFFECT), alpha(1.0),
106     current_alpha(1.0), remaining_fade_time(0),
107     draw_target(DrawingContext::NORMAL)
108 {
109   this->name = name;
110   tilemanager = tile_manager;
111
112   resize(width, height);
113 }
114
115 TileMap::~TileMap()
116 {
117 }
118
119 void
120 TileMap::write(lisp::Writer& writer)
121 {
122   writer.start_list("tilemap");
123
124   writer.write_int("z-pos", z_pos);
125
126   writer.write_bool("solid", solid);
127   writer.write_float("speed", speed);
128   writer.write_int("width", width);
129   writer.write_int("height", height);
130   writer.write_int_vector("tiles", tiles);
131
132   writer.end_list("tilemap");
133 }
134
135 void
136 TileMap::update(float elapsed_time)
137 {
138   // handle tilemap fading
139   if (current_alpha != alpha) {
140     remaining_fade_time = std::max(0.0f, remaining_fade_time - elapsed_time);
141     if (remaining_fade_time == 0.0f) {
142       current_alpha = alpha;
143     } else {
144       float amt = (alpha - current_alpha) / (remaining_fade_time / elapsed_time);
145       if (amt > 0) current_alpha = std::min(current_alpha + amt, alpha);
146       if (amt < 0) current_alpha = std::max(current_alpha + amt, alpha);
147     }
148   }
149
150   // if we have a path to follow, follow it
151   if (walker.get()) {
152     Vector v = walker->advance(elapsed_time);
153     set_x_offset(v.x);
154     set_y_offset(v.y);
155   }
156 }
157
158 void
159 TileMap::draw(DrawingContext& context)
160 {
161   context.push_transform();
162   context.push_target();
163   context.set_target(draw_target);
164
165   if(drawing_effect != 0) context.set_drawing_effect(drawing_effect);
166   if(current_alpha != 1.0) context.set_alpha(current_alpha);
167
168   float trans_x = roundf(context.get_translation().x);
169   float trans_y = roundf(context.get_translation().y);
170   context.set_translation(Vector(trans_x * speed, trans_y * speed));
171
172   /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
173    * I have no idea why */
174   float start_x = int((roundf(context.get_translation().x) - roundf(x_offset)) / 32) * 32 + roundf(x_offset);
175   float start_y = int((roundf(context.get_translation().y) - roundf(y_offset)) / 32) * 32 + roundf(y_offset);
176   float end_x = std::min(start_x + SCREEN_WIDTH + 32, float(width * 32 + roundf(x_offset)));
177   float end_y = std::min(start_y + SCREEN_HEIGHT + 32, float(height * 32 + roundf(y_offset)));
178   int tsx = int((start_x - roundf(x_offset)) / 32); // tilestartindex x
179   int tsy = int((start_y - roundf(y_offset)) / 32); // tilestartindex y
180
181   Vector pos;
182   int tx, ty;
183   for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
184     for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
185       if ((tx < 0) || (ty < 0)) continue;
186       const Tile* tile = tilemanager->get(tiles[ty*width + tx]);
187       assert(tile != 0);
188       tile->draw(context, pos, z_pos);
189     }
190   }
191
192   context.pop_target();
193   context.pop_transform();
194 }
195
196 void
197 TileMap::goto_node(int node_no)
198 {
199   if (!walker.get()) return;
200   walker->goto_node(node_no);
201 }
202
203 void
204 TileMap::start_moving()
205 {
206   if (!walker.get()) return;
207   walker->start_moving();
208 }
209
210 void
211 TileMap::stop_moving()
212 {
213   if (!walker.get()) return;
214   walker->stop_moving();
215 }
216
217 void
218 TileMap::expose(HSQUIRRELVM vm, SQInteger table_idx)
219 {
220   if (name.empty()) return;
221   if (!walker.get()) return;
222   Scripting::TileMap* interface = new Scripting::TileMap(this);
223   expose_object(vm, table_idx, interface, name, true);
224 }
225
226 void
227 TileMap::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
228 {
229   if (name.empty()) return;
230   if (!walker.get()) return;
231   Scripting::unexpose_object(vm, table_idx, name);
232 }
233
234 void
235 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
236     int new_z_pos, bool newsolid)
237 {
238   if(int(newt.size()) != newwidth * newheight)
239     throw std::runtime_error("Wrong tilecount count.");
240
241   width  = newwidth;
242   height = newheight;
243
244   tiles.resize(newt.size());
245   tiles = newt;
246
247   z_pos  = new_z_pos;
248   solid  = newsolid;
249
250   // make sure all tiles are loaded
251   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
252     tilemanager->get(*i);
253 }
254
255 void
256 TileMap::resize(int new_width, int new_height)
257 {
258   if(new_width < width) {
259     // remap tiles for new width
260     for(int y = 0; y < height && y < new_height; ++y) {
261       for(int x = 0; x < new_width; ++x) {
262         tiles[y * new_width + x] = tiles[y * width + x];
263       }
264     }
265   }
266
267   tiles.resize(new_width * new_height);
268
269   if(new_width > width) {
270     // remap tiles
271     for(int y = std::min(height, new_height)-1; y >= 0; --y) {
272       for(int x = new_width-1; x >= 0; --x) {
273         if(x >= width) {
274           tiles[y * new_width + x] = 0;
275           continue;
276         }
277
278         tiles[y * new_width + x] = tiles[y * width + x];
279       }
280     }
281   }
282
283   height = new_height;
284   width = new_width;
285 }
286
287 const Tile*
288 TileMap::get_tile(int x, int y) const
289 {
290   if(x < 0 || x >= width || y < 0 || y >= height) {
291     //log_warning << "tile outside tilemap requested" << std::endl;
292     return tilemanager->get(0);
293   }
294
295   return tilemanager->get(tiles[y*width + x]);
296 }
297
298 const Tile*
299 TileMap::get_tile_at(const Vector& pos) const
300 {
301   return get_tile(int(pos.x - x_offset)/32, int(pos.y - y_offset)/32);
302 }
303
304 void
305 TileMap::change(int x, int y, uint32_t newtile)
306 {
307   assert(x >= 0 && x < width && y >= 0 && y < height);
308   tiles[y*width + x] = newtile;
309 }
310
311 void
312 TileMap::change_at(const Vector& pos, uint32_t newtile)
313 {
314   change(int(pos.x - x_offset)/32, int(pos.y - y_offset)/32, newtile);
315 }
316
317 void
318 TileMap::change_all(uint32_t oldtile, uint32_t newtile)
319 {
320   for (size_t x = 0; x < get_width(); x++)
321     for (size_t y = 0; y < get_height(); y++) {
322       if (get_tile(x,y)->getID() == oldtile) change(x,y,newtile);
323     }
324 }
325
326 void 
327 TileMap::fade(float alpha, float seconds)
328 {
329   this->alpha = alpha;
330   this->remaining_fade_time = seconds;
331 }
332
333 IMPLEMENT_FACTORY(TileMap, "tilemap");