[Add] Star systems.

[Add] Planets.
This commit is contained in:
Allanis 2013-02-04 09:09:04 +00:00
parent f7dadaae29
commit 376eedc616
21 changed files with 335 additions and 57 deletions

View File

@ -31,7 +31,7 @@ LDFLAGS = -lm $(LDLUA) $(LDSDL) $(LDXML) $(LDTTF) $(LDGL)
DATA = data DATA = data
DATAFILES = $(shell find ../scripts/ai/ ../gfx/ ../dat/ -name '*.lua' -o -name '*.png' -o -name '*.xml') DATAFILES = $(shell find ../scripts/ai/ ../gfx/ ../dat/ -name '*.lua' -o -name '*.png' -o -name '*.xml')
%.o: %.c %.o: %.c %.h
@gcc -c $(CFLAGS) -o $@ $< @gcc -c $(CFLAGS) -o $@ $<
@echo -e "\tCC $@" @echo -e "\tCC $@"

View File

@ -11,7 +11,7 @@
<tech>0</tech> <tech>0</tech>
<commodities>1</commodities> <commodities>1</commodities>
</general> </general>
<GFX>KonoSphere</GFX> <GFX>konosphere.png</GFX>
</planet> </planet>
<planet name="SaraCraft"> <planet name="SaraCraft">
<pos> <pos>
@ -24,6 +24,6 @@
<tech>0</tech> <tech>0</tech>
<commodities>1</commodities> <commodities>1</commodities>
</general> </general>
<GFX>SaraCraft</GFX> <GFX>saracraft.png</GFX>
</planet> </planet>
</Planets> </Planets>

View File

@ -6,7 +6,7 @@
<y>8</y> <y>8</y>
</pos> </pos>
<general> <general>
<stars>50</stars> <stars>500</stars>
<asteroids>0</asteroids> <asteroids>0</asteroids>
<interference>0</interference> <interference>0</interference>
<faction>2</faction> <faction>2</faction>
@ -22,9 +22,6 @@
<fleet chance="50">Sml Merchant Convoy</fleet> <fleet chance="50">Sml Merchant Convoy</fleet>
<fleet chance="40">Sml Merchant Convoy</fleet> <fleet chance="40">Sml Merchant Convoy</fleet>
</fleets> </fleets>
<paths>
<path>KonoSys</path>
</paths>
</ssys> </ssys>
<ssys name="KonoSys"> <ssys name="KonoSys">
<pos> <pos>
@ -44,8 +41,5 @@
<fleet chance="80">Merchant Ship</fleet> <fleet chance="80">Merchant Ship</fleet>
<fleet chance="60">Merchant Ship</fleet> <fleet chance="60">Merchant Ship</fleet>
</fleets> </fleets>
<paths>
<path>SaraSys</path>
</paths>
</ssys> </ssys>
</Systems> </Systems>

BIN
gfx/planet/konosphere.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
gfx/planet/saracraft.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -5,7 +5,7 @@
#include <math.h> #include <math.h>
#include "def.h" #include "main.h"
#include "log.h" #include "log.h"
#include "pilot.h" #include "pilot.h"
#include "physics.h" #include "physics.h"

View File

@ -1,6 +1,6 @@
#include <SDL.h> #include <SDL.h>
#include <string.h> #include <string.h>
#include "def.h" #include "main.h"
#include "log.h" #include "log.h"
#include "joystick.h" #include "joystick.h"

View File

