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_server.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 
10 Pilot::Pilot( int faction )
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 }
27 
28 float Pilot::adjustSpecificRelationship( Unit *parent, void *aggressor, float factor, int faction )
29 {
30  relationmap::iterator i = effective_relationship.insert( std::pair< const void*, float > ( aggressor, 0 ) ).first;
31  if ( faction != FactionUtil::GetNeutralFaction() ) {
32  float rel = UnitUtil::getRelationToFaction( parent, faction ); /* What the bloody hell? */
33  bool abovezero = (*i).second+rel < 0;
34  if (!abovezero) {
35  static float slowrel = XMLSupport::parse_float( vs_config->getVariable( "AI", "SlowDiplomacyForEnemies", ".25" ) );
36  factor *= slowrel;
37  }
38  (*i).second += factor;
39  if (rel+factor < 0 && parent->Target() == NULL && parent->aistate)
40  parent->aistate->ChooseTarget();
41  } else {
42  static float lessrel = XMLSupport::parse_float( vs_config->getVariable( "AI", "UnknownRelationEnemy", "-.05" ) );
43  bool abovezero = (*i).second < lessrel;
44  if (!abovezero) {
45  static float slowrel = XMLSupport::parse_float( vs_config->getVariable( "AI", "SlowDiplomacyForEnemies", ".25" ) );
46  factor *= slowrel;
47  }
48  (*i).second += factor;
49  if ( (*i).second < lessrel && parent->Target() == NULL && parent->aistate )
50  parent->aistate->ChooseTarget();
51  }
52  return (*i).second;
53 }
54 void Pilot::DoHit( Unit *parent, void *aggressor, int faction )
55 {
56  static float hitcost = XMLSupport::parse_float( vs_config->getVariable( "AI", "UnknownRelationHitCost", ".01" ) );
57  if (hitcost) {
58  adjustSpecificRelationship( parent, aggressor, hitcost, faction );
59  int whichCp = _Universe->whichPlayerStarship( parent );
60  if (whichCp != -1 && faction != parent->faction) {
61  UniverseUtil::adjustRelationModifierInt( whichCp, faction, hitcost*getRank() );
62  } else {
63  /* Instead use the Aggressor's cockpit? */
64  whichCp = _Universe->whichPlayerStarship( (const Unit*) aggressor );
65  if (whichCp != -1) {
66  Flightgroup *fg = parent->getFlightgroup();
67  if (parent->faction != faction)
68  UniverseUtil::adjustRelationModifierInt( whichCp, parent->faction, hitcost*getRank() );
69  if (fg)
70  UniverseUtil::adjustFGRelationModifier( whichCp, fg->name, hitcost*getRank() );
71  }
72  }
73  }
74 }
75 float Pilot::getAnger( const Unit *parent, const Unit *target ) const
76 {
77  relationmap::const_iterator iter = effective_relationship.find( target );
78  float rel = 0;
79  if ( iter != effective_relationship.end() )
80  rel = iter->second;
81  if ( _Universe->isPlayerStarship( target ) ) {
82  if (FactionUtil::GetFactionName( faction ).find( "pirates" ) != std::string::npos) {
83  static unsigned int cachedCargoNum = 0;
84  static bool good = true;
85  if ( cachedCargoNum != target->numCargo() ) {
86  cachedCargoNum = target->numCargo();
87  good = true;
88  for (unsigned int i = 0; i < cachedCargoNum; ++i) {
89  Cargo *c = &target->pImage->cargo[i];
90  if (c->quantity != 0 && c->GetCategory().find( "upgrades" ) == string::npos) {
91  good = false;
92  break;
93  }
94  }
95  }
96  if (good) {
97  static float goodness_for_nocargo =
98  XMLSupport::parse_float( vs_config->getVariable( "AI", "pirate_bonus_for_empty_hold", ".75" ) );
99  rel += goodness_for_nocargo;
100  }
101  }
102  }
103  {
104  int fac = faction;
105  MapStringFloat::iterator mapiter = factions[fac]->ship_relation_modifier.find( target->name );
106  if ( mapiter != factions[fac]->ship_relation_modifier.end() )
107  rel += (*mapiter).second;
108  }
109  {
110  int parent_cp = _Universe->whichPlayerStarship( parent );
111  int target_cp = _Universe->whichPlayerStarship( target );
112  if (parent_cp != -1) {
113  Flightgroup *fg = target->getFlightgroup();
114  if (fg)
115  rel += UniverseUtil::getFGRelationModifier( parent_cp, fg->name );
116  }
117  if (target_cp != -1) {
118  //... do we count it both ways? else?
119  Flightgroup *fg = parent->getFlightgroup();
120  if (fg)
121  rel += UniverseUtil::getFGRelationModifier( target_cp, fg->name );
122  }
123  }
124 
125  return rel;
126 }
127 
128 float Pilot::GetEffectiveRelationship( const Unit *parent, const Unit *target ) const
129 {
130  return getAnger( parent, target )+UnitUtil::getFactionRelation( parent, target );
131 }
132 
133 extern float myroundclamp( float i );
134 
135 Animation* Pilot::getCommFace( Unit *parent, float mood, unsigned char &sex )
136 {
137  vector< Animation* > *ani = getCommFaces( sex );
138 //this #ifndef hack below by chuck_starchaser, to get around missing faction_util.o in vegaserver make list
139  if (ani->size() == 0)
140  return NULL;
141  mood += .1;
142  mood *= ( ani->size() )/.2;
143  unsigned int index = (unsigned int) myroundclamp( floor( mood ) );
144  if ( index >= ani->size() )
145  index = ani->size()-1;
146  return (*ani)[index];
147 }
148