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
order_comm.cpp
Go to the documentation of this file.
1 #include "order.h"
2 #include "communication.h"
3 #include "networking/netserver.h"
4 #include "networking/netclient.h"
5 
6 using std::list;
7 using std::vector;
8 void Order::AdjustRelationTo( Unit *un, float factor )
9 {
10  //virtual stub function
11 }
12 
14 {
15  if (this == NULL)
16  return;
17  if ( Network != NULL && !_Universe->netLocked() ) {
18  //Stupid constness rules...
19  int cp = _Universe->whichPlayerStarship( const_cast< UnitContainer& > (c.sender).GetUnit() );
20  if (cp != -1 && parent && parent->GetSerial() != 0)
21  Network[cp].communicationRequest( c, parent->GetSerial() );
22  return;
23  }
24  int completed = 0;
25  unsigned int i = 0;
27  for (i = 0; i < suborders.size(); i++)
28  if ( ( completed&( (suborders[i])->getType()&(MOVEMENT|FACING|WEAPON) ) ) == 0 ) {
29  (suborders[i])->Communicate( *newC );
30  completed |= (suborders[i])->getType();
31  }
32  Unit *un;
33  bool already_communicated = false;
34  for (list< CommunicationMessage* >::iterator ii = messagequeue.begin(); ii != messagequeue.end(); ii++) {
35  un = (*ii)->sender.GetUnit();
36  bool thisissender = ( un == newC->sender.GetUnit() );
37  if (un == NULL || thisissender) {
38  delete (*ii);
39  if (thisissender) already_communicated = true;
40  if ( ( ii = messagequeue.erase( ii ) ) == messagequeue.end() )
41  break;
42  }
43  }
44  if ( ( un = newC->sender.GetUnit() ) ) {
45  if (un != parent) {
46  static bool talk_more_helps = XMLSupport::parse_bool( vs_config->getVariable( "AI", "talking_faster_helps", "true" ) );
47  static float talk_factor = XMLSupport::parse_float( vs_config->getVariable( "AI", "talk_relation_factor", ".5" ) );
48  if (talk_more_helps || !already_communicated)
49  AdjustRelationTo( un, newC->getDeltaRelation()*talk_factor );
50  messagequeue.push_back( newC );
51  }
52  }
53  if (SERVER) {
54  Unit *plr = const_cast< UnitContainer& > (c.sender).GetUnit();
56  if (cp >= 1)
57  VSServer->sendCommunication( plr, parent, &c );
58  }
59 }
60 
62 void Order::ProcessCommunicationMessages( float AICommresponseTime, bool RemoveMessageProcessed )
63 {
64  float time = AICommresponseTime/SIMULATION_ATOM;
65  if (time <= .001)
66  time += .001;
67  if ( !messagequeue.empty() ) {
68  bool cleared = false;
69  if ( messagequeue.back()->curstate == messagequeue.back()->fsm->GetRequestLandNode() ) {
70  cleared = true;
71  RemoveMessageProcessed = true;
72  Unit *un = messagequeue.back()->sender.GetUnit();
73  if (un) {
74  CommunicationMessage c( parent, un, NULL, 0 );
75  if ( parent->getRelation( un ) >= 0
76  || (parent->getFlightgroup() && parent->getFlightgroup()->name == "Base") ) {
77  parent->RequestClearance( un );
78  c.SetCurrentState( c.fsm->GetAbleToDockNode(), NULL, 0 );
79  } else {
80  c.SetCurrentState( c.fsm->GetUnAbleToDockNode(), NULL, 0 );
81  }
82  Order *o = un->getAIState();
83  if (o)
84  o->Communicate( c );
85  }
86  }
87  if ( cleared || ( ( (float) rand() )/RAND_MAX ) < (1/time) ) {
88  FSM::Node *n;
89  if ( ( n = messagequeue.back()->getCurrentState() ) )
91  if (RemoveMessageProcessed) {
92  delete messagequeue.back();
93  messagequeue.pop_back();
94  } else {
95  messagequeue.push_front( messagequeue.back() );
96  messagequeue.pop_back();
97  }
98  }
99  }
100 }
101