vegastrike  0.5.1.r1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
faction_util_generic.cpp
Go to the documentation of this file.
1 #include <assert.h>
2 #include "faction_generic.h"
3 #include "vsfilesystem.h"
4 #include "universe_generic.h"
5 #include "config_xml.h"
6 #include "vs_globals.h"
7 #include "gfx/cockpit_generic.h"
8 #include "cmd/unit_generic.h"
9 
10 #include "options.h"
11 
13 
14 using namespace FactionUtil;
18 FSM* FactionUtil::GetConversation( int Myfaction, int TheirFaction )
19 {
20  assert( factions[Myfaction]->faction[TheirFaction].stats.index == TheirFaction );
21  return factions[Myfaction]->faction[TheirFaction].conversation.get();
22 }
23 
24 const char* FactionUtil::GetFaction( int i )
25 {
26  if ( i >= 0 && i < (int) factions.size() )
27  return factions[i]->factionname;
28  return NULL;
29 }
30 
31 static int GetFactionLookup( const char *factionname )
32 {
33 #ifdef _WIN32
34  #define strcasecmp stricmp
35 #endif
36  for (unsigned int i = 0; i < factions.size(); i++)
37  if (strcasecmp( factionname, factions[i]->factionname ) == 0)
38  return i;
39  return 0;
40 }
41 
43 {
44  return factions[faction]->contraband.get();
45 }
51 int FactionUtil::GetFactionIndex( const string& name )
52 {
53  static Hashtable< string, int, 47 >factioncache;
54  int *tmp = factioncache.Get( name );
55  if (tmp)
56  return *tmp;
57  int i = GetFactionLookup( name.c_str() );
58  tmp = new int;
59  *tmp = i;
60  factioncache.Put( name, tmp );
61  return i;
62 }
64 {
65  return factions[faction]->citizen;
66 }
67 bool FactionUtil::isCitizen( const std::string& name )
68 {
69  return isCitizenInt( GetFactionIndex( name ) );
70 }
71 string FactionUtil::GetFactionName( int index )
72 {
73  const char *tmp = GetFaction( index );
74  if (tmp) return tmp;
75  static std::string nullstr;
76  return nullstr;
77 }
78 
79 static bool isPlayerFaction( const int MyFaction )
80 {
81  unsigned int numplayers = _Universe->numPlayers();
82  for (unsigned int i = 0; i < numplayers; ++i) {
83  Unit *un = _Universe->AccessCockpit( i )->GetParent();
84  if (un)
85  if (un->faction == MyFaction)
86  return true;
87  }
88  return false;
89 }
90 void FactionUtil::AdjustIntRelation( const int Myfaction, const int TheirFaction, float factor, float rank )
91 {
92  assert( factions[Myfaction]->faction[TheirFaction].stats.index == TheirFaction );
93  if (strcmp( factions[Myfaction]->factionname, "neutral" ) != 0) {
94  if (strcmp( factions[Myfaction]->factionname, "upgrades" ) != 0) {
95  if (strcmp( factions[TheirFaction]->factionname, "neutral" ) != 0) {
96  if (strcmp( factions[TheirFaction]->factionname, "upgrades" ) != 0) {
97  if (isPlayerFaction( TheirFaction ) || game_options.AllowNonplayerFactionChange) {
98  if (game_options.AllowCivilWar || Myfaction != TheirFaction) {
99  factions[Myfaction]->faction[TheirFaction].relationship += factor*rank;
100  if (factions[Myfaction]->faction[TheirFaction].relationship > 1 && game_options.CappedFactionRating)
101  factions[Myfaction]->faction[TheirFaction].relationship = 1;
102  if (factions[Myfaction]->faction[TheirFaction].relationship < game_options.min_relationship)
103  factions[Myfaction]->faction[TheirFaction].relationship = game_options.min_relationship;
104  if (!game_options.AllowNonplayerFactionChange)
105  factions[TheirFaction]->faction[Myfaction].relationship =
106  factions[Myfaction]->faction[TheirFaction].relationship; //reflect if player
107  }
108  }
109  }
110  }
111  }
112  }
113 }
114 int FactionUtil::GetPlaylist( const int myfaction )
115 {
116  return factions[myfaction]->playlist; //can be -1
117 }
118 const float* FactionUtil::GetSparkColor( const int myfaction )
119 {
120  return factions[myfaction]->sparkcolor; //can be -1
121 }
123 {
124  return factions.size();
125 }
127 {
128  for (unsigned int i = 0; i < factions.size(); i++) {
129  for (unsigned int j = 0; j < factions[i]->faction.size(); j++)
130  VSFileSystem::vs_fprintf( fp, "%g ", factions[i]->faction[j].relationship );
131  VSFileSystem::vs_fprintf( fp, "\n" );
132  }
133 }
135 {
136  char temp[8192];
137  string ret( "" );
138  for (unsigned int i = 0; i < factions.size(); i++) {
139  for (unsigned int j = 0; j < factions[i]->faction.size(); j++) {
140  sprintf( temp, "%g ", factions[i]->faction[j].relationship );
141  ret += string( temp );
142  }
143  sprintf( temp, "\n" );
144  ret += string( temp );
145  }
146  return ret;
147 }
148 int FactionUtil::numnums( const char *str )
149 {
150  int count = 0;
151  for (int i = 0; str[i]; i++)
152  count += (str[i] >= '0' && str[i] <= '9') ? 1 : 0;
153  return count;
154 }
156 {
157  for (unsigned int i = 0; i < factions.size(); i++) {
158  char *tmp = new char[24*factions[i]->faction.size()];
159  static char *bogus_return; //added by chuck_starchaser to squash a warning
160  bogus_return = fgets( tmp, 24*factions[i]->faction.size()-1, fp );
161  char *tmp2 = tmp;
162  if (numnums( tmp ) == 0) {
163  i--;
164  continue;
165  }
166  for (unsigned int j = 0; j < factions[i]->faction.size(); j++) {
167  if ( 1 != sscanf( tmp2, "%f ", &factions[i]->faction[j].relationship ) )
168  printf( "err" );
169  int k = 0;
170  bool founddig = false;
171  while (tmp2[k]) {
172  if ( isdigit( tmp2[k] ) )
173  founddig = true;
174  if ( founddig && (!isdigit( tmp2[k] ) && tmp2[k] != '.') )
175  break;
176  k++;
177  }
178  tmp2 += k;
179  if (*tmp2 == '\r' || *tmp2 == '\n')
180  break;
181  }
182  delete[] tmp;
183  }
184 }
185 bool whitespaceNewline( char *inp )
186 {
187  for (; *inp; inp++) {
188  if (inp[0] == '\n' || inp[0] == '\r')
189  return true;
190  if (inp[0] != ' ' && inp[0] != '\t')
191  break;
192  }
193  return false;
194 }
197 {
198  if (buf == NULL) {
199  char *bleh = strdup( savedFactions.c_str() );
200  char *blah = bleh;
201  LoadSerializedFaction( blah );
202  free( bleh );
203  return;
204  }
205  if (factions.size() == 0) {
206  savedFactions = buf;
207  return;
208  }
209  for (unsigned int i = 0; i < factions.size(); i++) {
210  if (numnums( buf ) == 0)
211  return;
212  for (unsigned int j = 0; j < factions[i]->faction.size(); j++) {
213  sscanf( buf, "%f ", &factions[i]->faction[j].relationship );
214  int k = 0;
215  bool founddig = false;
216  while (buf[k]) {
217  if ( isdigit( buf[k] ) )
218  founddig = true;
219  if ( founddig && (!isdigit( buf[k] ) && buf[k] != '.') )
220  break;
221  k++;
222  }
223  buf += k;
224  if ( whitespaceNewline( buf ) )
225  break;
226  }
227  }
228 }
229