4 #include "cmd/script/mission.h"
7 #include "cmd/unit_generic.h"
8 #include "cmd/unit_factory.h"
9 #include "cmd/collection.h"
15 #include "cmd/unit_csv.h"
16 #include "gfx/cockpit_generic.h"
22 #include "cmd/unit_util.h"
23 #include "networking/netserver.h"
26 #include "cmd/unit_collide.h"
27 #include "cmd/unit_find.h"
29 #include "python/init.h"
41 #define activeSys _Universe->activeStarSystem()
42 using namespace VSFileSystem;
44 namespace UniverseUtil
46 Unit* PythonUnitIter::current()
49 while ( (ret = **
this) ) {
57 void PythonUnitIter::advanceSignificant()
60 while ( notDone() && !UnitUtil::isSignificant( **
this ) )
64 void PythonUnitIter::advanceInsignificant()
67 while ( notDone() && UnitUtil::isSignificant( **
this ) )
71 void PythonUnitIter::advancePlanet()
74 while ( notDone() && !(**this)->isPlanet() )
78 void PythonUnitIter::advanceJumppoint()
81 while ( notDone() && !(**this)->isJumppoint() )
85 void PythonUnitIter::advanceN(
int n )
87 while (notDone() && n > 0) {
93 void PythonUnitIter::advanceNSignificant(
int n )
95 if ( notDone() && !UnitUtil::isSignificant( **
this ) )
97 while ( notDone() && (n > 0) ) {
103 void PythonUnitIter::advanceNInsignificant(
int n )
105 if ( notDone() && UnitUtil::isSignificant( **
this ) )
106 advanceInsignificant();
107 while ( notDone() && (n > 0) ) {
108 advanceInsignificant();
113 void PythonUnitIter::advanceNPlanet(
int n )
115 if ( notDone() && !(**this)->isPlanet() )
117 while (notDone() && n > 0) {
123 void PythonUnitIter::advanceNJumppoint(
int n )
125 if ( notDone() && !(**this)->isJumppoint() )
127 while (notDone() && n > 0) {
140 if ( (*it)->GetSerial() == serial )
143 cout<<
"ERROR --> no unit for serial "<<serial<<endl;
146 std::string
vsConfig( std::string category, std::string option, std::string def )
152 string faction_string,
154 string unittype_string,
160 string destinations )
163 int clstype = UNITPTR;
164 if (unittype_string ==
"planet")
166 else if (unittype_string ==
"asteroid")
167 clstype = ASTEROIDPTR;
168 else if (unittype_string ==
"nebula")
170 CreateFlightgroup cf;
171 cf.fg = Flightgroup::newFlightgroup( name_string,
182 cf.waves = nr_of_waves;
183 cf.nr_ships = nr_of_ships;
185 cf.rot[0] = cf.rot[1] = cf.rot[2] = 0.0f;
186 Unit *tmp =
mission->call_unit_launch( &cf, clstype, destinations );
187 mission->number_of_ships += nr_of_ships;
195 unsigned int max = mpl->numCargo();
196 if ( !category.empty() ) {
198 mpl->GetSortedCargoCat( category, Begin, End );
200 unsigned int i = Begin+( rand()%(End-Begin) );
201 ret = &mpl->GetCargo( i );
203 }
else if ( mpl->numCargo() ) {
204 for (
unsigned int i = 0; i < 500; ++i) {
205 ret = &mpl->GetCargo( rand()%max );
206 if (ret->GetContent().find(
"mission" ) == string::npos)
211 Cargo tempret = *ret;
212 tempret.quantity = quantity;
239 static UnitContainer scratch_unit;
240 static QVector scratch_vector;
244 return UnitFactory::getMasterPartList();
248 return scratch_unit.GetUnit();
252 scratch_unit.SetUnit( un );
257 return scratch_vector;
293 if (un->faction == enfac && UnitUtil::getFlightgroupName( un ) == enfgname)
294 if ( (NULL == en) || (rand()%3 == 0) )
296 if (un->faction == fac && UnitUtil::getFlightgroupName( un ) == fgname)
300 UnitUtil::setFlightgroupLeader( al, al );
303 UnitUtil::setFgDirective( al,
"A." );
304 UnitUtil::setFlightgroupLeader( en, en );
307 UnitUtil::setFgDirective( en,
"h" );
319 if ( (un->faction == enfac && UnitUtil::getFlightgroupName( un ) == enfgname) ) {
321 UnitUtil::setFgDirective( un,
"b" );
322 }
else if (un->faction == fac && UnitUtil::getFlightgroupName( un ) == fgname) {
324 UnitUtil::setFgDirective( un,
"b" );
333 for (
unsigned int i = 0; i < nass; ++i)
339 float GetRelation( std::string myfaction, std::string theirfaction )
346 if (myfac == theirfac)
348 else if (myfac == un->faction)
350 else if (theirfac == un->faction)
355 void AdjustRelation( std::string myfaction, std::string theirfaction,
float factor,
float rank )
359 float realfactor = factor*rank;
363 if (myfac == theirfac)
365 else if (myfac == un->faction)
367 else if (theirfac == un->faction)
377 if (val > 1) val = 1;
386 fg =
"FG_Relation_"+fg;
393 if (delta > 1) delta = 1;
397 float val =
getSaveData( which_cp, saveVar, 0 )+delta;
398 if (val > 1) val = 1;
407 fg =
"FG_Relation_"+fg;
417 mission->player_num = whichplayer;
425 return activeSys->getUnitList().createIterator();
429 un_iter iter =
activeSys->getUnitList().createIterator();
431 for (
int i = -1; (un = *iter) && i < index; ++iter) {
432 if (un->GetHull() > 0)
442 UnitPtrLocator unitLocator( ptr );
443 findObjects(
activeSys->collidemap[Unit::UNIT_ONLY], finder->location[Unit::UNIT_ONLY], &unitLocator );
444 if (unitLocator.retval)
445 return reinterpret_cast< Unit*
> (ptr);
447 else if ( !finder->isSubUnit() )
452 un_iter it =
activeSys->getUnitList().createIterator();
453 while ( it.notDone() && ( (
void*) (*it) != ptr ) )
455 return ( (
void*) (*it) == ptr ) ? reinterpret_cast< Unit* > (ptr) : 0;
459 un_iter iter =
activeSys->getUnitList().createIterator();
460 while (iter.notDone() && UnitUtil::getName( *iter ) != name)
462 return iter.notDone() ? (*iter) : NULL;
466 #ifdef USE_STL_COLLECTION
472 un_iter iter =
activeSys->getUnitList().createIterator();
473 while ( iter.current() ) {
501 #define DEFAULT_FACTION_SAVENAME "FactionTookOver_"
506 vector< std::string > *ans =
514 vector< std::string > *ans =
519 ans->push_back( std::string( fac ) );
540 static string dontBlankOut(
string objective )
543 std::string::size_type where = objective.find(
".blank" );
544 if (where != string::npos)
545 objective = objective.substr( 0, where )+objective.substr( where+strlen(
".blank" ) );
546 else return objective;
554 VSServer->sendSaveData(
mission->player_num, Subcmd::Objective|Subcmd::SetValue,
556 mission->objectives.push_back( Mission::Objective( status, dontBlankOut( objective ) ) );
557 return mission->objectives.size()-1;
561 if (which < (
int)
mission->objectives.size() && which >= 0) {
563 VSServer->sendSaveData(
mission->player_num, Subcmd::Objective|Subcmd::SetValue,
564 which, NULL,
mission, &newobjective, &
mission->objectives[which].completeness );
565 mission->objectives[which].objective = dontBlankOut( newobjective );
570 if (which < (
int)
mission->objectives.size() && which >= 0) {
572 VSServer->sendSaveData(
mission->player_num, Subcmd::Objective|Subcmd::SetValue,
573 which, NULL,
mission, &
mission->objectives[which].objective, &completeNess );
574 mission->objectives[which].completeness = completeNess;
579 if (which < (
int)
mission->objectives.size() && which >= 0)
580 return mission->objectives[which].completeness;
594 if (which < (
int)
mission->objectives.size() && which >= 0) {
596 VSServer->sendSaveData(
mission->player_num, Subcmd::Objective|Subcmd::EraseValue,
597 which, NULL,
mission, NULL, NULL );
603 if (
mission->objectives.size() ) {
606 VSServer->sendSaveData(
mission->player_num, Subcmd::Objective|Subcmd::EraseValue,
607 -1, NULL,
mission, NULL, NULL );
612 if ( which < (
int)
mission->objectives.size() )
613 mission->objectives[which].setOwner( owner );
617 if ( which < (
int)
mission->objectives.size() )
618 return mission->objectives[which].getOwner();
631 void IOmessage(
int delay,
string from,
string to,
string message )
637 mission->msgcenter->add( from, to, message, delay );
660 if (global_auto == 1)
661 mission->global_autopilot = Mission::AUTO_ON;
662 else if (global_auto == -1)
663 mission->global_autopilot = Mission::AUTO_OFF;
665 mission->global_autopilot = Mission::AUTO_NORMAL;
666 if (player_auto == 1)
667 mission->player_autopilot = Mission::AUTO_ON;
668 else if (player_auto == -1)
669 mission->player_autopilot = Mission::AUTO_OFF;
671 mission->player_autopilot = Mission::AUTO_NORMAL;
677 for (
unsigned int k = 0; k < 10; ++k) {
679 bool collision =
false;
682 for (un_iter i = sts->
getUnitList().createIterator(); (un = *i) != NULL; ++i) {
683 if (UnitUtil::isAsteroid( un ) || un->isUnit() == NEBULAPTR)
685 double dist = ( pos-un->LocalPosition() ).Magnitude()-un->rSize()- radial_size;
687 QVector delta = pos-un->LocalPosition();
688 double mag = delta.Magnitude();
692 delta.Set( 0, 0, 1 );
693 delta = delta.Scale( dist+un->rSize()+radial_size );
698 QVector r( .5, .5, .5 );
699 pos += ( radial_size+un->rSize() )*r;
704 if (collision ==
false)
716 string faction_string,
739 CSVRow tmp( LookupUnitRow( unitname, faction ) );
741 return tmp[statname];
747 static std::vector< Unit* >cachedUnits;
766 std::replace( message.begin(), message.end(),
'\'',
'\"' );
767 std::replace( message.begin(), message.end(),
'\\',
'/' );
768 std::replace( message.begin(), message.end(),
'\n',
' ' );
769 std::replace( message.begin(), message.end(),
'\r',
' ' );
779 string pythonCode = game_options.
custompython+
"("+(trusted ?
"True" :
"False")
780 +
", r\'"+cmd+
"\', r\'"+args+
"\', r\'"+
id+
"\')\n";
781 COUT<<
"Executing python command: "<<endl;
782 cout<<
" "<<pythonCode;
783 const char *cpycode = pythonCode.c_str();
784 ::Python::reseterrors();
785 PyRun_SimpleString( const_cast< char* > (cpycode) );
786 ::Python::reseterrors();
804 std::string
getVariable( std::string section, std::string name, std::string def )
808 std::string
getSubVariable( std::string section, std::string subsection, std::string name, std::string def )
859 using namespace VSFileSystem;
864 cout<<
"\t\tcomputing serials for "<<systempath<<
"...";
868 std::vector< std::string >search_patterns;
870 bool newserials =
true;
871 if (system.find(
"serial=", 0 ) != std::string::npos) {
873 cout<<
"Found serial in system file : replacing serials..."<<endl;
875 cout<<
"Found no serial in system file : generating..."<<endl;
877 search_patterns.push_back(
"<planet " );
878 search_patterns.push_back(
"<Planet " );
879 search_patterns.push_back(
"<PLANET " );
880 search_patterns.push_back(
"<unit " );
881 search_patterns.push_back(
"<Unit " );
882 search_patterns.push_back(
"<UNIT " );
883 search_patterns.push_back(
"<nebula " );
884 search_patterns.push_back(
"<Nebula " );
885 search_patterns.push_back(
"<NEBULA " );
886 search_patterns.push_back(
"<jump " );
887 search_patterns.push_back(
"<Jump " );
888 search_patterns.push_back(
"<JUMP " );
889 for (std::vector< std::string >::iterator ti = search_patterns.begin(); ti != search_patterns.end(); ++ti) {
890 std::string search( (*ti) );
891 std::string::size_type search_length = (*ti).length();
892 std::string::size_type curpos = 0;
894 while ( ( curpos = system.find( search, curpos ) ) != std::string::npos ) {
900 system.replace( curpos, search_length, serial_str );
902 system.replace( curpos, search_length+15, serial_str );
904 curpos += search_length;
906 cerr<<
"\t\tFound "<<nboc<<
" occurences of "<<search<<endl;
915 cerr<<
"!!! ERROR : opening "<<systempath<<
" for writing"<<endl;
918 if (
f.
Write( system ) != system.length() ) {
919 cerr<<
"!!! ERROR : writing system file"<<endl;
926 cerr<<
"ERROR cannot open system file : "<<systempath<<endl;
933 cout<<
"Going through "<<stak.size()<<
" sectors"<<endl;
934 cout<<
"Generating random serial numbers :"<<endl;
935 for (; !stak.empty();) {
936 string sys( stak.back()+
".system" );
940 cout<<
"Computing done."<<endl;
948 static std::string simplePrettySystem( std::string system )
950 std::string::size_type where = system.find_first_of(
'/' );
951 std::string::size_type basewhere = system.find_first_of(
'@', where );
952 return std::string(
"Sec:" )+system.substr( 0, where )
953 +
" Sys:"+( where == string::npos ? std::string(
"") : system.substr( where+1, (basewhere!=string::npos) ? basewhere-where-1 : string::npos ) )
954 +( basewhere == string::npos ? std::string(
"") : std::string(
" ")+system.substr( basewhere+1 ) );
956 static std::string simplePrettyShip( std::string ship )
958 if (ship.length() > 0)
959 ship[0] = toupper( ship[0] );
960 std::string::size_type where = ship.find(
"." );
961 if (where != string::npos) {
962 ship = ship.substr( 0, where );
963 ship =
"Refurbished "+ship;
968 string getSaveInfo(
const std::string &filename,
bool formatForTextbox )
971 static set< string >campaign_score_vars;
972 static bool campaign_score_vars_init =
false;
973 if (!campaign_score_vars_init) {
974 string campaign_score =
vs_config->
getVariable(
"physics",
"campaigns",
"privateer_campaign vegastrike_campaign" );
976 string::size_type where = 0, when = game_options.
campaigns.find(
' ' );
977 while (where != string::npos) {
978 campaign_score_vars.insert( game_options.
campaigns.substr( where, ( (when == string::npos) ? when : when-where ) ) );
979 where = (when == string::npos) ? when : when+1;
980 when = game_options.
campaigns.find(
' ', where );
982 campaign_score_vars_init =
true;
985 std::string lf = (formatForTextbox ?
"#n#" :
"\n");
986 QVector pos( 0, 0, 0 );
987 bool updatepos =
false;
989 vector< std::string >Ships;
992 savegame.
ParseSaveGame( filename, system,
"", pos, updatepos, creds, Ships,
994 campaign_score_vars );
998 text =
"Savegame: "+text+lf+
"_________________"+lf;
1001 if ( 0 == stat( (
getSaveDir()+filename).c_str(), &attrib ) ) {
1002 text +=
"Saved on: ";
1003 text += ctime( &attrib.st_mtime )+lf;
1007 ( (
unsigned int) (creds*100) )%100 )+lf;
1008 text += simplePrettySystem( system )+lf;
1009 if ( Ships.size() ) {
1010 text +=
"Starship: "+simplePrettyShip( Ships[0] )+lf;
1011 if (Ships.size() > 2) {
1012 text +=
"Fleet:"+lf;
1013 for (
unsigned int i = 2; i < Ships.size(); i += 2)
1014 text +=
" "+simplePrettyShip( Ships[i-1] )+lf+
" Located At:"+lf+
" "+simplePrettySystem( Ships[i] )+lf;
1019 for (set< string >::const_iterator it = campaign_score_vars.begin(); it != campaign_score_vars.end(); ++it) {
1024 if (var.length() > 0)
1025 var[0] = toupper( var[0] );
1026 text += var.substr( 0, var.find(
"_" ) )+
" Campaign Score: "+
XMLSupport::tostring( curscore )+lf;
1030 text +=
"Campaign Score: 0"+lf;
1047 static string ngsn(
"New_Game" );
1053 vector< string > path;