File: cmd_queue.cc

package info (click to toggle)
widelands 1:19+repack-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 370,608 kB
  • ctags: 20,609
  • sloc: cpp: 108,404; ansic: 18,695; python: 5,155; sh: 487; xml: 460; makefile: 233
file content (178 lines) | stat: -rw-r--r-- 4,934 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (C) 2002-2004, 2007-2008, 2015 by the Widelands Development Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include "logic/cmd_queue.h"

#include "base/macros.h"
#include "base/wexception.h"
#include "io/fileread.h"
#include "io/filewrite.h"
#include "io/machdep.h"
#include "logic/game.h"
#include "logic/game_data_error.h"
#include "logic/map_objects/map_object.h"
#include "logic/map_objects/tribes/worker.h"
#include "logic/player.h"
#include "logic/playercommand.h"

namespace Widelands {

//
// class Cmd_Queue
//
CmdQueue::CmdQueue(Game& game)
   : game_(game),
     nextserial_(0),
     ncmds_(0),
     cmds_(kCommandQueueBucketSize, std::priority_queue<CmdItem>()) {
}

CmdQueue::~CmdQueue() {
	flush();
}

/*
 * flushs all commands from the queue. Needed for
 * game loading (while in game)
 */
// TODO(unknown): ...but game loading while in game is not possible!
// Note: Order of destruction of Items is not guaranteed
void CmdQueue::flush() {
	uint32_t cbucket = 0;
	while (ncmds_ && cbucket < kCommandQueueBucketSize) {
		std::priority_queue<CmdItem>& current_cmds = cmds_[cbucket];

		while (!current_cmds.empty()) {
			Command* cmd = current_cmds.top().cmd;
			current_cmds.pop();
			delete cmd;
			--ncmds_;
		}
		++cbucket;
	}
	assert(ncmds_ == 0);
}

/*
===============
Insert a new command into the queue; it will be executed at the given time
===============
*/
void CmdQueue::enqueue(Command* const cmd) {
	CmdItem ci;

	ci.cmd = cmd;
	if (upcast(PlayerCommand, plcmd, cmd)) {
		ci.category = cat_playercommand;
		ci.serial = plcmd->cmdserial();
	} else if (dynamic_cast<GameLogicCommand*>(cmd)) {
		ci.category = cat_gamelogic;
		ci.serial = nextserial_++;
	} else {
		// the order of non-gamelogic commands matters only with respect to
		// gamelogic commands; the order of non-gamelogic commands wrt other
		// non-gamelogic commands shouldn't matter, so we can assign a
		// constant serial number.
		ci.category = cat_nongamelogic;
		ci.serial = 0;
	}

	cmds_[cmd->duetime() % kCommandQueueBucketSize].push(ci);
	++ncmds_;
}

void CmdQueue::run_queue(int32_t const interval, uint32_t& game_time_var) {
	uint32_t const final = game_time_var + interval;

	while (game_time_var < final) {
		std::priority_queue<CmdItem>& current_cmds = cmds_[game_time_var % kCommandQueueBucketSize];

		while (!current_cmds.empty()) {
			Command& c = *current_cmds.top().cmd;
			if (game_time_var < c.duetime())
				break;

			current_cmds.pop();
			--ncmds_;
			assert(game_time_var == c.duetime());

			if (dynamic_cast<GameLogicCommand*>(&c)) {
				StreamWrite& ss = game_.syncstream();
				static uint8_t const tag[] = {0xde, 0xad, 0x00};
				ss.data(tag, 3);  // provide an easy-to-find pattern as debugging aid
				ss.unsigned_32(c.duetime());
				ss.unsigned_32(static_cast<uint32_t>(c.id()));
			}

			c.execute(game_);

			delete &c;
		}
		++game_time_var;
	}

	assert(final - game_time_var == 0);
	game_time_var = final;
}

Command::~Command() {
}

constexpr uint16_t kCurrentPacketVersion = 1;

/**
 * Write variables from the base command to a file.
 *
 * \note This function must be called by deriving objects that override it.
 */
void GameLogicCommand::write(FileWrite& fw,
#ifndef NDEBUG
                             EditorGameBase& egbase,
#else
                             EditorGameBase&,
#endif
                             MapObjectSaver&) {
	fw.unsigned_16(kCurrentPacketVersion);

	// Write duetime
	assert(egbase.get_gametime() <= duetime());
	fw.unsigned_32(duetime());
}

/**
 * Read variables for the base command from a file.
 *
 * \note This function must be called by deriving objects that override it.
 */
void GameLogicCommand::read(FileRead& fr, EditorGameBase& egbase, MapObjectLoader&) {
	try {
		uint16_t const packet_version = fr.unsigned_16();
		if (packet_version == kCurrentPacketVersion) {
			set_duetime(fr.unsigned_32());
			uint32_t const gametime = egbase.get_gametime();
			if (duetime() < gametime)
				throw GameDataError("duetime (%i) < gametime (%i)", duetime(), gametime);
		} else {
			throw UnhandledVersionError("GameLogicCommand", packet_version, kCurrentPacketVersion);
		}
	} catch (const WException& e) {
		throw GameDataError("game logic: %s", e.what());
	}
}
}