Adonthell  0.4
landmap.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 1999/2000/2001 Alexandre Courbot
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 /**
21  * @file landmap.cc
22  * Defines the landmap class.
23  */
24 
25 
26 #include "landmap.h"
27 #include "character.h"
28 
29 using namespace std;
30 
31 
33 {
34 }
35 
37 {
38  clear ();
39 }
40 
42 {
43  // Clear all events
45 
46  // Remove all mapcharacters from this map.
47  while (mapchar.size ())
48  mapchar.front ()->remove_from_map ();
49 
50  // Delete all mapobjects
51  vector <mapobject *>::iterator io;
52  for (io = mobj.begin (); io != mobj.end (); io++)
53  delete (*io);
54  mobj.clear ();
55 
56  // Delete all mapobjects filenames
57  mobjsrc.clear ();
58 
59  // Delete all submaps
60  vector <mapsquare_area *>::iterator is;
61  for (is = submap.begin (); is != submap.end (); is++)
62  delete (*is);
63  submap.clear ();
64 
65  // Reset the filename.
66  filename_ = "";
67 }
68 
70 {
71  // Update mapobjects
72  vector <mapobject *>::iterator io;
73  for (io = mobj.begin (); io != mobj.end (); io++)
74  (*io)->update ();
75 
76  // Update mapcharacters
77  vector <mapcharacter *>::iterator ic;
78  for (ic = mapchar.begin (); ic != mapchar.end (); ic++)
79  (*ic)->update ();
80 }
81 
83 {
84  u_int16 i, j;
85  string tstr;
86 
87  clear ();
88 
89  // Load mapobjects
90  i << file;
91  for (; i; i--)
92  {
93  mapobject * tobj = new mapobject;
94  tstr << file;
95  tobj->load (tstr);
96  insert_mapobject (tobj, nbr_of_mapobjects (), tstr.c_str ());
97  }
98 
99  // Load submaps
100  i << file;
101  for (j = 0; j < i; j++)
102  {
104  mapsquare_area * sm = submap[j];
105 
106  u_int16 k, l;
107  k << file;
108  l << file;
109  sm->resize_area (k, l);
110 
111  for (l = 0; l < sm->area_height (); l++)
112  for (k = 0; k < sm->area_length (); k++)
113  {
114  sm->area[k][l].can_use_for_pathfinding << file;
115  u_int16 n, t;
116  // Read the number of mapobjects which have their base tile here
117  n << file;
118  while (n)
119  {
120  // Get the mapobject number
121  t << file;
122  sm->put_mapobject (k, l, mobj[t]);
123  n--;
124  }
125  }
126  }
127 
128  return 0;
129 }
130 
131 s_int8 landmap::load (string fname)
132 {
133  igzstream file;
134  s_int8 retvalue = -1;
135  string fdef (MAPS_DIR);
136 
137  fdef += fname;
138  file.open (fdef);
139  if (!file.is_open ())
140  return -1;
141  if (fileops::get_version (file, 1, 1, fdef))
142  retvalue = get (file);
143  file.close ();
144  filename_ = fname;
145  return retvalue;
146 }
147 
149 {
150  u_int16 i;
151 
152  // Save mapobjects
153  nbr_of_mapobjects () >> file;
154  for (i = 0; i < nbr_of_mapobjects (); i++)
155  {
156  mobjsrc[i] >> file;
157  }
158 
159  // Save submaps
160  nbr_of_submaps () >> file;
161  for (i = 0; i < nbr_of_submaps (); i++)
162  {
163  u_int16 k, l;
164 
165  submap[i]->area_length () >> file;
166  submap[i]->area_height () >> file;
167 
168  for (l = 0; l < submap[i]->area_height (); l++)
169  for (k = 0; k < submap[i]->area_length (); k++)
170  {
171  u_int16 nbr_base = 0;
172  submap[i]->area[k][l].can_use_for_pathfinding >> file;
173  list <mapsquare_tile>::iterator it = submap[i]->area[k][l].tiles.begin ();
174  while (it != submap[i]->area[k][l].tiles.end ())
175  {
176  if (it->is_base) nbr_base++;
177  it++;
178  }
179  nbr_base >> file;
180 
181  it = submap[i]->area[k][l].tiles.begin ();
182  while (it != submap[i]->area[k][l].tiles.end ())
183  {
184  if (it->is_base)
185  {
186  u_int16 y = 0;
187  while (mobj[y] != (*it).mapobj) y++;
188  y >> file;
189  }
190  it++;
191  }
192  }
193  }
194  return 0;
195 }
196 
197 s_int8 landmap::save (string fname)
198 {
199  ogzstream file (MAPS_DIR + fname);
200  u_int8 retvalue;
201 
202  if (!file.is_open ())
203  return -1;
204  fileops::put_version (file, 1);
205  retvalue = put (file);
206  file.close ();
207  filename_ = fname;
208  return retvalue;
209 }
210 
212 {
213  mapcharacter *mc = NULL;
214  u_int16 nbr_of;
215  string id;
216 
217  // try to load event list
218  if (!event_list::get_state (file))
219  return false;
220 
221  // Load the mapcharacters
222  nbr_of << file;
223 
224  for (u_int16 i = 0; i < nbr_of; i++)
225  {
226  id << file;
227 
228  mc = (mapcharacter *) data::characters[id.c_str ()];
229  mc->set_map (this);
230  mc->get_state (file);
231  }
232 
233  return true;
234 }
235 
237 {
238  u_int16 nbr_of = nbr_of_mapcharacters ();
239  string id;
240 
241  // save all events attached to this map
242  event_list::put_state (file);
243 
244  // Save the mapcharacters and their status
245  nbr_of >> file;
246 
247  for (u_int16 i = 0; i < nbr_of; i++)
248  {
249  mapcharacter *mc = mapchar[i];
250 
251  id = mc->get_id ();
252  id >> file;
253 
254  mc->put_state (file);
255  }
256 
257  return 0;
258 }
259 
261  string srcfile)
262 {
263  if (pos > nbr_of_mapobjects ())
264  return -2;
265 
266  vector <mapobject *>::iterator i = mobj.begin ();
267  vector <string>::iterator j = mobjsrc.begin ();
268 
269  while (pos--)
270  {
271  i++;
272  j++;
273  }
274 
275  mobj.insert (i, an);
276  mobjsrc.insert (j, srcfile);
277 
278  return 0;
279 }
280 
282 {
283  mapobject * dptr = mobj[pos];
284 
285  // Update all the submaps to delete any mapsquare_tile that points
286  // to the deleted object.
287  u_int16 k;
288  for (k = 0; k < nbr_of_submaps (); k++)
289  {
290  u_int16 i, j;
291  for (i = 0; i < submap[k]->area_length (); i++)
292  for (j = 0; j < submap[k]->area_height (); j++)
293  {
294  mapsquare & ms = submap[k]->area[i][j];
295  list <mapsquare_tile>::iterator imt;
296  for (imt = ms.tiles.begin (); imt != ms.tiles.end (); imt++)
297  if (imt->mapobj == dptr)
298  {
299  remove_mapobject (k, i, j, pos);
300 
301  // The iterator is invalidated by the delete operation
302  imt = ms.tiles.begin ();
303  }
304  }
305  }
306 
307  vector <mapobject *>::iterator i;
308 
309  if (pos > nbr_of_mapobjects () - 1)
310  return -2;
311 
312  i = mobj.begin ();
313  while (pos--) i++;
314 
315  delete (*i);
316  mobj.erase (i);
317 
318  return 0;
319 }
320 
322 {
323  if (pos > nbr_of_mapobjects ())
324  return -2;
325 
326  // Update the mapcharacters so they are on the same map as before.
327  vector <mapcharacter *>::iterator ic;
328  for (ic = mapchar.begin (); ic != mapchar.end (); ic++)
329  if ((*ic)->submap_ >= pos) (*ic)->submap_++;
330 
331  // Insert the submap
332  vector <mapsquare_area *>::iterator i = submap.begin ();
333  while (pos--) i++;
334 
335  mapsquare_area * t = new mapsquare_area;
336  submap.insert (i, t);
337 
338  return 0;
339 }
340 
342 {
343  // Update the mapcharacters so they are on the same map as before
344  // and remove those who were on the deleted map.
345  vector <mapcharacter *>::iterator ic;
346  for (ic = mapchar.begin (); ic != mapchar.end (); ic++)
347  {
348  if ((*ic)->submap_ > pos) (*ic)->submap_--;
349  else if ((*ic)->submap_ == pos)
350  (*ic)->remove_from_map ();
351  }
352 
353  // Suppress the submap
354  vector <mapsquare_area *>::iterator i;
355 
356  if (pos > nbr_of_submaps () - 1)
357  return -2;
358 
359  i = submap.begin ();
360  while (pos--) i++;
361 
362  delete (*i);
363  submap.erase (i);
364 
365  return 0;
366 }
367 
369  u_int16 mobjnbr)
370 {
371  return submap[smap]->put_mapobject (px, py, mobj[mobjnbr]);
372 }
373 
375  u_int16 mobjnbr)
376 {
377  submap[smap]->remove_mapobject (px, py, mobj[mobjnbr]);
378 }
Declares the character class.
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
void close()
Close the file that was opened.
Definition: fileops.cc:63
void clear()
Unregisters and deletes all events owned by this list.
Definition: event_list.cc:48
#define MAPS_DIR
Subdirectory where maps are saved.
Definition: landmap.h:37
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
s_int8 delete_mapobject(u_int16 pos)
Delete a mapobject from a landmap.
Definition: landmap.cc:281
#define u_int16
16 bits long unsigned integer
Definition: types.h:38
s_int8 load(string fname)
Loads a mapobject from it&#39;s filename.
Definition: mapobject.cc:107
u_int16 area_height() const
Returns the height of the area.
Definition: mapsquare.h:416
s_int8 put_state(ogzstream &file) const
Saves the landmap&#39;s state into an opened file.
Definition: landmap.cc:236
Definition: str_hash.h:71
string get_id()
Returns an unique identifier of the character.
s_int8 put(ogzstream &file) const
Put a map into an opened file.
Definition: landmap.cc:148
landmap()
Default constructor.
Definition: landmap.cc:32
s_int8 insert_mapobject(mapobject *an, u_int16 pos, string srcfile="")
Adds a mapobject to a landmap.
Definition: landmap.cc:260
#define u_int8
8 bits long unsigned integer
Definition: types.h:35
u_int16 nbr_of_mapobjects() const
Get the number of mapobjects that the map owns.
Definition: landmap.h:89
bool get_state(igzstream &in)
Loads the event_list from a file and registers all loaded events.
Definition: event_list.cc:133
u_int16 area_length() const
Returns the length of the area.
Definition: mapsquare.h:405
void put_state(ogzstream &out) const
Save the event_list to a file.
Definition: event_list.cc:121
Declares the landmap class.
s_int8 insert_submap(u_int16 pos)
Inserts an empty landsubmap into the landmap.
Definition: landmap.cc:321
bool open(const string &fname)
Opens a file for read access.
Definition: fileops.cc:81
static bool get_version(igzstream &file, u_int16 min, u_int16 max, string name)
Definition: fileops.cc:369
s_int8 save(string fname)
Save a map into a file.
Definition: landmap.cc:197
s_int8 put_mapobject(u_int16 smap, u_int16 px, u_int16 py, u_int16 mobjnbr)
Put a mapobject on the map.
Definition: landmap.cc:368
void update()
Update the entire map (mapcharacters, mapobjects, etc...
Definition: landmap.cc:69
bool is_open()
Returns whether the file is opened or not.
Definition: fileops.h:103
s_int8 get(igzstream &file)
Load a map from an opened file.
Definition: landmap.cc:82
Base unit of a landsubmap, where you can place mapobjects or mapcharacters.
Definition: mapsquare.h:234
s_int8 load(string fname)
Load a map from a filename.
Definition: landmap.cc:131
u_int16 nbr_of_mapcharacters() const
Get the number of mapcharacters that are on this map.
Definition: landmap.h:107
void resize_area(u_int16 nl, u_int16 nh)
Resize the area.
Definition: mapsquare.cc:237
void set_map(landmap *m)
Puts the mapcharacter on a landmap.
Representation of characters on a landmap.
Definition: mapcharacter.h:139
void remove_mapobject(u_int16 smap, u_int16 px, u_int16 py, u_int16 mobjnbr)
Remove a mapobject from the map.
Definition: landmap.cc:374
~landmap()
Destructor.
Definition: landmap.cc:36
s_int8 put_state(ogzstream &file) const
Saves the mapcharacter&#39;s state into an opened file.
static void put_version(ogzstream &file, u_int16 version)
Sets the version number of a file.
Definition: fileops.cc:361
void clear()
Cleanup the map.
Definition: landmap.cc:41
Base class for objects that want to register events.
Definition: event_list.h:56
s_int8 get_state(igzstream &file)
Restore the landmap&#39;s state from an opened file.
Definition: landmap.cc:211
s_int8 delete_submap(u_int16 pos)
Remove a landsubmap from the landmap.
Definition: landmap.cc:341
u_int16 nbr_of_submaps() const
Get the number of landsubmaps that the map owns.
Definition: landmap.h:98
#define s_int8
8 bits long signed integer
Definition: types.h:44
Objects that can be placed on a landmap.
Definition: mapobject.h:50
s_int8 get_state(igzstream &file)
Restore the mapcharacter&#39;s state from an opened file.
Area of mapsquares, for use with landmap.
Definition: mapsquare.h:372