summaryrefslogtreecommitdiff
path: root/src/koopatlas/pathmanager.cpp
blob: 444684c4288068248fcc21039ba21b24f13f7587 (plain)
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
#include "koopatlas/pathmanager.h"
#include "koopatlas/core.h"
#include "koopatlas/hud.h"
#include "koopatlas/player.h"
#include <sfx.h>

void dWMPathManager_c::setup() {
	isMoving = false;
	isJumping = false;
	timer = 0.0;
	currentPath = 0;
	reverseThroughPath = false;

	pathLayer = dScKoopatlas_c::instance->mapData.pathLayer;

	SpammyReport("setting up PathManager\n");
	SaveBlock *save = GetSaveFile()->GetBlock(-1);

	mustComplainToMapCreator = false;

	// Figure out what path node to start at
	if (dScKoopatlas_c::instance->settings & 0x10000000) {
		// Start off from a "Change"
		u8 changeID = dScKoopatlas_c::instance->settings & 0xFF;
		SpammyReport("entering at Change ID %d\n", changeID);
		SpammyReport("Path layer: %p\n", pathLayer);
		SpammyReport("Node count: %d\n", pathLayer->nodeCount);

		bool found = false;

		for (int i = 0; i < pathLayer->nodeCount; i++) {
			dKPNode_s *node = pathLayer->nodes[i];
			SpammyReport("Checking node: %p\n", node);

			if (node->type == dKPNode_s::CHANGE && node->thisID == changeID) {
				found = true;
				currentNode = node;

				SpammyReport("a1 Node: %p\n", node);
				dKPPath_s *exit = node->getAnyExit();
				SpammyReport("a2 Exit: %p\n", exit);
				startMovementTo(exit);
				SpammyReport("a3\n");
				break;
			}
		}

		if (!found) {
			currentNode = pathLayer->nodes[0];
			mustComplainToMapCreator = true;
		}
		
	} else {
		SpammyReport("saved path node: %d\n", save->current_path_node);
		if (save->current_path_node >= pathLayer->nodeCount) {
			SpammyReport("out of bounds (%d), using node 0\n", pathLayer->nodeCount);
			currentNode = pathLayer->nodes[0];
		} else {
			currentNode = pathLayer->nodes[save->current_path_node];
			SpammyReport("OK %p\n", currentNode);
		}
	}

	SpammyReport("Unlocking paths\n");
	isEnteringLevel = false;
	unlockPaths();
	SpammyReport("done\n");
}

static u8 *PathAvailabilityData = 0;

dWMPathManager_c::~dWMPathManager_c() {
	if (PathAvailabilityData && !isEnteringLevel) {
		delete[] PathAvailabilityData;
		PathAvailabilityData = 0;
	}
}

void dWMPathManager_c::unlockPaths() {
	u8 *oldAvData = PathAvailabilityData;
	PathAvailabilityData = new u8[pathLayer->pathCount];

	// unlock all needed paths
	for (int i = 0; i < pathLayer->pathCount; i++)
		PathAvailabilityData[i] = dKPPath_s::NOT_AVAILABLE;

	SaveBlock *save = GetSaveFile()->GetBlock(-1);

	u8 *in = (u8*)dScKoopatlas_c::instance->mapData.data->unlockData;

	while (*in != 0) {
		// begin processing a block
		bool value = evaluateUnlockCondition(in, save);

		// , get what it's supposed to affect
		// for now we'll assume that it affects one or more paths
		u8 affectedCount = *(in++);

		for (int i = 0; i < affectedCount; i++) {
			u8 one = *(in++);
			u8 two = *(in++);
			u16 pathID = (one << 8) | two;

			dKPPath_s *path = pathLayer->paths[pathID];
			path->isAvailable = dKPPath_s::AVAILABLE;
			PathAvailabilityData[pathID] = dKPPath_s::AVAILABLE;
			// NEWLY_AVAILABLE is set later, when that stuff is figured out
		}

		// now evaluate a condition. simple!
	}

	// did anything become newly available?!
	int whatsNew = 0;

	if (oldAvData) {
		for (int i = 0; i < pathLayer->pathCount; i++) {
			if ((PathAvailabilityData[i] > 0) && (oldAvData[i] == 0)) {
				dKPPath_s *path = pathLayer->paths[i];
				path->isAvailable = dKPPath_s::NEWLY_AVAILABLE;
				whatsNew++;
			}
		}
		delete[] oldAvData;
	}

	// todo: set a flag on Map to do the path fade/sound, with whatsNew
}

