File: map.cpp

package info (click to toggle)
attal 0.9.2-1.2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,992 kB
  • ctags: 5,972
  • sloc: cpp: 44,510; sh: 160; makefile: 45
file content (248 lines) | stat: -rw-r--r-- 5,243 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/****************************************************************
**
** Attal : Lords of Doom
**
** map.cpp
** Manage the global view
**
** Version : $Id: map.cpp,v 1.8 2004/12/18 12:05:38 audoux Exp $
**
** Author(s) : Pascal Audoux - Cyrille Verrier
**
** Date : 02/08/2000
**
** Licence :    
**	This program is free software; you can redistribute it and/or modify
**   	it under the terms of the GNU General Public License as published by
**     	the Free Software Foundation; either version 2, or (at your option)
**      any later version.
**
**	This program is distributed in the hope that it will be useful,
** 	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
****************************************************************/

#include "map.h"

 
// generic include files
#include <stdlib.h>
// include files for QT
#include <qcanvas.h>
#include <qfile.h>
#include <qstring.h>
#include <qtextstream.h>
// application specific includes
#include "libCommon/log.h"
#include "libCommon/pathFinder.h"

#include "libClient/cell.h"
//#include "libClient/lord.h"

//
// ----- Map -----
//

Map::Map( QObject * parent , const char * name )
  : GraphicalMap( parent , name ),
    GenericMap()
{
	
}

Map::~Map()
{
	delete _path;

	// XXX: delete [][] 
	for( uint i = 0; i < _height ; i++) {
		delete [] _theCells[i];
		_theCells[i] = 0;
	}
	delete [] _theCells;
	_theCells = 0;
}

void Map::clear()
{
	if( _theCells != 0 ) {
		for( uint i = 0 ; i < _height ; i++ ) {
			for( uint j = 0; j < _width; j++ ) {
				delete (Cell*)_theCells[i][j];
			}
			delete [] _theCells[i];
		}
		delete [] _theCells;	
	}
	_theCells = 0;
	
	_height = 0;
	_width = 0;
	_path->clear();
}

void Map::newMap( int h, int w )
{
	clear();
	_height = h;
	_width = w;
	_theCells = new GenericCell **[_height];
	uint i;

	for( i = 0; i < _height; i++ ) {
		_theCells[i] = new GenericCell *[_width];
	}
	for( i = 0; i < _height; i++ ) {
		for( uint j = 0; j < _width; j++ ) {
			Cell * tempo;
			tempo = new Cell( i, j, this );
			tempo->show();
			// XXX: bof
			tempo->setType( 0 );
			_theCells[i][j] = (GenericCell *)tempo;
		}
	}
	_path->newMap( _height, _width, (GenericMap*)this );
	autoSize();
}

void Map::newUnknownMap( int h, int w )
{
	clear();
	_height = h;
	_width = w;
	_theCells = new GenericCell **[_height];
	uint i;

	for( i = 0; i < _height; i++ ) {
		_theCells[i] = new GenericCell *[_width];
	}
	for( i = 0; i < _height; i++ ) {
		for( uint j = 0; j < _width; j++ ) {
			Cell * tempo;
			tempo = new Cell( i, j, this );
			tempo->show();
			tempo->setType( 0 );
			_theCells[i][j] = (GenericCell *)tempo;
		}
	}

	_path->newMap( _height, _width, (GenericMap*)this );
	autoSize();
}

void Map::changeCell( int i,
		      int j,
		      int typ,
		      int transition,
		      int typtra,
		      uint decorationGroup, 
		      uint decorationItem )
{
	GenericMap::changeCell( i, j, typ, transition, typtra, decorationGroup, decorationItem );

	if( _theCells[i][j]->getType() == 0 ) {
		/*((Cell*)_theCells[i][j])->setType( typ );
		((Cell*)_theCells[i][j])->setTransition( transition );
		((Cell*)_theCells[i][j])->setTransitionCellType( typtra );
		((Cell*)_theCells[i][j])->setDecoration( decoration );*/
		((Cell*)_theCells[i][j])->show();
	}/* else {
		logEE( "Cell type should be unknown" );
	}*/

}

bool Map::load( QTextStream * ts, int width, int heigth )
{
	_width = width;
	_height = heigth;
	uint i, j;
	
	_theCells = (GenericCell ***) ( new Cell **[_height] );

	for( i = 0; i < _height; i++ ) {
		_theCells[i] = (GenericCell**) ( new Cell *[_width] );
	}
	int val;
	uint item;
	
	// Loading base cell
	for( i = 0; i < _height; i++ ) {
		for( j = 0; j < _width; j++ ) {
			if( ! ts->atEnd() ) {
				Cell * tempo;
				tempo = new Cell( i, j, this );
				tempo->show();
				*ts >> val;
				tempo->setType( val );
				_theCells[i][j] = (GenericCell*)tempo;
			} else {
				logEE("Stream too short");
				return false;
			}
		}
	}

	// Loading diversification cell
	for( i = 0; i < _height; i++ ) {
		for( j = 0; j < _width; j++ ) {
			if( ! ts->atEnd() ) {
				*ts >> val;
				_theCells[i][j]->setDiversification( val );
			} else {
				logEE("Stream too short");
				return false;
			}
		}
	}
	
	// Loading transition type
	for( i = 0; i < _height; i++ ) {
		for( j = 0; j < _width; j++ ) {
			if( ! ts->atEnd() ) {
				*ts >> val;
				_theCells[i][j]->setTransition( val );
			} else {
				logEE("Stream too short");
				return false;
			}
		}
	}	
	
	// Loading transition cell
	for( i = 0; i < _height; i++ ) {
		for( j = 0; j < _width; j++ ) {
			if( ! ts->atEnd() ) {
				*ts >> val;
				_theCells[i][j]->setTransitionCellType( val );
			} else {
				logEE("Stream too short");
				return false;
			}
		}
	}	
	
	// Loading decoration of the ground
	for( i = 0; i < _height; i++ ) {
		for( j = 0; j < _width; j++ ) {
			if( ! ts->atEnd() ) {
				*ts >> val;
				if( val ) {
					*ts >> item;	
					_theCells[i][j]->setDecoration( val, item );
				}
			} else {
				logEE("Stream too short");
				return false;
			}
		}
	}	
	
	
	_path = new PathFinder( _width, _height, this );
	autoSize();
	return true;	
}