Introduced FileSystem and Out classes
[openstranded.git] / src / map.cc
blobaa31206349549867aa65b4c1fc8201a3c7ba3869
1 /*
2 *This file includes the basic classes for map/savegame loading
4 * Copyright (C) 2008 Mathias Gottschlag
6 * This file is part of OpenStranded
8 * OpenStranded is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * OpenStranded is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with OpenStranded. If not, see <http://www.gnu.org/licenses/>.
22 #include "map.hh"
23 #include "bbfile.hh"
24 #include "kingdom.hh"
25 #include "object.hh"
26 #include "eal/eal.hh"
27 #include "eal/engine.hh"
29 #include <iostream>
31 Map::Map ()
33 environment = eal::Engine::get()->addEnvironment();
35 Map::~Map ()
39 bool
40 Map::load (std::string path)
42 // Open file
43 BBFile mapfile(path);
44 if (!mapfile)
46 std::cerr << "Map file couldn't be opened: " << path << std::endl;
47 return false;
50 // General map info
51 std::string format = mapfile.readLine();
52 std::string version = mapfile.readLine();
53 std::string date = mapfile.readLine();
54 std::string time = mapfile.readLine();
55 format = mapfile.readLine();
56 std::string mode = mapfile.readLine();
58 std::string typeformat = mapfile.readLine();
59 int objectformat = 0;
60 int unitformat = 0;
61 int infoformat = 0;
62 if (typeformat.size() >= 3)
64 objectformat = typeformat[0] - '0';
65 unitformat = typeformat[1] - '0';
66 infoformat = typeformat[2] - '0';
69 mapfile.readLine();
70 mapfile.readLine();
71 mapfile.readLine();
72 mapfile.readLine();
73 mapfile.readLine();
75 // Map image
76 uint8_t mapimage[96 * 72 * 3];
77 mapfile.readData(mapimage, 96 * 72 * 3);
79 // Password
80 // TODO
81 mapfile.readByte();
82 mapfile.readLine();
84 // Environment settings
85 int day = mapfile.readInt();
86 int hour = mapfile.readByte();
87 int minute = mapfile.readByte();
88 int freezetime = mapfile.readByte();
89 std::string skybox = mapfile.readString();
90 int multiplayer = mapfile.readByte();
91 int climate = mapfile.readByte();
92 std::string music = mapfile.readString();
93 std::string briefing = mapfile.readString();
94 int fogr = mapfile.readByte();
95 int fogg = mapfile.readByte();
96 int fogb = mapfile.readByte();
97 int fogmode = mapfile.readByte();
99 mapfile.readByte();
101 // Quick slots
102 for (int i = 0; i < 10; i++)
103 mapfile.readString();
105 // Color map
106 int colormapsize = mapfile.readInt();
107 printf("Color map size: %d\n", colormapsize);
108 uint8_t *colormap = new uint8_t[colormapsize * colormapsize * 3];
109 mapfile.readData(colormap, colormapsize * colormapsize * 3);
110 // Heightmap
111 int heightmapsize = mapfile.readInt();
112 printf("Height map size: %d\n", heightmapsize);
113 float *heightmap = new float[(heightmapsize + 1) * (heightmapsize + 1)];
114 mapfile.readData(heightmap, (heightmapsize + 1) * (heightmapsize + 1) * sizeof(float));
115 // Grass map
116 uint8_t *grassmap = new uint8_t[(colormapsize + 1) * (colormapsize + 1)];
117 mapfile.readData(grassmap, (colormapsize + 1) * (colormapsize + 1));
119 // Create terrain
120 environment->setSize(heightmapsize);
121 environment->setHeightData(heightmap);
122 environment->setColorMap(colormap, colormapsize);
123 environment->setSkyCube(skybox);
124 environment->setFog(eal::Color(fogr, fogg, fogb));
125 // TODO: Grass
127 // Entities
128 // Load objects
129 int objectcount = mapfile.readInt();
130 for (int i = 0; i < objectcount; i++)
132 ID id = mapfile.readInt();
133 ID type = 0;
134 if (objectformat)
136 type = mapfile.readShort();
138 else
140 type = mapfile.readByte();
142 float x = mapfile.readFloat();
143 float z = mapfile.readFloat();
144 float yaw = mapfile.readFloat();
145 float health = mapfile.readFloat();
146 float maxhealth = mapfile.readFloat();
147 int daytimer = mapfile.readInt();
149 // Create object
150 Entity *newobject = Kingdom::kingdomList[S2_OBJECT]->insertEntity(type, id);
151 newobject->setPosition(eal::Vector3(x, environment->getHeight(eal::Vector2(x, z)), z));
153 printf("%d objects.\n", objectcount);
154 // TODO: Load other entity types
156 return false;
158 void
159 Map::destroy ()