File: GridMap2D.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (502 lines) | stat: -rw-r--r-- 14,855 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
502
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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/>.
 *
 */

/*
 * Copyright (C) 2006-2010 - Frictional Games
 *
 * This file is part of HPL1 Engine.
 */

#include "hpl1/engine/scene/GridMap2D.h"
#include "hpl1/engine/math/Math.h"

#include "hpl1/engine/graphics/LowLevelGraphics.h"
#include "hpl1/engine/scene/Entity2D.h"

namespace hpl {

//////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

cGridMap2D::cGridMap2D(cVector2l avSize, cVector2l avGridSize, cVector2l avMaxGridSpan) {
	mvSize = avSize;
	mvGridSize = avGridSize;
	mvMaxGridSpan = avMaxGridSpan;
	mvGridNum = avSize / avGridSize + 1;
	mlGlobalCount = 0;

	mvGrids.resize(mvGridNum.x * mvGridNum.y);

	for (int i = 0; i < (int)mvGrids.size(); i++)
		mvGrids[i] = cGrid2D();
}

//-----------------------------------------------------------------------

cGridMap2D::~cGridMap2D() {
	for (tGrid2DObjectMapIt it = m_mapAllObjects.begin(); it != m_mapAllObjects.end(); it++) {
		it->second->Destroy();
		hplDelete(it->second);
	}

	m_mapAllObjects.clear();
}

//-----------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////
// GRIDMAP PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

iGridMap2DIt *cGridMap2D::GetRectIterator(const cRect2f &aRect) {
	cVector2l vPos = cVector2l((int)floor(aRect.x / (float)mvGridSize.x),
							   (int)floor(aRect.y / (float)mvGridSize.y));
	cVector2l vSize = cVector2l((int)(aRect.w / (float)mvGridSize.x) + 1,
								(int)(aRect.h / (float)mvGridSize.y) + 1);

	// Check if we need yet another grid for x and y
	if (aRect.x + aRect.w >= (vPos.x + vSize.x) * mvGridSize.x)
		vSize.x++;
	if (aRect.y + aRect.h >= (vPos.y + vSize.y) * mvGridSize.y)
		vSize.y++;

	// Log("\nPos: %d:%d\n",vPos.x,vPos.y);
	// Log("Size: %d:%d\n\n",vSize.x,vSize.y);

	return hplNew(cGridMap2DRectIt, (this, vPos, vSize));
}

//-----------------------------------------------------------------------

bool cGridMap2D::AddEntity(iEntity2D *apEntity) {
	cGrid2DObject *pObject = hplNew(cGrid2DObject, (apEntity, this, mlHandleCount));
	apEntity->SetGrid2DObject(pObject);

	// Insert into the all map.
	m_mapAllObjects.insert(tGrid2DObjectMap::value_type(mlHandleCount, pObject));

	mlHandleCount++;
	return true;
}

//-----------------------------------------------------------------------

bool cGridMap2D::RemoveEntity(iEntity2D *apEntity) {
	cGrid2DObject *pObject = apEntity->GetGrid2DObject();
	if (pObject == NULL)
		return false;

	pObject->Destroy();
	m_mapAllObjects.erase(pObject->GetHandle());

	hplDelete(pObject);
	apEntity->SetGrid2DObject(NULL);

	return true;
}

//-----------------------------------------------------------------------

cGrid2D *cGridMap2D::GetAt(int alX, int alY) {
	if (alX >= mvGridNum.x || alX < 0 || alY >= mvGridNum.y || alY < 0)
		return NULL;

	return &mvGrids[alX + alY * mvGridNum.x];
}

//-----------------------------------------------------------------------

void cGridMap2D::DrawGrid(iLowLevelGraphics *apLowLevel, float afZ, cColor aCol) {
	for (int x = 0; x < mvGridNum.x; x++)
		for (int y = 0; y < mvGridNum.y; y++) {
			apLowLevel->DrawLineRect2D(cRect2f((float)(x * mvGridSize.x), (float)(y * mvGridSize.y),
											   (float)(mvGridSize.x), (float)(mvGridSize.y)),
									   afZ, aCol);
		}
}

//-----------------------------------------------------------------------

void cGridMap2D::DrawEntityGrids(iLowLevelGraphics *apLowLevel, cVector2f avWorldPos, float afZ, cColor aCol) {
	tGrid2DObjectMapIt it = m_mapAllObjects.begin();
	while (it != m_mapAllObjects.end()) {
		iEntity2D *pEntity = it->second->GetEntity();
		cRect2f Rect = pEntity->GetBoundingBox();
		Rect.x -= avWorldPos.x;
		Rect.y -= avWorldPos.y;
		apLowLevel->DrawLineRect2D(Rect, afZ, aCol);
		it++;
	}
}

//-----------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////
// GRIDMAP PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

cGridMap2DRectIt::cGridMap2DRectIt(cGridMap2D *apGridMap, cVector2l avPos, cVector2l avSize) {
	mpGridMap = apGridMap;
	mvPos = avPos;
	mvSize = avSize;
	mlType = 0;
	mpObject = NULL;
	mlGridNum = 0;
	mlGridAdd = 0;
	mlGridRowCount = 0;
	mbUpdated = false;

	// Increase the counter so you can load all objects.
	mpGridMap->mlGlobalCount++;

	mIt = mpGridMap->m_mapGlobalObjects.begin();
}

//-----------------------------------------------------------------------

bool cGridMap2DRectIt::HasNext() {
	if (!mbUpdated) {
		GetGridObject();
		mbUpdated = true;
	}

	if (mpObject)
		return true;
	else
		return false;
}

//-----------------------------------------------------------------------

iEntity2D *cGridMap2DRectIt::Next() {
	iEntity2D *pEntity = NULL;
	if (!mbUpdated) {
		GetGridObject();
		mbUpdated = true;
	}

	if (mpObject) {
		pEntity = mpObject->GetEntity();
		mIt++;
		mbUpdated = false;
	}
	return pEntity;
}

//-----------------------------------------------------------------------
/////////////////////////// PRIVATE //////////////////////////////////////
//-----------------------------------------------------------------------

void cGridMap2DRectIt::GetGridObject() {
	// bool bEnd = false;

	switch (mlType) {
	//// GLOBAL OBJECTS //////////////////
	case 0: {
		// No need to check if the GridObject has been used.
		// All objects in globla only occur once
		if (mIt == mpGridMap->m_mapGlobalObjects.end()) {
			// Log("Reached end of global\n");
			mlType = 1;
			// Check if the coord is in an outer bound
			if (mvPos.x < 0 || mvPos.y < 0 ||
				mvPos.x + mvSize.x > mpGridMap->mvGridNum.x ||
				mvPos.y + mvSize.y > mpGridMap->mvGridNum.y) {
				// Log("Has outer!\n");
				mIt = mpGridMap->m_mapOuterObjects.begin();
			} else {
				// Log("Has NO outer!\n");
				mIt = mpGridMap->m_mapOuterObjects.end();
			}
			// FIXME: fallthrough intended from here?
			// fallthrough
		} else {
			mpObject = mIt->second;
			// Log("Found the object in global!\n");
			break;
		}
	}
	// FIXME: fallthrough intended?
	// fallthrough

	//// OUTER OBJECTS //////////////////
	case 1: {
		// No need to loop this one, since they only occur once and
		// no other calls to the objects inside the grid has been made.
		if (mIt == mpGridMap->m_mapOuterObjects.end()) {
			// Log("End of outer!\n");
			// Log("Pos: %d:%d\n",mvPos.x,mvPos.y);
			// Log("Size: %d:%d\n",mvSize.x,mvSize.y);
			// Log("----------\n");
			mlType = 2;
			// Check if the rect is outside of the grid map
			// If so, we are done
			if (mvPos.x >= mpGridMap->mvGridNum.x || mvPos.y >= mpGridMap->mvGridNum.y ||
				mvPos.x + mvSize.x - 1 < 0 || mvPos.y + mvSize.y - 1 < 0) {
				// Log("Outside Grid!\n");
				mpObject = NULL;
				mlType = -1;
				break;
			}
			// Remove grids outside of the grid.
			// Pos below (0,0):
			if (mvPos.x < 0) {
				mvSize.x += mvPos.x;
				mvPos.x = 0;
			}
			if (mvPos.y < 0) {
				mvSize.y += mvPos.y;
				mvPos.y = 0;
			}

			// Size large than grid map
			if (mvPos.x + mvSize.x > mpGridMap->mvGridNum.x) {
				mvSize.x -= (mvPos.x + mvSize.x) - (mpGridMap->mvGridNum.x);
			}
			if (mvPos.y + mvSize.y > mpGridMap->mvGridNum.y) {
				mvSize.y -= (mvPos.y + mvSize.y) - (mpGridMap->mvGridNum.y);
			}

			// Log("Pos: %d:%d\n",mvPos.x,mvPos.y);
			// Log("Size: %d:%d\n",mvSize.x,mvSize.y);

			// Calculate numbers needed to iterate the grid
			mlGridNum = mvPos.x + mvPos.y * mpGridMap->mvGridNum.x;
			mlGridAdd = mpGridMap->mvGridNum.x - mvSize.x;
			mlGridRowCount = mvSize.y;
			mlGridColCount = mvSize.x;

			// Log("Num: %d Add: %d Row: %d Col: %d\n",mlGridNum,mlGridAdd,
			//		mlGridRowCount,mlGridColCount);

			// Rect is outsize of map.
			if (mvSize.x <= 0 || mvSize.y <= 0) {
				mlGridNum = 0;
				mIt = mpGridMap->mvGrids[mlGridNum].m_mapObjects.end();
				mlGridColCount = 0;
				mlGridRowCount = 0;
			} else {
				mIt = mpGridMap->mvGrids[mlGridNum].m_mapObjects.begin();
			}
			// FIXME: fallthrough intended from here?
			// fallthrough
		} else {
			// Log("Found the object in outer!\n");
			mpObject = mIt->second;

			// Set that is object has been used.
			mpObject->FirstTime(mpGridMap->mlGlobalCount);
			break;
		}
	}
	// FIXME: fallthrough intended?
	// fallthrough

	//// GRID OBJECTS //////////////////
	case 2: {
		// Search through the grids til one with GridObjects is found
		// or we are at the end
		while (true) {
			if (mIt == mpGridMap->mvGrids[mlGridNum].m_mapObjects.end()) {
				// Log("End of grid[%d] list!\n",mlGridNum);
				mlGridNum++;
				mlGridColCount--;
				if (mlGridColCount <= 0) {
					// Log("New row!\n\n");
					mlGridColCount = mvSize.x;
					mlGridRowCount--;
					if (mlGridRowCount <= 0) {
						mpObject = NULL;
						break;
					}
					mlGridNum += mlGridAdd;
				}
				mIt = mpGridMap->mvGrids[mlGridNum].m_mapObjects.begin();
			} else {
				mpObject = mIt->second;

				// Check if object already have been loaded.
				if (mpObject->FirstTime(mpGridMap->mlGlobalCount)) {
					// Log("Found the object in grid[%d]!\n",mlGridNum);
					break;
				} else {
					// Log("Found OLD object in grid[%d]!\n",mlGridNum);
					mIt++;
				}
			}
		}
	}
	}
}

//-----------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////
// GRIDOBJECT PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

cGrid2DObject::cGrid2DObject(iEntity2D *apEntity, cGridMap2D *apGridMap, unsigned int alHandle) {
	mpEntity = apEntity;
	mpGridMap = apGridMap;

	mvGridParents.resize(mpGridMap->GetMaxArraySize());
	for (int i = 0; i < (int)mvGridParents.size(); i++)
		mvGridParents[i] = NULL;

	mvPosition = cVector2l(-1000);
	mvGridSpan = cVector2l(-1, -1);
	mlHandle = alHandle;

	mbIsInOuter = false;
	mbIsInGLobal = false;
	mlCount = 0;

	mpEntity->UpdateBoundingBox();
	Update(mpEntity->GetBoundingBox());
}

//-----------------------------------------------------------------------

void cGrid2DObject::Destroy() {
	// Remove all old grids. This should only remove stuff that are necessary.
	for (int x = 0; x < mvGridSpan.x; x++)
		for (int y = 0; y < mvGridSpan.y; y++) {
			int lNum = x + y * mpGridMap->GetMaxGridSpan().x;
			if (mvGridParents[lNum]) {
				mvGridParents[lNum]->Erase(mlHandle);
				mvGridParents[lNum] = NULL;
			}
		}

	if (mbIsInGLobal) {
		mpGridMap->m_mapGlobalObjects.erase(mlHandle);
		mbIsInGLobal = false;
	} else if (mbIsInOuter) {
		mpGridMap->m_mapOuterObjects.erase(mlHandle);
		mbIsInOuter = false;
	}
}

//-----------------------------------------------------------------------

/**
 * \todo This function should be as optimized as possible.
 * \param &aRect
 */
void cGrid2DObject::Update(const cRect2f &aRect) {
	cVector2f vGSize((float)mpGridMap->GetGridSize().x, (float)mpGridMap->GetGridSize().y);
	cVector2l vPos((int)floor(aRect.x / vGSize.x), (int)floor(aRect.y / vGSize.y));

	// Calculate the num of extra grids in x,y direction that are needed.
	cVector2l vGridSpan((int)(aRect.w / vGSize.x), (int)(aRect.h / vGSize.y));
	vGridSpan += 1;

	// Log("Rect: %f:%f:%f:%f\n",aRect.x,aRect.y,aRect.w,aRect.h);
	// Log("GSize: %f:%f\n",vGSize.x,vGSize.y);
	// Log("Pos: %d:%d\n",vPos.x,vPos.y);

	// Test if the rect is to large or has negative size.
	// If so put it in the global map.
	// TODO: this is a long test and it would good to skip it for most stuff
	//  Fix that!
	if (aRect.h < 0 || aRect.w < 0 || vGridSpan.x >= mpGridMap->GetMaxGridSpan().x ||
		vGridSpan.y >= mpGridMap->GetMaxGridSpan().y) {
		if (mbIsInGLobal == false) {
			mbIsInGLobal = true;

			// Erase old positions
			for (int x = 0; x < mvGridSpan.x; x++)
				for (int y = 0; y < mvGridSpan.y; y++) {
					int lNum = x + y * mpGridMap->GetMaxGridSpan().x;
					if (mvGridParents[lNum]) {
						mvGridParents[lNum]->Erase(mlHandle);
						mvGridParents[lNum] = NULL;
					}
				}

			mpGridMap->m_mapGlobalObjects.insert(tGrid2DObjectMap::value_type(mlHandle, this));
			// Log("Added to global\n");
		}

		return;
	}

	// The amount not covered in the span calc above
	// Then check if this piece is out side of the last grid.
	if (aRect.x + aRect.w >= (vPos.x + vGridSpan.x) * vGSize.x)
		vGridSpan.x++;
	if (aRect.y + aRect.h >= (vPos.y + vGridSpan.y) * vGSize.y)
		vGridSpan.y++;

	// Log("GridSpan: %d:%d\n",vGridSpan.x,vGridSpan.y);

	if (vPos != mvPosition || vGridSpan != mvGridSpan) {
		// Remove all old grids. This should only remove stuff that are necessary.
		for (int x = 0; x < mvGridSpan.x; x++)
			for (int y = 0; y < mvGridSpan.y; y++) {
				int lNum = x + y * mpGridMap->GetMaxGridSpan().x;
				if (mvGridParents[lNum] != NULL) {
					mvGridParents[lNum]->Erase(mlHandle);
					mvGridParents[lNum] = NULL;
				}
			}

		if (mbIsInGLobal) {
			mpGridMap->m_mapGlobalObjects.erase(mlHandle);
			mbIsInGLobal = false;
		} else if (mbIsInOuter) {
			mpGridMap->m_mapOuterObjects.erase(mlHandle);
			mbIsInOuter = false;
		}

		mvGridSpan = vGridSpan;

		cGrid2D *pGrid = NULL;
		for (int x = 0; x < mvGridSpan.x; x++)
			for (int y = 0; y < mvGridSpan.y; y++) {
				pGrid = mpGridMap->GetAt(vPos.x + x, vPos.y + y);
				if (pGrid == NULL) {
					if (!mbIsInOuter) {
						mpGridMap->m_mapOuterObjects.insert(tGrid2DObjectMap::value_type(
							mlHandle, this));
						mbIsInOuter = true;
					}

				} else {
					pGrid->Add(this);
					mvGridParents[x + y * mpGridMap->GetMaxGridSpan().x] = pGrid;
				}
			}
	}
}

//-----------------------------------------------------------------------

} // namespace hpl