Codebase list holotz-castle / c65ee577-f9eb-41fd-b02a-de608a9c8b30/main src / HCEnemyChaser.cpp
c65ee577-f9eb-41fd-b02a-de608a9c8b30/main

Tree @c65ee577-f9eb-41fd-b02a-de608a9c8b30/main (Download .tar.gz)

HCEnemyChaser.cpp @c65ee577-f9eb-41fd-b02a-de608a9c8b30/mainraw · history · blame

  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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
 * Holotz's Castle
 * Copyright (C) 2004 Juan Carlos Seijo Pérez
 * 
 * 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 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, write to the Free Software Foundation, Inc., 59 
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Juan Carlos Seijo Pérez
 * jacob@mainreactor.net
 */

/** Defines a chaser enemy.
 * @file    HCEnemyChaser.cpp
 * @author  Juan Carlos Seijo Pérez
 * @date    27/05/2004
 * @version 0.0.1 - 27/05/2004 - First version.
 */

#include <HCEnemyChaser.h>

HCEnemyChaser::HCEnemyChaser() : HCEnemy(HCENEMYTYPE_CHASER)
{
	chased = 0;
	state = HCCS_LEFT;
	cells = 0;
	chasePath = 0;
	tmpPath = 0;
	chaseIndex = 0;
}

bool HCEnemyChaser::Init(JImageSprite *sprites, HCMap *_map, HCRope **_ropes, s32 nRopes)
{
	if (HCEnemy::Init(sprites, _map, _ropes, nRopes))
	{
		// Uses param2 to set search depth for this enemy.
		// Param2 represents the percentage of the map covered by the search in the greates direction (H or V).
		searchDepth = (s32)JMax(param2 * (map->Rows() + map->Cols())/10, 3);
		ResetChaseMap();
		
		chasePath = new HCChasePath[searchDepth + 2];
		tmpPath = new HCChasePath[searchDepth + 2];
		
		return true;
	}
	
	return false;
}

void HCEnemyChaser::ResetChaseMap()
{
	if (!cells)
	{
		// Creates the cell array from the map
		cells = new HCChaseCell *[map->Rows()];

		for (s32 j = 0; j < map->Rows(); ++j)
		{
			cells[j] = new HCChaseCell[map->Cols()];
			for (s32 i = 0; i < map->Cols(); ++i)
			{
				cells[j][i].cur = map->Cell(j, i);
				cells[j][i].prev = 0;
				cells[j][i].tried = 0;
				cells[j][i].path.row = j;
				cells[j][i].path.col = i;
				cells[j][i].path.action = 0;
				cells[j][i].visited = false;
				cells[j][i].depth = 0;
				cells[j][i].numJumpedRows = 0;
			}
		}
	}
	else
	{
		// Only zeroes the chase map
		for (s32 j = 0; j < map->Rows(); ++j)
		{
			for (s32 i = 0; i < map->Cols(); ++i)
			{
				cells[j][i].prev = 0;

				// Search optimization
				switch (map->Cell(j, i)->Type())
				{
				case HCCELLTYPE_FLOOR:
				case HCCELLTYPE_CONTFLOOR:
					// Blocks this cell and boundary cells towards this cell
					cells[j][i].tried = HCCHASECELL_CLOSED;
					if (j > 0) cells[j - 1][i].tried |= HCCA_DOWN;
					if (i > 0) cells[j][i - 1].tried |= HCCA_RIGHT;
					if (j < map->Rows()) cells[j + 1][i].tried |= HCCA_UP;
					if (i < map->Cols()) cells[j][i + 1].tried |= HCCA_LEFT;
					break;
					
				case HCCELLTYPE_LADDER:
					if (map->Cell(j + 1, i - 1)->Type() == HCCELLTYPE_BLANK)
					{
						// Can't go left while climbing because there's no floor bellow-left (limitation)
						cells[j][i].tried |= HCCA_LEFT;
					}

					if (map->Cell(j + 1, i - 1)->Type() == HCCELLTYPE_BLANK)
					{
						// Can't go right while climbing because there's no floor bellow-right (limitation)
						cells[j][i].tried |= HCCA_LEFT;
					}
					break;

				case HCCELLTYPE_BAR:
					// Cannot go left or right while sliding
					cells[j][i].tried |= HCCA_LEFT | HCCA_RIGHT;
					break;

				case HCCELLTYPE_BREAK:
					if (((HCBreak *)map->Cell(j, i))->State() == HCBREAKSTATE_NORMAL)
					{
						// Treats it as a floor
						if (j > 0) cells[j - 1][i].tried |= HCCA_DOWN;
						if (i > 0) cells[j][i - 1].tried |= HCCA_RIGHT;
						if (j < map->Rows()) cells[j + 1][i].tried |= HCCA_UP;
						if (i < map->Cols()) cells[j][i + 1].tried |= HCCA_LEFT;
						break;
					}
					break;
					
				default:
					break;
				}

				cells[j][i].path.row = j;
				cells[j][i].path.col = i;
				cells[j][i].path.action = 0;
				cells[j][i].visited = false;
				cells[j][i].depth = 0;
				cells[j][i].numJumpedRows = 0;
			}
		}
	}
}

