File: genericBase.h

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 (501 lines) | stat: -rw-r--r-- 11,184 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/****************************************************************
**
** Attal : Lords of Doom
**
** genericBase.h
** in order to manage bases...
**
** Version : $Id: genericBase.h,v 1.13 2004/12/04 14:25:51 lusum Exp $
**
** Author(s) : Pascal Audoux
**
** Date : 10/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.
**
****************************************************************/


#ifndef GENERICBASE_H
#define GENERICBASE_H
 

// generic include files
// include files for QT
#include <qstring.h>
#include <qptrlist.h>
#include <qxml.h>
// application specific include files
#include "libCommon/creature.h"
#include "libCommon/define.h"
#include "libCommon/log.h"
#include "libCommon/genericLord.h"
#include "libCommon/genericInsideBuilding.h"
#include "libCommon/genericMapDisposition.h"

class QTextStream;

class InsideAction;
class GenericBuilding;
class GenericFightUnit;
class GenericPlayer;
class PriceMarket;
//class InsideBuildingModel;

/**              ------------------------------
 *                          GenericBase
 **              ------------------------------ */

class GenericBase
{
public:
	/** Constructor */
	GenericBase();

	virtual ~GenericBase();
	
	/** When a lord enters in the base */
	virtual void enter( GenericLord * lord ) { 
		if( !setVisitorLord( lord ) ) {
			logEE( "Enter base failed" );
		}
	}
	
	/** When the lord goes out of the base */
	virtual void out( GenericLord * ) {
		if( isVisitorLord() ) {
			removeVisitorLord();
		} else { 
			logEE( "No visiting lord found" );
		}
	}
	
	/** Load base */
	virtual bool load( QTextStream * );

	/** Save base */
	void save( QTextStream * ts, int indent = 0 );

	/** Collect ressource 'num' */
	int collectRessource( int num );
	
	/** Set id to the base */
	void setId( int id ) { _id = id; }
	
	/** Get id */
	int getId() { return _id; }
	
	/** Set 'race' */
	virtual void setRace( int race ) { _race = race; }

	/** Get 'race' */
	int getRace() { return _race; }

	/** Set name */
	void setName( const QString & name ) { _name = name; }
	
	/** Get name */
	QString getName() { return _name; } 

	GenericCell * getCell() { return _currentCell; }

	virtual void setPosition( GenericCell * cell );

	/** Set owner of the building */
	virtual void setOwner( GenericPlayer * player ) { _player = player; }

	/** Return owner of the building */
	GenericPlayer * getOwner() { return _player; }
	
	/** Exchange lord (garrison and visitor) */
	void exchangeLords();
	
	/** Set visitor */
	inline bool setVisitorLord( GenericLord * lord );
	
	/** Set garrison */
	inline bool setGarrisonLord( GenericLord * lord );
	
	/** Get visitor */
	inline GenericLord * getVisitorLord();
	
	/** Get visitor unit 'num' */
	inline GenericFightUnit * getVisitorUnit( int num );
	
	/** Set visitor unit 'num' */
	inline void setVisitorUnit( int num, GenericFightUnit * troop );
	
	/** Get garrison */
	inline GenericLord * getGarrisonLord();
	
	/** Get garrison unit 'num' */
	inline GenericFightUnit * getGarrisonUnit( int num );
	
	/** Set garrison unit 'num' */
	inline void setGarrisonUnit( int num, GenericFightUnit * troop );
	
	/** Remove visitor */
	inline GenericLord * removeVisitorLord();
	
	/** Remove garrison */
	inline GenericLord * removeGarrisonLord();
	
	/** Tell if there is a visitor */
	inline bool isVisitorLord();
	
	/** Tell if there is a garrison */
	inline bool isGarrisonLord();

	/** Give unit 'num' of the base */
	GenericFightUnit * getUnit( int num ) { return _units[num]; }
	
	/** Set unit 'num' of the base */
	void setUnit( int num, GenericFightUnit * troop ) { _units[num] = troop; }