bool dWMPathManager_c::evaluateUnlockCondition(u8 *&in, SaveBlock *save) {
	u8 controlByte = *(in++);

	u8 conditionType = (controlByte >> 6);

	if (conditionType == 0)
		return true;

	if (conditionType == 1) {
		// Simple level

		bool isSecret = (controlByte & 0x10);
		u8 worldNumber = controlByte & 0xF;
		u8 levelNumber = *(in++);

		u32 conds = save->GetLevelCondition(worldNumber, levelNumber);

		if (isSecret)
			return (conds & COND_SECRET) != 0;
		else
			return (conds & COND_NORMAL) != 0;
	}

	// Type: 2 = AND, 3 = OR
	bool isAnd = (conditionType == 2);
	bool isOr = (conditionType == 3);

	bool value = isOr ? false : true;

	u8 termCount = (controlByte & 0x3F) + 1;

	for (int i = 0; i < termCount; i++) {
		bool what = evaluateUnlockCondition(in, save);

		if (isOr)
			value |= what;
		else
			value &= what;
	}

	return value;
}


void dWMPathManager_c::execute() {
	int nowPressed = Remocon_GetPressed(GetActiveRemocon());

	if (isMoving) {
		moveThroughPath();
	} else {
		if (nowPressed & WPAD_LEFT && canUseExit(currentNode->leftExit))
			startMovementTo(currentNode->leftExit);
		else if (nowPressed & WPAD_RIGHT && canUseExit(currentNode->rightExit))
			startMovementTo(currentNode->rightExit);
		else if (nowPressed & WPAD_UP && canUseExit(currentNode->upExit))
			startMovementTo(currentNode->upExit);
		else if (nowPressed & WPAD_DOWN && canUseExit(currentNode->downExit))
			startMovementTo(currentNode->downExit);
		else if (nowPressed & WPAD_TWO)
			activatePoint();
	}
}


