From 23fbdd37ecbff448f6e2b62f35752698f8711de5 Mon Sep 17 00:00:00 2001 From: Treeki Date: Thu, 19 May 2011 03:42:25 +0200 Subject: world map code refactoring and cleanup: dWMPathManager_c added --- src/wm_path_manager.cpp | 211 ++++++++++++++++++++++++++++++++++++++++++++++++ src/worldmap.cpp | 187 ++++-------------------------------------- src/worldmap.h | 184 ++++++++++++++++++++++------------------- worldmap.yaml | 6 ++ 4 files changed, 336 insertions(+), 252 deletions(-) create mode 100644 src/wm_path_manager.cpp diff --git a/src/wm_path_manager.cpp b/src/wm_path_manager.cpp new file mode 100644 index 0000000..d94e5be --- /dev/null +++ b/src/wm_path_manager.cpp @@ -0,0 +1,211 @@ +#include "worldmap.h" + +dWMPathManager_c *dWMPathManager_c::instance = 0; + +dWMPathManager_c *dWMPathManager_c::build() { + OSReport("Creating WM_PathManager\n"); + + void *buffer = AllocFromGameHeap1(sizeof(dWMPathManager_c)); + dWMPathManager_c *c = new(buffer) dWMPathManager_c; + + OSReport("Created WM_PathManager @ %p\n", c); + + instance = c; + return c; +} + + + +dWMPathManager_c::dWMPathManager_c() { +} + + +int dWMPathManager_c::onCreate() { + return true; +} + + +int dWMPathManager_c::onDelete() { + return true; +} + + +int dWMPathManager_c::onExecute() { + if (!dScNewerWorldMap_c::instance->canDoStuff()) return true; + + int heldButtons = Remocon_GetButtons(GetActiveRemocon()); + int nowPressed = Remocon_GetPressed(GetActiveRemocon()); + + if (isMoving) { + moveThroughPath(); + } else { + if (nowPressed & WPAD_LEFT && currentPoint->exits.asDirection.left.isValid()) + startMovementTo(LEFT); + else if (nowPressed & WPAD_RIGHT && currentPoint->exits.asDirection.right.isValid()) + startMovementTo(RIGHT); + else if (nowPressed & WPAD_UP && currentPoint->exits.asDirection.up.isValid()) + startMovementTo(UP); + else if (nowPressed & WPAD_DOWN && currentPoint->exits.asDirection.down.isValid()) + startMovementTo(DOWN); + else if (nowPressed & WPAD_TWO) + activatePoint(); + } + + return true; +} + + +void dWMPathManager_c::moveThroughPath() { + // figure out how much to move on each step + Vec from, to; + if (reverseThroughPath) { + from = currentSegment->end; + to = currentSegment->start; + } else { + from = currentSegment->start; + to = currentSegment->end; + } + + Vec move; + move.x = to.x - from.x; + move.y = to.y - from.y; + move.z = to.z - from.z; + + VECNormalize(&move, &move); + VECScale(&move, &move, currentSegment->stepsPerFrame); + + daWMPlayer_c *player = daWMPlayer_c::instance; + player->pos.x += move.x; + player->pos.y += move.y; + player->pos.z += move.z; + + // have we reached the end? + bool xAtEnd = false; + bool yAtEnd = false; + bool zAtEnd = false; + + if (move.x > 0) { + xAtEnd = (player->pos.x >= to.x); + } else { + xAtEnd = (player->pos.x <= to.x); + } + + if (move.y > 0) { + yAtEnd = (player->pos.y >= to.y); + } else { + yAtEnd = (player->pos.y <= to.y); + } + + if (move.z > 0) { + zAtEnd = (player->pos.z >= to.z); + } else { + zAtEnd = (player->pos.z <= to.z); + } + + if (xAtEnd && yAtEnd && zAtEnd) { + MapReport("reached end of segment %d\n", currentSegmentID); + + int nextSegment = currentSegmentID + (reverseThroughPath ? -1 : 1); + if (nextSegment == -1 || nextSegment == currentPath->segCount) { + MapReport("reached end of path\n"); + + currentPoint = nextPoint; + player->pos = currentPoint->position; + player->startAnimation(0, 1.2, 10.0, 0.0); + isMoving = false; + + SaveBlock *save = GetSaveFile()->GetBlock(-1); + //save->current_world = newPage; ? + save->current_path_node = pathData.getPointID(currentPoint); + + dWMHud_c::instance->updateText(); + } else { + moveToSegment(nextSegment); + } + } +} + + +void dWMPathManager_c::startMovementTo(WMDirection direction) { + isMoving = true; + + WMPathEntrance *thisExit = ¤tPoint->exits.asArray[direction]; + MapReport("Using an exit in direction %d\n", direction); + + currentPath = thisExit->path; + if (thisExit->isEndSide) { + nextPoint = thisExit->path->startPoint; + reverseThroughPath = true; + moveToSegment(thisExit->path->segCount - 1); + } else { + nextPoint = thisExit->path->endPoint; + reverseThroughPath = false; + moveToSegment(0); + } +} + +void dWMPathManager_c::moveToSegment(int id) { + MapReport("Moving to segment %d\n", id); + + currentSegmentID = id; + currentSegment = currentPath->segments[id]; + + // calculate rotation + Vec from, to; + if (reverseThroughPath) { + from = currentSegment->end; + to = currentSegment->start; + } else { + from = currentSegment->start; + to = currentSegment->end; + } + + MapReport("From: %f,%f,%f To: %f,%f,%f\n", from.x, from.y, from.z, to.x, to.y, to.z); + + daWMPlayer_c *player = daWMPlayer_c::instance; + player->pos = from; + + // update rotation + if (!currentSegment->useLastDir) { + player->rot.x = 0; + player->rot.y = currentSegment->direction; + player->rot.z = 0; + + if (reverseThroughPath && !currentSegment->alwaysSameDir) { + player->rot.y = ((currentSegment->direction) + 0x8000) & 0xFFFF; + } + } + + player->startAnimation(currentSegment->animationType, currentSegment->animationSpeed, 10.0, 0.0); +} + +void dWMPathManager_c::activatePoint() { + MapReport("Point activated!\n"); + daWMPlayer_c::instance->startAnimation(170, 1.2, 10.0, 0.0); + + if (currentPoint->type == WMPathPoint::LEVEL_TYPE) { + int w = currentPoint->params[0] - 1; + int l = currentPoint->params[1] - 1; + LevelInfo::Entry *level = dScNewerWorldMap_c::instance->levelInfo.search(w, l); + dScNewerWorldMap_c::instance->startLevel(level); + } +} + + +int dWMPathManager_c::onDraw() { + return true; +} + + +void dWMPathManager_c::setup() { + pathData.load(dScNewerWorldMap_c::instance->resMng['PATH']); + + SaveBlock *save = GetSaveFile()->GetBlock(-1); + if (save->current_path_node >= pathData.pointCount()) { + currentPoint = pathData.getPath(0)->startPoint; + } else { + currentPoint = pathData.getPoint(save->current_path_node); + } + +} + diff --git a/src/worldmap.cpp b/src/worldmap.cpp index 33bda60..288d2bb 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -174,24 +174,20 @@ bool WMInit_SetupExtra(void *ptr) { } // since we've got all the resources, set up the path data too - wm->pathData.load(wm->resMng['PATH']); - - SaveBlock *save = GetSaveFile()->GetBlock(-1); - if (save->current_path_node >= wm->pathData.pointCount()) { - wm->currentPoint = wm->pathData.getPath(0)->startPoint; - } else { - wm->currentPoint = wm->pathData.getPoint(save->current_path_node); - } + wm->pathManager->setup(); // and now Player setup wm->player = (daWMPlayer_c*)CreateParentedObject(WM_PLAYER, wm, 0, 2); wm->player->modelHandler->mdlClass->setPowerup(2); wm->player->modelHandler->mdlClass->startAnimation(0, 1.2f, 10.0f, 0.0f); - wm->player->pos = wm->currentPoint->position; + wm->player->pos = wm->pathManager->currentPoint->position; // is last param correct? must check :/ wm->map = (dWMMap_c*)CreateParentedObject(WM_MAP, wm, 0, 0); wm->hud = (dWMHud_c*)CreateParentedObject(WM_HUD, wm, 0, 0); + // note: world_camera and wm_path_manager are not created here + // because we require them earlier + // they are created in dScNewerWorldMap_c::onCreate return true; } @@ -246,7 +242,7 @@ bool WMInit_SetupWipe(void *ptr) { #define STATE_SAVE_ERROR 27 -void dScNewerWorldMap_c::StartLevel(LevelInfo::Entry *entry) { +void dScNewerWorldMap_c::startLevel(LevelInfo::Entry *entry) { for (int i = 0; i < 4; i++) { bool isThere = QueryPlayerAvailability(i); int id = Player_ID[i]; @@ -340,6 +336,8 @@ int dScNewerWorldMap_c::onCreate() { SpammyReport("world camera\n"); CreateParentedObject(WORLD_CAMERA, this, 0, 0); + pathManager = (dWMPathManager_c*)CreateParentedObject(WM_PATH_MANAGER, this, 0, 0); + SpammyReport("setting NewerMapDrawFunc\n"); *CurrentDrawFunc = NewerMapDrawFunc; @@ -367,11 +365,16 @@ int dScNewerWorldMap_c::onDelete() { return true; } -int dScNewerWorldMap_c::onExecute() { +bool dScNewerWorldMap_c::canDoStuff() { + if (QueryGlobal5758(0xFFFFFFFF)) return false; + if (CheckIfWeCantDoStuff()) return false; + if (this->state == STATE_LIMBO) return false; + return true; +} + - if (QueryGlobal5758(0xFFFFFFFF)) return true; - if (CheckIfWeCantDoStuff()) return true; - if (this->state == STATE_LIMBO) return true; +int dScNewerWorldMap_c::onExecute() { + if (!canDoStuff()) return true; /**************************************************************************/ // Read Wiimote Buttons @@ -406,8 +409,6 @@ int dScNewerWorldMap_c::onExecute() { // STATE_NORMAL : Nothing related to the menu is going on case STATE_NORMAL: { - HandleMovement(); - if (nowPressed & WPAD_ONE) { STKI_SHOW(this->stockItem) = true; this->state = STATE_POWERUPS_WAIT; @@ -805,160 +806,6 @@ int dScNewerWorldMap_c::onExecute() { -void dScNewerWorldMap_c::HandleMovement() { - int heldButtons = Remocon_GetButtons(GetActiveRemocon()); - int nowPressed = Remocon_GetPressed(GetActiveRemocon()); - - if (isMoving) { - MoveThroughPath(); - } else { - if (nowPressed & WPAD_LEFT && currentPoint->exits.asDirection.left.isValid()) - StartMovementTo(LEFT); - else if (nowPressed & WPAD_RIGHT && currentPoint->exits.asDirection.right.isValid()) - StartMovementTo(RIGHT); - else if (nowPressed & WPAD_UP && currentPoint->exits.asDirection.up.isValid()) - StartMovementTo(UP); - else if (nowPressed & WPAD_DOWN && currentPoint->exits.asDirection.down.isValid()) - StartMovementTo(DOWN); - - - if (nowPressed & WPAD_TWO) - ActivatePoint(); - } -} - -void dScNewerWorldMap_c::MoveThroughPath() { - // figure out how much to move on each step - Vec from, to; - if (this->reverseThroughPath) { - from = this->currentSegment->end; - to = this->currentSegment->start; - } else { - from = this->currentSegment->start; - to = this->currentSegment->end; - } - - Vec move; - move.x = to.x - from.x; - move.y = to.y - from.y; - move.z = to.z - from.z; - - VECNormalize(&move, &move); - VECScale(&move, &move, this->currentSegment->stepsPerFrame); - - this->player->pos.x += move.x; - this->player->pos.y += move.y; - this->player->pos.z += move.z; - - // have we reached the end? - bool xAtEnd = false; - bool yAtEnd = false; - bool zAtEnd = false; - - if (move.x > 0) { - xAtEnd = (this->player->pos.x >= to.x); - } else { - xAtEnd = (this->player->pos.x <= to.x); - } - - if (move.y > 0) { - yAtEnd = (this->player->pos.y >= to.y); - } else { - yAtEnd = (this->player->pos.y <= to.y); - } - - if (move.z > 0) { - zAtEnd = (this->player->pos.z >= to.z); - } else { - zAtEnd = (this->player->pos.z <= to.z); - } - - if (xAtEnd && yAtEnd && zAtEnd) { - MapReport("reached end of segment %d\n", this->currentSegmentID); - - int nextSegment = this->currentSegmentID + (this->reverseThroughPath ? -1 : 1); - if (nextSegment == -1 || nextSegment == this->currentPath->segCount) { - MapReport("reached end of path\n"); - this->currentPoint = this->nextPoint; - this->player->pos = this->currentPoint->position; - this->player->startAnimation(0, 1.2, 10.0, 0.0); - this->isMoving = false; - - SaveBlock *save = GetSaveFile()->GetBlock(-1); - //save->current_world = newPage; ? - save->current_path_node = pathData.getPointID(this->currentPoint); - - dWMHud_c::instance->updateText(); - } else { - this->MoveToSegment(nextSegment); - } - } -} - -void dScNewerWorldMap_c::StartMovementTo(WMDirection direction) { - this->isMoving = true; - - WMPathEntrance *thisExit = ¤tPoint->exits.asArray[direction]; - MapReport("Using an exit in direction %d\n", direction); - - this->currentPath = thisExit->path; - if (thisExit->isEndSide) { - this->nextPoint = thisExit->path->startPoint; - this->reverseThroughPath = true; - this->MoveToSegment(thisExit->path->segCount - 1); - } else { - this->nextPoint = thisExit->path->endPoint; - this->reverseThroughPath = false; - this->MoveToSegment(0); - } -} - -void dScNewerWorldMap_c::MoveToSegment(int id) { - MapReport("Moving to segment %d\n", id); - - this->currentSegmentID = id; - this->currentSegment = this->currentPath->segments[id]; - - // calculate rotation - Vec from, to; - if (this->reverseThroughPath) { - from = this->currentSegment->end; - to = this->currentSegment->start; - } else { - from = this->currentSegment->start; - to = this->currentSegment->end; - } - - MapReport("From: %f,%f,%f To: %f,%f,%f\n", from.x, from.y, from.z, to.x, to.y, to.z); - - this->player->pos = from; - - // update rotation - if (!this->currentSegment->useLastDir) { - this->player->rot.x = 0; - this->player->rot.y = this->currentSegment->direction; - this->player->rot.z = 0; - - if (this->reverseThroughPath && !this->currentSegment->alwaysSameDir) { - this->player->rot.y = ((this->currentSegment->direction) + 0x8000) & 0xFFFF; - } - } - - this->player->startAnimation(this->currentSegment->animationType, this->currentSegment->animationSpeed, 10.0, 0.0); -} - -void dScNewerWorldMap_c::ActivatePoint() { - MapReport("Point activated!\n"); - this->player->startAnimation(170, 1.2, 10.0, 0.0); - - if (this->currentPoint->type == WMPathPoint::LEVEL_TYPE) { - int w = this->currentPoint->params[0] - 1; - int l = this->currentPoint->params[1] - 1; - LevelInfo::Entry *level = this->levelInfo.search(w, l); - StartLevel(level); - } -} - void NewerMapDrawFunc() { int keepCamera = GetCurrentCameraID(); diff --git a/src/worldmap.h b/src/worldmap.h index 038d9c2..ef08b99 100644 --- a/src/worldmap.h +++ b/src/worldmap.h @@ -48,122 +48,142 @@ void NewerMapDrawFunc(); // Replacing some process IDs #define WM_HUD WM_ANTLION +#define WM_PATH_MANAGER WM_ANTLION_MNG class dWMHud_c : public dBase_c { -public: - dWMHud_c(); + public: + dWMHud_c(); - int onCreate(); - int onDelete(); - int onExecute(); - int onDraw(); + int onCreate(); + int onDelete(); + int onExecute(); + int onDraw(); - bool layoutLoaded; - m2d::EmbedLayout_c layout; + bool layoutLoaded; + m2d::EmbedLayout_c layout; - void updateText(); + void updateText(); - static dWMHud_c *build(); - static dWMHud_c *instance; + static dWMHud_c *build(); + static dWMHud_c *instance; +}; + + +class dWMPathManager_c : public dBase_c { + public: + dWMPathManager_c(); + + int onCreate(); + int onDelete(); + int onExecute(); + int onDraw(); + + dWMPathData_c pathData; + + bool isMoving; + WMPathPoint *currentPoint; + WMPathPoint *nextPoint; + + WMPathDef *currentPath; + WMPathSegment *currentSegment; + int currentSegmentID; + bool reverseThroughPath; // direction we are going through the path + + void setup(); + + void startMovementTo(WMDirection direction); + void moveToSegment(int id); + + void moveThroughPath(); + + void activatePoint(); + + static dWMPathManager_c *build(); + static dWMPathManager_c *instance; }; class dWorldCamera_c : public dBase_c { -public: - int onCreate(); - int onDelete(); - int onExecute(); - int onDraw(); - - Point3d camPos; - Vec camRotate; - - float camDist; - float targetDist; - float camY; - float targetY; - - static dWorldCamera_c *build(); - static dWorldCamera_c *instance; + public: + int onCreate(); + int onDelete(); + int onExecute(); + int onDraw(); + + Point3d camPos; + Vec camRotate; + + float camDist; + float targetDist; + float camY; + float targetY; + + static dWorldCamera_c *build(); + static dWorldCamera_c *instance; }; class daWMPlayer_c : public dActor_c { -public: - dPlayerModelHandler_c *modelHandler; + public: + dPlayerModelHandler_c *modelHandler; - int onCreate(); - int onDelete(); - int onExecute(); - int onDraw(); + int onCreate(); + int onDelete(); + int onExecute(); + int onDraw(); - int current_param; + int current_param; - int currentAnim; - float currentFrame; - float currentUnk; - float currentUpdateRate; + int currentAnim; + float currentFrame; + float currentUnk; + float currentUpdateRate; - void startAnimation(int id, float frame, float unk, float updateRate); + void startAnimation(int id, float frame, float unk, float updateRate); - static daWMPlayer_c *build(); - static daWMPlayer_c *instance; + static daWMPlayer_c *build(); + static daWMPlayer_c *instance; }; // WORLD MAP CLASS LAYOUT class dScNewerWorldMap_c : public dScene_c { -public: - dScNewerWorldMap_c(); - - FunctionChain initChain; - - int state; - void *csMenu; - void *selectCursor; - void *numPeopleChange; - void *yesNoWindow; - void *continueObj; - void *stockItem; - void *stockItemShadow; - void *easyPairing; - - File levelInfoFile; - LevelInfo levelInfo; - - dWMResourceMng_c resMng; - dWMPathData_c pathData; + public: + dScNewerWorldMap_c(); - daWMPlayer_c *player; - dWMMap_c *map; - dWMHud_c *hud; + FunctionChain initChain; - bool isMoving; - WMPathPoint *currentPoint; - WMPathPoint *nextPoint; + int state; + void *csMenu; + void *selectCursor; + void *numPeopleChange; + void *yesNoWindow; + void *continueObj; + void *stockItem; + void *stockItemShadow; + void *easyPairing; - WMPathDef *currentPath; - WMPathSegment *currentSegment; - int currentSegmentID; - bool reverseThroughPath; // direction we are going through the path + File levelInfoFile; + LevelInfo levelInfo; - void HandleMovement(); + dWMResourceMng_c resMng; - void StartMovementTo(WMDirection direction); - void MoveToSegment(int id); + daWMPlayer_c *player; + dWMMap_c *map; + dWMHud_c *hud; + dWMPathManager_c *pathManager; - void MoveThroughPath(); - void ActivatePoint(); + void startLevel(LevelInfo::Entry *entry); - void StartLevel(LevelInfo::Entry *entry); + bool canDoStuff(); - int onCreate(); - int onDelete(); - int onExecute(); + int onCreate(); + int onDelete(); + int onExecute(); - static dScNewerWorldMap_c *build(); - static dScNewerWorldMap_c *instance; + static dScNewerWorldMap_c *build(); + static dScNewerWorldMap_c *instance; }; #endif diff --git a/worldmap.yaml b/worldmap.yaml index 679b81e..101b285 100644 --- a/worldmap.yaml +++ b/worldmap.yaml @@ -4,6 +4,7 @@ source_files: - ../src/wm_player.cpp - ../src/wm_map.cpp - ../src/wm_hud.cpp + - ../src/wm_path_manager.cpp - ../src/world_camera.cpp - ../src/worldmapdata.cpp - ../src/wmresourcemng.cpp @@ -39,6 +40,11 @@ hooks: src_addr_pal: 0x80981A3C target_func: 'dWMHud_c::build(void)' + - name: BuildWMPathManager + type: add_func_pointer + src_addr_pal: 0x80981BD4 + target_func: 'dWMPathManager_c::build(void)' + # fix the STOCK_ITEM references - name: StockItemFix type: patch -- cgit v1.2.3