From 35969bab385620ab1bdf2e1e2cb661e1a7ad44d4 Mon Sep 17 00:00:00 2001 From: Allanis Date: Sat, 10 Aug 2013 20:23:06 +0100 Subject: [PATCH] [Add] Can now override pilot AI in a fleet. [Change] Made fleet_parse() more up to date with xml.h --- src/misn_lua.c | 13 ++++++++----- src/pilot.c | 34 ++++++++++++++++++++++------------ src/pilot.h | 7 ++++--- src/space.c | 22 ++++++++++++---------- 4 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/misn_lua.c b/src/misn_lua.c index aa3527c..ab3f58d 100644 --- a/src/misn_lua.c +++ b/src/misn_lua.c @@ -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, diff --git a/src/pilot.c b/src/pilot.c index 558a4e8..f66de93 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -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)); diff --git a/src/pilot.h b/src/pilot.h index 5afc6a6..2d0ead1 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -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_ { diff --git a/src/space.c b/src/space.c index e5020df..95d8a32 100644 --- a/src/space.c +++ b/src/space.c @@ -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); } }