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
mission.h
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 /*
23  * xml Mission written by Alexander Rawass <alexannika@users.sourceforge.net>
24  */
27 
28 #ifndef _MISSION_H_
29 #define _MISSION_H_
30 #include "config.h"
31 #include <gnuhash.h>
32 
33 #include <expat.h>
34 #include <string>
35 #include <fstream>
36 
37 //#include "xml_support.h"
38 #include "easydom.h"
39 
40 #ifndef VS_MIS_SEL
41 #include "vegastrike.h"
42 #include "vs_globals.h"
43 #include "msgcenter.h"
44 #include "cmd/container.h"
45 class Unit;
46 class Order;
47 class MessageCenter;
48 #endif
49 
50 #include <assert.h>
51 using std::string;
52 
54 
55 #define qu( x ) ("\""+x+"\"")
56 
57 /* *********************************************************** */
58 
59 #ifdef VS_MIS_SEL
60 #define missionNode easyDomNode
61 #endif
62 
63 class varInst;
64 
65 typedef vector< varInst* > olist_t;
66 typedef vsUMap< string, varInst* >omap_t;
67 std::string varToString( varInst *s );
68 class Flightgroup;
69 #ifndef VS_MIS_SEL
70 
71 /* *********************************************************** */
72 
73 #define SCRIPT_PARSE 0
74 #define SCRIPT_RUN 1
75 #define SCRIPT_PARSE_DECL 2
76 
78 
80 {
83 };
84 
86 {
91  CMT_STD_getSystemFile, //useful when comparing _jumps_
111 };
113 {
125 };
127 {
139 };
140 
142 {
150 };
151 
153 {
171 
185 };
186 
188 {
195 };
196 
198 {
200  CMT_UNIT_equal, //checks for sameness
210  CMT_UNIT_getSaveData, //gets saved data for this unit
266  CMT_UNIT_correctStarSystem, //useful when comparing _jumps_
273 };
274 
276 {
291 };
293 
295 
296 /* *********************************************************** */
297 
298 class missionNode;
299 
301 
302 class varInst
303 {
304 public: varInst( scope_type sctype )
305  {
306  scopetype = sctype;
307  objectname = string();
308  object = NULL;
309  }
311  {
312  std::cout<<"varInst() obsolete\n"<<std::endl;
313  assert( 0 );
314  }
315 
316  string name;
318 
320  double float_val;
321  bool bool_val;
322  int int_val;
323  string string_val;
324 
325  string objectname;
326  void *object;
327 
330 
331  unsigned int varId;
332 };
333 
334 /* *********************************************************** */
335 
336 class varInstVec : public vector< varInst* >
337 {
338 public:
339  unsigned int addVar( varInst *vi )
340  {
341  push_back( vi );
342  int index = size()-1;
343  vi->varId = index;
344  return index;
345  }
346 };
347 class varInstMap : public vsUMap< string, varInst* >
348 {
349 public:
351 };
352 
353 /* *********************************************************** */
354 
356 {
357 public:
360 
362  {
363  varinsts = NULL;
364  block_node = NULL;
365  }
366 };
367 
368 /* *********************************************************** */
369 
371 {
372 public:
373  vector< scriptContext* >contexts;
375 };
376 
377 /* *********************************************************** */
378 
379 class missionNode;
380 
382 {
383 public:
384  int i;
385 };
387 {
388 protected:
389 public:
390  virtual ~missionThread() {}
391  vector< contextStack* >exec_stack;
392  vector< missionNode* > module_stack;
393  vector< unsigned int > classid_stack;
394 };
395 
396 /* *********************************************************** */
397 
398 class missionNode : public tagDomNode
399 {
400 public:
401  struct script_t
402  {
403  string name; //script,defvar,module
404  varInstMap variables; //script,module
405  vector< varInstMap* >classvars; //module
406  varInst *varinst; //defvar,const
408  missionNode *while_arg[2]; //while
409  int tester; //test
410  missionNode *test_arg[2]; //test
411  enum var_type vartype; //defvar,script
412  string initval;
414  vsUMap< string, missionNode* >scripts; //module
415  missionNode *exec_node; //exec, return
416  int nr_arguments; //script
419  unsigned int classinst_counter;
421  int varId;
424  }
425  script;
426 };
427 
428 /* *********************************************************** */
429 
430 class pythonMission;
432 #endif //VS_MIS_SEL
433 
434 class Mission
435 {
436 public:
438  unsigned int player_num;
441  struct Objective
442  {
444  std::string objective;
445  Unit * getOwner();
446  void setOwner( Unit *u )
447  {
448  Owner.SetUnit( u );
449  }
452  {
453  completeness = 0;
454  }
455  Objective( float complete, std::string obj )
456  {
457  completeness = complete;
458  objective = obj;
459  }
460  };
461  vector< Objective >objectives;
462  void SetUnpickleData( std::string dat )
463  {
464  unpickleData = dat;
465  }
467  static double gametime;
468  string mission_name;
469  void terminateMission();
470  Unit *call_unit_launch( class CreateFlightgroup*fg, int type /*clsptr type*/, const std::string&destinations );
471 
472  Mission( const char *configfile, bool loadscripts = true );
473  Mission( const char *filename, const std::string &pythonscript, bool loadscripts = true );
474  std::string Pickle(); //returns filename\npickleddata
475  void UnPickle( std::string pickled ); //takes in pickeddata
476  void AddFlightgroup( Flightgroup *fg );
477  void initMission( bool loadscripts = true );
478 
479  int getPlayerMissionNumber(); //-1 if not found or invalid player_num.
480  static Mission * getNthPlayerMission( int cp, int num );
481 
483  ~Mission();
484 //deletes missions pushed back onto a delete queue at a *safe time*
485  static void wipeDeletedMissions();
487  static int number_of_ships;
488 
489  static vector< Flightgroup* >flightgroups;
490 
491  Flightgroup * findFlightgroup( const string &fg_name, const string &faction );
492 
493  string getVariable( string name, string defaultval );
494 
495 #ifndef VS_MIS_SEL
496  void GetOrigin( QVector &pos, string &planetname );
497 
498  void DirectorLoop();
499  void DirectorStart( missionNode *node );
501  void DirectorInitgame();
502  void DirectorEnd();
503  void DirectorBenchmark();
504  void DirectorShipDestroyed( Unit *unit );
505  void BriefingStart();
506 //clobbers the cam view & renders btw 0,0, and 1,1
507  class TextPlane * BriefingRender();
508  void BriefingLoop();
509  void BriefingUpdate();
510  void BriefingEnd();
511  bool BriefingInProgress();
512  double getGametime();
513 
514  void loadMissionModules();
515  void loadModule( string modulename );
516  void addModule( string modulename );
517  bool runScript( string modulename, const string &scriptname, unsigned int classid = 0 );
518  bool runScript( missionNode*, const string &scriptname, unsigned int classid = 0 );
519  void RunDirectorScript( const string& );
520  unsigned int createClassInstance( string modulename );
521  void setCurrentAIUnit( Unit *unit )
522  {
523  current_ai_unit = unit;
524  }
525  void setCurrentAIOrder( Order *order )
526  {
527  current_ai_order = order;
528  }
529  varInst * lookupClassVariable( string modulename, string varname, unsigned int classid );
530  void destroyClassInstance( string modulename, unsigned int classid );
531 
533 
534  void call_vector_into_olist( varInst *vec_vi, QVector vec3 );
535  void deleteVarInst( varInst *vi, bool del_local = false );
536 #endif //VS_MIS_SEL
537 
538 private:
539 //string getVariable(easyDomNode *section,string name,string defaultval);
540  void ConstructMission( const char *configfile, const std::string &pythonscript, bool loadscripts = true );
541  missionNode *top;
542 
543  easyDomNode *variables;
544  easyDomNode *origin_node;
545 
546 #ifndef VS_MIS_SEL
547  Unit *current_ai_unit;
548  Order *current_ai_order;
549 
550  int debuglevel;
551  bool start_game;
552  bool do_trace;
553  int tracelevel; //unusued
554 
555  static int total_nr_frames;
556 
557 //ofstream var_out;
558 
559  parsemode_type parsemode;
560 
561  missionNode *director;
563 
564  tagMap tagmap;
565  char *nextpythonmission;
566  std::string unpickleData;
567 public:
568  struct Runtime
569  {
570  vector< missionThread* >threads;
572  vsUMap< string, missionNode* >modules;
575  vsUMap< string, missionNode* >global_variables;
577  //vector<const void *()> callbacks;
578  }
579  runtime;
580 private:
581  friend void UnpickleMission( std::string pickled );
582 //used only for parsing
583  vector< missionNode* >scope_stack;
584  missionNode *current_module;
585  missionNode *current_script;
586 
587  vector< string >import_stack;
588  vsUMap< string, callback_module_string_type > module_string_map;
589  vsUMap< string, callback_module_omap_type > module_omap_map;
590  vsUMap< string, callback_module_olist_type > module_olist_map;
591  vsUMap< string, callback_module_order_type > module_order_map;
592  vsUMap< string, callback_module_unit_type > module_unit_map;
593  vsUMap< string, callback_module_std_type > module_std_map;
594  vsUMap< string, callback_module_briefing_type >module_briefing_map;
595  vsUMap< string, callback_module_type >module_map;
596 
597  void saveVariables( const ostream &out );
598  void initTagMap();
599 
600  void initCallbackMaps();
601 #endif //VS_MIS_SEL
602 
603  bool checkMission( easyDomNode *node, bool loadscripts );
604  void doVariables( easyDomNode *node );
605  void checkVar( easyDomNode *node );
606  void doFlightgroups( easyDomNode *node );
607  void doOrder( easyDomNode *node, Flightgroup *fg );
608  void checkFlightgroup( easyDomNode *node );
609  bool doPosition( easyDomNode*node, double pos[3], class CreateFlightgroup* );
610  bool doRotation( easyDomNode*node, float rot[3], class CreateFlightgroup* );
611  void doOrigin( easyDomNode *node );
612  void doSettings( easyDomNode *node );
613 
614 #ifndef VS_MIS_SEL
615  void doModule( missionNode *node, int mode );
616 
617  scriptContext * addContext( missionNode *node );
618  void removeContext();
619  void removeContextStack();
620  void addContextStack( missionNode *node );
621 
622  void trace( missionNode *node, int mode );
623 
624  varInst * doScript( missionNode *node, int mode, varInstMap *varmap = NULL );
625  void doBlock( missionNode *node, int mode );
626  bool doBooleanVar( missionNode *node, int mode );
627  varInst * lookupLocalVariable( missionNode *asknode );
628  varInst * lookupModuleVariable( string mname, missionNode *asknode );
629  varInst * lookupModuleVariable( missionNode *asknode );
631  varInst * lookupGlobalVariable( missionNode *asknode );
632  varInst * doVariable( missionNode *node, int mode );
633  void checkStatement( missionNode *node, int mode );
634  void doIf( missionNode *node, int mode );
635  void doWhile( missionNode *node, int mode );
636  bool checkBoolExpr( missionNode *node, int mode );
637  bool doAndOr( missionNode *node, int mode );
638  bool doNot( missionNode *node, int mode );
639  bool doTest( missionNode *node, int mode );
640  void doDefVar( missionNode *node, int mode, bool global_var = false );
641  void doSetVar( missionNode *node, int mode );
642  varInst * doCall( missionNode *node, int mode );
643  varInst * doExec( missionNode *node, int mode );
644  varInst * doConst( missionNode *node, int mode );
645 
646  int doIntVar( missionNode *node, int mode );
647  int checkIntExpr( missionNode *node, int mode );
648  int doIMath( missionNode *node, int mode );
649  varInst * doMath( missionNode *node, int mode );
650  int intMath( string mathname, int res1, int res2 );
651  double floatMath( string mathname, double res1, double res2 );
652  varInst * checkExpression( missionNode *node, int mode );
653 
654  void assignVariable( varInst *v1, varInst *v2 );
655 
656  scriptContext * makeContext( missionNode *node );
657  bool checkVarType( varInst *var, enum var_type check_type );
658 
659  double checkFloatExpr( missionNode *node, int mode );
660  double doFloatVar( missionNode *node, int mode );
661  double doFMath( missionNode *node, int mode );
662 
663  void doArguments( missionNode *node, int mode, varInstMap *varmap = NULL );
664  void doReturn( missionNode *node, int mode );
665  void doGlobals( missionNode *node, int mode );
666  void doImport( missionNode *node, int mode );
667 
668  bool have_return( int mode );
669  missionNode * lookupScript( string scriptname, string modulename );
670 
671  var_type vartypeFromString( string type );
672 
673  varInst * doObjectVar( missionNode *node, int mode );
674  varInst * checkObjectExpr( missionNode *node, int mode );
675 
676  void fatalError( missionNode *node, int mode, string message );
677  void runtimeFatal( string message );
678  void warning( string message );
679  void debug( missionNode *node, int mode, string message );
680  void debug( int level, missionNode *node, int mode, string message );
681 
682  void printNode( missionNode *node, int mode );
683 
684  void printRuntime();
685  void printThread( missionThread *thread );
686  void printVarmap( const varInstMap &vmap );
687  void printVarInst( varInst *vi );
688  void saveVarInst( varInst *vi, ostream &out );
689  void printVarInst( int dbg_level, varInst *vi );
690  void printGlobals( int dbg_level );
691  void printModules();
692 
693  string modestring( int mode );
694 
695  varInst * searchScopestack( string name );
696 
697  varInst * callRnd( missionNode *node, int mode );
698  varInst * callPrintFloats( missionNode *node, int mode );
699  varInst * callGetGameTime( missionNode *node, int mode );
700  varInst * callResetTimeCompression( missionNode *node, int mode );
701  varInst * callGetSystemName( missionNode *node, int mode );
702  varInst * callGetSystemFile( missionNode *node, int mode, StarSystem *ss = NULL );
703  varInst * callGetNumAdjacentSystems( missionNode *node, int mode );
704  varInst * callGetGalaxyProperty( missionNode *node, int mode );
705  varInst * callGetAdjacentSystem( missionNode *node, int mode );
706  varInst * call_isNull( missionNode *node, int mode );
707  varInst * call_setNull( missionNode *node, int mode );
708  varInst * call_isequal( missionNode *node, int mode );
709  varInst * callGetCurrentAIUnit( missionNode *node, int mode );
710  varInst * callGetCurrentAIOrder( missionNode *node, int mode );
711  varInst * call_musicAddList( missionNode *node, int mode );
712  varInst * call_musicPlaySong( missionNode *node, int mode );
713  varInst * call_musicPlayList( missionNode *node, int mode );
714 
715  varInst * call_int_cast( missionNode *node, int mode );
716  varInst * call_float_cast( missionNode *node, int mode );
717 
718  varInst * call_io_sprintf( missionNode *node, int mode );
719  varInst * call_io_printf( missionNode *node, int mode );
720  string replaceNewline( string origstr );
721 
722  varInst * call_io_message( missionNode *node, int mode );
723  varInst * call_io_printmsglist( missionNode *node, int mode );
724 
725  string method_str( missionNode *node );
726  missionNode * getArgument( missionNode *node, int mode, int arg_nr );
727 
728  varInst * call_olist( missionNode *node, int mode );
729  olist_t * getOListObject( missionNode *node, int mode, varInst *ovi );
730  varInst * call_olist_new( missionNode *node, int mode );
731  void call_olist_pop_back( missionNode *node, int mode, varInst *ovi );
732  void call_olist_push_back( missionNode *node, int mode, varInst *ovi, varInst *push );
733  varInst * call_olist_at( missionNode *node, int mode, varInst *ovi, int index );
734  varInst * call_olist_back( missionNode *node, int mode, varInst *ovi );
735  void call_olist_toxml( missionNode *node, int mode, varInst *ovi );
736  QVector call_olist_tovector( missionNode *node, int mode, varInst *ovi );
737  void call_olist_set( missionNode *node, int mode, varInst *ovi, int index, varInst *new_vi );
738 
739  varInst * call_omap( missionNode *node, int mode );
740  omap_t * getOMapObject( missionNode *node, int mode, varInst *ovi );
741  varInst * call_omap_new( missionNode *node, int mode );
742 
743  varInst * getObjectArg( missionNode *node, int mode );
744 
745  varInst * call_unit( missionNode *node, int mode );
746  varInst * call_briefing( missionNode *node, int mode );
747  Unit * getUnitObject( missionNode *node, int mode, varInst *ovi );
748 
749 //void call_unit_launch(missionNode *node,int mode,string name,string faction,string type,string ainame,int nr_ships,Vector &pos);
750 
751  void call_unit_toxml( missionNode *node, int mode, varInst *ovi );
752 
753  varInst * call_string( missionNode *node, int mode );
754  void call_string_print( missionNode *node, int mode, varInst *ovi );
755  varInst * call_string_new( missionNode *node, int mode, string initstring );
756  string * getStringObject( missionNode *node, int mode, varInst *ovi );
757  string call_string_getstring( missionNode *node, int mode, varInst *ovi );
758  string getStringArgument( missionNode *node, int mode, int arg_nr );
759 
760  void findNextEnemyTarget( Unit *my_unit );
761 
762  varInst * doCall( missionNode *node, int mode, string module, string method );
763  void doCall_toxml( string module, varInst *ovi );
764 
765  varInst * newVarInst( scope_type scopetype );
766  int vi_counter, old_vi_counter;
767  int olist_counter, old_olist_counter;
768  int string_counter, old_string_counter;
769 
770  void deleteVarMap( varInstMap *vmap );
771 //pushes this mission onto a destruct queue for future destruction and removes from activeMission
772  varInst * call_terminateMission( missionNode *node, int mode );
773 
774  varInst * call_order( missionNode *node, int mode );
775  Order * getOrderObject( missionNode *node, int mode, varInst *ovi );
776 
777  QVector getVec3Arg( missionNode *node, int mode, int arg_nr );
778  double getFloatArg( missionNode *node, int mode, int arg_nr );
779  int getIntArg( missionNode *node, int mode, int arg_nr );
780  bool getBoolArg( missionNode *node, int mode, int arg_nr );
781  Unit * getUnitArg( missionNode *node, int mode, int arg_nr );
782 #endif //VS_MIS_SEL
783 };
784 
785 #endif //_MISSION_H_
786