	bool canAddGarrison( Creature * creature );

	void addGarrison( Creature * creature, int num );

	uint getDoorRow();

	uint getDoorCol();

	GenericMapDisposition::DispositionType getDisposition( uint row, uint col );

	int getPopulation() { return _population; }

	void setPopulation( int population ) { _population = population; }

	void getInitPopul();
	
	void getPopGrowth();
	
	void getPopLoss();

	QString getModelName();

	void addBuilding( GenericInsideBuilding * );

	void addBuilding( int type );

	uint getBuildingCount() { return _buildings.count(); }

	GenericInsideBuilding * getBuilding( uint num );

	GenericInsideBuilding * getBuildingByType( uint type );

	bool hasBuildingType( uint type );

	void removeBuilding( GenericInsideBuilding * building );
	
	void removeBuilding( int type );

	void removeBuildingType( uint type );

	uint getForbiddenBuildingsNumber();

	int getForbiddenBuilding( uint num );

	void addForbiddenBuilding( int num );

	bool isForbidden( int num );

	bool canBuild() { return _canBuild; }

	void canBuild( bool state ) { _canBuild = state; }
	
	bool canSell() { return _canSell; }

	void canSell( bool state ) { _canSell = state; }
	
	bool isUnitBought() { return _bought; }

	void isUnitBought( bool state ) { _bought = state; }

	int getCreatureProduction( Creature * creature );

	void initCreatureProduction();

	void buyCreature( Creature * creature, int number );

	int getVision();
	
	PriceMarket * getPriceMarket() { return _price;}
	
	void setPriceMarket(PriceMarket * price) { _price = price;}

protected:
	int _id;
	int _race;
	QString _name;
	bool _canBuild;
	bool _canSell;
	bool _bought;
	GenericCell * _currentCell;
	uint _population;

	GenericPlayer * _player;
	QPtrList<GenericInsideBuilding> _buildings;
	GenericLord * _visitorLord, * _garrisonLord;
	GenericFightUnit * _units[ MAX_UNIT ];
	QPtrList<int> _forbidden;
	CreatureCounter _counter;
	PriceMarket  * _price;
};


/** Model for genericBase */
class GenericBaseModel : public GenericMapDisposition
{
public:
	/** Constructor */
	GenericBaseModel();

	/** \return Returns the name of the model */
	QString getModelName() { return _modelName; }

	/** Sets the name of the model */
	void setModelName( QString name ) { _modelName = name; }

	int getPopulation() { return _population; }

	void setPopulation( int population ) { _population = population; }
	
	int getPopGrowth() { return _growth; }

	void setPopGrowth( int growth ) { _growth = growth; }
	
	int getPopLoss() { return _loss; }

	void setPopLoss( int loss ) { _loss = loss; }

	int getRace() { return _race; }

	void setRace( int race ) { _race = race; }

	int getVision() { return _vision; }

	void setVision( int vision ) { _vision = vision; }
	
	void addBuilding( InsideBuildingModel * );

	void removeBuilding( uint num );
	
	InsideBuildingModel * getBuildingModel( int );

	uint getBuildingCount() { return _buildings.count(); }
	
	PriceMarket * getPriceMarket() { return _price;}
	
	void setPriceMarket(PriceMarket * price) { _price = price;}
	
	void save( QTextStream * ts, int indent = 0 );
	
protected:	
	QString _modelName;
	int _race;
	int _population,_growth,_loss;
	int _vision;
	QPtrList<InsideBuildingModel> _buildings;
	PriceMarket * _price;
};

/** List of GenericBaseModel */
class BaseList : public QPtrList<GenericBaseModel>
{
public:
	/** Constructor */
	BaseList();

	/** Return base corresponding to 'type' */
	GenericBaseModel * at( int type ) { return QPtrList<GenericBaseModel>::at( type ); }
	
	/** Return base corresponding to 'typeName' */
	GenericBaseModel * at( QString typeName );
	
// 	/** Clear list */
// 	void clear();

