Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pilot.cpp
Go to the documentation of this file.
1 #include "faction_generic.h"
2 #include "unit_generic.h"
3 #include "pilot.h"
4 #include "ai/order.h"
5 #include "universe_util.h"
6 #include "cmd/unit_util.h"
7 
8 #include <vector>
9 
11 {
12  static float reaction = XMLSupport::parse_float( vs_config->getVariable( "AI", "Firing", "ReactionTime", ".2" ) );
13  static float ran = XMLSupport::parse_float( vs_config->getVariable( "AI", "DefaultRank", ".01" ) );
14  this->rank = ran;
15 
16  this->reaction_time = reaction;
17  this->faction = faction;
18  comm_face = NULL;
19  gender = 0;
20 }
21 
22 void Pilot::SetComm( Unit *parent )
23 {
24  this->faction = parent->faction;
25  //GET BETTER REACTION TIME AND RANK HERE
26  comm_face = FactionUtil::GetRandCommAnimation( faction, parent, gender );
27 }
28 
29 float Pilot::adjustSpecificRelationship( Unit *parent, void *aggressor, float factor, int faction )
30 {
31  relationmap::iterator i = effective_relationship.insert( std::pair< const void*, float > ( aggressor, 0 ) ).first;
32  if ( faction != FactionUtil::GetNeutralFaction() ) {
33  float rel = UnitUtil::getRelationToFaction( parent, faction ); /* What the bloody hell? */
34  bool abovezero = (*i).second+rel < 0;
35  if (!abovezero) {
36  static float slowrel = XMLSupport::parse_float( vs_config->getVariable( "AI", "SlowDiplomacyForEnemies", ".25" ) );
37  factor *= slowrel;
38  }
39  (*i).second += factor;
40  if (rel+factor < 0 && parent->Target() == NULL && parent->aistate)
41  parent->aistate->ChooseTarget();
42  } else {
43  static float lessrel = XMLSupport::parse_float( vs_config->getVariable( "AI", "UnknownRelationEnemy", "-.05" ) );
44  bool abovezero = (*i).second < lessrel;
45  if (!abovezero) {
46  static float slowrel = XMLSupport::parse_float( vs_config->getVariable( "AI", "SlowDiplomacyForEnemies", ".25" ) );
47  factor *= slowrel;
48  }
49  (*i).second += factor;
50  if ( (*i).second < lessrel && parent->Target() == NULL && parent->aistate )
51  parent->aistate->ChooseTarget();
52  }
53  return (*i).second;
54 }
55 void Pilot::DoHit( Unit *parent, void *aggressor, int faction )
56 {
57  static float hitcost = XMLSupport::parse_float( vs_config->getVariable( "AI", "UnknownRelationHitCost", ".01" ) );
58  if (hitcost) {
59  adjustSpecificRelationship( parent, aggressor, hitcost, faction );
60  int whichCp = _Universe->whichPlayerStarship( parent );
61  if (whichCp != -1 && faction != parent->faction) {
62  UniverseUtil::adjustRelationModifierInt( whichCp, faction, hitcost*getRank() );
63  } else {
64  /* Instead use the Aggressor's cockpit? */
65  whichCp = _Universe->whichPlayerStarship( (const Unit*) aggressor );
66  if (whichCp != -1) {
67  Flightgroup *fg = parent->getFlightgroup();
68  if (parent->faction != faction)
69  UniverseUtil::adjustRelationModifierInt( whichCp, parent->faction, hitcost*getRank() );
70  if (fg)
71  UniverseUtil::adjustFGRelationModifier( whichCp, fg->name, hitcost*getRank() );
72  }
73  }
74  }
75 }
76 float Pilot::getAnger( const Unit *parent, const Unit *target ) const
77 {
78  relationmap::const_iterator iter = effective_relationship.find( target );
79  float rel = 0;
80  if ( iter != effective_relationship.end() )
81  rel = iter->second;
82  if ( _Universe->isPlayerStarship( target ) ) {
83  if (FactionUtil::GetFactionName( faction ).find( "pirates" ) != std::string::npos) {
84  static unsigned int cachedCargoNum = 0;
85  static bool good = true;
86  if ( cachedCargoNum != target->numCargo() ) {
87  cachedCargoNum = target->numCargo();
88  good = true;
89  for (unsigned int i = 0; i < cachedCargoNum; ++i) {
90  Cargo *c = &target->pImage->cargo[i];
91  if (c->quantity != 0 && c->GetCategory().find( "upgrades" ) == string::npos) {
92  good = false;
93  break;
94  }
95  }
96  }
97  if (good) {
98  static float goodness_for_nocargo =
99  XMLSupport::parse_float( vs_config->getVariable( "AI", "pirate_bonus_for_empty_hold", ".75" ) );
100  rel += goodness_for_nocargo;
101  }
102  }
103  }
104  {
105  int fac = faction;
106  MapStringFloat::iterator mapiter = factions[fac]->ship_relation_modifier.find( target->name );
107  if ( mapiter != factions[fac]->ship_relation_modifier.end() )
108  rel += (*mapiter).second;
109  }
110  {
111  int parent_cp = _Universe->whichPlayerStarship( parent );
112  int target_cp = _Universe->whichPlayerStarship( target );
113  if (parent_cp != -1) {
114  Flightgroup *fg = target->getFlightgroup();
115  if (fg)
116  rel += UniverseUtil::getFGRelationModifier( parent_cp, fg->name );
117  }
118  if (target_cp != -1) {
119  //... do we count it both ways? else?
120  Flightgroup *fg = parent->getFlightgroup();
121  if (fg)
122  rel += UniverseUtil::getFGRelationModifier( target_cp, fg->name );
123  }
124  }
125 
126  return rel;
127 }
128 
129 float Pilot::GetEffectiveRelationship( const Unit *parent, const Unit *target ) const
130 {
131  return getAnger( parent, target )+UnitUtil::getFactionRelation( parent, target );
132 }
133 
134 extern float myroundclamp( float i );
135 
136 Animation* Pilot::getCommFace( Unit *parent, float mood, unsigned char &sex )
137 {
138  vector< Animation* > *ani = getCommFaces( sex );
139  if (ani == NULL) {
140  ani = FactionUtil::GetRandCommAnimation( parent->faction, parent, sex );
141  if (ani == NULL)
142  return NULL;
143  }
144  if (ani->size() == 0)
145  return NULL;
146  mood += .1;
147  mood *= ( ani->size() )/.2;
148  unsigned int index = (unsigned int) myroundclamp( floor( mood ) );
149  if ( index >= ani->size() )
150  index = ani->size()-1;
151  return (*ani)[index];
152 }
153