@ -6,7 +6,7 @@
#include <string.h> #include <string.h>
#include <getopt.h> #include <getopt.h>
#include "def.h" #include "main.h"
#include "log.h" #include "log.h"
#include "physics.h" #include "physics.h"
#include "opengl.h" #include "opengl.h"
@ -196,11 +196,12 @@ int main(int argc, char** argv) {
// Data loading. // Data loading.
ships_load(); ships_load();
space_load();
// Testing. // Testing.
pilot_create(get_ship("Ship"), "Player", NULL, NULL, PILOT_PLAYER); pilot_create(get_ship("Ship"), "Player", NULL, NULL, PILOT_PLAYER);
gl_bindCamera(&player->solid->pos); gl_bindCamera(&player->solid->pos);
space_init(); space_init("SaraSys");
pilot_create(get_ship("Test"), NULL, NULL, NULL, 0); pilot_create(get_ship("Test"), NULL, NULL, NULL, 0);
@ -264,6 +265,8 @@ static void update_all(void) {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
space_render(dt); space_render(dt);
planets_render();
pilots_update(dt); pilots_update(dt);
display_fps(dt); display_fps(dt);

View File

@ -5,5 +5,5 @@
#define ABS(X) ((X<0)?-X:X) #define ABS(X) ((X<0)?-X:X)
#define DATA "data" #define DATA "data" // Data file.

View File

@ -7,7 +7,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include "def.h" #include "main.h"
#include "log.h" #include "log.h"
#include "pack.h" #include "pack.h"
#include "opengl.h" #include "opengl.h"

View File

@ -53,7 +53,7 @@ static off_t getfilesize(const char* filename) {
} }
// Return true if filename is a Packfile. // Return true if filename is a Packfile.
int pack_check(char* filename) { int pack_check(const char* filename) {
int fd = open(filename, O_RDONLY); int fd = open(filename, O_RDONLY);
if(fd == -1) { if(fd == -1) {
ERR("Error opening %s: %s", filename, strerror(errno)); ERR("Error opening %s: %s", filename, strerror(errno));
@ -77,7 +77,7 @@ int pack_check(char* filename) {
ERR("Error writing to file: %s", strerror(errno)); \ ERR("Error writing to file: %s", strerror(errno)); \
free(buf); return -1; } free(buf); return -1; }
int pack_files(char* outfile, char** infiles, uint32_t nfiles) { int pack_files(const char* outfile, const char** infiles, const uint32_t nfiles) {
void* buf; void* buf;
struct stat file; struct stat file;
int i, namesize; int i, namesize;
@ -162,7 +162,7 @@ int pack_files(char* outfile, char** infiles, uint32_t nfiles) {
#define READ(f,b,n) if(read(f,b,n) != n) { \ #define READ(f,b,n) if(read(f,b,n) != n) { \
ERR("Too few bytes read. Expected more."); \ ERR("Too few bytes read. Expected more."); \
free(buf); return -1; } free(buf); return -1; }
int pack_open(Packfile* file, char* packfile, char* filename) { int pack_open(Packfile* file, const char* packfile, const char* filename) {
int i, j; int i, j;
uint32_t nfiles; uint32_t nfiles;
char* buf = (char*)malloc(MAX_FILENAME); char* buf = (char*)malloc(MAX_FILENAME);
@ -237,7 +237,7 @@ ssize_t pack_read(Packfile* file, void* buf, size_t count) {
} }
// Loads an entire file into memory and returns a pointer to it. // Loads an entire file into memory and returns a pointer to it.
void* pack_readfile(char* packfile, char* filename, uint32_t* filesize) { void* pack_readfile(const char* packfile, const char* filename, uint32_t* filesize) {
Packfile* file = (Packfile*)malloc(sizeof(Packfile)); Packfile* file = (Packfile*)malloc(sizeof(Packfile));
void* buf; void* buf;
int size, bytes; int size, bytes;

View File

@ -11,12 +11,12 @@ typedef struct {
// Packfile manipulation. Automatically allocated and freed (with open and close). // Packfile manipulation. Automatically allocated and freed (with open and close).
// Basic. // Basic.
int pack_check(char* filename); int pack_check(const char* filename);
int pack_files(char* outfile, char** infiles, uint32_t nfiles); int pack_files(const char* outfile, const char** infiles, const uint32_t nfiles);
int pack_open(Packfile* file, char* packfile, char* filename); int pack_open(Packfile* file, const char* packfile, const char* filename);
ssize_t pack_read(Packfile* file, void* buf, size_t count); ssize_t pack_read(Packfile* file, void* buf, const size_t count);
int pack_close(Packfile* file); int pack_close(Packfile* file);
// Fancy stuff. // Fancy stuff.
void* pack_readfile(char* packfile, char* filename, uint32_t* filesize); void* pack_readfile(const char* packfile, const char* filename, uint32_t* filesize);

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "def.h" #include "main.h"
#define VX(v) ((v).x) #define VX(v) ((v).x)
#define VY(v) ((v).y) #define VY(v) ((v).y)

View File

@ -3,7 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include "def.h" #include "main.h"
#include "log.h" #include "log.h"
#include "pilot.h" #include "pilot.h"

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "def.h" #include "main.h"
#include "physics.h" #include "physics.h"
#include "ai.h" #include "ai.h"
#include "ship.h" #include "ship.h"

View File

@ -1,5 +1,5 @@
#include <malloc.h> #include <malloc.h>
#include "def.h" #include "main.h"
#include "pilot.h" #include "pilot.h"
#include "log.h" #include "log.h"
#include "player.h" #include "player.h"

View File

@ -1,16 +1,15 @@
#include <string.h> #include <string.h>
#include <libxml/xmlreader.h> #include <libxml/parser.h>
#include "main.h"
#include "log.h" #include "log.h"
#include "pack.h" #include "pack.h"
#include "ship.h" #include "ship.h"
#define MAX_PATH_NAME 20 // Maximum size of the path. #define MAX_PATH_NAME 30 // Maximum size of the path.
#define XML_NODE_START 1 #define XML_NODE_START 1
#define XML_NODE_TEXT 3 #define XML_NODE_TEXT 3
#define XML_NODE_CLOSE 15
#define XML_NODE_CDATA 4
#define XML_ID "Ships" // XML section identifier. #define XML_ID "Ships" // XML section identifier.
#define XML_SHIP "ship" #define XML_SHIP "ship"
@ -125,42 +124,39 @@ Ship* ship_parse(xmlNodePtr parent) {
} }
int ships_load(void) { int ships_load(void) {
xmlNodePtr node;
uint32_t bufsize; uint32_t bufsize;
char* buf = pack_readfile(DATA, SHIP_DATA, &bufsize); char* buf = pack_readfile(DATA, SHIP_DATA, &bufsize);
xmlNodePtr node;
xmlDocPtr doc = xmlParseMemory(buf, bufsize); xmlDocPtr doc = xmlParseMemory(buf, bufsize);
Ship* tmp = NULL; Ship* tmp = NULL;
node = doc->xmlChildrenNode; // Ships node. node = doc->xmlChildrenNode; // Ships node.
if(strcmp((char*)node->name, XML_ID)) { if(strcmp((char*)node->name, XML_ID)) {
ERR("Malformed ships xml file: missing tag %s", XML_ID); ERR("Malformed "SHIP_DATA" file: missing root element '"XML_ID"'");
return -1; return -1;
} }
node = node->xmlChildrenNode; // First ship node. node = node->xmlChildrenNode; // First ship node.
if(node == NULL) { if(node == NULL) {
ERR("Malformed ships xml file: is missing ships"); ERR("Malformed "SHIP_DATA" file: Does not contain elements");
return -1; return -1;
} }
do { do {
if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_SHIP)==0) { if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_SHIP)==0) {
if(ship_stack == NULL) { tmp = ship_parse(node);
ship_stack = tmp = ship_parse(node); ship_stack = realloc(ship_stack, sizeof(Ship)*(++ships));
ships = 1; memcpy(ship_stack+ships-1, tmp, sizeof(Ship));
} else { free(tmp);
tmp = ship_parse(node);
ship_stack = realloc(ship_stack, sizeof(Ship)*(++ships));
memcpy(ship_stack+ships-1, tmp, sizeof(Ship));
free(tmp);
}
} }
} while((node = node->next)); } while((node = node->next));
xmlFreeDoc(doc); xmlFreeDoc(doc);
free(buf); free(buf);
xmlCleanupParser();
return 0; return 0;
} }

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "def.h" #include "main.h"
#include "opengl.h" #include "opengl.h"
enum ship_class { SHIP_CLASS_NULL, SHIP_CLASS_CIVILIAN }; enum ship_class { SHIP_CLASS_NULL, SHIP_CLASS_CIVILIAN };

View File

@ -1,3 +1,4 @@
#include <libxml/parser.h>
#include <malloc.h> #include <malloc.h>
#include <math.h> #include <math.h>
@ -6,20 +7,98 @@
#include "opengl.h" #include "opengl.h"
#include "rng.h" #include "rng.h"
#include "pilot.h" #include "pilot.h"
#include "pack.h"
#include "space.h" #include "space.h"
#define MAX_PATH_NAME 30 // Max size of the path.
#define XML_NODE_START 1
#define XML_NODE_TEST 3
#define XML_PLANET_ID "Planets"
#define XML_PLANET_TAG "planet"
#define XML_SYSTEM_ID "Systems"
#define XML_SYSTEM_TAG "ssys"
#define PLANET_DATA "../dat/planet.xml"
#define SPACE_DATA "../dat/ssys.xml"
#define PLANET_GFX "../gfx/planet/"
// Planet types. I didn't take them from Star Trek, I promise.
typedef enum {
PLANET_CLASS_A, // Geothermal.
PLANET_CLASS_B, // Geomorteus.
PLANET_CLASS_C, // Geoinactive.
PLANET_CLASS_D, // Asteroid/Moon.
PLANET_CLASS_E, // Geoplastic.
PLANET_CLASS_F, // Geometallic.
PLANET_CLASS_G, // GroCrystaline.
PLANET_CLASS_H, // Desert.
PLANET_CLASS_I, // Gas Supergiant.
PLANET_CLASS_J, // Gas Giant.
PLANET_CLASS_K, // Adaptable.
PLANET_CLASS_L, // Marginal.
PLANET_CLASS_M, // Terrestrial.
PLANET_CLASS_N, // Reducing.
PLANET_CLASS_O, // Pelagic.
PLANET_CLASS_P, // Glaciated.
PLANET_CLASS_Q, // Variable.
PLANET_CLASS_R, // Rogue.
PLANET_CLASS_S, // Ultragiant.
PLANET_CLASS_T, // Ultragiant.
PLANET_CLASS_X, // Demon.
PLANET_CLASS_Y, // Demon.
PLANER_CLASS_Z // Demon.
} PlanetClass;
typedef struct {
char* name;
double x, y; // Position in star system.
PlanetClass class;
gl_texture* gfx_space; // Graphics in space.
} Planet;
typedef struct {
char* name;
double x, y; // Position.
int stars, asteroids; // Un numero!
double interference; // Un uh.. Percentage.
// Factions.
Planet* planets; // Planets.
int nplanets;
// TODO: Throw some fleets here.
} StarSystem;
static StarSystem* systems = NULL;
static int nsystems = 0;
static StarSystem* cur_system = NULL; // Current star system.
#define STAR_BUF 100 // Area to leave around screen. #define STAR_BUF 100 // Area to leave around screen.
typedef struct { typedef struct {
double x, y; double x, y; // Position. It is simpler ligher to use two doubles than the physics.
double brightness; double brightness;
} Star; } Star;
Star* stars; static Star* stars = NULL; // Star array.
int nstars; static int nstars = 0; // Total stars.
void space_init(void) { static Planet* planet_get(const char* name);
static StarSystem* system_parse(const xmlNodePtr parent);
// Init the system.
void space_init(const char* sysname) {
int i; int i;
nstars = (500*gl_screen.w*gl_screen.h + STAR_BUF*STAR_BUF)/(800*640);; for(i = 0; i < nsystems; i++)
if(strcmp(sysname, systems[i].name)==0)
break;
if(i == nsystems) ERR("System %s not found in stack", sysname);
cur_system = systems++;
nstars = (cur_system->stars*gl_screen.w*gl_screen.h+STAR_BUF*STAR_BUF)/(800*640);
stars = malloc(sizeof(Star)*nstars); stars = malloc(sizeof(Star)*nstars);
for(i = 0; i < nstars; i++) { for(i = 0; i < nstars; i++) {
stars[i].brightness = (double)RNG(50, 200)/256.; stars[i].brightness = (double)RNG(50, 200)/256.;
@ -28,6 +107,186 @@ void space_init(void) {
} }
} }
// Load the planets of name 'name'.
static Planet* planet_get(const char* name) {
Planet* tmp = NULL;
char str[MAX_PATH_NAME] = "\0";
uint32_t bufsize;
char* buf = pack_readfile(DATA, PLANET_DATA, &bufsize);
xmlNodePtr node, cur;
xmlDocPtr doc = xmlParseMemory(buf, bufsize);
node = doc->xmlChildrenNode;
if(strcmp((char*)node->name, XML_PLANET_ID)) {
ERR("Malformed "PLANET_DATA" file: missing root element '"XML_PLANET_ID"'");
return NULL;
}
node = node->xmlChildrenNode; // First system node.
if(node == NULL) {
ERR("Malformed "PLANET_DATA" file: does not contain elements");
return NULL;
}
do {
if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_PLANET_TAG)==0) {
if(strcmp((char*)xmlGetProp(node, (xmlChar*)"name"), name)==0) { // Found.
tmp = CALLOC_L(Planet);
tmp->name = strdup(name);
node = node->xmlChildrenNode;
while((node = node->next)) {
if(strcmp((char*)node->name, "GFX")==0) {
cur = node->children;
if(strcmp((char*)cur->name, "text")==0) {
snprintf(str, strlen((char*)cur->content)+sizeof(PLANET_GFX),
PLANET_GFX"%s", (char*)cur->content);
tmp->gfx_space = gl_newSprite(str, 1, 1);
}
}
else if(strcmp((char*)node->name, "pos")==0) {
cur = node->children;
while((cur = cur->next)) {
if(strcmp((char*)cur->name, "x")==0)
tmp->x = atof((char*)cur->children->content);
else if(strcmp((char*)cur->name, "y")==0)
tmp->y = atof((char*)cur->children->content);
}
}
else if(strcmp((char*)node->name, "general")==0) {
cur = node->children;
while((cur = cur->next)) {
if(strcmp((char*)cur->name, "class")==0)
tmp->class = atoi((char*)cur->children->content);
}
}
}
break;
}
}
} while((node = node->next));
xmlFreeDoc(doc);
free(buf);
xmlCleanupParser();
// Check elements.
if(tmp) {
#define MELEMENT(o,s) if(o == 0) WARN("Planet '%s' missing '"s"' element", tmp->name)
MELEMENT(tmp->x, "x");
MELEMENT(tmp->x, "y");
MELEMENT(tmp->class, "class");
#undef MELEMENT
} else
WARN("No planet found matching name '%s'", name);
return tmp;
}
// Parse node 'parent' which should be the node of a system.
// Return the StarSystem fully loaded.
static StarSystem* system_parse(const xmlNodePtr parent) {
Planet* planet = NULL;
StarSystem* tmp = CALLOC_L(StarSystem);
xmlNodePtr cur, node;
tmp->name = strdup((char*) xmlGetProp(parent, (xmlChar*)"name"));
node = parent->xmlChildrenNode;
while((node = node->next)) {
// Load all the things!
if(strcmp((char*)node->name, "pos")==0) {
cur = node->children;
while((cur = cur->next)) {
if(strcmp((char*)cur->name, "x")==0)
tmp->x = atof((char*)cur->children->content);
if(strcmp((char*)cur->name, "y")==0)
tmp->y = atof((char*)cur->children->content);
}
}
else if(strcmp((char*)node->name, "general")==0) {
cur = node->children;
while((cur = cur->next)) {
if(strcmp((char*)cur->name, "stars")==0)
tmp->stars = atoi((char*)cur->children->content);
else if(strcmp((char*)cur->name, "asteroids")==0)
tmp->asteroids = atoi((char*)cur->children->content);
else if(strcmp((char*)cur->name, "interference")==0)
tmp->interference = atof((char*)cur->children->content);
}
}
else if(strcmp((char*)node->name, "planets")==0) {
cur = node->children;
while((cur = cur->next)) {
if(strcmp((char*)cur->name, "planet")==0) {
planet = planet_get((const char*)cur->children->content);
tmp->planets = realloc(tmp->planets, sizeof(Planet)*(++tmp->nplanets));
memcpy(tmp->planets+tmp->nplanets-1, planet, sizeof(Planet));
free(planet);
}
}
}
}
// Check elements.
#define MELEMENT(o,s) if(o == 0) WARN("Star System '%s' missing '"s"' element", tmp->name)
MELEMENT(tmp->x, "x");
MELEMENT(tmp->x, "y");
MELEMENT(tmp->stars, "stars");
/*MELEMENT(tmp->asteroids, "asteroids"); // Can be 0.
MELEMENT(tmp->interference, "interference");*/
#undef MELEMENT
DEBUG("Loaded Star System '%s' with %d Planets%s", tmp->name, tmp->nplanets, (tmp->nplanets > 1) ? "s" : "");
return tmp;
}
// Load the ENTIRE universe into RAM. -- WOAH! -- Wasn't that bad. :P
int space_load(void) {
uint32_t bufsize;
char* buf = pack_readfile(DATA, SPACE_DATA, &bufsize);
StarSystem* tmp;
xmlNodePtr node;
xmlDocPtr doc = xmlParseMemory(buf, bufsize);
node = doc->xmlChildrenNode;
if(strcmp((char*)node->name, XML_SYSTEM_ID)) {
ERR("Malformed "SPACE_DATA" file: missing root element '"XML_SYSTEM_ID"'");
return -1;
}
node = node->xmlChildrenNode; // First system node.
if(node == NULL) {
ERR("Malformed "SPACE_DATA" file: does not contain elements");
return -1;
}
do {
if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_SYSTEM_TAG)==0) {
if(systems == NULL) {
systems = tmp = system_parse(node);
nsystems = 1;
} else {
tmp = system_parse(node);
systems = realloc(systems, sizeof(StarSystem)*(++nsystems));
memcpy(systems+nsystems-1, tmp, sizeof(StarSystem));
free(tmp);
}
}
} while((node = node->next));
xmlFreeDoc(doc);
free(buf);
xmlCleanupParser();
return 0;
}
// Render the system. -- Just playing god now.
void space_render(double dt) { void space_render(double dt) {
int i; int i;
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
@ -51,7 +310,30 @@ void space_render(double dt) {
glPopMatrix(); // Projection translation matrix. glPopMatrix(); // Projection translation matrix.
} }
void space_exit(void) { // Render the planets.
free(stars); void planets_render(void) {
int i;
Vec2 v;
for(i = 0; i < cur_system->nplanets; i++) {
v.x = cur_system->planets[i].x;
v.y = cur_system->planets[i].y;
gl_blitSprite(cur_system->planets[i].gfx_space, &v, 0, 0);
}
}
// Clean up the system.
void space_exit(void) {
int i,j;
for(i = 0; i < nsystems; i++) {
free(systems[i].name);
for(j = 0; j < systems[i].nplanets; j++) {
free(systems[i].planets[j].name);
if(systems[i].planets[j].gfx_space)
gl_freeTexture(systems[i].planets[j].gfx_space);
}
free(systems[i].planets);
}
free(systems);
if(stars) free(stars);
} }

View File

@ -1,8 +1,11 @@
#pragma once #pragma once
#include "def.h"
void space_init(void); // Load/Exit.
void space_init(const char* sysname);
int space_load(void);
void space_exit(void); void space_exit(void);
// Render.
void space_render(double dt); void space_render(double dt);
void planets_render(void);

View File

@ -19,7 +19,7 @@ int main(int argc, char** argv) {
uint32_t nfiles = (uint32_t)argc - 2; uint32_t nfiles = (uint32_t)argc - 2;
argv += 2; argv += 2;
pack_files(outfile, argv, nfiles); pack_files(outfile, (const char**)argv, nfiles);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);