/** Goes to the given cell. The cell must be in the same row or in the same column.
 * @return 0 if going, 1 if reached, 2 before leaving it (at the edge), -1 if unreachable by simple means.
 */
s32 HCEnemyChaser::GoToSimple(s32 row, s32 col)
{
	s32 curRow = map->ToRow((s32)pos.y), curCol = map->ToCol((s32)pos.x);
	
	if (row != curRow && col != curCol)
	{
		return -1;
	}
	
	if (row != curRow)
	{
		if (row > curRow)
		{
			// Going down, test the cells between
			for (s32 i = curRow; i <= row; ++i)
			{
				if (!(map->Cell(i, curCol)->Actions() & HCACTION_DOWN) &&
						!(map->Cell(i, curCol)->Actions() & HCACTION_FALL))
				{
					return -1;
				}
			}

			actions = HCCA_DOWN;
		}
		else
		{
			// Going up, test the cells between
			for (s32 i = curRow; i >= row; --i)
			{
				if (!(map->Cell(i, curCol)->Actions() & HCACTION_UP))
				{
					return -1;
				}
			}

			actions = HCCA_UP;
		}
	}
	else
  if (col != curCol)
	{
		if (col > curCol)
		{
			// Going right, test the cells between
			for (s32 i = curCol; i <= col; ++i)
			{
				if (!(map->Cell(curRow, i)->Actions() & HCACTION_RIGHT))
				{
					return -1;
				}
			}

			actions = HCCA_RIGHT;
		}
		else
		{
			// Going left, test the cells between
			for (s32 i = curCol; i >= col; --i)
			{
				if (!(map->Cell(curRow, i)->Actions() & HCACTION_LEFT))
				{
					return -1;
				}
			}

			actions = HCCA_LEFT;
		}
	}
	else
	{
		// Reached
		return 1;
	}
	
	nextRow = row;
	nextCol = col;
	
	return 0;
}

void HCEnemyChaser::CheckBestPath()
{
	// Compares the length of each path
	if (chasePathNumCells > tmpPathNumCells)
	{
		//fprintf(stderr, "Mejor path con %d celdas frente a %d\n", tmpPathNumCells, chasePathNumCells);
		// Tmp path is shorter
		memcpy(chasePath, tmpPath, (searchDepth + 1) * sizeof(HCChasePath));
		chasePathNumCells = tmpPathNumCells;
	}
}

