all repos — hyperkaos @ 456cba7e6c1f00986e18f695c63050d5cccdb96f

lightweight modular puzzle/adventure game engine in C with SDL 1.2

started implementing scripting language for map generation
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAlxX744ACgkQO3+8IhRO
Y5h7BQ//acA4PiXQgNN/ZC3m1Jqy7YcN8VFysH8e8SsE3JsnnCIMIOpbzfhMbTux
ih4xAYcqQ1PUcsbCuxSZosE3NBF9di0NpHR3jJB3/bclLBI067oZfLfqVWA6lDUg
cXQRHZt3S2hBo0GH6H8oWa+vlsPSsExpDVGF2i5NAo0qCJ4DaZQTYJwlXfZtxfH3
YJqm6+B62WcorXifqFH9P+XFvHwgdTvMemprYbbBiIS54Vkyii4crkSiZTyHTilw
pLVIXs6Kv306WftlqChqkBIvhS9BE7fmIh1JJK9pRviWQtjAoa2xDxYKMs2HmUCN
v/rWuSlf+dm3I75Dlr2i3zyRnY6MLxXKoatN4upN/Dx9OlB5W3gs+tq7uzB4LVUR
W9JVzgNga2EvOhUTqMPNuG3epbzT6yFRHrJxEUH2PtGnBQJ6H86jAySDe4FA3p+R
oVQx/c4+oqrQ5pXbcC5rR5UH9AtgoYbtbtKnxBp++4MTdvIv7QDhcVg5Dvg7ukfr
04+D/FB5xOYvXFYivnKCp55rX1OU3qNUdbKWRyYb5jX7ytNC+Oej35gt7d9yJLnA
G8ITvVxiX02koF3F//QalybkX+HaEg6iFRzf9sDT9pkfNp2GZIAvNCguqUozNr0y
JNrF3R83mctK6VwdLSWy2ypwgCh79+w2CKclnwTzkg6MXwLxiu4=
=O5q6
-----END PGP SIGNATURE-----
commit

456cba7e6c1f00986e18f695c63050d5cccdb96f

parent

1f86f43d8f83e096b0b9a82ea13fcf9ccbecce2a

4 files changed, 410 insertions(+), 142 deletions(-)

jump to
A WorldData.bk

@@ -0,0 +1,249 @@

+#include <stdio.h> + +#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; + } +}
M WorldData.cWorldData.c

@@ -1,4 +1,5 @@

#include <stdio.h> +#include <string.h> #include "config.h"

@@ -17,6 +18,7 @@ #include "HyperKaos.h"

#include "TextBox.h" #include "Scene.h" #include "Synergy.h" +#include "WorldData.h" typedef struct timer Timer; #include "extern.h"

@@ -24,160 +26,168 @@

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: + if(worldBuilder(chunk) != 0) + { + quit = 1; playing = 0; + printf("Fatal map error. Quitting\n"); + }; + printf("Loaded map chunk\n"); +} -#ifdef SOUND_ON - bgmData[0] = loadBGM("assets/snd/bgm/artificial sun stage (mamon machine mix).mp3"); -#endif +int hashCmd(char* x) +{ + int hash = 7951; + int coef = 181; + while (*x) + { + hash += coef*(*x); + x++; + } + hash %= 256; + return hash; +} - 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); +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; - 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); + sprintf(cchunk, "%d", chunk); - 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); + strcpy(datafile, "mapdata/"); + strcat(datafile, cchunk); + strcat(datafile, ".txt"); - addWarp(mapBuffer[2], 312,0,8,180, LEVEL2,0, 16,165); + worldInfo = fopen(datafile, "r"); + if (!worldInfo) + { + printf("Couldn't read world data\n"); + return 1; + } - 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)) + while (fgets(lineBuffer, 1024, worldInfo)) + { + if (lineBuffer[0] == '#') continue; + if (lineBuffer[0] == '<') { - 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; + conditional = 0; + continue; } - - Player* randomDude; - if (notCompleted(5)) - randomDude = newPlayer("assets/img/characters/kmage.png", -16, -16); - else - randomDude = newPlayer("assets/img/characters/kmage.png", 160, 90); + 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; - 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 (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; +} - if(notCompleted(5)) +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) { - HyperKaos* tpDude = newHyperKaos(5, 0, 300, 0, 20, 180); - addKaos(tpDude, kaosData[10]); - addKaos(tpDude, kaosData[11]); - addTrigger(mapBuffer[1],tpDude); - tpDude = NULL; + return 1; } + addWarp(mapBuffer[r], x, y, w, h, dC, dR, dX, dY); + return 0; +} - 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; +int countMapThings(char x, enum dataChunks chunk) +{ + int count = 0; + char datafile[256]; + char lineBuffer[1024]; + char cchunk[4]; - break; + FILE* worldInfo; - 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); + sprintf(cchunk, "%d", chunk); - 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; + strcpy(datafile, "mapdata/"); + strcat(datafile, cchunk); + strcat(datafile, ".txt"); - case LEVEL3: - break; + worldInfo = fopen(datafile, "r"); + while (fgets(lineBuffer, 1024, worldInfo)) + { + if (lineBuffer[0] == x) + count++; } - printf("Loaded map chunk\n"); + return count; } void dataPurge(int a, int b, int c, int d, int e)

@@ -201,18 +211,14 @@ }

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"); }
M WorldData.hWorldData.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();
A 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