File: gameData.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 (269 lines) | stat: -rw-r--r-- 6,058 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
/****************************************************************
**
** Attal : Lords of Doom
**
** gameData.cpp
** Manage data of a whole game
**
** Version : $Id: gameData.cpp,v 1.14 2004/07/08 19:24:49 lusum Exp $
**
** Author(s) : Pascal Audoux
**
** Date : 05/01/2002
**
** 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 "gameData.h"

// generic include files
// include files for QT
#include <qtextstream.h>
// application specific include files
#include "libCommon/artefact.h"
#include "libCommon/dataTheme.h"
#include "libCommon/genericBase.h"
#include "libCommon/genericBonus.h"
#include "libCommon/genericBuilding.h"
#include "libCommon/genericCell.h"
#include "libCommon/genericChest.h"
#include "libCommon/genericEvent.h"
#include "libCommon/genericLord.h"
#include "libCommon/genericMap.h"
#include "libCommon/genericMapCreature.h"
#include "libCommon/genericPlayer.h"
#include "libServer/parser.h"
#include "libCommon/quest.h"
#include "libServer/questManager.h"

extern DataTheme DataTheme;

/** add comments here */
GameData::GameData()
	:_nbPlayer( 0 ), _map( 0 )
{
	_players.setAutoDelete( true );
	_bases.setAutoDelete( true );
	_buildings.setAutoDelete( true );
	_lords.setAutoDelete( true );
	_events.setAutoDelete( true );
	_creatures.setAutoDelete( true );
	_quests = new QuestManager();
	_nbArtefact = 0;
	_nbBonus = 0;
	_nbChest = 0;

	reinit();
}

GameData::~GameData()
{
/*	if( _quests ) {
		delete _quests;
	}*/
}

void GameData::save( QTextStream * ts )
{
	uint i;

	*ts << "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE scenario>" << endl;
	*ts << "<scenario nbPlayer=\"" << getNbPlayer() << "\">" << endl;
	*ts << "\t<name>" << getScenarioName() << "</name>" << endl;
	*ts << "\t<theme>" << getScenarioTheme() << "</theme>" << endl;
	*ts << "\t<description>" << endl;
	*ts << "\t\t" << getScenarioDescription() << endl;
	*ts << "\t</description>" << endl;

	_map->save( ts );

	for( i = 0; i < _events.count(); i++ ) {
		_events.at( i )->save( ts, 1 );
	}

	for( i = 0; i < _bases.count(); i++ ) {
		_bases.at( i )->save( ts, 1 );
	}

	for( i = 0; i < _buildings.count(); i++ ) {
		_buildings.at( i )->save( ts, 1 );
	}

	for( i = 0; i < _lords.count(); i++ ) {
		_lords.at( i )->save( ts, 1 );
	}

	for( i = 0; i < _creatures.count(); i++ ) {
		_creatures.at( i )->save( ts, 1 );
	}

	for( i = 0; i < getNbPlayer(); i++ ) {
		_players.at( i )->save( ts, 1 );
	}

	_quests->save( ts, 1 );

	*ts << "</scenario>" << endl;
}

void GameData::reinit()
{
	if( _map ) {
		_map->clear();
	}
	_bases.clear();
	_buildings.clear();
	_lords.clear();
	_events.clear();
	_creatures.clear();
	_quests->clear();
	_nbPlayer = 0;
	_nbArtefact = 0;
	_nbBonus = 0;
	_nbChest = 0;
	for( uint i = 0; i < DataTheme.lords.count(); i++ ) {
		GenericLord * lord = new GenericLord();
		lord->setId( i );
		_lords.append( lord );
	}
}

GenericCell * GameData::getCell( uint row, uint col )
{
	if( ( row < _map->getHeight() ) && ( col < _map->getWidth() ) ) {
		return _map->at( row, col );
	} else {
		return 0;
	}
}

uint GameData::getBaseNumber()
{
	return _bases.count();
}

void GameData::loadMap( QTextStream * stream, int width, int height )
{
	if( _map ) {
		delete _map;
	}
	_map = new GenericMap();
	_map->load( stream, width, height );
}

GenericBase * GameData::getNewBase() 
{
	GenericBase * base = new GenericBase();
	_bases.append( base );
	base->setOwner(0);
	return base;
}
	
void GameData::setBase2Player( int idBase, int player ) 
{
	if( ( _players.at( player ) ) && ( _bases.at( idBase ) ) ) {
		_bases.at( idBase )->setOwner( _players.at( player ) );
		_players.at( player )->addBase( _bases.at( idBase ) );
	}
}
	
void GameData::setBuilding2Player( int idBuild, int player )
{
	if( ( _players.at( player ) ) && ( _buildings.at( idBuild ) ) ) {
		_buildings.at( idBuild )->setOwner( _players.at( player ) );
		_players.at( player )->addBuilding( _buildings.at( idBuild ) );		
	}
}

GenericLord * GameData::getLord( int idLord )
{
	return _lords.at( idLord );
}

GenericPlayer * GameData::getPlayer( int num )
{
	return _players.at( num );
}

void GameData::setLord2Player( int idLord, int player )
{
	if( ( _players.at( player ) ) && ( _lords.at( idLord ) ) ) {
		_lords.at( idLord )->setOwner( _players.at( player ) );
		_players.at( player )->addLord( _lords.at( idLord ) );		
	}
}

GenericEvent * GameData::getNewArtefact()
{
	GenericEvent * ret = new GenericEvent();
	GenericArtefact * artefact = new GenericArtefact();
	ret->setArtefact( artefact );
	_events.append( ret );
	return ret;
}

GenericEvent * GameData::getNewBonus()
{
	GenericEvent * ret = new GenericEvent();
	GenericBonus * bonus = new GenericBonus();
	ret->setBonus( bonus );
	_events.append( ret );
	return ret;

}

GenericEvent * GameData::getNewChest()
{
	GenericEvent * ret = new GenericEvent();
	GenericChest * chest = new GenericChest();
	ret->setChest( chest );
	_events.append( ret );
	return ret;

}

GenericBuilding * GameData::getNewBuilding()
{
	GenericBuilding * ret = new GenericBuilding();
	ret->setId( _buildings.count() );
	_buildings.append( ret );
	return ret;
}

Quest * GameData::getNewQuest()
{
	Quest * ret = new Quest();
	_quests->appendSecondaryQuest( ret );
	return ret;
}

Quest * GameData::getMainQuest()
{
	Quest * ret = _quests->getMainQuest();

	if( ! ret ) {
		ret = new Quest();
		_quests->setMainQuest( ret );
	}

	return ret;
}


GenericMapCreature * GameData::getNewMapCreature()
{
	GenericMapCreature * ret = new GenericMapCreature();
	_creatures.append( ret );
	return ret;
}