32 using namespace Orders;
65 vsUMap< string, AIEvents::ElemAttrMap* >
logic;
73 while ( NULL != (un = *iter) ) {
85 vsUMap< string, string >ret;
90 vsUMap< std::string, int >::iterator browser = table.
rows.begin();
91 for (; browser != table.rows.end(); ++browser) {
92 string rowname = (*browser).first;
93 CSVRow row( &table, rowname );
94 for (
unsigned int i = 1;
i < table.key.size(); ++
i) {
95 string hasher = rowname;
97 hasher = rowname+
"%"+table.key[
i];
98 string rawrow = row[
i];
99 if (rawrow.length() > 0)
100 ret[hasher] = rawrow;
110 if (inp.length() == 0)
113 string::size_type len = inp.length();
114 for (
unsigned int i = 0;
i < len; ++
i)
120 for (j = 0; j < len; ++j) {
128 inp = inp.substr( j );
129 if ( ( len = inp.find(
" " ) ) != string::npos )
130 inp = inp.substr( 0, len );
137 vsUMap< string, AIEvents::ElemAttrMap* > &mymap,
138 int personalityseed )
140 string append =
"agg";
141 static vsUMap< string, string >myappend =
getAITypes();
142 vsUMap< string, string >::iterator iter;
144 if ( ( iter = myappend.find( factionname+
"%"+unittype ) ) != myappend.end() )
146 else if ( ( iter = myappend.find( factionname ) ) != myappend.end() )
148 if (append.length() == 0) append =
"agg";
149 string hashname = name+
"."+append;
150 vsUMap< string, AIEvents::ElemAttrMap* >::iterator
i = mymap.find( hashname );
151 if ( i == mymap.end() ) {
153 string filename( name+
"."+append+
".xml" );
155 mymap.insert( pair< string, AIEvents::ElemAttrMap* > ( hashname, attr ) );
165 int personalityseed )
173 string nam =
"eject";
195 sprintf( str,
"#%2X%2X00", (
int) ( (1-rel)*256 ), (
int) (rel*256) );
201 unsigned int dummy = 0;
203 string myname(
"[Static]" );
207 if (fg && fg->
name !=
"base" && fg->
name !=
"Base")
209 else if (myname.length() == 0)
211 if (player_un != NULL) {
212 if (player_un == un) {
213 myname = std::string(
"#0033FF" )+myname+
"#000000";
224 void LeadMe(
Unit *un,
string directive,
string speech,
bool changetarget )
241 if ( (directive ==
"") )
256 last_jump_distance = FLT_MAX;
260 last_time_insys =
true;
276 string filename(
string(
"default.agg.xml" ) );
277 string interruptname(
string(
"default.int.xml" ) );
278 if (which != string::npos) {
323 if (item.
script.length() != 0) {
337 switch ( abs( item.
type ) )
347 float pdmag = PosDifference.Magnitude();
384 value = delta_v/delta_t;
399 value = delta_v/delta_t;
414 value = delta_v/delta_t;
429 value = delta_v/delta_t;
456 value = delta_v/delta_t;
475 PosDelta = PosDelta/PosDelta.Magnitude();
477 value = PosDelta.Dot( R );
491 PosDelta = PosDelta/PosDelta.Magnitude();
493 value = PosDelta.Dot( S );
520 value = ( (
float) rand() )/RAND_MAX;
525 return item.
Eval( value );
532 std::vector< std::list< AIEvents::AIEvresult > >::iterator
i = logi.
result.begin();
533 for (; i != logi.
result.end(); i++) {
534 std::list< AIEvents::AIEvresult >::iterator
j;
536 for (j = i->begin(); j != i->end(); j++)
541 if ( trueit && j == i->end() ) {
543 if ( j != i->begin() )
545 if ( j != i->end() ) {
546 float priority = (*j).priority;
547 if (priority > this->currentpriority || !inter) {
555 if ( j != i->end() ) {
556 while ( j != i->end() ) {
558 this->currentpriority = priority;
578 bool targetted =
false;
579 float mindist = FLT_MAX;
585 bool thistargetted = (un->
Target() == leader);
586 if ( !th || (thistargetted && !targetted) || ( ( thistargetted || (!targetted) ) && d < mindist ) ) {
588 targetted = thistargetted;
607 obedient = !obedient;
613 bool attacking = fg->
directive.length() > 0;
615 attacking = tolower( fg->
directive[0] ) ==
'a';
629 void *leaderowner = leader;
631 leaderowner = leader->
owner ? leader->
owner : leader;
632 if (fg->
directive.find(
"k" ) != string::npos || fg->
directive.find(
"K" ) != string::npos) {
646 if (
parent == leaderowner) {
654 true ),
true,
false, true );
678 }
else if (fg->
directive.find(
"a" ) != string::npos || fg->
directive.find(
"A" ) != string::npos) {
680 targ = targ != NULL ? targ->
Target() : NULL;
698 }
else if (fg->
directive.find(
"f" ) != string::npos || fg->
directive.find(
"F" ) != string::npos) {
699 if (leader != NULL) {
703 last_time_insys =
true;
721 int i = nam.length()-1;
722 for (; i >= 0; --i) {
724 if (digit >=
'0' && digit <=
'9') {
726 tempnum += digit-
'0';
733 float left = fgnum%2 ? 1 : -1;
735 double dist = esc_percent*(1+abs( fgnum-1 )/2)*left*(
parent->
rSize()+leader->
rSize() );
744 last_time_insys =
false;
751 else if (fg->
directive.find(
"s" ) != string::npos || fg->
directive.find(
"S" ) != string::npos) {
757 else if (fg->
directive.find(
"t" ) != string::npos || fg->
directive.find(
"T" ) != string::npos) {
763 ord =
new Orders::DockingOps(targ,
new Orders::MatchVelocity(
Vector(0,0,0),
Vector(0,0,0),
true,
false),
true,
true);
775 else if (fg->
directive.find(
"l" ) != string::npos || fg->
directive.find(
"L" ) != string::npos) {
776 if (leader != NULL) {
780 last_time_insys =
true;
799 int i = nam.length()-1;
800 for (; i >= 0; --i) {
802 if (digit >=
'0' && digit <=
'9') {
804 tempnum += digit-
'0';
827 int alternate = fgnum%2 ? 1 : -1;
875 float formdist = esc_percent*(1+fgnum*2)*alternate*(dist);
877 if (parentowner == leader) {
887 else if ( parentowner && leaderowner && (parentowner == leaderowner) ) {
888 Unit *leaderownerun =
895 0.5*qdist ),
true, 4 );
904 else if (
parent == leaderowner) {
910 true ),
true,
false, true );
933 last_time_insys =
false;
938 }
else if (fg->
directive.find(
"e" ) != string::npos || fg->
directive.find(
"E" ) != string::npos) {
940 if (LeaderPosition.Magnitude() > 0 || leader != NULL) {
943 if (LeaderPosition.Magnitude() == 0)
944 LeaderPosition = leader->
Position();
946 last_time_insys =
true;
958 int i = nam.length()-1;
959 for (; i >= 0; --i) {
961 if (digit >=
'0' && digit <=
'9') {
963 tempnum += digit-
'0';
1040 0.5*qdist ),
true, 4 );
1061 last_time_insys =
false;
1066 }
else if (fg->
directive.find(
"h" ) != string::npos || fg->
directive.find(
"H" ) != string::npos) {
1070 if ( ( th = leader->
Threat() ) ) {
1099 true ),
true,
false, true );
1118 }
else if (fg->
directive.find(
"p" ) != string::npos || fg->
directive.find(
"P" ) != string::npos) {
1119 bool callme =
false;
1124 if ( targ && ( th = targ->
Threat() ) ) {
1148 if (
parent == leaderowner) {
1155 true ),
true,
false, true );
1187 return ( *s.begin() ) != toupper( *s.begin() );
1190 extern void LeadMe(
Unit *un,
string directive,
string speech,
bool changetarget );
1196 "TargetCommandierTime",
1204 if (
float( rand() )/RAND_MAX <
SIMULATION_ATOM/time_to_recommand_wing) {
1212 LeadMe(
parent,
"b",
"I'm taking over this wing. Break and attack",
false );
1222 size_t total_size = navs[0].size()+navs[1].size()+navs[2].size();
1223 if (total_size == 0)
return NULL;
1224 randnum %= total_size;
1225 if ( randnum >= navs[0].
size() ) {
1226 randnum -= navs[0].size();
1227 if ( randnum >= navs[1].
size() ) {
1228 randnum -= navs[1].size();
1229 return navs[2][randnum].GetUnit();
1231 return navs[1][randnum].GetUnit();
1233 return navs[0][randnum].GetUnit();
1243 *lurk_on_arrival = 0;
1244 if (script.length() > 0) {
1250 if (ret != NULL && ret != parent)
1260 static int num_ships_per_roid =
1264 bool hostile = sysrel < 0;
1266 float timehash = select_time;
1267 if (hostile && !anarchy)
1268 timehash = hostile_select_time;
1269 unsigned int firstRand, thirdRand;
1271 const unsigned int maxrand = 5;
1272 unsigned int additionalrand[maxrand];
1277 for (
unsigned int i = 0;
i < maxrand; ++
i)
1278 additionalrand[
i] = thirdRand+
i;
1282 std::string::const_iterator
start = key.begin();
1283 for (; start != key.end(); start++)
1284 k += (k*128)+*
start;
1289 for (
unsigned int i = 0;
i < maxrand; ++
i)
1292 bool asteroidhide =
false;
1301 std::string::size_type whereconvoy = fgname.find(
arrowString );
1302 bool convoy = (whereconvoy != std::string::npos);
1303 size_t total_size = stats->
navs[0].size()+stats->
navs[whichlist].size();
1305 if (hostile && bad_units_lurk) {
1306 if (anarchy && !siege) {
1308 total_size = stats->
navs[0].size()+stats->
navs[whichlist].size();
1311 total_size = stats->
navs[whichlist].size();
1313 }
else if (civilian) {
1314 if (anarchy || siege) {
1316 total_size = stats->
navs[0].size();
1317 }
else if (insys || convoy) {
1319 total_size = stats->
navs[1].size();
1322 if (hostile && ( (anarchy ==
false && asteroidhide ==
false) || total_size == 0 ) && civilian ==
false && bad_units_lurk) {
1329 int retrycount = maxrand;
1332 if (retrycount != 0) {
1334 *lurk_on_arrival = lurk_time;
1340 std::string srcdst[2] = {fgname.substr( 0, whereconvoy ), fgname.substr( whereconvoy+2 )};
1342 srcdst[0] = srcdst[1];
1344 srcdst[1] = srcdst[0];
1345 if (thirdRand < 2) {
1346 vsUMap< std::string, UnitContainer >::iterator
i = stats->
jumpPoints.find( srcdst[thirdRand] );
1348 Unit *un = i->second.GetUnit();
1352 total_size = stats->
navs[whichlist].size()+stats->
navs[0].size();
1357 if (total_size > 0) {
1358 firstRand %= total_size;
1359 if ( firstRand >= stats->
navs[whichlist].size() ) {
1360 firstRand -= stats->
navs[whichlist].size();
1363 return stats->
navs[whichlist][firstRand].GetUnit();
1371 if (suggestion)
return suggestion;
1372 Unit *candidate = NULL;
1373 float dist = FLT_MAX;
1386 if (candidate == NULL || newdist <= dist) {
1397 static float how_far_to_stop_moving =
1401 if (dist < SIMULATION_ATOM*parent->Velocity.Magnitude()*parent->
predicted_priority*how_far_to_stop_moving)
1404 return ( nav-parent->
Position() ).MagnitudeSquared() < 4*parent->
rSize()*parent->
rSize();
1415 bool terminating =
true,
1416 float creationtime = 0,
1418 Unit *destUnit = NULL ) :
MoveTo( target, aft, leniency, terminating )
1420 this->creationtime = creationtime;
1421 this->destUnit = destUnit;
1429 Unit *un = destUnit.GetUnit();
1436 && ( un =
ChooseNearNavPoint( parent, destUnit.GetUnit(), targetlocation, 0 ) ) != NULL) {
1440 if (playa == NULL || playa->
Target() != parent || 1)
1441 WarpToP( parent, targetlocation, 0,
true );
1449 return Vector( (rand()/(
float) RAND_MAX)*2-1, (rand()/(
float) RAND_MAX)*2-1, (rand()/(
float) RAND_MAX)*2-1 );
1456 bool boonies =
false,
1457 Unit *destUnit = NULL )
1460 Order *mt =
new FlyTo( nav, can_afterburn,
true, creationtime, boonies ? 16 : 6, destUnit );
1464 mt->SetParent( parent );
1474 if (
nav.i == 0 &&
nav.j == 0 &&
nav.k == 0) {
1475 Unit *otherdest = NULL;
1487 Vector unitdir = dir.Normalize();
1493 dir *= (planetpct+1.0f);
1497 dir += (unitdir*safetyspacing);
1502 *randspacingfactor) )
1515 printf(
"%s:%s %s going to %s:%s",
parent->
name.get().c_str(), pfullname.c_str(), fgname.c_str(),
1516 dest->
name.get().c_str(), dfullname.c_str() );
1519 printf(
" between %s:%s\n", otherdest->
name.get().c_str(), ofullname.c_str() );
1520 }
else {printf(
"\n" ); }
1522 GoTo(
this,
parent,
nav, creationtime, otherdest != NULL, otherdest == NULL ? dest : NULL );
1567 if (jump_time_check == 0) {
1569 if (last_jump_distance < dist || last_jump_time > jump_time_limit) {
1577 last_jump_distance = dist;
1589 jump_time_check %= 5;
1594 if (rand() == 0) printf(
"ahoy, a pirates!" );
1597 static bool resistance_to_side_movement =
1599 if (resistance_to_side_movement) {
1606 if (forceforwardness > 0)
1608 if (forwardness > 0)
1610 static float resistance_percent =
1612 static float force_resistance_percent =
1622 bool isjumpable = target ? ( !target->
GetDestinations().empty() ) :
false;
1628 static bool AIjumpCheat =
1656 currentpriority = -FLT_MAX;
1663 last_jump_distance = FLT_MAX;
1684 last_jump_distance = FLT_MAX;