s32 HCEnemyChaser::GoTo(s32 row, s32 col)
{
	if (finalRow != row || finalCol != col || chasePathNumCells == (u32)-1)
	{
		s32 curRow = Row(), curCol = Col();

		// New final target cell, computes the best path
		ResetChaseMap();
		HCChaseCell *cc = &cells[curRow][curCol];       // Current chase cell
		HCChaseCell *sc = cc;                           // Search cell
		HCChaseCell *tc = &cells[row][col];             // Target cell

		if (sc == tc)
		{
			return 0;
		}

		HCChaseCell *tmp;                               // Temporary cell
		HCCell *c, bellow;                              // Current map cell and cell bellow
		sc->visited = true;
		
		s32 index = 0;                                  // Index into the search path
		chasePathNumCells = (u32)-1;                    // Init to greatest u32 so it's always worse
		tmpPathNumCells = 0;

		s32 pathNum = 0;
		
		while (1)
		{
			c = sc->cur;
//  			SDL_Rect rc;
//  			rc.x = map->ToX(sc->path.col) - 2;
//  			rc.y = map->ToY(sc->path.row) - (map->CellHeight()/2 - 2);
//  			rc.w = 2;
//  			rc.h = 2;
		
//  			SDL_FillRect(SDL_GetVideoSurface(), &rc, 0xffffffff);
//  			SDL_UpdateRect(SDL_GetVideoSurface(), rc.x, rc.y, rc.w, rc.h);
// 			fprintf(stderr, "Path %d: Estoy en %d %d  index es %d searchDepth es %d ", pathNum, sc->path.row, sc->path.col, index, searchDepth);
// 			fprintf(stderr, 
// 							"ACCIONES PROBADAS:  %s %s %s %s (%s)\n", 
// 							(sc->tried & HCCA_LEFT) ? "L" : " ", 
// 							(sc->tried & HCCA_RIGHT) ? "R" : " ", 
// 							(sc->tried & HCCA_UP) ? "U" : " ", 
// 							(sc->tried & HCCA_DOWN) ? "D" : " ",
// 							sc->visited ? "*** VISITED! ***" : "not visited");
// 			getchar();

			if (sc->depth > searchDepth)
			{
				// Maximum reachable search depth reached!, stop searching in this cell
				sc->tried = HCCHASECELL_CLOSED;
			}

			if (map->Cell(sc->path.row + 1, sc->path.col)->Type() == HCCELLTYPE_BLANK && sc->prev && !(sc->prev->path.action & HCCA_JUMP))
			{
				// Only go by floor, don't jump or fall off in ladders
				sc->tried |= (HCCA_LEFT | HCCA_RIGHT);
			}

			if (!(sc->tried & HCCA_DOWN))
			{
				// Goes to the neighbour bellow
				sc->tried |= HCCA_DOWN;
				tmp = &cells[sc->path.row + 1][sc->path.col];
				
				if (sc->path.row < map->Rows() &&
						((c->Actions() & HCACTION_DOWN) || 
						 (c->Actions() & HCACTION_FALL) || 
						 (map->Cell(sc->path.row + 1, sc->path.col)->Actions() & HCACTION_DOWN)) &&
						!tmp->visited)
				{
					tmp->prev = sc;
					tmpPath[index].row = sc->path.row;
					tmpPath[index].col = sc->path.col;
					tmpPath[index].action = HCCA_DOWN;
					++index;
					sc->visited = true;
					tmp->depth = sc->depth + 1;
					sc = tmp;
					sc->tried |= HCCA_UP;
				}
			}
			else
			if (!(sc->tried & HCCA_LEFT))
			{
				// Goes to the left neighbour
				sc->tried |= HCCA_LEFT;
				tmp = &cells[sc->path.row][sc->path.col - 1];
				
				if ((c->Actions() & HCACTION_LEFT) && sc->path.col > 0 && !tmp->visited)
				{
					tmp->prev = sc;
					tmpPath[index].row = sc->path.row;
					tmpPath[index].col = sc->path.col;
					tmpPath[index].action = HCCA_LEFT;
					++index;
					sc->visited = true;
					tmp->depth = sc->depth + 1;
					sc = tmp;
					sc->tried |= HCCA_RIGHT;
				}
			}
			else
			if (!(sc->tried & HCCA_RIGHT))
			{
				// Goes to the right neighbour
				sc->tried |= HCCA_RIGHT;
				tmp = &cells[sc->path.row][sc->path.col + 1];
				
				if ((c->Actions() & HCACTION_RIGHT) && sc->path.col < map->Cols() && !tmp->visited)
				{
					tmp->prev = sc;
					tmpPath[index].row = sc->path.row;
					tmpPath[index].col = sc->path.col;
					tmpPath[index].action = HCCA_RIGHT;
					++index;
					sc->visited = true;
					tmp->depth = sc->depth + 1;
					sc = tmp;
					sc->tried |= HCCA_LEFT;
				}
			}
			else
			if (!(sc->tried & HCCA_UP))
			{
				// Goes to the upper neighbour
				sc->tried |= HCCA_UP;
				tmp = &cells[sc->path.row - 1][sc->path.col];
				/*				bool cellBellow = sc->path.row < map->Rows();
				HCCell *lower = 0;
				if (cellBellow)
				{
					//lower = map->Cell(sc->path.row + 1, sc->path.col);
					lower = map->Cell(sc->path.row, sc->path.col);
					}*/
				
				if (sc->path.row > 0 && !tmp->visited)
				{
					// Tests up
					if ((c->Actions() & HCACTION_UP)/* ||                       // Current cell allows going up or
																						 (cellBellow && (lower->Actions() & HCACTION_UP))*/)      // lower allows going up (since sprite's hotspot for up is above)
					{
						tmp->prev = sc;
						sc->visited = true;
						tmpPath[index].row = sc->path.row;
						tmpPath[index].col = sc->path.col;
						tmpPath[index].action = HCCA_UP;
						++index;
						tmp->depth = sc->depth + 1;
						sc = tmp;
						sc->tried |= HCCA_DOWN;
					}
				}
			}
			/*
			else
			if (!(sc->tried & HCCA_JUMP))
			{
				// Goes to the upper neighbour
				sc->tried |= HCCA_JUMP;
				tmp = &cells[sc->path.row - 1][sc->path.col];
				bool cellBellow = sc->path.row < map->Rows();
				HCCell *lower = 0;
				if (cellBellow)
				{
					lower = map->Cell(sc->path.row + 1, sc->path.col);
				}
				
				if (sc->path.row > 0 && !tmp->visited)
				{
					// Tests jump
					if ((c->Actions() & HCACTION_FALL) &&                                  // current allows jumping and, or 
							((cellBellow && !(lower->Actions() & HCACTION_FALL)) ||            // there is floor bellow, or 
							 (sc->prev && (sc->prev->action & HCCA_JUMP) && sc->numJumpedRows < maxJumpRows))) // previous action was jump and not reached yet 
					                                                                       // the jump limit (phew!)
					{
						fprintf(stderr, "Salto funciona %d saltadas %d max\n", sc->numJumpedRows, maxJumpRows);
						getchar();
						tmp->prev = sc;
						sc->visited = true;
						tmpPath[index].row = sc->path.row;
						tmpPath[index].col = sc->path.col;
						tmpPath[index].action = HCCA_JUMP;
						++index;
						tmp->depth = sc->depth + 1;
						sc = tmp;
						sc->tried |= HCCA_DOWN;
						
						if (sc->prev)
						{
							sc->numJumpedRows = sc->prev->numJumpedRows + 1;
						}
						else
						{
							sc->numJumpedRows = 1;
						}
					}
				}
				}*/
			else
			{
// 				rc.x = map->ToX(sc->path.col) - 2;
// 				rc.y = map->ToY(sc->path.row) - (map->CellHeight()/2 - 2);
// 				rc.w = 2;
// 				rc.h = 2;
				
// 				SDL_FillRect(SDL_GetVideoSurface(), &rc, 0x00000000);
// 				SDL_UpdateRect(SDL_GetVideoSurface(), rc.x, rc.y, rc.w, rc.h);
// 				getchar();

				// Closed cell
				sc->tried = 0;
				sc->visited = false;
				sc->depth = 0;
				sc->numJumpedRows = 0;
				sc = sc->prev;

				if (index > 0)
					--index;

				if (sc)
				{
					sc->visited = false;
				}
				else
				{
					// Starting cell closed, done
					//fprintf(stderr, "CLOSED!\n");
					break;
				}
			}

			if (sc == tc)
			{
				// The map now contains the search path
				//fprintf(stderr, "Encontrado, index es %d searchDepth es %d!\n", index, searchDepth);
				
				if (sc)
				{
					sc->visited = false;

					if (sc->prev)
					{
						tmpPath[index].row = sc->path.row;
						tmpPath[index].col = sc->path.col;
						tmpPath[index].action = sc->path.action;
					}

					finalRow = row;
					finalCol = col;
				
					++pathNum;
					tmpPathNumCells = index + 1;
					CheckBestPath();
					
					// Continue searching paths
					sc->visited = false;
					sc->depth = 0;
					sc->numJumpedRows = 0;
					sc = sc->prev;
					
					if (sc)
					{
						sc->visited = false;
					}
					
					if (index > 0)
						--index;

					// Searchs ends after param2 successfull paths have been tried
					if (pathNum > param2)
					{
						break;
					}
				}
				else
				{
					break;
				}
			}
		}

		if (pathNum != 0)
		{
			chaseIndex = 0;
			return 0;
		}
		else
		{
			//fprintf(stderr, "NO PATH FOUND!\n");
			return -1;
		}
	}

	// OK, going
	return 0;
}

