mid dev inventory - may not be working
[sdlbotor.git] / Map.cpp
blob218cbd47e89af4fe31335272b9bf8173d3313df5
1 #include <iostream>
3 #include "globalfunc.h"
5 #include "Map.h"
6 #include "Game.h"
7 #include "Tile.h"
8 #include "Player.h"
9 #include "Robot.h"
10 #include "Object.h"
12 namespace botor
15 Map::Map() : tiles( MAP_WIDTH, std::vector<Tile>( MAP_HEIGHT ) ), robots(), objects()
18 void Map::Initialize()
21 lua_State *L = lua_newthread( Game::LUA );
23 int s = luaL_loadfile( L, "map.lua" );
25 if( !s )
27 s = lua_pcall( L, 0, 0, 0 ); //run file
28 if( s )
30 std::cerr << "Error loading Map:" << std::endl
31 << lua_tostring( L, -1 ) << std::endl;
32 exit(1);
35 else
37 std::cerr << "Error loading file: map.lua:" << std::endl;
38 exit(1);
43 bool Map::load( const char *fmap )
45 char tilefile[255];
46 FILE *in = fopen( fmap, "r" );
47 if( !in )
48 return false;
50 fscanf( in, "%s", (char*)tilefile );
52 if( !tileset.load( tilefile ) )
53 return false;
55 for( unsigned int y = 0; y < MAP_HEIGHT; y++ )
57 for( unsigned int x = 0; x < MAP_WIDTH; x++ )
59 Tile temp;
60 temp.moving = false;
61 temp.acid = 0;
63 fscanf( in, "%d:%d ", &temp.tileID, &temp.typeMask );
65 tiles[x][y] = temp;
67 fscanf( in, "\n" );
70 fclose( in );
72 return true;
76 void Map::unload()
78 tileset.unload();
81 void Map::Update()
83 for( unsigned int x = 0; x < MAP_WIDTH; x++ )
85 for( unsigned int y = 0; y < MAP_HEIGHT; y++ )
87 Tile &t = tiles[x][y];
88 if( t.moving )
91 Sint16 sx,sy;
92 Map::transformM2S( x, y, sx, sy );
93 if( dst( t.movementX, t.movementY, (float)sx, (float)sy ) < 0.5 )
95 t.moving = false;
97 else
99 float dirX, dirY;
101 dirX = sign( sx - t.movementX ) * Player::PLAYER_SPEED;
102 dirY = sign( sy - t.movementY ) * Player::PLAYER_SPEED;
104 t.movementX += dirX;
105 t.movementY += dirY;
109 if( t.acid > 0 )
111 if( time(0) > t.timer )
113 t.acid--;
114 if(!t.acid)
115 RemoveTile(x,y);
116 t.timer = time(0)+1;
123 for( unsigned int i = 0; i < robots.size(); i++ )
125 Robot *r = robots[i];
127 r->Update();
129 if( !r->isAlive() )
131 robots.erase( robots.begin() + i );
132 delete r;
136 for( unsigned int i = 0; i < objects.size(); i++ )
138 Object *o = objects[i];
140 o->Update();
142 if( o->getLocation() != Object::LOC_GROUND )
144 objects.erase( objects.begin() + i );
150 void Map::Draw( )
153 for( unsigned int x = 0; x < MAP_WIDTH; x++ )
155 for( unsigned int y = 0; y < MAP_HEIGHT; y++ )
157 Tile &t = tiles[x][y];
159 if( t.tileID == -1 )
160 continue;
162 if( t.moving )
164 tileset.Draw( (Sint16)t.movementX, (Sint16)t.movementY, t.tileID );
166 else if( t.acid > 0 )
168 Sint16 sx,sy;
169 Map::transformM2S( (Uint8)x, (Uint8)y, sx, sy );
171 tileset.Draw( sx, sy, 5+(4-t.acid) );
174 else
176 Sint16 sx,sy;
177 Map::transformM2S( (Uint8)x, (Uint8)y, sx, sy );
178 tileset.Draw( sx, sy, tiles[x][y].tileID );
185 for( std::vector<Object*>::iterator o = objects.begin(); o != objects.end(); o++ )
187 (*o)->Draw();
190 for( std::vector<Robot*>::iterator r = robots.begin(); r != robots.end(); r++ )
192 (*r)->Draw();
199 Tile *Map::tileAt( Uint8 mapX, Uint8 mapY )
201 return &tiles[mapX][mapY];
204 bool Map::PushPushable( Uint8 fromX, Uint8 fromY, Uint8 dirX, Uint8 dirY )
206 Uint8 toX = fromX+dirX;
207 Uint8 toY = fromY+dirY;
209 Sint16 posX, posY;
211 if( isBitSet( tiles[toX][toY].typeMask, Tile::TILE_NOTPUSHABLE ) ||
212 isBitSet( tiles[toX][toY].typeMask, Tile::TILE_WALL ) ||
213 isRobotOn( toX, toY ) ||
214 getObject( toX, toY ) )
216 return false;
219 if( isBitSet( tiles[toX][toY].typeMask, Tile::TILE_PUSHABLE ) )
221 if(!PushPushable( toX, toY, dirX, dirY ) )
223 return false;
227 Map::transformM2S( fromX, fromY, posX, posY );
229 tiles[toX][toY] = tiles[fromX][fromY];
230 tiles[toX][toY].moving = true;
231 tiles[toX][toY].movementX = (float)posX;
232 tiles[toX][toY].movementY = (float)posY;
234 RemoveTile( fromX, fromY );
236 return true;
240 void Map::RemoveTile( Uint8 mapX, Uint8 mapY )
242 tiles[mapX][mapY].typeMask = Tile::TILE_WALKABLE;
243 tiles[mapX][mapY].tileID = -1;
244 tiles[mapX][mapY].moving = false;
245 tiles[mapX][mapY].acid = 0;
248 void Map::transformM2S( Uint8 mapX, Uint8 mapY, Sint16 &x, Sint16 &y )
250 x = mapX*20;
251 y = mapY*20 + MAP_SHIFT;
254 bool Map::isRobotOn( Uint8 mapX, Uint8 mapY )
256 for( std::vector<Robot*>::iterator r = robots.begin(); r != robots.end(); r++ )
258 if( (*r)->getMapX() == mapX && (*r)->getMapY() == mapY )
259 return true;
261 return false;
264 void Map::Acid( Uint8 cX, Uint8 cY )
266 for( Uint8 y = cY-1; y <= cY+1; y++ )
268 for( Uint8 x = cX-1; x <= cX+1; x++ )
270 Tile &t = tiles[x][y];
271 if( isBitSet( t.typeMask, Tile::TILE_WALL ) &&
272 !isBitSet( t.typeMask, Tile::TILE_SOLID) &&
273 !t.acid )
275 t.acid = 4;
276 t.timer = time(0)+1;
282 Object *Map::getObject( Uint8 X, Uint8 Y )
284 for( std::vector<Object*>::iterator o = objects.begin(); o != objects.end(); o++ )
286 if((*o)->getLocation() == Object::LOC_GROUND )
288 if( (*o)->getMapX() == X && (*o)->getMapY() == Y )
289 return *o;
293 return 0;
296 void Map::insertObject( Object *o, Uint8 X, Uint8 Y )
298 o->setLocation( Object::LOC_GROUND );
299 o->Teleport( X, Y );
301 objects.push_back( o );
304 void Map::insertRobot( Uint8 X, Uint8 Y )
306 robots.push_back( new Robot( X, Y ) );