From 456cba7e6c1f00986e18f695c63050d5cccdb96f Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Sun, 3 Feb 2019 23:53:50 -0800 Subject: [PATCH] started implementing scripting language for map generation --- WorldData.bk | 249 ++++++++++++++++++++++++++++++++++++++ WorldData.c | 328 +++++++++++++++++++++++++------------------------- WorldData.h | 8 ++ mapdata/1.txt | 5 + 4 files changed, 429 insertions(+), 161 deletions(-) create mode 100644 WorldData.bk create mode 100644 mapdata/1.txt diff --git a/WorldData.bk b/WorldData.bk new file mode 100644 index 0000000..39b4f22 --- /dev/null +++ b/WorldData.bk @@ -0,0 +1,249 @@ +#include + +#include "config.h" + +#include "SDL/SDL.h" +#include "SDL/SDL_image.h" +#include "SDL/SDL_ttf.h" +#include "SDL/SDL_mixer.h" + +#include "enum.h" + +#include "Engine.h" +#include "Player.h" +#include "Room.h" +#include "Kaos.h" +#include "HyperKaos.h" +#include "TextBox.h" +#include "Scene.h" +#include "Synergy.h" + +typedef struct timer Timer; +#include "extern.h" + +void bufferData(enum dataChunks chunk) +{ + hasMusic = 0; + applySurface(8,8, loadingTxt, screen, NULL); + frameAdvance(); + printf("Loading map chunk\n"); + switch (chunk){ + case LEVEL1: + +#ifdef SOUND_ON + bgmData[0] = loadBGM("assets/snd/bgm/artificial sun stage (mamon machine mix).mp3"); +#endif + + mapBuffer[0] = newRoom("assets/img/backgrounds/blueroom.gif", 10); + mapBuffer[1] = newRoom("assets/img/backgrounds/redroom.gif", 4); + mapBuffer[2] = newRoom("assets/img/backgrounds/greenroom.gif", 4); + mapBuffer[3] = newRoom("assets/img/backgrounds/darkroom.gif", 4); + + addObstacle(mapBuffer[0], 64,92,22,16); + addFgObj(mapBuffer[0], 64,64,22,55,"assets/img/objects/floatingChips.png", 4, 1, 255); + addObstacle(mapBuffer[0], 320 - 64 - 22, 92,22,16); + addFgObj(mapBuffer[0], SCREEN_WIDTH - 64 - 22, 64, 22, 55, "assets/img/objects/floatingChips.png", 4, 1, 255); + + addWarp(mapBuffer[0], 0,0,8,180, LEVEL1, 1, 304,90); + addWarp(mapBuffer[0], 312,0,8,180, LEVEL1, 2, 16,90); + addWarp(mapBuffer[0], 0,0,320,8, LEVEL1, 3, 160,164); + + addWarp(mapBuffer[2], 0,0,8,180, LEVEL1, 0, 304,90); + addWarp(mapBuffer[1], 312,0,8,180, LEVEL1, 0, 16,90); + addWarp(mapBuffer[3], 0,172,320,8, LEVEL1, 0, 160,16); + + addWarp(mapBuffer[2], 312,0,8,180, LEVEL2,0, 16,165); + + + dialogueData[0] = newGTextBox(loadImage("assets/img/portraits/test.gif")); + addText(dialogueData[0], "I can't help but feel like I'm"); + addText(dialogueData[0], "trapped in the universe..."); + addText(dialogueData[0], "Do you ever feel that way?"); + addText(dialogueData[0], "It's maddening."); + addText(dialogueData[0], "Let me know if you feel it."); + + dialogueData[1] = newGTextBox(loadImage("assets/img/portraits/kmage.gif")); + addText(dialogueData[1], "Whoa! You and I look so alike!"); + + dialogueData[2] = newTextBox(); + addText(dialogueData[2], "It's a floating computer chip."); + addText(dialogueData[2], "How it can spin forever"); + addText(dialogueData[2], "without slowing or stopping"); + addText(dialogueData[2], "is a mystery to me..."); + addText(dialogueData[2], "Looks damn cool though!"); + + dialogueData[3] = newGTextBox(loadImage("assets/img/portraits/test.gif")); + addText(dialogueData[3], "Well fine then!"); + + dialogueData[4] = newGTextBox(loadImage("assets/img/portraits/test.gif")); + addText(dialogueData[4], "Thanks buddy."); + + kaosData[0] = newConversation(0); + kaosData[1] = newErase('t', 2); + kaosData[2] = newManip(hero, 0, 0); + kaosData[3] = newConversation(1); + kaosData[4] = newManip(hero, 0 ,0); + kaosData[5] = newConversation(2); + kaosData[6] = newConversation(3); + kaosData[7] = newConversation(4); + kaosData[12] = newErase('f', 1); + kaosData[13] = newErase('o', 1); + kaosData[14] = newErase('s', 0); + + HyperKaos* choiceBranchA = newHyperKaos(1,0, 0, 0, 0, 0); + addKaos(choiceBranchA, kaosData[7]); + addKaos(choiceBranchA, kaosData[2]); + addTrigger(mapBuffer[3], choiceBranchA); + choiceBranchA = NULL; + + HyperKaos* choiceBranchB = newHyperKaos(1,0, 0, 0, 0, 0); + addKaos(choiceBranchB, kaosData[6]); + addKaos(choiceBranchB, kaosData[2]); + addTrigger(mapBuffer[3], choiceBranchB); + choiceBranchB = NULL; + + HyperKaos* destroyChp2 = newHyperKaos(1,0,0,0,0,0); + + addKaos(destroyChp2, kaosData[12]); + addKaos(destroyChp2, kaosData[13]); + addKaos(destroyChp2, kaosData[14]); + + Synergy* testSigil = newSynergy(0,'e', 0, 74, 256, 48, destroyChp2); + addSigil(mapBuffer[0], testSigil); + + destroyChp2 = NULL; + testSigil = NULL; + + kaosData[8] = newChoice("Let her know?", "Sure thing", "Nope", mapBuffer[3]->eventTriggers[0], mapBuffer[3]->eventTriggers[1]); + + if(notCompleted(3)) + { + HyperKaos* testTextBox = newHyperKaos(3, 0, 0, 0, 320, 180); + addKaos(testTextBox, kaosData[4]); + addKaos(testTextBox, kaosData[0]); + addKaos(testTextBox, kaosData[1]); + addKaos(testTextBox, kaosData[8]); + + addTrigger(mapBuffer[3], testTextBox); + testTextBox = NULL; + } + + Player* randomDude; + if (notCompleted(5)) + randomDude = newPlayer("assets/img/characters/kmage.png", -16, -16); + else + randomDude = newPlayer("assets/img/characters/kmage.png", 160, 90); + + + addPerson(mapBuffer[1], randomDude); + addObstacle(mapBuffer[1], 152,76,16,22); + kaosData[9] = newFaceEachother(hero, randomDude); + kaosData[10] = newTeleport(randomDude, 160, 90, 0); + kaosData[11] = newErase('t', 0); + randomDude = NULL; + + if(notCompleted(5)) + { + HyperKaos* tpDude = newHyperKaos(5, 0, 300, 0, 20, 180); + addKaos(tpDude, kaosData[10]); + addKaos(tpDude, kaosData[11]); + addTrigger(mapBuffer[1],tpDude); + tpDude = NULL; + } + + HyperKaos* randomDudeConvo = newHyperKaos(1, 1, 152, 82, 16, 16); + addKaos(randomDudeConvo, kaosData[3]); + addKaos(randomDudeConvo, kaosData[9]); + addKaos(randomDudeConvo, kaosData[2]); + addTrigger(mapBuffer[1], randomDudeConvo); + randomDudeConvo = NULL; + + + break; + + case LEVEL2: + mapBuffer[0] = newRoom("assets/img/backgrounds/crystal_path.png", 4); + addObstacle(mapBuffer[0], 0,0,320,155); + addObstacle(mapBuffer[0], 0,175,320,10); + addWarp(mapBuffer[0], 0,155,8,20, LEVEL1, 2, 304, 90); + addWarp(mapBuffer[0], 312,155,8,20, LEVEL2, 1, 295, 158); + + mapBuffer[1] = newRoom("assets/img/backgrounds/crystal_room.png", 4); + addObstacle(mapBuffer[1], 0,0,320,115); + addObstacle(mapBuffer[1], 0,115,180,65); + addWarp(mapBuffer[1], 312,172,8,8, LEVEL2, 0, 304, 165); + break; + + case LEVEL3: + break; + } + printf("Loaded map chunk\n"); +} + +void dataPurge(int a, int b, int c, int d, int e) +{ + int i; + for (i = 0; i < a; i++) + deleteRoom(mapData[i]); + for (i = 0; i < b; i++) + deleteTextBox(dialogueData[i]); + for (i = 0; i < c; i++) + kaosData[i]->destroy(kaosData[i]); + +#ifdef SOUND_ON + for (i = 0; i < d; i++) + Mix_FreeMusic(bgmData[i]); + for (i = 0; i < e; i++) + Mix_FreeChunk(sfxData[i]); +#endif + +} + +void unloadData(enum dataChunks chunk) +{ + printf("Unloading old map chunk\n"); + switch(chunk) + { + case LEVEL1: + dataPurge(4,5,15,1,0); + break; + case LEVEL2: + dataPurge(2,0,0,0,0); + break; + case LEVEL3: + break; + } + printf("Unloaded old map chunk\n"); +} + +void pushBufferData() +{ + printf("Pushing map buffer\n"); + mapData = mapBuffer; + mapBuffer = (Room**)malloc(64*sizeof(Room*)); + +#ifdef SOUND_ON + if (hasMusic) + Mix_PlayMusic(bgmData[0], -1); +#endif + + printf("Map buffer clean\n"); +} + +void pager() +{ + if (thisChunk != nextChunk) + { + +#ifdef SOUND_ON + if (Mix_PlayingMusic()) + { + Mix_HaltMusic(); + } +#endif + + unloadData(thisChunk); + pushBufferData(); + thisChunk=nextChunk; + } +} \ No newline at end of file diff --git a/WorldData.c b/WorldData.c index 553be7c..b64678e 100644 --- a/WorldData.c +++ b/WorldData.c @@ -1,4 +1,5 @@ #include +#include #include "config.h" @@ -17,6 +18,7 @@ #include "TextBox.h" #include "Scene.h" #include "Synergy.h" +#include "WorldData.h" typedef struct timer Timer; #include "extern.h" @@ -24,162 +26,170 @@ typedef struct timer Timer; void bufferData(enum dataChunks chunk) { hasMusic = 0; - applySurface(0,0, loadingTxt, screen, NULL); + applySurface(8,8, loadingTxt, screen, NULL); frameAdvance(); printf("Loading map chunk\n"); - switch (chunk){ - case LEVEL1: - -#ifdef SOUND_ON - bgmData[0] = loadBGM("assets/snd/bgm/artificial sun stage (mamon machine mix).mp3"); -#endif - - mapBuffer[0] = newRoom("assets/img/backgrounds/blueroom.gif", 10); - mapBuffer[1] = newRoom("assets/img/backgrounds/redroom.gif", 4); - mapBuffer[2] = newRoom("assets/img/backgrounds/greenroom.gif", 4); - mapBuffer[3] = newRoom("assets/img/backgrounds/darkroom.gif", 4); - - addObstacle(mapBuffer[0], 64,92,22,16); - addFgObj(mapBuffer[0], 64,64,22,55,"assets/img/objects/floatingChips.png", 4, 1, 255); - addObstacle(mapBuffer[0], 320 - 64 - 22, 92,22,16); - addFgObj(mapBuffer[0], 320 - 64 - 22, 64, 22, 55, "assets/img/objects/floatingChips.png", 4, 1, 255); - - addWarp(mapBuffer[0], 0,0,8,180, LEVEL1, 1, 304,90); - addWarp(mapBuffer[0], 312,0,8,180, LEVEL1, 2, 16,90); - addWarp(mapBuffer[0], 0,0,320,8, LEVEL1, 3, 160,164); - - addWarp(mapBuffer[2], 0,0,8,180, LEVEL1, 0, 304,90); - addWarp(mapBuffer[1], 312,0,8,180, LEVEL1, 0, 16,90); - addWarp(mapBuffer[3], 0,172,320,8, LEVEL1, 0, 160,16); - - addWarp(mapBuffer[2], 312,0,8,180, LEVEL2,0, 16,165); - - - dialogueData[0] = newGTextBox(loadImage("assets/img/portraits/test.gif")); - addText(dialogueData[0], "I can't help but feel like I'm"); - addText(dialogueData[0], "trapped in the universe..."); - addText(dialogueData[0], "Do you ever feel that way?"); - addText(dialogueData[0], "It's maddening."); - addText(dialogueData[0], "Let me know if you feel it."); - - dialogueData[1] = newGTextBox(loadImage("assets/img/portraits/kmage.gif")); - addText(dialogueData[1], "Whoa! You and I look so alike!"); - - dialogueData[2] = newTextBox(); - addText(dialogueData[2], "It's a floating computer chip."); - addText(dialogueData[2], "How it can spin forever"); - addText(dialogueData[2], "without slowing or stopping"); - addText(dialogueData[2], "is a mystery to me..."); - addText(dialogueData[2], "Looks damn cool though!"); - - dialogueData[3] = newGTextBox(loadImage("assets/img/portraits/test.gif")); - addText(dialogueData[3], "Well fine then!"); - - dialogueData[4] = newGTextBox(loadImage("assets/img/portraits/test.gif")); - addText(dialogueData[4], "Thanks buddy."); - - kaosData[0] = newConversation(0); - kaosData[1] = newErase('t', 2); - kaosData[2] = newManip(hero, 0, 0); - kaosData[3] = newConversation(1); - kaosData[4] = newManip(hero, 0 ,0); - kaosData[5] = newConversation(2); - kaosData[6] = newConversation(3); - kaosData[7] = newConversation(4); - kaosData[12] = newErase('f', 1); - kaosData[13] = newErase('o', 1); - kaosData[14] = newErase('s', 0); - - HyperKaos* choiceBranchA = newHyperKaos(1,0, 0, 0, 0, 0); - addKaos(choiceBranchA, kaosData[7]); - addKaos(choiceBranchA, kaosData[2]); - addTrigger(mapBuffer[3], choiceBranchA); - choiceBranchA = NULL; - - HyperKaos* choiceBranchB = newHyperKaos(1,0, 0, 0, 0, 0); - addKaos(choiceBranchB, kaosData[6]); - addKaos(choiceBranchB, kaosData[2]); - addTrigger(mapBuffer[3], choiceBranchB); - choiceBranchB = NULL; - - HyperKaos* destroyChp2 = newHyperKaos(1,0,0,0,0,0); - - addKaos(destroyChp2, kaosData[12]); - addKaos(destroyChp2, kaosData[13]); - addKaos(destroyChp2, kaosData[14]); - - Synergy* testSigil = newSynergy(0,'e', 0, 74, 256, 48, destroyChp2); - addSigil(mapBuffer[0], testSigil); - - destroyChp2 = NULL; - testSigil = NULL; - - kaosData[8] = newChoice("Let her know?", "Sure thing", "Nope", mapBuffer[3]->eventTriggers[0], mapBuffer[3]->eventTriggers[1]); - - if(notCompleted(3)) - { - HyperKaos* testTextBox = newHyperKaos(3, 0, 0, 0, 320, 180); - addKaos(testTextBox, kaosData[4]); - addKaos(testTextBox, kaosData[0]); - addKaos(testTextBox, kaosData[1]); - addKaos(testTextBox, kaosData[8]); - - addTrigger(mapBuffer[3], testTextBox); - testTextBox = NULL; - } - - Player* randomDude; - if (notCompleted(5)) - randomDude = newPlayer("assets/img/characters/kmage.png", -16, -16); - else - randomDude = newPlayer("assets/img/characters/kmage.png", 160, 90); - - - addPerson(mapBuffer[1], randomDude); - addObstacle(mapBuffer[1], 152,76,16,22); - kaosData[9] = newFaceEachother(hero, randomDude); - kaosData[10] = newTeleport(randomDude, 160, 90, 0); - kaosData[11] = newErase('t', 0); - randomDude = NULL; - - if(notCompleted(5)) + if(worldBuilder(chunk) != 0) { - HyperKaos* tpDude = newHyperKaos(5, 0, 300, 0, 20, 180); - addKaos(tpDude, kaosData[10]); - addKaos(tpDude, kaosData[11]); - addTrigger(mapBuffer[1],tpDude); - tpDude = NULL; - } - - HyperKaos* randomDudeConvo = newHyperKaos(1, 1, 152, 82, 16, 16); - addKaos(randomDudeConvo, kaosData[3]); - addKaos(randomDudeConvo, kaosData[9]); - addKaos(randomDudeConvo, kaosData[2]); - addTrigger(mapBuffer[1], randomDudeConvo); - randomDudeConvo = NULL; - - - break; - - case LEVEL2: - mapBuffer[0] = newRoom("assets/img/backgrounds/crystal_path.png", 4); - addObstacle(mapBuffer[0], 0,0,320,155); - addObstacle(mapBuffer[0], 0,175,320,10); - addWarp(mapBuffer[0], 0,155,8,20, LEVEL1, 2, 304, 90); - addWarp(mapBuffer[0], 312,155,8,20, LEVEL2, 1, 295, 158); - - mapBuffer[1] = newRoom("assets/img/backgrounds/crystal_room.png", 4); - addObstacle(mapBuffer[1], 0,0,320,115); - addObstacle(mapBuffer[1], 0,115,180,65); - addWarp(mapBuffer[1], 312,172,8,8, LEVEL2, 0, 304, 165); - break; - - case LEVEL3: - break; - } + quit = 1; playing = 0; + printf("Fatal map error. Quitting\n"); + }; printf("Loaded map chunk\n"); } +int hashCmd(char* x) +{ + int hash = 7951; + int coef = 181; + while (*x) + { + hash += coef*(*x); + x++; + } + hash %= 256; + return hash; +} + +int worldBuilder(enum dataChunks chunk) +{ + char datafile[256]; + char lineBuffer[1024]; + char objBuffer[24]; + char cchunk[4]; + char propsBuffer[998]; + int savequery; + int conditional = 0; + FILE* worldInfo; + + sprintf(cchunk, "%d", chunk); + + strcpy(datafile, "mapdata/"); + strcat(datafile, cchunk); + strcat(datafile, ".txt"); + + worldInfo = fopen(datafile, "r"); + + if (!worldInfo) + { + printf("Couldn't read world data\n"); + return 1; + } + + while (fgets(lineBuffer, 1024, worldInfo)) + { + if (lineBuffer[0] == '#') continue; + if (lineBuffer[0] == '<') + { + conditional = 0; + continue; + } + if (!conditional) + { + if (sscanf(lineBuffer, "%[^:]: %[^\t\n]", objBuffer, propsBuffer) < 2) + { + printf("World data line malformed\n"); + return 1; + } + else + { + switch (hashCmd(objBuffer)) + { + case 200: //mkRoom + if (buildRoom(propsBuffer) != 0) + return 1; + break; + case 45: //mkKaos +// buildKaos(propsBuffer); + break; + case 109: //mkTextBox +// buildTextbox(propsBuffer); + break; + case 181: //addText +// modTextBox(propsBuffer); + break; + case 116: //SFX +// buildSFX(propsBuffer); + break; + case 93: //BGM +// buildBGM(propsBuffer); + break; + case 100: //addSigil +// buildSynergy(propsBuffer); + break; + case 47: //addPerson +// buildPerson(propsBuffer); + break; + case 88: //addObstruction +// buildObstruction(propsBuffer); + break; + case 141: //addImg +// buildFGImage(propsBuffer); + break; + case 240: //addTrigger +// buildHyper(propsBuffer); + break; + case 238: //addWarp + buildWarp(propsBuffer); + break; + } + } + }} + fclose(worldInfo); + return 0; +} + +int buildRoom(char* props) +{ + int slot; + char filename[256]; + int breathe; + + if (sscanf(props, "slot %d, sprite %[^,], spd %d", &slot, filename, &breathe) != 3) + { + return 1; + } + printf("Building room in map slot %d: bg = %s; spd = %d\n", slot, filename, breathe); + mapBuffer[slot] = newRoom(filename, breathe); + return 0; +} + +int buildWarp(char* props) +{ + int r, x, y, w, h, dC, dR, dX, dY; + if (sscanf(props, "room %d, x %d, y %d, w %d, h %d, dest %d,%d, dX %d, dY %d", + &r, &x, &y, &w, &h, &dC, &dR, &dX, &dY) != 9) + { + return 1; + } + addWarp(mapBuffer[r], x, y, w, h, dC, dR, dX, dY); + return 0; +} + +int countMapThings(char x, enum dataChunks chunk) +{ + int count = 0; + + char datafile[256]; + char lineBuffer[1024]; + char cchunk[4]; + + FILE* worldInfo; + + sprintf(cchunk, "%d", chunk); + + strcpy(datafile, "mapdata/"); + strcat(datafile, cchunk); + strcat(datafile, ".txt"); + + worldInfo = fopen(datafile, "r"); + while (fgets(lineBuffer, 1024, worldInfo)) + { + if (lineBuffer[0] == x) + count++; + } + return count; +} + void dataPurge(int a, int b, int c, int d, int e) { int i; @@ -201,18 +211,14 @@ void dataPurge(int a, int b, int c, int d, int e) void unloadData(enum dataChunks chunk) { + int a, b, c, d, e; printf("Unloading old map chunk\n"); - switch(chunk) - { - case LEVEL1: - dataPurge(4,5,15,1,0); - break; - case LEVEL2: - dataPurge(2,0,0,0,0); - break; - case LEVEL3: - break; - } + a = countMapThings('R', chunk); + b = countMapThings('T', chunk); + c = countMapThings('K', chunk); + d = countMapThings('M', chunk); + e = countMapThings('S', chunk); + dataPurge(a,b,c,d,e); printf("Unloaded old map chunk\n"); } diff --git a/WorldData.h b/WorldData.h index fa1d3cc..7c23179 100644 --- a/WorldData.h +++ b/WorldData.h @@ -1,6 +1,14 @@ void bufferData(enum dataChunks chunk); +int worldBuilder(enum dataChunks chunk); + +int buildRoom(char* props); + +int buildWarp(char* props); + +int countMapThings(char x, enum dataChunks chunk); + void unloadData(enum dataChunks chunk); void pushBufferData(); diff --git a/mapdata/1.txt b/mapdata/1.txt new file mode 100644 index 0000000..47e341e --- /dev/null +++ b/mapdata/1.txt @@ -0,0 +1,5 @@ +mkRoom: slot 0, sprite assets/img/backgrounds/blueroom.png, spd 4 +mkRoom: slot 1, sprite assets/img/backgrounds/darkroom.png, spd 4 +addWarp: room 0, x 0, y 0, w 320, h 8, dest 1,1, dX 160, dY 164 +addWarp: room 1, x 0, y 172, w 320, h 8, dest 1,0, dX 160, dY 16 +#addPerson: room 0, sprite assets/img/people/kmage.png, x 20, y 30