void dWMPathManager_c::startMovementTo(dKPPath_s *path) {
	SpammyReport("moving to path %p\n", path);
	dWMHud_c::instance->hidePointBar();
	SpammyReport("point bar hidden\n");

	if (!path->isAvailable) { return; }

	SpammyReport("a\n");
	isMoving = true;
	reverseThroughPath = (path->end == currentNode);
	SpammyReport("b\n");

	currentPath = path;
	SpammyReport("c\n");

	// calculate direction of the path
	short deltaX = path->end->x - path->start->x;
	short deltaY = path->end->y - path->start->y;
	SpammyReport("d\n");
	u16 direction = (u16)(atan2(deltaX, deltaY) / ((M_PI * 2) / 65536.0));
	SpammyReport("e\n");

	if (reverseThroughPath) {
	SpammyReport("e2\n");
		direction += 0x8000;
	}

	SpammyReport("f\n");
	daWMPlayer_c *player = daWMPlayer_c::instance;
	SpammyReport("g %p\n", player);


	// Consider adding these as options
	// wall_walk_l = 60,
	// wall_walk_r = 61,

	// hang_walk_l = 65,
	// hang_walk_r = 66,

	SpammyReport("h\n");
	player->rot.y = direction;
	player->hasSound = false;
	player->hasEffect = false;
	moveSpeed = 3.0f;

	switch (path->animation) {

		// Running
		case 0:
			player->startAnimation(run, 2.0, 10.0, 0.0);
			player->hasSound = true;
			player->soundName = SE_PLY_FOOTNOTE_DIRT;
			break;
		case 1:
			player->startAnimation(run, 2.0, 10.0, 0.0);
			player->hasSound = true;
			player->hasEffect = true;
			player->soundName = SE_PLY_FOOTNOTE_CS_SAND;
			player->effectName = "Wm_mr_foot_sand";
			break;
		case 2:
			player->startAnimation(run, 2.0, 10.0, 0.0);
			player->hasSound = true;
			player->hasEffect = true;
			player->soundName = SE_PLY_FOOTNOTE_CS_SNOW;
			player->effectName = "Wm_mr_foot_snow";
			break;
		case 3:
			player->startAnimation(run, 2.0, 10.0, 0.0);
			player->hasSound = true;
			player->hasEffect = true;
			player->soundName = SE_PLY_FOOTNOTE_CS_WATER;
			player->effectName = "Wm_mr_foot_water";
			break;

		// Jumping
		case 4:
			player->startAnimation(jump, 1.0, 1.0, 0.0);
			MapSoundPlayer(SoundRelatedClass, SE_PLY_JUMP, 1);
			isJumping = true;
			moveSpeed = 2.5f;
			break;
		case 5:
			player->startAnimation(jump, 1.0, 10.0, 0.0);
			MapSoundPlayer(SoundRelatedClass, SE_PLY_JUMP, 1);
			isJumping = true;
			moveSpeed = 2.5f;
			break;
		case 6:
			player->startAnimation(jump, 1.0, 10.0, 0.0);
			MapSoundPlayer(SoundRelatedClass, SE_PLY_JUMP, 1);
			isJumping = true;
			moveSpeed = 2.5f;
			break;
		case 7:
			player->startAnimation(jump, 1.0, 10.0, 0.0);
			MapSoundPlayer(SoundRelatedClass, SE_PLY_JUMP, 1);
			isJumping = true;
			SpawnEffect("Wm_mr_waterwave_out", 0, &player->pos, 0, &player->scale);
			moveSpeed = 2.0f;
			break;

		// Climbing
		case 8:
			player->startAnimation(pea_plant, 1.2, 10.0, 0.0);
			player->rot.y = 0x8000;
			player->hasSound = true;
			player->soundName = SE_PLY_FOOTNOTE_CS_ROCK_CLIMB;
			moveSpeed = 1.5f;
			break;
		case 9:
			player->startAnimation(tree_climb, 1.2, 10.0, 0.0);
			player->rot.y = 0xC000;
			player->hasSound = true;
			player->soundName = SE_PLY_FOOTNOTE_CS_ROCK_CLIMB;
			moveSpeed = 1.5f;
			break;
		case 10:
			player->startAnimation(tree_climb, 1.2, 10.0, 0.0);
			player->rot.y = 0x4000;
			player->hasSound = true;
			player->soundName = SE_PLY_FOOTNOTE_CS_ROCK_CLIMB;
			moveSpeed = 1.5f;
			break;

		// Others
		case 12:
			player->startAnimation(swim_wait, 1.2, 10.0, 0.0);
			player->hasSound = true;
			player->hasEffect = true;
			player->soundName = SE_PLY_SWIM;
			player->effectName = "Wm_mr_waterswim";
			moveSpeed = 2.0f;
			break;
		case 13:
			player->startAnimation(Tjumped, 2.0, 0.0, 0.0);
			break;
		case 14:
			player->startAnimation(b_dash2, 3.0, 10.0, 0.0);
			player->hasSound = true;
			player->soundName = SE_PLY_FOOTNOTE_DIRT;
			moveSpeed = 5.0f;
			break;
		case 15:
			player->startAnimation(wait, 2.0, 10.0, 0.0);
			player->rot.y = 0x0000;
			MapSoundPlayer(SoundRelatedClass, SE_PLY_DOKAN_IN_OUT, 1);
			moveSpeed = 1.0f;
			break;
		case 16:
			player->startAnimation(wait, 2.0, 10.0, 0.0);
			player->rot.y = 0x8000;			
			MapSoundPlayer(SoundRelatedClass, SE_OBJ_DOOR_OPEN, 1);
			moveSpeed = 0.2f;
			break;
		default:
			OSReport("No animtaion?!");
			player->startAnimation(run, 2.0, 10.0, 0.0);
			player->hasSound = true;
			player->soundName = SE_PLY_FOOTNOTE_DIRT;
			break;
	}
	
	SpammyReport("i\n");
}

