[Add] Faction support.
This commit is contained in:
parent
2e78211acd
commit
a392c2c67b
@ -12,7 +12,7 @@ function attack()
|
||||
dir = face(target)
|
||||
dist = getdist(getpos(target))
|
||||
if dir < 10 and dist > 100 then
|
||||
accel(dist/100-1)
|
||||
accel()
|
||||
elseif dir < 10 and dist < 100 then
|
||||
shoot()
|
||||
end
|
||||
|
103
src/faction.c
Normal file
103
src/faction.c
Normal file
@ -0,0 +1,103 @@
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <libxml/parser.h>
|
||||
|
||||
#include "main.h"
|
||||
#include "log.h"
|
||||
#include "pack.h"
|
||||
#include "faction.h"
|
||||
|
||||
#define XML_NODE_START 1
|
||||
#define XML_NODE_TEXT 3
|
||||
|
||||
#define XML_FACTION_ID "Factions" // XML section id.
|
||||
#define XML_FACTION_TAG "faction"
|
||||
|
||||
#define FACTION_DATA "../dat/faction.xml"
|
||||
|
||||
Faction* faction_stack = NULL;
|
||||
int nfactions = 0;
|
||||
|
||||
static Faction* faction_parse(xmlNodePtr parent);
|
||||
|
||||
// Return the faction of name "name".
|
||||
Faction* get_faction(const char* name) {
|
||||
int i;
|
||||
for(i = 0; i < nfactions; i++)
|
||||
if(strcmp(faction_stack[i].name, name)==0)
|
||||
break;
|
||||
|
||||
if(i != nfactions)
|
||||
return faction_stack+i;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Return 1 if Faction a and b are enemies.
|
||||
int areEnemies(Faction* a, Faction* b) {
|
||||
int i = 0;
|
||||
while(a->enemies[i] != NULL && b != a->enemies[i]) i++;
|
||||
|
||||
if(a->enemies[i] == NULL) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Return 1 if Faction a and b are allies.
|
||||
int areAllies(Faction* a, Faction* b) {
|
||||
int i = 0;
|
||||
while(a->allies[i] != NULL && b != a->allies[i]) i++;
|
||||
|
||||
if(a->allies[i] == NULL) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static Faction* faction_parse(xmlNodePtr parent) {
|
||||
Faction* tmp = CALLOC_L(Faction);
|
||||
tmp->name = (char*)xmlGetProp(parent, (xmlChar*)"name");
|
||||
return tmp;
|
||||
}
|
||||
|
||||
int factions_load(void) {
|
||||
uint32_t bufsize;
|
||||
char* buf = pack_readfile(DATA, FACTION_DATA, &bufsize);
|
||||
|
||||
xmlNodePtr node;
|
||||
xmlDocPtr doc = xmlParseMemory(buf, bufsize);
|
||||
|
||||
Faction* tmp = NULL;
|
||||
|
||||
node = doc->xmlChildrenNode; // Ships node.
|
||||
if(strcmp((char*)node->name, XML_FACTION_ID)) {
|
||||
ERR("Malformed "FACTION_DATA" file: missing root element '"XML_FACTION_ID"'");
|
||||
return -1;
|
||||
}
|
||||
|
||||
node = node->xmlChildrenNode; // First ship node.
|
||||
if(node == NULL) {
|
||||
ERR("Malformed "FACTION_DATA" file: does not contain elements");
|
||||
return -1;
|
||||
}
|
||||
|
||||
do {
|
||||
if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_FACTION_TAG) == 0) {
|
||||
tmp = faction_parse(node);
|
||||
faction_stack = realloc(faction_stack, sizeof(Faction)*(++nfactions));
|
||||
memcpy(faction_stack+nfactions-1, tmp, sizeof(Faction));
|
||||
free(tmp);
|
||||
}
|
||||
} while((node = node->next));
|
||||
|
||||
xmlFreeDoc(doc);
|
||||
free(buf);
|
||||
xmlCleanupParser();
|
||||
|
||||
DEBUG("Loaded %d factions", nfactions);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void factions_free(void) {
|
||||
free(faction_stack);
|
||||
nfactions = 0;
|
||||
}
|
||||
|
17
src/faction.h
Normal file
17
src/faction.h
Normal file
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
typedef struct Faction {
|
||||
char* name;
|
||||
|
||||
struct Faction** enemies;
|
||||
struct Faction** allies;
|
||||
} Faction;
|
||||
|
||||
Faction* get_faction(const char* name);
|
||||
|
||||
int areEnemies(Faction* a, Faction* b);
|
||||
int areAllies(Faction* a, Faction* b);
|
||||
|
||||
int factions_load(void);
|
||||
void factions_free(void);
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include "outfit.h"
|
||||
#include "pack.h"
|
||||
#include "weapon.h"
|
||||
#include "faction.h"
|
||||
#include "pilot.h"
|
||||
|
||||
#define APPNAME "Lephisto"
|
||||
@ -245,6 +246,7 @@ int main(int argc, char** argv) {
|
||||
gui_init(); // Init the GUI crap.
|
||||
|
||||
// Data loading.
|
||||
factions_load();
|
||||
outfit_load();
|
||||
ships_load();
|
||||
space_load();
|
||||
@ -282,6 +284,7 @@ int main(int argc, char** argv) {
|
||||
gui_free(); // Free up the gui.
|
||||
ships_free();
|
||||
outfit_free();
|
||||
factions_free();
|
||||
|
||||
gl_freeFont(NULL);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user