	bool save();
	
	/** Init list */
	bool init();
	
// 	/** Add a new type of base */
// 	int addType( QString typeName );
	
// 	/** Get type name corresponding to 'type' */
// 	QString getTypeName( int type ) { return *(_type.at( type )); }
	
private:
	QPtrList<QString> _type;
};

/** Parser for BaseList */
class BaseHandler : public QXmlDefaultHandler
{

public:
	/** Construtor */
	BaseHandler( BaseList * list );

	/** Return the error protocol if parsing failed */
	QString errorProtocol() { return _errorProt; }

	/** Before starting parsing */
	bool startDocument();

	/** Define Start elements and associated actions */
	bool startElement( const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& atts );

	/** Define End elements and associated actions */
	bool endElement( const QString& namespaceURI, const QString& localName, const QString& qName );

	/** Define what to do of characters */
	bool characters( const QString& ch );

	/** Error function */
	bool fatalError( const QXmlParseException& exception );

private:
	QString _errorProt;
	BaseList * _list;
	GenericBaseModel * _base;
	int _race, _level;
	int _unRace, _unLevel;
	InsideBuildingModel * _building;
	InsideAction * _action;
	QTextStream * _stream;
	QString _baseName;
	int _ress,_mress,_csress;
	uint _height, _width;
	PriceMarket * _price;

	enum State {
		StateInit,
		StateDocument,
		StateBase,
		StateName,
		StateDisposition,
		StatePopulation,
		StatePopulationValue,
		StatePopulationGrowth,
		StatePopulationLoss,
		StateRess,
		StateRessCost,
		StateBuilding,
		StateBuildingName,
		StateBuildingDescription,
		StateBuildingCost,
		StateBuildingMantCost,
		StateBuildingRequire,
		StateBuildingReplace,
		StateBuildingX,
		StateBuildingY,
		StateBuildingAction,
		StateBuildingActionParam
	};
	State _state;
};


/*************************************************************/

inline bool GenericBase::setVisitorLord( GenericLord * lord )
{
	if( isVisitorLord() )
		return false;
	
	_visitorLord = lord;
	return true;
}

inline bool GenericBase::setGarrisonLord( GenericLord * lord )
{
	if( isGarrisonLord() )
		return false;
	
	_garrisonLord = lord;
	return true;
}

inline GenericLord * GenericBase::getVisitorLord()
{
	return _visitorLord;
}

inline GenericFightUnit * GenericBase::getVisitorUnit( int num )
{
	if( isVisitorLord() ) {
		return getVisitorLord()->getUnit( num );
	}
	
	return 0;
}

inline void GenericBase::setVisitorUnit( int num, GenericFightUnit * troop )
{
	if( isVisitorLord() ) {
		getVisitorLord()->setUnit( num, troop );
	}
}

inline GenericLord * GenericBase::getGarrisonLord()
{
	return _garrisonLord;
}
	
inline GenericFightUnit * GenericBase::getGarrisonUnit( int num )
{
	if( isGarrisonLord() ) {
		return getGarrisonLord()->getUnit( num );
	} else {
		return _units[num];
	}
}

inline void GenericBase::setGarrisonUnit( int num, GenericFightUnit * troop )
{
	if( isGarrisonLord() ) {
		getGarrisonLord()->setUnit( num, troop );
	} else {
		_units[num] = troop;
	}
}

inline GenericLord * GenericBase::removeVisitorLord()
{
	GenericLord * temp = _visitorLord;
	_visitorLord = 0;
	
	return temp;
}

inline GenericLord * GenericBase::removeGarrisonLord()
{
	GenericLord * temp = _garrisonLord;
	_garrisonLord = 0;
	
	return temp;
}

inline bool GenericBase::isVisitorLord()
{
	return( _visitorLord != 0 );
}

inline bool GenericBase::isGarrisonLord()
{
	return( _garrisonLord != 0 );
}

#endif // GENERICBASE_H