From 1cc1afe0f971121f7aed34460305e835de90e984 Mon Sep 17 00:00:00 2001 From: Treeki Date: Sun, 14 Aug 2011 00:39:47 +0200 Subject: merged random tiles into level-select --- src/randtiles.cpp | 218 ++++++++++++++++++++++++++++++++++++++++++++++++------ src/randtiles.h | 63 ---------------- 2 files changed, 196 insertions(+), 85 deletions(-) delete mode 100644 src/randtiles.h (limited to 'src') diff --git a/src/randtiles.cpp b/src/randtiles.cpp index 0bc2ede..34e2daf 100644 --- a/src/randtiles.cpp +++ b/src/randtiles.cpp @@ -1,22 +1,196 @@ -#include "randtiles.h" - -u32 djb2(u8 *str) { - u32 hash = 5381; - int c; - - while (c = *str++) - hash = ((hash << 5) + hash) + c; - - return hash; -} - -RandTiles_Section *RandTiles_Search(void *file, u32 nameHash) { - for (int i = 0; i < RandTiles_GetSectionCount(file); i++) { - RandTiles_Section *sect = RandTiles_GetSection(file, i); - - if (sect->nameHash == nameHash) - return sect; - } - - return 0; -} +#include + +class RandomTileData { +public: + enum Type { + CHECK_NONE = 0, + CHECK_HORZ = 1, + CHECK_VERT = 2, + CHECK_BOTH = 3 + }; + + enum Special { + SP_NONE = 0, + SP_VDOUBLE_TOP = 1, + SP_VDOUBLE_BOTTOM = 2 + }; + + class NameList { + public: + u32 count; + u32 offsets[1]; // variable size + + const char *getName(int index) { + return ((char*)this) + offsets[index]; + } + + bool contains(const char *name) { + OSReport("NameList[%p] with %d names being compared against %s\n", this, count, name); + for (int i = 0; i < count; i++) { + OSReport("Checking %s\n", getName(i)); + if (strcmp(name, getName(i)) == 0) + return true; + OSReport("Did not pass\n"); + } + + return false; + } + }; + + class Entry { + public: + u8 lowerBound, upperBound; + u8 count, type; + u32 tileNumOffset; + + u8 *getTileNums() { + return ((u8*)this) + tileNumOffset; + } + }; + + class Section { + public: + u32 nameListOffset; + u32 entryCount; + Entry entries[1]; // variable size + + NameList *getNameList() { + return (NameList*)(((u32)this) + nameListOffset); + } + }; + + u32 magic; + u32 sectionCount; + u32 offsets[1]; // variable size + + Section *getSection(int id) { + return (Section*)(((char*)this) + offsets[id]); + } + + Section *getSection(const char *name); + + static RandomTileData *instance; +}; + +class RTilemapClass : public TilemapClass { +public: + // NEWER ADDITIONS + RandomTileData::Section *sections[4]; +}; + +RandomTileData::Section *RandomTileData::getSection(const char *name) { + for (int i = 0; i < sectionCount; i++) { + RandomTileData::Section *sect = getSection(i); + + if (sect->getNameList()->contains(name)) + return sect; + } + + return 0; +} + + +// Real tile handling code + +RandomTileData *RandomTileData::instance = 0; + +dDvdLoader_c RandTileLoader; + +extern "C" bool RandTileLoadHook() { + OSReport("Trying to load..."); + void *buf = RandTileLoader.load("/NewerRes/RandTiles.bin"); + if (buf == 0) { + OSReport("Failed.\n"); + return false; + } else { + OSReport("Successfully loaded RandTiles.bin [%p].\n", buf); + RandomTileData::instance = (RandomTileData*)buf; + return true; + } +} + + +extern "C" void IdentifyTilesets(RTilemapClass *self) { + self->_C0C = 0xFFFFFFFF; + + for (int i = 0; i < 4; i++) { + const char *tilesetName = BGDatClass::instance->getTilesetName(self->areaID, i); + + self->sections[i] = RandomTileData::instance->getSection(tilesetName); + OSReport("[%d] Chose %p for %s\n", i, self->sections[i], tilesetName); + } +} + +extern "C" void TryAndRandomise(RTilemapClass *self, BGRender *bgr) { + int fullTile = bgr->tileToPlace & 0x3FF; + int tile = fullTile & 0xFF; + int tileset = fullTile >> 8; + + RandomTileData::Section *rtSect = self->sections[tileset]; + if (rtSect == 0) + return; + + for (int i = 0; i < rtSect->entryCount; i++) { + RandomTileData::Entry *entry = &rtSect->entries[i]; + + if (tile >= entry->lowerBound && tile <= entry->upperBound) { + // Found it!! + // Try to make one until we meet the conditions + u8 type = entry->type & 3; + u8 special = entry->type >> 2; + + u8 *tileNums = entry->getTileNums(); + u16 chosen = 0xFF; + + // If it's the top special, then ignore this tile, we'll place that one + // once we choose the bottom one + if (special == RandomTileData::SP_VDOUBLE_TOP) + break; + + u16 *top = 0, *left = 0, *right = 0, *bottom = 0; + if (type == RandomTileData::CHECK_HORZ || type == RandomTileData::CHECK_BOTH) { + left = self->getPointerToTile(bgr->curX - 1, bgr->curY); + right = self->getPointerToTile(bgr->curX + 1, bgr->curY); + } + + if (type == RandomTileData::CHECK_VERT || type == RandomTileData::CHECK_BOTH) { + top = self->getPointerToTile(bgr->curX, bgr->curY - 1); + bottom = self->getPointerToTile(bgr->curX, bgr->curY + 1); + } + + int attempts = 0; + while (true) { + // is there even a point to using that special random function? + chosen = (tileset << 8) | tileNums[MakeRandomNumberForTiles(entry->count)]; + + // avoid infinite loops + attempts++; + if (attempts > 5) + break; + + if (top != 0 && *top == chosen) + continue; + if (bottom != 0 && *bottom == chosen) + continue; + if (left != 0 && *left == chosen) + continue; + if (right != 0 && *right == chosen) + continue; + break; + } + + bgr->tileToPlace = chosen; + + if (special == RandomTileData::SP_VDOUBLE_BOTTOM) { + if (top == 0) + top = self->getPointerToTile(bgr->curX, bgr->curY - 1); + + *top = (chosen - 0x10); + } + + return; + } + } +} + + diff --git a/src/randtiles.h b/src/randtiles.h deleted file mode 100644 index 14355da..0000000 --- a/src/randtiles.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef __NEWER_RANDTILES_H -#define __NEWER_RANDTILES_H - -#include -#include "fileload.h" - -#define RAND_CHECK_HORZ -#define RAND_CHECK_VERT -#define RAND_CHECK_BOTH - -struct RandTiles_Header { - u32 magic; - u32 sectionCount; -}; - -struct RandTiles_Section { - u32 nameHash; - u32 entryCount; -}; - -struct RandTiles_Entry { - u16 startTile; - u16 endTile; - u8 count; - u8 type; - u16 reserved; - u32 dataOffset; -}; - -inline u32 RandTiles_GetSectionCount(void *file) { - return ((RandTiles_Header*)file)->sectionCount; -} - -inline u32 *RandTiles_GetOffsets(void *file) { - return (u32*)(((RandTiles_Header*)file)+1); -} - -inline RandTiles_Section *RandTiles_GetSection(void *file, int id) { - u32 offs = RandTiles_GetOffsets(file)[id]; - return (RandTiles_Section*)(((char*)file)+offs); -}; - -inline RandTiles_Entry *RandTiles_GetTiles(void *file, RandTiles_Section *section) { - return (RandTiles_Entry*)(section+1); -} - -inline RandTiles_Entry *RandTiles_GetTiles(void *file, int sectionID) { - return (RandTiles_Entry*)(RandTiles_GetSection(file, sectionID)+1); -} - -inline char *RandTiles_GetName(void *file, RandTiles_Section *section) { - return ((char*)file)+section->nameOffset; -} - -inline u16 *RandTiles_GetData(void *file, RandTiles_Entry *entry) { - return (u16*)(((char*)file)+entry->dataOffset); -} - -u32 djb2(u8 *str); -RandTiles_Section *RandTiles_Search(void *file, u32 nameHash); - - -#endif -- cgit v1.2.3