[Add] Can now override pilot AI in a fleet.

[Change] Made fleet_parse() more up to date with xml.h
This commit is contained in:
Allanis 2013-08-10 20:23:06 +01:00
parent b9c1f021b2
commit 35969bab38
4 changed files with 46 additions and 30 deletions

View File

@ -781,6 +781,7 @@ static int pilot_addFleet(lua_State* L) {
unsigned int p;
double a;
Vec2 vv, vp, vn;
FleetPilot* plt;
/* Parse first argument - Fleet name. */
if(lua_isstring(L, 1)) fltname = (char*) lua_tostring(L, 1);
@ -805,19 +806,21 @@ static int pilot_addFleet(lua_State* L) {
/* Now we start adding pilots and toss ids into the table we return. */
j = 0;
for(i = 0; i < flt->npilots; i++) {
if(RNG(0, 100) <= flt->pilots[i].chance) {
plt = &flt->pilots[i];
if(RNG(0, 100) <= plt->chance) {
/* Fleet displacement. */
vect_cadd(&vp, RNG(75, 150) & (RNG(0, 1) ? 1: -1),
RNG(75, 150) * (RNG(0, 1) ? 1 : -1));
a = vect_angle(&vp, &vn);
vectnull(&vv);
p = pilot_create(flt->pilots[i].ship,
flt->pilots[i].name,
p = pilot_create(plt->ship,
plt->name,
flt->faction,
(fltai != NULL) ? /* AI override */
(fltai != NULL) ? /* Lua AI override */
ai_getProfile(fltai) :
flt->ai,
(plt->ai != NULL) ? /* Pilot AI override. */
plt->ai : flt->ai,
a,
&vp,
&vv,

View File

@ -1178,31 +1178,41 @@ static Fleet* fleet_parse(const xmlNodePtr parent) {
do {
/* Load all the data. */
if(strcmp((char*)node->name, "faction")==0)
tmp->faction = faction_get((char*)node->children->content);
else if(strcmp((char*)node->name, "ai")==0)
tmp->ai = ai_getProfile((char*)node->children->content);
else if(strcmp((char*)node->name, "pilots")==0) {
if(xml_isNode(node, "faction"))
tmp->faction = faction_get(xml_get(node));
else if(xml_isNode(node, "ai"))
tmp->ai = ai_getProfile(xml_get(node));
else if(xml_isNode(node, "pilots")) {
cur = node->children;
do {
if(strcmp((char*)cur->name, "pilot")==0) {
if(xml_isNode(cur, "pilot")) {
tmp->npilots++; /* Pilot count. */
pilot = MALLOC_L(FleetPilot);
/* Name is not obligatory. Will only override ship name. */
c = (char*)xmlGetProp(cur, (xmlChar*)"name"); /* Mallocs. */
pilot->name = c; /* No need to free here however. */
/* Check for name override. */
xmlr_attr(cur, "name", c);
pilot->name = c; /* No need to free since it will have to later. */
pilot->ship = ship_get((char*)cur->children->content);
/* Check for ai override. */
xmlr_attr(cur, "ai", c);
if(c != NULL) {
pilot->ai = ai_getProfile(c);
free(c);
}
else pilot->ai = NULL;
/* Load pilots ship. */
pilot->ship = ship_get(xml_get(cur));
if(pilot->ship == NULL)
WARN("Pilot %s in Fleet %s has null ship", pilot->name, tmp->name);
c = (char*)xmlGetProp(cur, (xmlChar*)"chance"); /* Mallocs. */
/* Load chance. */
xmlr_attr(cur, "chance", c);
pilot->chance = atoi(c);
if(pilot->chance == 0)
WARN("Pilot %s in Fleet %s has 0%% chance of appearing",
pilot->name, tmp->name);
if(c) free(c); /* Free the external malloc. */
if(c != NULL) free(c); /* Free the external malloc. */
tmp->pilots = realloc(tmp->pilots, sizeof(FleetPilot)*tmp->npilots);
memcpy(tmp->pilots+(tmp->npilots-1), pilot, sizeof(FleetPilot));

View File

@ -124,9 +124,10 @@ typedef struct Pilot_ {
/* Fleets. */
typedef struct FleetPilot_ {
Ship* ship; /* Ship that the pilot is flying. */
char* name; /* For special 'unique' names. */
int chance; /* Chance of this pilot appearing in the fleet. */
Ship* ship; /* Ship that the pilot is flying. */
char* name; /* For special 'unique' names. */
int chance; /* Chance of this pilot appearing in the fleet. */
AI_Profile* ai; /* AI different of fleets global ai. */
} FleetPilot;
typedef struct Fleet_ {

View File

@ -361,6 +361,7 @@ void space_update(const double dt) {
/* Crate a fleet. */
static void space_addFleet(Fleet* fleet) {
FleetPilot* plt;
int i;
double a;
Vec2 vv, vp, vn;
@ -371,21 +372,22 @@ static void space_addFleet(Fleet* fleet) {
vectnull(&vn);
for(i = 0; i < fleet->npilots; i++)
if(RNG(0, 100) <= fleet->pilots[i].chance) {
plt = &fleet->pilots[i];
if(RNG(0, 100) <= plt->chance) {
vect_cadd(&vp, RNG(75, 150) * (RNG(0,1) ? 1 : -1),
RNG(75, 150) * (RNG(0,1) ? 1 : -1));
a = vect_angle(&vp, &vn);
vect_pset(&vv, fleet->pilots[i].ship->speed * 2., a);
vect_pset(&vv, plt->ship->speed * 2., a);
pilot_create(fleet->pilots[i].ship,
fleet->pilots[i].name,
fleet->faction,
fleet->ai,
a,
&vp,
&vv,
0);
pilot_create(plt->ship,
plt->name,
fleet->faction,
(plt->ai != NULL) ? fleet->ai : fleet->ai, /* Pilot AI override. */
a,
&vp,
&vv,
0);
}
}