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
director.cpp
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 Scripting written by Alexander Rawass <alexannika@users.sourceforge.net>
24  */
25 #include "config.h"
26 #include "python/python_class.h"
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <time.h>
31 #include <ctype.h>
32 #include <assert.h>
33 #ifndef WIN32
34 //this file isn't available on my system (all win32 machines?) i dun even know what it has or if we need it as I can compile without it
35 #include <unistd.h>
36 #include <pwd.h>
37 #endif
38 
39 #include <expat.h>
40 
41 #include <fstream>
42 
43 #include "xml_support.h"
44 
45 #include "vegastrike.h"
46 #include "vsfilesystem.h"
47 #include "lin_time.h"
48 #include "cmd/unit_generic.h"
49 
50 #include "cmd/ai/order.h"
51 #include "mission.h"
52 #include "easydom.h"
53 
54 #include "vs_globals.h"
55 #include "config_xml.h"
56 #include "savegame.h"
57 #include "msgcenter.h"
58 #include "cmd/briefing.h"
59 #include "pythonmission.h"
60 #ifdef HAVE_PYTHON
61 #include "Python.h"
62 #endif
63 #include "flightgroup.h"
64 #include "gldrv/winsys.h"
65 #include <boost/version.hpp>
66 #if BOOST_VERSION != 102800
67 #include <boost/python/class.hpp>
68 #else
69 #include <boost/python/detail/extension_class.hpp>
70 #endif
71 #include "gfx/cockpit_generic.h"
72 
73 /* *********************************************************** */
74 //ADD_FROM_PYTHON_FUNCTION(pythonMission)
76 {
77  double oldgametime = gametime;
78  gametime += SIMULATION_ATOM; //elapsed;
79  if (getTimeCompression() >= .1)
80  if (gametime <= oldgametime)
82  try {
83  BriefingLoop();
84  if (runtime.pymissions)
86  }
87  catch (...) {
88  if ( PyErr_Occurred() ) {
89  PyErr_Print();
90  PyErr_Clear();
91  fflush( stderr );
92  fflush( stdout );
93  } throw;
94  }
95 }
97 {
98  if (director == NULL)
99  return;
100  RunDirectorScript( "endgame" );
101 }
102 
104 {
105  Flightgroup *fg = unit->getFlightgroup();
106  if (fg == NULL) {
107  printf( "ship destroyed-no flightgroup\n" );
108  return;
109  }
110  if (fg->nr_ships_left <= 0 && fg->nr_waves_left > 0) {
111  printf( "WARNING: nr_ships_left<=0\n" );
112  return;
113  }
114  fg->nr_ships_left -= 1;
115 
116  char buf[512];
117 
118  if ( (fg->faction.length()+fg->type.length()+fg->name.length()+12+30) < sizeof (buf) )
119  sprintf( buf, "Ship destroyed: %s:%s:%s-%d", fg->faction.c_str(), fg->type.c_str(),
120  fg->name.c_str(), unit->getFgSubnumber() );
121  else
122  sprintf( buf, "Ship destroyed: (ERROR)-%d", unit->getFgSubnumber() );
123 
124  msgcenter->add( "game", "all", buf );
125 
126  if (fg->nr_ships_left == 0) {
127  VSFileSystem::vs_dprintf( 2, "no ships left in fg %s\n", fg->name.c_str() );
128  if (fg->nr_waves_left > 0) {
129  //sprintf( buf, "Relaunching %s wave", fg->name.c_str() ); FIXME
130  //mission->msgcenter->add( "game", "all", buf );
131  VSFileSystem::vs_dprintf( 1, "Relaunching %s wave", fg->name.c_str() );
132 
133  //launch new wave
134  fg->nr_waves_left -= 1;
135  fg->nr_ships_left = fg->nr_ships;
136 
137  Order *order = NULL;
138  order = unit->getAIState() ? unit->getAIState()->findOrderList() : NULL;
139  fg->orderlist = NULL;
140  if (order) {
141  fg->orderlist = order->getOrderList();
142  }
144  cf.fg = fg;
146  cf.fg->pos = unit->Position();
147  cf.waves = fg->nr_waves_left;
148  cf.nr_ships = fg->nr_ships;
149 
150  call_unit_launch( &cf, UNITPTR, string( "" ) );
151  } else {
152  mission->msgcenter->add( "game", "all", "Flightgroup "+fg->name+" destroyed" );
153  }
154  }
155 }
156 
158 {
159  return briefing != NULL;
160 }
162 {
163  if (briefing)
164  BriefingEnd();
165  briefing = new Briefing();
166  if (runtime.pymissions)
167  runtime.pymissions->callFunction( "initbriefing" );
168 }
170 {
171  if (briefing)
172  briefing->Update();
173 }
174 
176 {
177  if (briefing)
178  if (runtime.pymissions)
179  runtime.pymissions->callFunction( "loopbriefing" );
180 }
182 {
183  if (briefing) {
184  vector< std::string >who;
185  who.push_back( "briefing" );
186  string str1;
187  gameMessage g1, g2;
188  if ( msgcenter->last( 0, g1, who ) )
189  str1 = g1.message;
190  if ( msgcenter->last( 1, g2, who ) )
191  str1 = str1+string( "\n" )+g2.message;
192  briefing->tp.SetText( str1 );
193  briefing->Render();
194  return &briefing->tp;
195  }
196  return NULL;
197 }
198 
200 {
201  if (briefing) {
202  if (runtime.pymissions)
203  runtime.pymissions->callFunction( "endbriefing" );
204  delete briefing;
205  briefing = NULL;
206  }
207 }
208 
210 {
211  total_nr_frames++;
212  if (benchmark > 0.0 && benchmark < gametime) {
213  std::cout<<"Game was running for "<<gametime<<" secs, av. framerate "<<( (double) total_nr_frames )/gametime
214  <<std::endl;
215  winsys_exit( 0 );
216  }
217 }
218