void dWMPathManager_c::moveThroughPath() {
	dKPNode_s *from, *to;

	from = reverseThroughPath ? currentPath->end : currentPath->start;
	to = reverseThroughPath ? currentPath->start : currentPath->end;

	Vec move = (Vec){to->x - from->x, to->y - from->y, 0};
	VECNormalize(&move, &move);
	VECScale(&move, &move, moveSpeed);

	daWMPlayer_c *player = daWMPlayer_c::instance;

	if (isJumping) {
		float ys = (float)from->y;
		float ye = (float)to->y;
		float midpoint = (from->y + to->y) / 2;

		float top, len;
		if (ys > ye) { len = ys - ye; top = ys - midpoint + 10.0; }
		else		 { len = ye - ys; top = ye - midpoint + 10.0; }

		if (len == 0.0) { len = 2.0; }

		float a;
		if (timer > 0.0) { a = -timer; }
		else			 { a =  timer; }

		player->jumpOffset = -sin(a * 3.14 / len) * top;
		timer -= move.y;
	}

	player->pos.x += move.x;
	player->pos.y -= move.y;

	// Check if we've reached the end yet
	if (
			((move.x > 0) ? (player->pos.x >= to->x) : (player->pos.x <= to->x)) &&
			((move.y > 0) ? (-player->pos.y >= to->y) : (-player->pos.y <= to->y))
	   ) {

   		currentNode = to;
   		player->pos.x = to->x;
   		player->pos.y = -to->y;

   		isJumping = false;
   		timer = 0.0;

   		SpammyReport("reached path end (%p)\n", to);

		if (to->type == dKPNode_s::CHANGE) {
			// Go to another map
			isMoving = false;

			SaveBlock *save = GetSaveFile()->GetBlock(-1);
			save->current_world = dScKoopatlas_c::instance->getIndexForMapName(to->destMap);

			SpammyReport("Change to map ID %d (%s), entrance ID %d\n", save->current_world, to->destMap, to->foreignID);

			DoSceneChange(WORLD_MAP, 0x10000000 | to->foreignID, 0);

		} else if (to->type != dKPNode_s::PASS_THROUGH) {
	   		// Stop here
	   		player->startAnimation(0, 1.2, 10.0, 0.0);
	   		player->hasEffect = false;
	   		player->hasSound = false;

	   		SpammyReport("stopping here\n");

	   		isMoving = false;

			SaveBlock *save = GetSaveFile()->GetBlock(-1);
			save->current_path_node = pathLayer->findNodeID(to);

			dWMHud_c::instance->showPointBar();
			SpammyReport("Point bar shown\n");
		} else {
			startMovementTo(to->getOppositeExitTo(currentPath));
			SpammyReport("passthrough node, continuing to next path\n");
		}
	}
}

void dWMPathManager_c::activatePoint() {
	if (currentNode->type == dKPNode_s::LEVEL) {
		int w = currentNode->levelNumber[0] - 1;
		int l = currentNode->levelNumber[1] - 1;

		if ((l > 29) && (l < 38)) {
			SaveBlock *save = GetSaveFile()->GetBlock(-1);
			u32 conds = save->GetLevelCondition(w, l);

			SpammyReport("Toad House Flags: %x", conds);
			if (conds & 0xFF0) { 

				MapSoundPlayer(SoundRelatedClass, SE_SYS_INVALID, 1);
				return; }
		}

		MapSoundPlayer(SoundRelatedClass, SE_SYS_GAME_START, 1);
		daWMPlayer_c::instance->startAnimation(170, 1.2, 10.0, 0.0);

		isEnteringLevel = true;

		dLevelInfo_c::entry_s *level = dScKoopatlas_c::instance->levelInfo.search(w, l);
		dScKoopatlas_c::instance->startLevel(level);
	}
}


void dWMPathManager_c::unlockAllPaths() {

	for (int i = 0; i < pathLayer->pathCount; i++) {
		dKPPath_s *path = pathLayer->paths[i];
		path->isAvailable = true;
	}

	// Can't change node models - the price we pay for not using anims
	// for (int i = 0; i < pathLayer->nodeCount; i++) {
	// 	dKPNode_s *node = pathLayer->nodes[i];
	// 	node->setupNodeExtra();
	// }
	
}