~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
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
/* 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 "gui.h"
#include "screenPlayfield.h"
#include "engine/objects/object.h"
#include "engine/objects/mapCell.h"
#include "engine/objects/unit.h"
#include "engine/objects/structure.h"

#include "engine/scenario.h"
#include "engine/screen.h"
#include "engine/house.h"

#include "engine/sidebar/sidebar.h"
#include "engine/sidebar/sidebarAction.h"
#include "engine/sidebar/sidebarStructurePlace.h"

#include "map.h"
#include "minimap.h"

#include "engine/mapCellAnimSingle.h"

cScreenPlayfield::cScreenPlayfield(cOD2 *pEngine, cInterface< cOD2 > *pParent) : cButton<cOD2>(pEngine, pParent, UPoint(240, 160)), _scale(engineGet()->resourcesGet()->getMapScale(engineGet()->scenarioGet()->mapScaleLevelGet())) {
    word Tactical = engineGet()->scenarioGet()->mapTacticalGet();

    _map = engineGet()->scenarioGet()->mapGet()->posFromIndex(Tactical);

    _mapCell = engineGet()->scenarioGet()->mapGet()->mapCellGet(engineGet()->scenarioGet()->mapCursorGet());

    _surfaceFog			= new cVideoSurface(_size);
    _surfaceLandscape	= new cVideoSurface(_size);
    _surfaceUnits		= new cVideoSurface(_size);
    _surfaceCellAnims	= new cVideoSurface(_size);
    _surfaceTileHighlight = 0;

    _landscapeRedraw = true;

    _surfaceUnits->colorKeySet(0xFF);
}

cScreenPlayfield::~cScreenPlayfield() {

    delete _surfaceFog;
    delete _surfaceLandscape;
    delete _surfaceUnits;
    delete _surfaceTileHighlight;
    delete _surfaceCellAnims;
}

void cScreenPlayfield::screenPrep() {
    // Select the structure
    if((*_mapCell)->hasStructure())
	engineGet()->screenGet()->sidebarGet()->selectStructure((cStructure*) (*_mapCell)->objectGet());
}

void cScreenPlayfield::drawLandscape() {
    UPoint	tilesMax = engineGet()->screenGet()->getScreenTilesMax();
    uint16_t	tilesY = engineGet()->screenGet()->getScreenTilesY();
    word		  mapIndex	= 0;
    cMapCell	**mapCell;

    // Update the drawn X/Y
    _landscape = _map;

    // Calculate the top-left index
    mapIndex = engineGet()->scenarioGet()->mapGet()->posToIndex(_map);

    // Loop for each tile of the visible playfield
    for(UPoint p(0,0); p.y < tilesMax.y; ++p.y, mapIndex += 0x40) {

	// Get pointer to tiles
	mapCell = engineGet()->scenarioGet()->mapGet()->mapCellGet(mapIndex);

	for(p.x = 0; p.x < tilesMax.x; ++p.x) {

	    // FIXME: Error out here?
	    if(!mapCell)
		break;

	    // Draw tile to playfield
	    SDL_Surface *tile = *engineGet()->resourcesGet()->mapTileGet((*mapCell)->tileGet());

	    // If there is an object at this tile, use the house palette to draw the tile
	    if((*mapCell)->objectGet())
		SDL_SetColors(tile, (*mapCell)->objectGet()->houseGet()->paletteGet()->colors, 0, (*mapCell)->objectGet()->houseGet()->paletteGet()->ncolors);

	    // Draw the tile
	    _surfaceLandscape->surfacePut(tile, p<<4);

	    // Draw any temp tile or overlay
	    if((*mapCell)->tileTmpGet() != 0xFFFFFFFF) {

		tile = *engineGet()->resourcesGet()->mapTileGet((*mapCell)->tileTmpGet());

		if((*mapCell)->tmpHouseGet())
		    SDL_SetColors(tile, (*mapCell)->tmpHouseGet()->paletteGet()->colors, 0, (*mapCell)->tmpHouseGet()->paletteGet()->ncolors);

		SDL_SetColorKey(tile, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(tile->format, 0,0,0));
		_surfaceLandscape->surfacePut(tile, p<<4);

	    }

	    mapCell++;
	}
    }

}

void cScreenPlayfield::drawUnit(cUnit *pUnit) {
    UPoint p;
    pUnit->objectXYGet(p);

    p -= _map;

    p <<= 4;

    p += (pUnit->getMapTile() & 0xFF) >> 4;

    pUnit->draw();
    p -= 20;
    _surfaceUnits->surfacePut(pUnit->surfaceGet(), p);
}

void cScreenPlayfield::drawObjects() {
    map< uint32_t, cUnit* >			 *units = engineGet()->unitsGet();
    map< uint32_t, cUnit* >::iterator  unitIT;

    _surfaceUnits->clear(0xFF);

    for(unitIT = units->begin(); unitIT != units->end(); ++unitIT) {
	if(unitIT->second->dataGet()->movementType == _UnitMovement_Flying)
	    continue;

	if(unitIT->second->isInPlayfield() && !unitIT->second->deleteGet())
	    drawUnit(unitIT->second);

    }

    for(unitIT = units->begin(); unitIT != units->end(); ++unitIT) {
	if(unitIT->second->dataGet()->movementType != _UnitMovement_Flying)
	    continue;

	if(unitIT->second->isInPlayfield() && !unitIT->second->deleteGet())
	    drawUnit(unitIT->second);

    }
}

void cScreenPlayfield::drawMapCellAnims() {
    UPoint	tilesMax = engineGet()->screenGet()->getScreenTilesMax();
    uint16_t	tilesY = engineGet()->screenGet()->getScreenTilesY();
    word		  mapIndex	= 0;
    cMapCellAnimSingle *anim = 0;

    // Calculate the top-left index
    mapIndex = engineGet()->scenarioGet()->mapGet()->posToIndex(_landscape);

    _surfaceCellAnims->clear(0xFF);
    _surfaceCellAnims->colorKeySet(0xFF);

    // Loop for each tile of the visible playfield
    for(UPoint p(0,0); p.y < tilesMax.y; ++p.y, mapIndex += 0x40) {

	for(p.x = 0; p.x < tilesMax.x; ++p.x) {

	    anim = engineGet()->mapCellAnimGet(mapIndex + p.x);
	    if(anim) {
		_surfaceCellAnims->surfacePut(*engineGet()->resourcesGet()->shpGet(anim->shpGet(), 0), p<<4);
	    }

	}

    }


}

void cScreenPlayfield::drawFog() {
    UPoint	tilesMax = engineGet()->screenGet()->getScreenTilesMax();
    uint16_t	tilesY = engineGet()->screenGet()->getScreenTilesY();
    word		  mapIndex	= 0;
    cMapCell	**mapCell;

    // Calculate the top-left index
    mapIndex = engineGet()->scenarioGet()->mapGet()->posToIndex(_landscape);

    _surfaceFog->clear(0xFF);
    _surfaceFog->colorKeySet(0xFF);

    // Loop for each tile of the visible playfield
    for(UPoint p(0,0); p.y < tilesMax.y; ++p.y, mapIndex += 0x40) {

	// Get pointer to tiles
	mapCell = engineGet()->scenarioGet()->mapGet()->mapCellGet(mapIndex);

	for(p.x = 0; p.x < tilesMax.x; ++p.x) {
	    SDL_Surface *tileOverlay = *engineGet()->resourcesGet()->mapTileGet((*mapCell)->fogWarGet());

	    if(((*mapCell++)->isRevealed()))
		continue;

	    SDL_SetColorKey(tileOverlay, SDL_RLEACCEL, SDL_MapRGB(tileOverlay->format, 255,0,255));

	    _surfaceFog->surfacePut(tileOverlay, p<<4);
	}
    }
}

void cScreenPlayfield::draw() {

    // Does the landscape surface need updating?
    if(_landscapeRedraw || _landscape != _map)
	drawLandscape();

    //
    drawMapCellAnims();
    drawObjects();
    drawFog();

    surfaceGet()->surfacePut(_surfaceLandscape);
    surfaceGet()->surfacePut(_surfaceUnits);

    surfaceGet()->surfacePut(_surfaceCellAnims);
    surfaceGet()->surfacePut(_surfaceFog);

    if(!(*_mapCell)->hasStructure())
	return;

    drawTileSquares((*_mapCell)->getMapIndex(), (cStructure*) (*_mapCell)->objectGet());
}

void cScreenPlayfield::drawTileSquares(word pMapIndex, cStructure *pStructure) {
    cStructure  *structure  = 0;
    UPoint	size(1,1);

    cMapCell **mapCell = engineGet()->scenarioGet()->mapGet()->mapCellGet(pMapIndex);

    // Get top-left
    if((*mapCell)->objectGet())
	mapCell = ((cStructure*) (*mapCell)->objectGet())->mapCellGet();

    if((*mapCell)->getMapIndex() != 0xFFFF) 
	pMapIndex = (*mapCell)->getMapIndex();

    // Get the number of tiles and the layout
    uint16_t   foundationTiles	= engineGet()->resourcesGet()->foundationSizeGet(pStructure->dataGet()->foundationSize);
    const std::vector<uint16_t> &layoutTile = engineGet()->resourcesGet()->getLayoutTiles(pStructure->dataGet()->foundationSize);

    for(std::vector<uint16_t>::const_iterator layout = layoutTile.begin(); foundationTiles--; ++layout) {
	if((*layout & 0xC0) && !(*layout & 0x03))
	    size.y++;

	// Across but not down
	if((!(*layout & 0xC0) && *layout & 0x03))
	    size.x++;
    }

    // Draw a rectangle
    drawTileSquare(size * 16);

    // Calcualte the screen X/Y of the top-left of the structure
    UPoint p = engineGet()->scenarioGet()->mapGet()->posFromIndex(pMapIndex) - _map;

    // Draw the rectangle to the surface
    surfaceGet()->surfacePut(_surfaceTileHighlight, p<<4);
}

void cScreenPlayfield::drawTileSquare(UPoint pMap) {
    dword	White = SDL_MapRGB(static_cast<SDL_Surface*>(surfaceSDLGet())->format, 255, 255, 255);
    dword	Black = SDL_MapRGB(static_cast<SDL_Surface*>(surfaceSDLGet())->format, 0, 0, 0);

    //FIXME: destructor is fscked...
    //delete _surfaceTileHighlight;
    _surfaceTileHighlight = new cVideoSurface(pMap);

    _surfaceTileHighlight->colorKeySet(0);
    _surfaceTileHighlight->clear(0);

    SDL_Rect dstRect;
    dstRect.x = 0;
    dstRect.y = 0;
    dstRect.w = pMap.x;
    dstRect.h = pMap.y;
    SDL_FillRect(_surfaceTileHighlight->surfaceGet(), &dstRect, White);

    dstRect.x = 1;
    dstRect.y = 1;
    dstRect.w = pMap.x - 2;
    dstRect.h = pMap.y - 2;
    SDL_FillRect(_surfaceTileHighlight->surfaceGet(), &dstRect, Black);
}

void cScreenPlayfield::buttonClear() {

}

void cScreenPlayfield::buttonPressLeft(UPoint p) {
    cMapCell				**mapCell = 0;
    cSidebarAction			 *action = 0;

    // Convert screen X/Y into Tiles position
    p >>= 4;

    // Get the map cell for the position we have clicked
    mapCell = engineGet()->scenarioGet()->mapGet()->mapCellGet(_map + p);

    // Is the user executing a unit action?
    action = engineGet()->screenGet()->sidebarGet()->selectedActionGet();

    if(action) {
	action->actionExecute((*mapCell));
	return;
    }

    if(engineGet()->screenGet()->sidebarGet()->structurePlaceGet()) {
	engineGet()->screenGet()->sidebarGet()->structurePlaceGet()->placeStructure(mapCell);
	return;
    }

    if(engineGet()->screenGet()->sidebarGet()->selectedUnitGet())
	engineGet()->screenGet()->sidebarGet()->selectUnit(0);

    if(engineGet()->screenGet()->sidebarGet()->selectedStructGet()) 
	engineGet()->screenGet()->sidebarGet()->selectStructure(0);

    // Deactive the current map cell
    (*_mapCell)->objectDeActivate();

    //if((*mapCell)->isEmpty())
    _mapCell = mapCell;

    // Activate the map cell
    (*_mapCell)->objectActivate();	
}

void cScreenPlayfield::buttonPressRight(UPoint p) {

}
void cScreenPlayfield::buttonReleaseLeft(UPoint p) {

}

void cScreenPlayfield::buttonReleaseRight(UPoint p) {
    cSidebarStructurePlace *structPlace = engineGet()->screenGet()->sidebarGet()->structurePlaceGet();

    if(structPlace) {
	engineGet()->screenGet()->sidebarGet()->structurePlaceSet(0);
	delete structPlace;
    }

}

bool cScreenPlayfield::scrollCheck(UPoint p) {
    UPoint	tilesMax = engineGet()->screenGet()->getScreenTilesMax();

    // Get mouse column/row
    p /= 2;

    // Left of play field
    if(p.x < 1)
	if(_map.x > _scale.x) {
	    --_map.x;
	    redrawSet();
	}

    // Top of playfield
    if(p.y < 1)
	if(_map.y > _scale.y) {
	    --_map.y;
	    redrawSet();
	}

    // Right of playfield
    if(p.x > 118)
	if(_map.x < _scale.w + _scale.x - tilesMax.x) {
	    _map.x++;
	    redrawSet();
	}

    // Bottom of playfield
    if(p.y > 78)
	if(_map.y < _scale.h + _scale.y - tilesMax.y) {
	    _map.y++;
	    redrawSet();
	}

    // If we have to redraw the screen now, the minimaps frame position will need updating
    if(redrawGet())
	((cMinimap*) engineGet()->screenGet()->sidebarGet()->minimapGet())->positionUpdate();

    return true;
}

bool cScreenPlayfield::mouseMove(UPoint p) {

    scrollCheck(p);

    p /= 16;

    if(engineGet()->screenGet()->sidebarGet()->structurePlaceGet()) {

	word mapIndex = engineGet()->scenarioGet()->mapGet()->posToIndex(_map + p);

	draw();
	drawTileSquares(mapIndex, (cStructure*) engineGet()->screenGet()->sidebarGet()->structurePlaceGet()->structureGet()->objectHoldGet());
	redrawSet(false);
    }

    return true;
}