s32 HCEnemyChaser::UpdateChase()
{
	if (chased == 0)
	{
		return 0;
	}

	actions = 0;
	s32 r = map->ToRow((s32)chased->Y()), c = map->ToCol((s32)chased->X());
	s32 curRow = Row(), curCol = Col();
	
	if (chased->State() == HCCS_JUMP ||
			chased->State() == HCCS_JUMPLEFT ||
			chased->State() == HCCS_JUMPRIGHT || 
			chased->State() == HCCS_HANG ||
			(abs(curRow - r) + abs(curCol - c) <= searchDepth && 0 == GoTo(r, c)))
		{
		if (chasePath[chaseIndex].col != curCol || chasePath[chaseIndex].row != curRow && chaseIndex < searchDepth - 1)
		{
			// Updates the chase index within the path when the row or column changes
			++chaseIndex;
		}

		if (chasePath[chaseIndex].col == curCol && chasePath[chaseIndex].row == curRow)
		{
			// OK, path hasn't been lost
			actions = chasePath[chaseIndex].action;
			
			// Resets idle movement
			nextRow = curRow;
			nextCol = curCol;
			return 0;
		}
		else
		{
			// Path lost
			chasePathNumCells = (u32)-1;
		}
	}

	// No path to target, moves-simple to the randomly generated cell; if reached, select another one
	if (-1 == GoToSimple(nextRow, nextCol))
	{
		// Selects a random cell within the search depth square and tries to simple move over there,
		// when the chased character is not reachable
		s32 sign;

		switch (rand()%4)
		{
		case 0:
		case 1:
		case 3:
			sign = (rand()%2) == 0 ? 1 : -1;
			r = sign * ((rand()%searchDepth) + 1);
			if (0 != GoToSimple(curRow + r, curCol))
			{
				c = sign * ((rand()%searchDepth) + 1);
				return GoToSimple(curRow, curCol + c);
			}
			break;
			
		case 2:
			sign = (rand()%2) == 0 ? 1 : -1;
			c = sign * (rand()%searchDepth) + 1;
			if (0 != GoToSimple(curRow, curCol + c))
			{
				r = sign * ((rand()%searchDepth) + 1);
				return GoToSimple(curRow + r, curCol);
			}
			break;
		}
	}

	return 0;
}

s32 HCEnemyChaser::Update()
{
	states[state].Update();
	UpdateChase();
	Act(actions);
	
	if (state == HCCS_HANG)
	{
		UpdateHang();
	}
	else
	{
		UpdateVeloccity();
		
		// OJITO, igual se hace pesado!!
		CheckHang();
	}

	// Collisions
	UpdateCollisions();

	Pos(pos.x + v.x, pos.y + v.y);

	// Updates dialogs
	UpdateDialog();

	return 0;
}

void HCEnemyChaser::Destroy()
{
	if (cells)
	{
		for (s32 j = 0; j < map->Rows(); ++j)
		{
			JDELETE_ARRAY(cells[j]);
		}

		// Creates the cell array from the map
		delete[] cells;
	}
	
	JDELETE_ARRAY(chasePath);
	JDELETE_ARRAY(tmpPath);
}