~ubuntu-branches/ubuntu/precise/widelands/precise-backports

« back to all changes in this revision

Viewing changes to src/cmd_queue.h

  • Committer: Bazaar Package Importer
  • Author(s): Martin Quinson
  • Date: 2005-02-14 10:41:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050214104112-6v08iux9fptxpva9
Tags: upstream-build9
Import upstream version build9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002-2004 by the Widelands Development Team
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 *
 
18
 */
 
19
 
 
20
#ifndef __S__CMD_QUEUE_H
 
21
#define __S__CMD_QUEUE_H
 
22
 
 
23
#include <queue>
 
24
#include "filesystem.h"
 
25
#include "queue_cmd_ids.h"
 
26
 
 
27
class FileRead;
 
28
class FileWrite;
 
29
class Editor_Game_Base;
 
30
class Widelands_Map_Map_Object_Saver;
 
31
class Widelands_Map_Map_Object_Loader;
 
32
 
 
33
// Define here all the possible users
 
34
#define SENDER_MAPOBJECT 0
 
35
#define SENDER_PLAYER1 1 // those are just place holder, a player can send commands with
 
36
#define SENDER_PLAYER2 2 // it's player number
 
37
#define SENDER_PLAYER3 3
 
38
#define SENDER_PLAYER4 4
 
39
#define SENDER_PLAYER5 5
 
40
#define SENDER_PLAYER6 6
 
41
#define SENDER_PLAYER7 7
 
42
#define SENDER_PLAYER8 8
 
43
#define SENDER_CMDQUEUE 100   // The Cmdqueue sends itself some action request
 
44
 
 
45
 
 
46
enum {
 
47
        FLAGACTION_GEOLOGIST = 0,       // call a geologist
 
48
   
 
49
   
 
50
   ENEMYFLAGACTION_ATTACK = 10,   // Performs an attack
 
51
};
 
52
// ---------------------- END    OF CMDS ----------------------------------
 
53
 
 
54
//
 
55
// This is finally the command queue. It is fully widelands specific,
 
56
// it needs to know nearly all modules.
 
57
//
 
58
class Game;
 
59
 
 
60
class BaseCommand {
 
61
        private:
 
62
                int duetime;
 
63
 
 
64
        public:
 
65
                BaseCommand (int);
 
66
                virtual ~BaseCommand ();
 
67
 
 
68
                virtual void execute (Game*)=0;
 
69
                
 
70
                int get_duetime() const { return duetime; }
 
71
                void set_duetime(int t) { duetime=t; }
 
72
      
 
73
      // Write these commands to a file (for savegames)
 
74
      virtual void Write(FileWrite*, Editor_Game_Base*, Widelands_Map_Map_Object_Saver*)=0;
 
75
      virtual void Read(FileRead*, Editor_Game_Base*, Widelands_Map_Map_Object_Loader*)=0;
 
76
 
 
77
      virtual int get_id(void) = 0; // Get this command id
 
78
      
 
79
      // Write commands for BaseCommand. Must be called from upper classes
 
80
      void BaseCmdWrite(FileWrite*, Editor_Game_Base*, Widelands_Map_Map_Object_Saver*);
 
81
      void BaseCmdRead(FileRead*, Editor_Game_Base*, Widelands_Map_Map_Object_Loader*);
 
82
 
 
83
};
 
84
 
 
85
class Cmd_Queue {
 
86
   friend class Game_Cmd_Queue_Data_Packet;
 
87
 
 
88
        struct cmditem {
 
89
                BaseCommand*    cmd;
 
90
                unsigned long   serial;
 
91
                
 
92
                bool operator< (const cmditem& c) const
 
93
                {
 
94
                        if (cmd->get_duetime()==c.cmd->get_duetime())
 
95
                                return serial > c.serial;
 
96
                        else
 
97
                                return cmd->get_duetime() > c.cmd->get_duetime();
 
98
                }
 
99
        };
 
100
 
 
101
   public:
 
102
        Cmd_Queue(Game *g);
 
103
        ~Cmd_Queue(void);
 
104
 
 
105
        void enqueue (BaseCommand*);
 
106
        int run_queue (int interval, int* game_time_var);
 
107
 
 
108
   void flush(void); // delete all commands in the queue now
 
109
 
 
110
   private:
 
111
        Game*                           m_game;
 
112
        std::priority_queue<cmditem>    m_cmds;
 
113
        unsigned long                   nextserial;
 
114
};
 
115
 
 
116
#endif // __S__CMD_QUEUE_H