[Fix] AI's landing, it's still not as good as I wish however.
[Add] A few more xml macros and cleaned up a little code to use them.
This commit is contained in:
parent
184d527abf
commit
9fae81c389
@ -7,8 +7,8 @@
|
|||||||
<mass>1</mass>
|
<mass>1</mass>
|
||||||
</general>
|
</general>
|
||||||
<specific type = "1">
|
<specific type = "1">
|
||||||
<sound>laser.wav</sound>
|
<sound>laser</sound>
|
||||||
<gfx>lasergreen.png</gfx>
|
<gfx>lasergreen</gfx>
|
||||||
<delay>500</delay>
|
<delay>500</delay>
|
||||||
<speed>550</speed>
|
<speed>550</speed>
|
||||||
<range>300</range>
|
<range>300</range>
|
||||||
|
2
src/ai.c
2
src/ai.c
@ -491,7 +491,7 @@ static int ai_minbrakedist(lua_State* L) {
|
|||||||
double time = VMOD(cur_pilot->solid->vel) /
|
double time = VMOD(cur_pilot->solid->vel) /
|
||||||
(cur_pilot->ship->thrust / cur_pilot->solid->mass);
|
(cur_pilot->ship->thrust / cur_pilot->solid->mass);
|
||||||
|
|
||||||
double dist = VMOD(cur_pilot->solid->vel) * (time + cur_pilot->ship->turn/180.) -
|
double dist = VMOD(cur_pilot->solid->vel) * (time + 180./cur_pilot->ship->turn) -
|
||||||
0.5 * (cur_pilot->ship->thrust / cur_pilot->solid->mass)*time*time;
|
0.5 * (cur_pilot->ship->thrust / cur_pilot->solid->mass)*time*time;
|
||||||
|
|
||||||
lua_pushnumber(L, dist); // return
|
lua_pushnumber(L, dist); // return
|
||||||
|
58
src/outfit.c
58
src/outfit.c
@ -81,27 +81,20 @@ static void outfit_parseSWeapon(Outfit* tmp, const xmlNodePtr parent) {
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
// Load all the things.
|
// Load all the things.
|
||||||
if(strcmp((char*)node->name, "speed")==0)
|
if(xml_isNode(node, "speed")) tmp->speed = xml_getFloat(node);
|
||||||
tmp->speed = (double)atoi((char*)node->children->content);
|
else if(xml_isNode(node, "delay")) tmp->delay = xml_getInt(node);
|
||||||
else if(strcmp((char*)node->name, "delay")==0)
|
else if(xml_isNode(node, "range")) tmp->range = xml_getFloat(node);
|
||||||
tmp->delay = atoi((char*)node->children->content);
|
else if(xml_isNode(node, "accuracy")) tmp->accuracy = xml_getFloat(node);
|
||||||
else if(strcmp((char*)node->name, "range")==0)
|
else if(xml_isNode(node, "gfx")) {
|
||||||
tmp->range = atof((char*) node->children->content);
|
snprintf(str, strlen(xml_get(node))+sizeof(OUTFIT_GFX)+4, OUTFIT_GFX"%s.png", xml_get(node));
|
||||||
else if(strcmp((char*)node->name, "accuracy")==0)
|
|
||||||
tmp->accuracy = atof((char*)node->children->content);
|
|
||||||
else if(strcmp((char*)node->name, "gfx")==0) {
|
|
||||||
snprintf(str, strlen((char*)node->children->content)+sizeof(OUTFIT_GFX),
|
|
||||||
OUTFIT_GFX"%s", (char*)node->children->content);
|
|
||||||
tmp->gfx_space = gl_newSprite(str, 6, 6);
|
tmp->gfx_space = gl_newSprite(str, 6, 6);
|
||||||
}
|
}
|
||||||
if(strcmp((char*)node->name, "damage")==0) {
|
else if(xml_isNode(node, "damage")) {
|
||||||
cur = node->children;
|
cur = node->children;
|
||||||
while((cur = cur->next)) {
|
do {
|
||||||
if(strcmp((char*)cur->name, "armor")==0)
|
if(xml_isNode(cur, "armor")) tmp->damage_armor = xml_getFloat(cur);
|
||||||
tmp->damage_armor = atof((char*)cur->children->content);
|
else if(xml_isNode(cur, "shield")) tmp->damage_shield = xml_getFloat(cur);
|
||||||
else if(strcmp((char*)cur->name, "shield")==0)
|
} while((cur = cur->next));
|
||||||
tmp->damage_shield = atof((char*)cur->children->content);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} while((node = node->next));
|
} while((node = node->next));
|
||||||
#define MELEMENT(o,s) if((o) == 0) WARN("Outfit '%s' missing '"s"' element", tmp->name)
|
#define MELEMENT(o,s) if((o) == 0) WARN("Outfit '%s' missing '"s"' element", tmp->name)
|
||||||
@ -119,31 +112,28 @@ static void outfit_parseSWeapon(Outfit* tmp, const xmlNodePtr parent) {
|
|||||||
static Outfit* outfit_parse(const xmlNodePtr parent) {
|
static Outfit* outfit_parse(const xmlNodePtr parent) {
|
||||||
Outfit* tmp = CALLOC_L(Outfit);
|
Outfit* tmp = CALLOC_L(Outfit);
|
||||||
xmlNodePtr cur, node;
|
xmlNodePtr cur, node;
|
||||||
xmlChar* prop;
|
char* prop;
|
||||||
|
|
||||||
tmp->name = (char*)xmlGetProp(parent, (xmlChar*)"name"); // Already mallocs.
|
tmp->name = xml_nodeProp(parent, "name"); // Already malloced.
|
||||||
if(tmp->name == NULL) WARN("Outfit in "OUTFIT_DATA" has invalid or no name");
|
if(tmp->name == NULL) WARN("Outfit in "OUTFIT_DATA" has invalid or no name");
|
||||||
|
|
||||||
node = parent->xmlChildrenNode;
|
node = parent->xmlChildrenNode;
|
||||||
do {
|
do {
|
||||||
// Load all the things.
|
// Load all the things.
|
||||||
if(strcmp((char*)node->name, "general")==0) {
|
if(xml_isNode(node, "general")) {
|
||||||
cur = node->children;
|
cur = node->children;
|
||||||
while((cur = cur->next)) {
|
do {
|
||||||
if(strcmp((char*)cur->name, "max")==0)
|
if(xml_isNode(cur, "max")) tmp->max = xml_getInt(cur);
|
||||||
tmp->max = atoi((char*)cur->children->content);
|
else if(xml_isNode(cur, "tech")) tmp->tech = xml_getInt(cur);
|
||||||
else if(strcmp((char*)cur->name, "tech")==0)
|
else if(xml_isNode(cur, "mass")) tmp->mass = xml_getInt(cur);
|
||||||
tmp->tech = atoi((char*)cur->children->content);
|
} while((cur = cur->next));
|
||||||
else if(strcmp((char*)cur->name, "mass")== 0)
|
|
||||||
tmp->mass = atoi((char*)cur->children->content);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if(strcmp((char*)node->name, "specific")==0) {
|
else if(xml_isNode(node, "specific")) {
|
||||||
// Has to be processed seperately.
|
// Has to be processed seperately.
|
||||||
prop = xmlGetProp(node,(xmlChar*)"type");
|
prop = xml_nodeProp(node, "type");
|
||||||
if(prop == NULL)
|
if(prop == NULL)
|
||||||
ERR("Outfit '%s' element 'specific' missing property 'type'", tmp->name);
|
ERR("Outfit '%s' element 'specific' missing property 'type'", tmp->name);
|
||||||
tmp->type = atoi((char*)prop);
|
tmp->type = atoi(prop);
|
||||||
free(prop);
|
free(prop);
|
||||||
switch(tmp->type) {
|
switch(tmp->type) {
|
||||||
case OUTFIT_TYPE_NULL:
|
case OUTFIT_TYPE_NULL:
|
||||||
@ -180,7 +170,7 @@ int outfit_load(void) {
|
|||||||
xmlDocPtr doc = xmlParseMemory(buf, bufsize);
|
xmlDocPtr doc = xmlParseMemory(buf, bufsize);
|
||||||
|
|
||||||
node = doc->xmlChildrenNode;
|
node = doc->xmlChildrenNode;
|
||||||
if(strcmp((char*)node->name, XML_OUTFIT_ID)) {
|
if(!xml_isNode(node, XML_OUTFIT_ID)) {
|
||||||
ERR("Malformed "OUTFIT_DATA" file: missing root element '"XML_OUTFIT_ID"'");
|
ERR("Malformed "OUTFIT_DATA" file: missing root element '"XML_OUTFIT_ID"'");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -192,7 +182,7 @@ int outfit_load(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_OUTFIT_TAG)==0) {
|
if(xml_isNode(node, XML_OUTFIT_TAG)) {
|
||||||
tmp = outfit_parse(node);
|
tmp = outfit_parse(node);
|
||||||
outfit_stack = realloc(outfit_stack, sizeof(Outfit)*(++outfits));
|
outfit_stack = realloc(outfit_stack, sizeof(Outfit)*(++outfits));
|
||||||
memcpy(outfit_stack+outfits-1, tmp, sizeof(Outfit));
|
memcpy(outfit_stack+outfits-1, tmp, sizeof(Outfit));
|
||||||
|
26
src/outfit.h
26
src/outfit.h
@ -6,19 +6,19 @@
|
|||||||
|
|
||||||
// Outfit types.
|
// Outfit types.
|
||||||
typedef enum {
|
typedef enum {
|
||||||
OUTFIT_TYPE_NULL = 0,
|
OUTFIT_TYPE_NULL = 0,
|
||||||
OUTFIT_TYPE_BOLT,
|
OUTFIT_TYPE_BOLT = 1,
|
||||||
OUTFIT_TYPE_BEAM,
|
OUTFIT_TYPE_BEAM = 2,
|
||||||
OUTFIT_TYPE_MISSILE_DUMB,
|
OUTFIT_TYPE_MISSILE_DUMB = 3,
|
||||||
OUTFIT_TYPE_MISSILE_DUMB_AMMO,
|
OUTFIT_TYPE_MISSILE_DUMB_AMMO = 4,
|
||||||
OUTFIT_TYPE_MISSILE_SEEK,
|
OUTFIT_TYPE_MISSILE_SEEK = 5,
|
||||||
OUTFIT_TYPE_MISSILE_SEEK_AMMO,
|
OUTFIT_TYPE_MISSILE_SEEK_AMMO = 6,
|
||||||
OUTFIT_TYPE_MISSILE_SEEK_SMART,
|
OUTFIT_TYPE_MISSILE_SEEK_SMART = 7,
|
||||||
OUTFIT_TYPE_MISSILE_SEEK_SMART_AMMO,
|
OUTFIT_TYPE_MISSILE_SEEK_SMART_AMMO = 8,
|
||||||
OUTFIT_TYPE_MISSILE_SWARM,
|
OUTFIT_TYPE_MISSILE_SWARM = 9,
|
||||||
OUTFIT_TYPE_MISSILE_SWARM_AMMO,
|
OUTFIT_TYPE_MISSILE_SWARM_AMMO = 10,
|
||||||
OUTFIT_TYPE_MISSILE_SWARM_SMART,
|
OUTFIT_TYPE_MISSILE_SWARM_SMART = 11,
|
||||||
OUTFIT_TYPE_MISSILE_SWARM_SMART_AMMO
|
OUTFIT_TYPE_MISSILE_SWARM_SMART_AMMO = 12
|
||||||
} OutfitType;
|
} OutfitType;
|
||||||
|
|
||||||
// An outfit depends a lot on the type.
|
// An outfit depends a lot on the type.
|
||||||
|
40
src/pilot.c
40
src/pilot.c
@ -40,15 +40,19 @@ static Fleet* fleet_parse(const xmlNodePtr parent);
|
|||||||
|
|
||||||
// Get the next pilot based on id.
|
// Get the next pilot based on id.
|
||||||
unsigned int pilot_getNext(const unsigned int id) {
|
unsigned int pilot_getNext(const unsigned int id) {
|
||||||
// Regular search.
|
// Dichotmical search.
|
||||||
int i;
|
int l, m, h;
|
||||||
for(i = 0; i < pilots; i++)
|
l = 0;
|
||||||
if(pilot_stack[i]->id == id)
|
h = pilots-1;
|
||||||
break;
|
while(l <= h) {
|
||||||
|
m = (l+h)/2;
|
||||||
|
if(pilot_stack[m]->id > id) h = m-1;
|
||||||
|
else if(pilot_stack[m]->id < id) l = m+1;
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
|
||||||
if(i == pilots-1) return PLAYER_ID;
|
if(m == (pilots-1)) return PLAYER_ID;
|
||||||
|
else return pilot_stack[m+1]->id;
|
||||||
return pilot_stack[i+1]->id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the nearest enemy to the pilot -- Tamir's (insightful) request.
|
// Get the nearest enemy to the pilot -- Tamir's (insightful) request.
|
||||||
@ -87,6 +91,7 @@ unsigned pilot_getHostile(void) {
|
|||||||
// Pull a pilot out of the pilot_stack based on id.
|
// Pull a pilot out of the pilot_stack based on id.
|
||||||
Pilot* pilot_get(const unsigned int id) {
|
Pilot* pilot_get(const unsigned int id) {
|
||||||
// Regular search.
|
// Regular search.
|
||||||
|
#if 0
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < pilots; i++)
|
for(i = 0; i < pilots; i++)
|
||||||
if(pilot_stack[i]->id == id)
|
if(pilot_stack[i]->id == id)
|
||||||
@ -94,13 +99,20 @@ Pilot* pilot_get(const unsigned int id) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if(id == 0) return player;
|
if(id == 0) return player;
|
||||||
#if 0
|
|
||||||
// Dichotomical search.
|
|
||||||
int i, n;
|
|
||||||
for(i = 0, n = pilots/2; n > 0; n /= 2)
|
|
||||||
i += (pilot_stack[i+n]->id > id) ? 0 : n;
|
|
||||||
return (pilot_stack[i]->id == id) ? pilot_stack[i] : NULL;
|
|
||||||
#endif
|
#endif
|
||||||
|
if(id == PLAYER_ID) return player; // Special case player.
|
||||||
|
|
||||||
|
// Dichotomical search.
|
||||||
|
int l, m, h;
|
||||||
|
l = 0;
|
||||||
|
h = pilots-1;
|
||||||
|
while(l <= h) {
|
||||||
|
m = (l+h)/2;
|
||||||
|
if(pilot_stack[m]->id > id) h = m-1;
|
||||||
|
else if(pilot_stack[m]->id < id) l = m+1;
|
||||||
|
else return pilot_stack[m];
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mkay, this is how we shoot. Listen up.
|
// Mkay, this is how we shoot. Listen up.
|
||||||
|
@ -152,6 +152,9 @@ int space_hyperspace(Pilot* p) {
|
|||||||
if(d < MIN_HYPERSPACE_DIST)
|
if(d < MIN_HYPERSPACE_DIST)
|
||||||
return (int)(MIN_HYPERSPACE_DIST - d);
|
return (int)(MIN_HYPERSPACE_DIST - d);
|
||||||
}
|
}
|
||||||
|
// Too fast.
|
||||||
|
if(VMOD(p->solid->vel) > MAX_HYPERSPACE_VEL) return -1;
|
||||||
|
|
||||||
// TODO: All hyperspace worky work.
|
// TODO: All hyperspace worky work.
|
||||||
if(p == player) {
|
if(p == player) {
|
||||||
// Player crap.
|
// Player crap.
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "pilot.h"
|
#include "pilot.h"
|
||||||
|
|
||||||
#define MIN_HYPERSPACE_DIST 1500
|
#define MIN_HYPERSPACE_DIST 1500
|
||||||
|
#define MAX_HYPERSPACE_VEL 3
|
||||||
|
|
||||||
// Planet types. I didn't take them from Star Trek, I promise.
|
// Planet types. I didn't take them from Star Trek, I promise.
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
@ -6,8 +6,13 @@
|
|||||||
#define XML_NODE_TEXT 3
|
#define XML_NODE_TEXT 3
|
||||||
|
|
||||||
// Check if node n is of name s.
|
// Check if node n is of name s.
|
||||||
#define xml_isNode(n,s) ((n)->type == XML_NODE_START) && \
|
#define xml_isNode(n,s) (((n)->type == XML_NODE_START) && \
|
||||||
(strcmp((char*)(n)->name, s)==0)
|
(strcmp((char*)(n)->name, s)==0))
|
||||||
|
|
||||||
|
// Get the property s of node n. This mallocs.
|
||||||
#define xml_nodeProp(n,s) (char*)xmlGetProp(n, (xmlChar*)s)
|
#define xml_nodeProp(n,s) (char*)xmlGetProp(n, (xmlChar*)s)
|
||||||
|
|
||||||
|
#define xml_get(n) (char*)(n)->children->content
|
||||||
|
#define xml_getInt(n) atoi((char*)(n)->children->content)
|
||||||
|
#define xml_getFloat(n) atof((char*)(n)->children->content)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user