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

View File

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

View File

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

View File

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