Advanced Strategic Command
campaignactionrecorder.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 #include "campaignactionrecorder.h"
22 
23 #include "dialogs/fileselector.h"
24 
25 
26 
27 void CampaignActionLogger::endTurn( Player& player )
28 {
29  gamemap->actions.breakUndo();
30  commands.push_back( "-- Ending turn " + ASCString::toString( player.getParentMap()->time.turn() ) + "\n" );
31  commands.push_back( "asc.endTurn()\n" );
32 }
33 
34 void CampaignActionLogger::mapWon( Player& player )
35 {
36  ASCString filename = selectFile( "*.lua", false );
37  if ( !filename.empty() ) {
38  tn_file_buf_stream stream ( filename, tnstream::writing );
39  for ( CommandList::const_iterator i = commands.begin(); i != commands.end(); ++i )
40  stream.writeString( *i, false );
41  }
42 }
43 
44 
45 void CampaignActionLogger::commitCommand( GameMap* map, Command& command )
46 {
47  if ( map == gamemap && map->getCurrentPlayer().isHuman() ) {
48  writer.printComment( command.getDescription() );
49  writer.printCommand( command.getCommandString() );
50  }
51 }
52 
53 CampaignActionLogger::CampaignActionLogger ( GameMap* map ) : gamemap ( map ), commands(), writer( commands )
54 {
55  gamemap->sigPlayerUserInteractionEnds.connect( sigc::mem_fun( *this, &CampaignActionLogger::endTurn ));
56  gamemap->sigMapWon.connect( sigc::mem_fun( *this, &CampaignActionLogger::mapWon ));
57  gamemap->actions.commitCommand.connect( sigc::mem_fun( *this, &CampaignActionLogger::commitCommand ));
58 }
59 
60 
62 {
63  stream.readInt();
64  int count = stream.readInt();
65  for ( int i = 0; i <count; ++i ) {
66  ASCString s;
67  stream.readTextString( s, true );
68  commands.push_back( s );
69  }
70  int check = stream.readInt();
71  if ( check != 0xbac0 )
72  throw ASCmsgException("marker not matched when loading CampaignActionLogger");
73 
74 }
75 
77 {
78  stream.writeInt(1);
79  stream.writeInt( commands.size() );
80  for ( CommandList::const_iterator i = commands.begin(); i != commands.end(); ++i ) {
81  stream.writeString( *i );
82  }
83  stream.writeInt( 0xbac0 );
84 };
85 
Player & getCurrentPlayer()
Definition: gamemap.h:268
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
const GameMap * getParentMap() const
Definition: player.h:114
sigc::signal< void, Player & > sigMapWon
Definition: gamemap.h:498
int turn() const
Definition: typen.h:181
virtual ASCString getDescription() const =0
void readData(tnstream &stream)
bool isHuman() const
Definition: player.h:152
The interface for all kinds of IO stream.
sigc::signal< void, Player & > sigPlayerUserInteractionEnds
Definition: gamemap.h:495
A Command is an action that the player initiates.
Definition: command.h:114
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
virtual void writeString(const string &pc, bool binary=true)
writes the C++ String pc to the stream.
Definition: basestrm.cpp:545
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
virtual ASCString getCommandString() const =0
void printCommand(const ASCString &command)
void breakUndo()
to be called when some modification to the map is taking place that is not recorded by undo ...
static sigc::signal< void, GameMap *, Command & > commitCommand
The command, which has already been executed, is finally commited and will not be undone any more Thi...
void writeData(tnstream &stream)
ASCString selectFile(const ASCString &ext, bool load, bool overwriteMessage)
virtual void printComment(const ASCString &comment)
ActionContainer actions
Records all action that have been done for undo/redo purposes.
Definition: gamemap.h:390
the different players in ASC. There may be 8 players (0..7) and neutral units (8) ...
Definition: player.h:99
virtual bool readTextString(ASCString &s, bool includeCR=false)
Reads a string.
Definition: basestrm.cpp:511
GameTime time
the time in the game, mesured in a turns and moves
Definition: gamemap.h:235
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182