hyperkaos/WorldData.c

447 lines
9 KiB
C
Raw Normal View History

#include <stdio.h>
#include <string.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"
#include "WorldData.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");
if(worldBuilder(chunk) != 0)
{
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];
2019-02-09 01:23:13 +00:00
char cmdBuffer[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)
{
2019-02-09 01:23:13 +00:00
if (sscanf(lineBuffer, "%[^:]: %[^\t\n]", cmdBuffer, propsBuffer) < 2)
{
printf("World data line malformed\n");
return 1;
}
else
{
2019-02-09 01:23:13 +00:00
switch (hashCmd(cmdBuffer))
{
case 200: //mkRoom
if (buildRoom(propsBuffer) != 0)
2019-02-09 01:23:13 +00:00
return 1;
break;
case 45: //mkKaos
2019-02-09 01:23:13 +00:00
if (buildKaos(propsBuffer) != 0) return 1;
break;
case 109: //mkTextBox
2019-02-09 01:23:13 +00:00
if (buildTextBox(propsBuffer) != 0) return 1;
break;
case 181: //addText
2019-02-09 01:23:13 +00:00
if (modTextBox(propsBuffer) != 0) return 1;
break;
case 116: //SFX
// buildSFX(propsBuffer);
2019-02-09 01:23:13 +00:00
break;
case 93: //BGM
// buildBGM(propsBuffer);
2019-02-09 01:23:13 +00:00
break;
case 100: //addSigil
// buildSynergy(propsBuffer);
2019-02-09 01:23:13 +00:00
break;
case 47: //addPerson
2019-02-09 01:23:13 +00:00
if (buildPerson(propsBuffer) != 0)
return 1;
break;
case 88: //addObstruction
2019-02-09 01:23:13 +00:00
if (buildObstruction(propsBuffer) != 0)
return 1;
break;
case 141: //addImg
2019-02-09 01:23:13 +00:00
if (buildFGImage(propsBuffer) != 0)
return 1;
break;
case 240: //addTrigger
2019-02-09 01:23:13 +00:00
if (buildHyper(propsBuffer) != 0)
return 1;
break;
2019-02-08 22:38:55 +00:00
case 148: //chainKaos
2019-02-09 01:23:13 +00:00
if (chainKaos(propsBuffer) != 0)
return 1;
break;
case 238: //addWarp
2019-02-09 01:23:13 +00:00
if (buildWarp(propsBuffer) != 0)
return 1;
break;
}
}
}}
fclose(worldInfo);
return 0;
}
int buildRoom(char* props)
{
int slot;
char filename[256];
int breathe;
2019-02-08 22:38:55 +00:00
if (sscanf(props, "slot %u, sprite %[^,], spd %u", &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;
}
2019-02-09 01:23:13 +00:00
int buildTextBox(char* props)
{
int slot;
char portrait[256];
if (sscanf(props, "slot %u, portrait %[^\n]",
&slot, portrait) != 2)
{
return 1;
}
printf("building textbox...\n");
if (strcmp(portrait, "none") != 0)
{
dialogueData[slot] = newTextBox();
}
else
{
dialogueData[slot] = newGTextBox(loadImage(portrait));
}
return 0;
}
int modTextBox(char* props)
{
int slot;
char textData[64];
if (sscanf(props, "slot %u, %[^\n]", &slot, textData) != 2)
{
return 1;
}
printf("adding line of text to textbox...\n");
addText(dialogueData[slot], textData);
return 0;
}
int buildKaos(char* props)
{
int slot;
char kType, kProps[990];
if (sscanf(props, "slot %u, class %c, %[^\n]",
&slot, &kType, kProps) != 3)
{
return 1;
}
printf("building kaos...\n");
switch (kType)
{
case 'C':
if (buildConvo(slot, kProps) != 0) return 1;
break;
case 'W':
//if (buildWait(slot, kProps) != 0) return 1;
break;
case 'M':
if (buildManip(slot, kProps) != 0) return 1;
break;
case 'L':
//if (buildLook(slot, kProps) != 0) return 1;
break;
case 'F':
// if (buildFaceEachother(slot, kProps) != 0) return 1;
break;
case 'Y':
//if (buildChoice(slot, kProps) != 0) return 1;
break;
case 'S':
// if (buildSound(slot, kProps) != 0) return 1;
break;
case 'T':
//if (buildTeleport(slot, kProps) != 0) return 1;
break;
}
return 0;
}
int buildConvo(int slot, char* kProps)
{
int tSlot;
if (sscanf(kProps, "textbox %u", &tSlot) != 1)
return 1;
printf(">>convo\n");
kaosData[slot] = newConversation(tSlot);
return 0;
}
int buildManip(int slot, char* kProps)
{
int rm, pSlot, x, y;
pSlot = -1;
Player* target = hero;
if (sscanf(kProps, "room %u, person %u, xSpd %d, ySpd %d",
&rm, &pSlot, &x, &y) != 4)
{
}
else if (sscanf(kProps, "player, xSpd %d, ySpd %d",
&x, &y) != 2)
{
return 1;
}
if (pSlot >= 0)
{
target = mapBuffer[rm]->people[pSlot];
}
printf(">>manip\n");
kaosData[slot] = newManip(target, x, y);
return 0;
}
2019-02-08 22:38:55 +00:00
int buildObstruction(char* props)
{
int rm, x, y, w, h;
if (sscanf(props, "room %u, x %d, y %d, w %u, h %u",
&rm, &x, &y, &w, &h) != 5)
{
return 1;
}
2019-02-09 01:23:13 +00:00
printf("Building obstacle...\n");
2019-02-08 22:38:55 +00:00
addObstacle(mapBuffer[rm], x, y, w, h);
return 0;
}
int buildFGImage(char* props)
{
int rm, x, y, w, h, f, d, a;
char filename[256];
if (sscanf(props, "room %u, x %d, y %d, w %u, h %u, sprite %[^,], frames %u, dual %u, alpha %u",
&rm, &x, &y, &w, &h, filename, &f, &d, &a) !=9)
{
return 1;
}
2019-02-09 01:23:13 +00:00
printf("Building FG Image...\n");
addFgObj(mapBuffer[rm], x, y, w, h, filename, f, d, a);
return 0;
}
int buildWarp(char* props)
{
int r, x, y, w, h, dC, dR, dX, dY;
2019-02-08 22:38:55 +00:00
if (sscanf(props, "room %u, x %d, y %d, w %u, h %u, dest %u,%u, dX %d, dY %d",
&r, &x, &y, &w, &h, &dC, &dR, &dX, &dY) != 9)
{
return 1;
}
2019-02-08 22:38:55 +00:00
printf("Building warp...\n");
addWarp(mapBuffer[r], x, y, w, h, dC, dR, dX, dY);
return 0;
}
2019-02-08 22:38:55 +00:00
int buildHyper(char* props)
{
int rm, id, tType, x, y, w, h;
if (sscanf(props, "room %u, id %u, type %u, x %d, y %d, w %u, h %u",
&rm, &id, &tType, &x, &y, &w, &h) != 7)
{
return 1;
}
printf("Building hyperkaos...\n");
HyperKaos* temp = newHyperKaos(id, tType, x, y, w, h);
addTrigger(mapBuffer[rm], temp);
return 0;
}
2019-02-09 01:23:13 +00:00
int chainKaos(char* props)
{
int rm, trig, k;
if (sscanf(props, "room %u, trigger %u, kaos %u",
&rm, &trig, &k) != 3)
{
return 1;
}
printf("Chaining kaos...\n");
addKaos(mapBuffer[rm]->eventTriggers[trig], kaosData[k]);
return 0;
}
2019-02-08 22:38:55 +00:00
int buildPerson(char* props)
{
int rm, x, y;
char filename[256];
if (sscanf(props, "room %u, sprite %[^,], x %d, y%d",
&rm, filename, &x, &y) != 4)
{
return 1;
}
printf("Building person...\n");
Player* temp = newPlayer(filename, x, y);
addPerson(mapBuffer[rm], temp);
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;
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)
{
int a, b, c, d, e;
printf("Unloading old map chunk\n");
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");
}
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;
}
}