~proyvind/doonlunacy/od2

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
/* OD2 - Dune II Clone
 *  
 * Copyright (C) 2009 Robert Crossfield		<robert.crossfield@strobs.com>
 
 * 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 3 of the License, 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.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * 
 * $Id$
 * 
 */

#include "stdafx.h"
#include "od2.h"
#include "engine/house.h"
#include "engine/objects/object.h"
#include "engine/objects/unit.h"
#include "engine/objects/structure.h"
#include "engine/script/scriptEmc.h"
#include "engine/script/scriptEmcUnit.h"
#include "engine/script/scriptEmcStructure.h"

#include<iostream>

cDebug::cDebug( cOD2 *pEngine ) : cBase< cOD2 >( pEngine ) {
	_break = false;

	commandSetup();

	if(pEngine->debugLevelGet() > 0) {

		engineGet()->gameImmediateBuild( true );
		engineGet()->gameFreeBuild( true );
	}


}

cDebug::~cDebug() {

}

string cDebug::dumpStack( cScriptData *pScriptData ) {
	stringstream tmp;

	return tmp.str();
}


string cDebug::dumpRegisters( cScriptData *pScriptData ) {
	stringstream tmp;

	for( short int i = 0; i < 5; ++i ) {

		// Register3 is always set to the humanhouse 
		if( i == 3 )
			continue;

		tmp << "R" << i << ": 0x" << hex << pScriptData->Register[i];	
		tmp << "  ";
	}
	tmp << "\n";

	return tmp.str();
}

void cDebug::dumpUnit( cUnit *pUnit ) {
	stringstream tmp;

	cScriptUnitData *scriptData = (cScriptUnitData*) pUnit->scriptDataGet();
	tmp << scriptData->_unit->houseGet()->dataGet()->name;
	tmp << "[" << scriptData->_unit->indexGet() << "] ";

	tmp << " PC: " << scriptData->scriptLine;
	tmp << "  Regs: ";
	tmp << dumpRegisters( scriptData );

	write(0, tmp.str(), false);
}

void cDebug::dumpUnits( cHouse *pHouse ) {
	multimap< uint32_t, cUnit* >			*units;
	multimap< uint32_t, cUnit* >::iterator unitIT;

	units = pHouse->unitsGet();

	for( unitIT = units->begin(); unitIT != units->end(); ++unitIT ) {
		dumpUnit( unitIT->second );
	}

}

void cDebug::dumpStructure( cStructure *pStructure ) {

}

void cDebug::dumpStructures( cHouse *pHouse ) {
	multimap< uint32_t, cStructure* >				*structures;
	multimap< uint32_t, cStructure* >::iterator	 structureIT;

	structures = pHouse->structuresGet();

	for( structureIT = structures->begin(); structureIT != structures->end(); ++structureIT ) {

	}

}

void cDebug::dumpHouses() {
	map< eHouse, cHouse* >				*houses = engineGet()->housesGet();
	map< eHouse, cHouse* >::iterator	 houseIT;
	stringstream	tmp;

	for( houseIT = houses->begin(); houseIT != houses->end(); ++houseIT ) {

		tmp << "[" << houseIT->second->houseIDGet() << "]: " << houseIT->second->dataGet()->name;
		tmp << "\n";
	
		dumpUnits( houseIT->second );
	}

	write( 0, tmp.str() );
}

bool cDebug::commandSetVar( string pParameters ) {
	
	if( pParameters == "build" )
		engineGet()->gameImmediateBuild( true );

	return false;
}

bool cDebug::commandChangeHouse( string pParameters ) {
	uint32_t House = atoi(pParameters.c_str());
	
	engineGet()->houseSet( (eHouse) House );

	return false;
}

bool cDebug::commandContinue( string pParameters ) {
	
	_break = false;
	return false;
}

bool cDebug::commandHelp( string pParameters ) {
	stringstream			 response;
	const dbgCommand		*Items = _commands;
	
	write( 0, "Commands" );

	for(Items = _commands; Items->_Command; Items++) {
		response.str("");
		response << "'" << Items->_CommandShort << "' :  " << Items->_Details;

		write(0, response );
	}

	return true;
}

bool cDebug::commandDump( string pParameters ) {

	if(!pParameters.size()) {
		dumpHouses();		
	}

	return true;
}

bool cDebug::commandScenario( string pParameters ) {
	uint32_t scenario = atoi(pParameters.c_str());

	engineGet()->missionStart( engineGet()->houseHumanGet()->houseIDGet(), scenario );

	return false;
}

void cDebug::commandSetup() {
	static const dbgCommand cmdHelp[24] = {
	    {"setting",	"set",	"set variable",	&cDebug::commandSetVar},
	    {"house",	"ch","change house",	&cDebug::commandChangeHouse},
	    {"continue","x", "continue game",	&cDebug::commandContinue},
	    {"dumpo",	"d", "Dump an object",	&cDebug::commandDump},
	    {"help",	"h", "Show this help",	&cDebug::commandHelp},
	    {"scen",	"",  "Load scenario",	&cDebug::commandScenario},
	    {0},
	};

	_commands = cmdHelp;

}

bool cDebug::commandProcess( string pCommand ) {
	const dbgCommand		*Items = _commands;
	string					 parameters;
	
	uint32_t					 pos;
	pos = pCommand.find(" ");

	if(pos!=string::npos)
		parameters = pCommand.substr(pos+1);

	pCommand = pCommand.substr(0, pos);

	for(Items = _commands; Items->_Command; Items++) {
		
		if( pCommand.compare( Items->_Command ) == 0 || (pCommand == Items->_CommandShort) ) {
			if(Items->_Func) {
				return (this->*Items->_Func)( parameters );
			}
			//return -1;
		}
	}

	return true;
}

bool cDebug::cycle() {

	const SDL_Event &Event = engineGet()->sdlEventGet();
	
	if(Event.type == SDL_KEYUP ) {
		SDL_KeyboardEvent  key = Event.key;	
		
		if( key.keysym.sym == SDLK_d && key.keysym.mod & KMOD_LCTRL) {

			_break = true;
			
			write( 0, "Debugger Entered", true);
		}
	}
	
	if(_break) {

		while( commandProcess( prompt("") ));
		
	}

	return true;
}

string cDebug::prompt( string pText ) {
	string response;
	
	write( 0, pText );
	write( 0, ">", false );

	getline( cin, response );

	return response;
}

void cDebug::write( uint32_t pWidth, string pText, bool pEndLine ) {
	

	cout << setw(pWidth) << pText;
	if(pEndLine)
		cout << endl;
}

void cDebug::write( uint32_t pWidth, stringstream &pText, bool pEndLine) { 
	write( pWidth, pText.str(), pEndLine);  
}