]> git.localhorst.tv Git - l2e.git/blob - src/map/Map.cpp
added Sort function to Inventory
[l2e.git] / src / map / Map.cpp
1 /*
2  * Map.cpp
3  *
4  *  Created on: Sep 29, 2012
5  *      Author: holy
6  */
7
8 #include "Map.h"
9
10 #include "Area.h"
11 #include "Tile.h"
12 #include "Trigger.h"
13 #include "../graphics/Sprite.h"
14 #include "../loader/Interpreter.h"
15 #include "../loader/TypeDescription.h"
16 #include "../sdl/utility.h"
17
18 #include <stdexcept>
19
20 using geometry::Vector;
21 using graphics::Sprite;
22 using loader::FieldDescription;
23 using loader::Interpreter;
24 using loader::TypeDescription;
25
26 namespace map {
27
28 Map::Map()
29 : tileset(0)
30 , battlebg(0)
31 , areas(0)
32 , numAreas(0)
33 , triggers(0)
34 , numTriggers(0)
35 , entities(0)
36 , numEntities(0)
37 , width(0) {
38
39 }
40
41
42 Area *Map::AreaAt(const Vector<int> &offset) {
43         if (numAreas > 0) {
44                 Vector<int> coords(TileCoordinates(offset));
45                 Vector<int> areaOffset(coords / areas[0].Size());
46                 int areaIndex(areaOffset.Index(width));
47                 if (areaIndex < numAreas) {
48                         return areas + areaIndex;
49                 }
50         }
51         return 0;
52 }
53
54 const Area *Map::AreaAt(const Vector<int> &offset) const {
55         if (numAreas > 0) {
56                 Vector<int> coords(TileCoordinates(offset));
57                 Vector<int> areaOffset(coords / areas[0].Size());
58                 int areaIndex(areaOffset.Index(width));
59                 if (areaIndex < numAreas) {
60                         return areas + areaIndex;
61                 }
62         }
63         return 0;
64 }
65
66 Tile *Map::TileAt(const Vector<int> &offset) {
67         Area *area(AreaAt(offset));
68         if (area) {
69                 Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
70                 return area->TileAt(tileOffset);
71         } else {
72                 return 0;
73         }
74 }
75
76 const Tile *Map::TileAt(const Vector<int> &offset) const {
77         const Area *area(AreaAt(offset));
78         if (area) {
79                 Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
80                 return area->TileAt(tileOffset);
81         } else {
82                 return 0;
83         }
84 }
85
86 Trigger *Map::TriggerAt(const geometry::Vector<int> &offset) {
87         // TODO: add support for multiple triggers on a tile?
88         Vector<int> coords(TileCoordinates(offset));
89         for (Trigger *i(triggers); i != triggers + numTriggers; ++i) {
90                 if (i->TilePosition() == coords) {
91                         return i;
92                 }
93         }
94         return 0;
95 }
96
97 SDL_Surface *Map::BattleBackgroundAt(const geometry::Vector<int> &position) {
98         Tile *tile(TileAt(position));
99         if (tile && tile->BattleBackground()) {
100                 return tile->BattleBackground();
101         }
102         Area *area(AreaAt(position));
103         if (area && area->BattleBackground()) {
104                 return area->BattleBackground();
105         }
106         return battlebg;
107 }
108
109 Vector<int> Map::TileCoordinates(const Vector<int> &position) const {
110         return position / tileset->Size();
111 }
112
113
114 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
115         // TODO: skip invisible areas
116         for (int i(0); i < numAreas; ++i) {
117                 const Area &area(areas[i]);
118                 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
119                 area.Render(dest, tileset, offset);
120         }
121 }
122
123 void Map::RenderDebug(SDL_Surface *dest, const Vector<int> &inOffset) const {
124         // TODO: skip invisible areas
125         for (int i(0); i < numAreas; ++i) {
126                 const Area &area(areas[i]);
127                 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
128                 area.RenderDebug(dest, tileset, offset);
129         }
130         for (int i(0); i < numTriggers; ++i) {
131                 Vector<int> offset((triggers[i].TilePosition() * tileset->Size()) + inOffset);
132                 switch (triggers[i].GetType()) {
133                         case Trigger::TYPE_NORTH:
134                                 sdl::HorizontalLine(dest, offset + (tileset->Size() / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
135                                 break;
136                         case Trigger::TYPE_EAST:
137                                 sdl::VerticalLine(dest, offset + Vector<int>(tileset->Width() * 3 / 4, tileset->Height() / 4), tileset->Height() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
138                                 break;
139                         case Trigger::TYPE_SOUTH:
140                                 sdl::HorizontalLine(dest, offset + Vector<int>(tileset->Width() / 4, tileset->Height() * 3 / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
141                                 break;
142                         case Trigger::TYPE_WEST:
143                                 sdl::VerticalLine(dest, offset + (tileset->Size() / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
144                                 break;
145                         case Trigger::TYPE_CONTACT: {
146                                         SDL_Rect destRect;
147                                         destRect.x = offset.X() + (tileset->Width() / 4);
148                                         destRect.y = offset.Y() + (tileset->Height() / 4);
149                                         destRect.w = tileset->Width() / 2;
150                                         destRect.h = tileset->Height() / 2;
151                                         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
152                                 }
153                                 break;
154                 }
155         }
156 }
157
158
159 void Map::CreateTypeDescription() {
160         Map m;
161
162         TypeDescription &td(TypeDescription::Create(TYPE_ID, "Map"));
163         td.SetConstructor(&Construct);
164         td.SetSize(sizeof(Map));
165
166         td.AddField("tileset", FieldDescription(((char *)&m.tileset) - ((char *)&m), Sprite::TYPE_ID).SetReferenced());
167         td.AddField("battlebg", FieldDescription(((char *)&m.battlebg) - ((char *)&m), Interpreter::IMAGE_ID).SetReferenced());
168         td.AddField("areas", FieldDescription(((char *)&m.areas) - ((char *)&m), Area::TYPE_ID).SetReferenced().SetAggregate());
169         td.AddField("triggers", FieldDescription(((char *)&m.triggers) - ((char *)&m), Trigger::TYPE_ID).SetReferenced().SetAggregate());
170         td.AddField("entities", FieldDescription(((char *)&m.entities) - ((char *)&m), Entity::TYPE_ID).SetReferenced().SetAggregate());
171         td.AddField("width", FieldDescription(((char *)&m.width) - ((char *)&m), Interpreter::NUMBER_ID));
172 }
173
174 void Map::Construct(void *data) {
175         new (data) Map;
176 }
177
178 }