beginning conversion of all kaos construcors to be of the form: newKaosType(char* args) to make map generation code maintainable

This commit is contained in:
Iris Lightshard 2019-03-05 17:26:01 -08:00
parent 1c26ee6a82
commit ffde2dc832
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
5 changed files with 72 additions and 63 deletions

View file

@ -327,7 +327,7 @@ int init(int argc, char* args[])
void writeSpellBook() void writeSpellBook()
{ {
HyperKaos* testSpell = newHyperKaos(1,0,0,0,0,0); HyperKaos* testSpell = newHyperKaos(1,0,0,0,0,0);
Kaos* stopPlayer = newManip(hero, 0,0); Kaos* stopPlayer = newManip("hero, xSpd 0, ySpd 0");
Kaos* beam = newSpell_Beam(); Kaos* beam = newSpell_Beam();
addKaos(testSpell, stopPlayer); addKaos(testSpell, stopPlayer);
addKaos(testSpell, beam); addKaos(testSpell, beam);
@ -336,7 +336,7 @@ void writeSpellBook()
spellBook[0] = testSpell; spellBook[0] = testSpell;
testSpell = newHyperKaos(1,0,0,0,0,0); testSpell = newHyperKaos(1,0,0,0,0,0);
Kaos* stopPlayer2 = newManip(hero, 0,0); Kaos* stopPlayer2 = newManip("hero, xSpd 0, ySpd 0");
Kaos* flash = newSpell_Flash(); Kaos* flash = newSpell_Flash();
addKaos(testSpell, stopPlayer2); addKaos(testSpell, stopPlayer2);
addKaos(testSpell, flash); addKaos(testSpell, flash);

34
Kaos.c
View file

@ -24,11 +24,21 @@ Kaos* rawKaos()
return self; return self;
} }
Kaos* newConversation(int i) Kaos* newConversation(char* args)
{ {
int i;
Kaos* core = rawKaos(); Kaos* core = rawKaos();
Conversation* self = malloc(sizeof(Conversation)); Conversation* self = malloc(sizeof(Conversation));
if (sscanf(args, "textBox %d", &i) != 1)
{
free(core);
free(self);
self = NULL;
core = NULL;
return core;
}
self->index = i; self->index = i;
core->kType = self; core->kType = self;
@ -132,11 +142,31 @@ void deleteChoice(Kaos* self)
free(self); free(self);
} }
Kaos* newManip(Player* t, int x, int y) Kaos* newManip(char* args)
{ {
Player* t;
int rm, x, y;
int pSlot = -1;
Kaos* core = rawKaos(); Kaos* core = rawKaos();
Manip* self = malloc(sizeof(Manip)); Manip* self = malloc(sizeof(Manip));
t = hero;
if (sscanf(args, "room %u, person %u, xSpd %d, ySpd %d",
&rm, &pSlot, &x, &y) != 4)
if (sscanf(args, "hero, xSpd %d, ySpd %d",
&x, &y) != 2)
{
free(core);
core = NULL;
free(self);
self = NULL;
return core;
}
if (pSlot >=0)
t = mapBuffer[rm]->people[pSlot];
self->target= t; self->target= t;
self->xSpd = x; self->xSpd = x;
self->ySpd = y; self->ySpd = y;

4
Kaos.h
View file

@ -91,7 +91,7 @@ typedef struct kaos_Spell_Flash
Kaos* rawKaos(); Kaos* rawKaos();
Kaos* newConversation(int i); Kaos* newConversation(char* args);
void runConversation(Kaos* self); void runConversation(Kaos* self);
void deleteConversation(Kaos* target); void deleteConversation(Kaos* target);
@ -99,7 +99,7 @@ Kaos* newChoice(char* q, char* a1, char* a2, HyperKaos* p1, HyperKaos* p2);
void runChoice(Kaos* self); void runChoice(Kaos* self);
void deleteChoice(Kaos* target); void deleteChoice(Kaos* target);
Kaos* newManip(Player* t, int x, int y); Kaos* newManip(char* args);
void runManip(Kaos* self); void runManip(Kaos* self);
void deleteManip(Kaos* target); void deleteManip(Kaos* target);

View file

@ -111,7 +111,7 @@ int worldBuilder(enum dataChunks chunk)
case 229: //loadFX case 229: //loadFX
fp = &buildSFX; fp = &buildSFX;
break; break;
case 125: //BGM case 125: //loadBGM
fp = &buildBGM; fp = &buildBGM;
break; break;
case 100: //addSigil case 100: //addSigil
@ -232,7 +232,7 @@ int buildKaos(char* props)
{ {
int slot; int slot;
char kType, kProps[990]; char kType, kProps[990];
int (*fp)(int s, char* props); Kaos* (*fp)(char* args);
if (sscanf(props, "slot %u, class %c, %[^\n]", if (sscanf(props, "slot %u, class %c, %[^\n]",
&slot, &kType, kProps) != 3) &slot, &kType, kProps) != 3)
@ -241,13 +241,13 @@ int buildKaos(char* props)
switch (kType) switch (kType)
{ {
case 'C': case 'C':
fp = &buildConvo; fp = &newConversation;
break; break;
case 'W': case 'W':
// fp = &buildWait; // fp = &buildWait;
break; break;
case 'M': case 'M':
fp = &buildManip; fp = &newManip;
break; break;
case 'L': case 'L':
//if (buildLook(slot, kProps) != 0) return 1; //if (buildLook(slot, kProps) != 0) return 1;
@ -266,40 +266,11 @@ int buildKaos(char* props)
break; break;
} }
if (!fp(slot, kProps)) if (!(kaosData[slot] = fp(kProps)))
return 0; return 0;
return 1; return 1;
} }
int buildConvo(int slot, char* kProps)
{
int tSlot;
if (sscanf(kProps, "textbox %u", &tSlot) != 1)
return 0;
printf(">>convo\n");
kaosData[slot] = newConversation(tSlot);
return 1;
}
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)
if (sscanf(kProps, "player, xSpd %d, ySpd %d",
&x, &y) != 2)
return 0;
if (pSlot >= 0)
target = mapBuffer[rm]->people[pSlot];
printf(">>manip\n");
kaosData[slot] = newManip(target, x, y);
return 1;
}
int buildObstruction(char* props) int buildObstruction(char* props)
{ {
int rm, x, y, w, h; int rm, x, y, w, h;
@ -378,12 +349,12 @@ int buildPerson(char* props)
return 1; return 1;
} }
int countMapThings(enum dataChunks chunk) int* countMapThings(int* count, enum dataChunks chunk)
{ {
int count = 0;
char datafile[256]; char datafile[256];
char lineBuffer[1024]; char lineBuffer[1024];
char cmdBuffer[24];
char propsBuffer[998];
char cchunk[4]; char cchunk[4];
FILE* worldInfo; FILE* worldInfo;
@ -397,26 +368,43 @@ int countMapThings(enum dataChunks chunk)
worldInfo = fopen(datafile, "r"); worldInfo = fopen(datafile, "r");
while (fgets(lineBuffer, 1024, worldInfo)) while (fgets(lineBuffer, 1024, worldInfo))
{ {
if (lineBuffer[0] == x) sscanf(lineBuffer, "%[^:]: %[^\t\n]", cmdBuffer, propsBuffer);
count++; switch(hashCmd(cmdBuffer))
{
case 200:
count[0]++;
break;
case 45:
count[1]++;
break;
case 109:
count[2]++;
break;
case 229:
count[3]++;
break;
case 125:
count[4]++;
break;
}
} }
return count; return count;
} }
void dataPurge(int a, int b, int c, int d, int e) void dataPurge(int* objs)
{ {
int i; int i;
for (i = 0; i < a; i++) for (i = 0; i < objs[0]; i++)
deleteRoom(mapData[i]); deleteRoom(mapData[i]);
for (i = 0; i < b; i++) for (i = 0; i < objs[2]; i++)
deleteTextBox(dialogueData[i]); deleteTextBox(dialogueData[i]);
for (i = 0; i < c; i++) for (i = 0; i < objs[1]; i++)
kaosData[i]->destroy(kaosData[i]); kaosData[i]->destroy(kaosData[i]);
#ifdef SOUND_ON #ifdef SOUND_ON
for (i = 0; i < d; i++) for (i = 0; i < objs[3]; i++)
Mix_FreeMusic(bgmData[i]); Mix_FreeMusic(bgmData[i]);
for (i = 0; i < e; i++) for (i = 0; i < objs[4]; i++)
Mix_FreeChunk(sfxData[i]); Mix_FreeChunk(sfxData[i]);
#endif #endif
@ -424,14 +412,9 @@ void dataPurge(int a, int b, int c, int d, int e)
void unloadData(enum dataChunks chunk) void unloadData(enum dataChunks chunk)
{ {
int a, b, c, d, e; int thingsCount[5] = {0,0,0,0,0};
printf("Unloading old map chunk\n"); printf("Unloading old map chunk\n");
a = countMapThings('R', chunk); dataPurge(countMapThings(thingsCount, 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"); printf("Unloaded old map chunk\n");
} }

View file

@ -29,11 +29,7 @@ int modTextBox(char* props);
int chainKaos(char* props); int chainKaos(char* props);
int buildConvo(int slot, char* kProps); int* countMapThings(int* counts, enum dataChunks chunk);
int buildManip(int slot, char* kProps);
int countMapThings(char x, enum dataChunks chunk);
void unloadData(enum dataChunks chunk); void unloadData(enum dataChunks chunk);