From 4f6633db18e2213a151782d995486491c8db3ff7 Mon Sep 17 00:00:00 2001 From: Treeki Date: Mon, 25 Jul 2011 03:16:21 +0200 Subject: Random tiles added. Untested. --- src/randtiles.cpp | 158 ++++++++++++++++++++++++++++++++++++++++++++++++------ src/randtiles.h | 80 ++++++++++++--------------- 2 files changed, 174 insertions(+), 64 deletions(-) (limited to 'src') diff --git a/src/randtiles.cpp b/src/randtiles.cpp index c7388dd..4af3bf7 100644 --- a/src/randtiles.cpp +++ b/src/randtiles.cpp @@ -1,22 +1,146 @@ -#include "randtiles.h" - -u32 djb2(u8 *str) { - u32 hash = 5381; - int c; - - while (c = *str++) - hash = ((hash << 5) + hash) + c; - - return hash; -} +#include + +class RandomTileData { +public: + enum Type { + CHECK_NONE = 0, + CHECK_HORZ = 1, + CHECK_VERT = 2, + CHECK_BOTH = 3 + }; + + class Entry { + public: + u8 lowerBound, upperBound; + u8 count, type; + u32 tileNumOffset; + + u8 *getTileNums() { + return ((u8*)this) + tileNumOffset; + } + }; + + class Section { + public: + u32 nameOffset; + u32 entryCount; + Entry entries[1]; // variable size + + const char *getName() { + return ((char*)this) + nameOffset; + } + }; + + 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; +}; -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) +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 (strcmp(name, sect->getName()) == 0) { 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.\n"); + 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); + } +} + +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 *tileNums = entry->getTileNums(); + u8 chosen = 0xFF; + + u16 *top = 0, *left = 0, *right = 0, *bottom = 0; + if (entry->type == RandomTileData::CHECK_HORZ || entry->type == RandomTileData::CHECK_BOTH) { + left = self->getPointerToTile(bgr->curX - 1, bgr->curY); + right = self->getPointerToTile(bgr->curX + 1, bgr->curY); + } + + if (entry->type == RandomTileData::CHECK_VERT || entry->type == RandomTileData::CHECK_BOTH) { + top = self->getPointerToTile(bgr->curX, bgr->curY - 1); + bottom = self->getPointerToTile(bgr->curX, bgr->curY + 1); + } + + while (true) { + chosen = tileNums[MakeRandomNumber(entry->count)]; + + 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; + + return; + } + } +} + + diff --git a/src/randtiles.h b/src/randtiles.h index ab2553d..03005b2 100644 --- a/src/randtiles.h +++ b/src/randtiles.h @@ -2,62 +2,48 @@ #define __NEWER_RANDTILES_H #include -#include "fileload.h" -#define RAND_CHECK_HORZ -#define RAND_CHECK_VERT -#define RAND_CHECK_BOTH +class RandomTileData { + enum Type { + CHECK_NONE = 0, + CHECK_HORZ = 1, + CHECK_VERT = 2, + CHECK_BOTH = 3 + }; + + class Entry { + u8 startTile, endTile; + u8 count, type; + u32 dataOffset; + + u8 *getData() { + return ((u8*)this) + dataOffset; + } + }; + + class Section { + u32 nameHash; + u32 nameOffset; + u32 entryCount; + Entry entries[1]; // variable size + + const char *getName() { + return ((char*)this) + nameOffset; + } + }; -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; -}; + u32 offsets[1]; // variable size -inline u32 RandTiles_GetSectionCount(void *file) { - return ((RandTiles_Header*)file)->sectionCount; -} + Section *getSection(int id) { + return (Section*)(((char*)this) + offsets[id]); + } -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); + Section *getSection(char *name); }; -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