~widelands-dev/widelands/trunk

« back to all changes in this revision

Viewing changes to src/logic/cmd_queue.cc

  • Committer: The Widelands Bunnybot
  • Date: 2025-02-05 18:23:09 UTC
  • Revision ID: bunnybot@widelands.org-20250205182309-2g424fbzhfwykt6y
install-dependencies.sh: Don't error on MacOS when all dependencies already installed. (CB #4983 / GH #6623)

Co-authored-by: Malte Dostal <malte.dostal@gmail.com>

(by bunnybot)
d4c9023cc7b76bdc01bb80fddee5f8fd407496eb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002-2025 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, see <https://www.gnu.org/licenses/>.
 
16
 *
 
17
 */
 
18
 
 
19
#include "logic/cmd_queue.h"
 
20
 
 
21
#include "base/macros.h"
 
22
#include "base/wexception.h"
 
23
#include "io/fileread.h"
 
24
#include "io/filewrite.h"
 
25
#include "logic/game.h"
 
26
#include "logic/game_data_error.h"
 
27
#include "logic/map_objects/tribes/worker.h"
 
28
#include "logic/player.h"
 
29
#include "logic/playercommand.h"
 
30
 
 
31
namespace Widelands {
 
32
 
 
33
//
 
34
// class Cmd_Queue
 
35
//
 
36
CmdQueue::CmdQueue(Game& game)
 
37
   : game_(game),
 
38
 
 
39
     cmds_(kCommandQueueBucketSize, std::priority_queue<CmdItem>()) {
 
40
}
 
41
 
 
42
CmdQueue::~CmdQueue() {
 
43
        flush();
 
44
}
 
45
 
 
46
/*
 
47
 * flushs all commands from the queue. Needed for
 
48
 * game loading (while in game)
 
49
 */
 
50
// TODO(unknown): ...but game loading while in game is not possible!
 
51
// Note: Order of destruction of Items is not guaranteed
 
52
void CmdQueue::flush() {
 
53
        uint32_t cbucket = 0;
 
54
        while ((ncmds_ != 0u) && cbucket < kCommandQueueBucketSize) {
 
55
                std::priority_queue<CmdItem>& current_cmds = cmds_[cbucket];
 
56
 
 
57
                while (!current_cmds.empty()) {
 
58
                        Command* cmd = current_cmds.top().cmd;
 
59
                        current_cmds.pop();
 
60
                        delete cmd;
 
61
                        --ncmds_;
 
62
                }
 
63
                ++cbucket;
 
64
        }
 
65
        assert(ncmds_ == 0);
 
66
}
 
67
 
 
68
/*
 
69
===============
 
70
Insert a new command into the queue; it will be executed at the given time
 
71
===============
 
72
*/
 
73
void CmdQueue::enqueue(Command* const cmd) {
 
74
        CmdItem ci;
 
75
 
 
76
        ci.cmd = cmd;
 
77
        if (upcast(PlayerCommand, plcmd, cmd)) {
 
78
                ci.category = cat_playercommand;
 
79
                ci.serial = plcmd->cmdserial();
 
80
        } else if (dynamic_cast<GameLogicCommand*>(cmd) != nullptr) {
 
81
                ci.category = cat_gamelogic;
 
82
                ci.serial = nextserial_++;
 
83
        } else {
 
84
                // the order of non-gamelogic commands matters only with respect to
 
85
                // gamelogic commands; the order of non-gamelogic commands wrt other
 
86
                // non-gamelogic commands shouldn't matter, so we can assign a
 
87
                // constant serial number.
 
88
                ci.category = cat_nongamelogic;
 
89
                ci.serial = 0;
 
90
        }
 
91
 
 
92
        assert(cmd->duetime() >= game_.get_gametime());
 
93
        cmds_[cmd->duetime().get() % kCommandQueueBucketSize].push(ci);
 
94
        ++ncmds_;
 
95
}
 
96
 
 
97
void CmdQueue::run_queue(const Duration& interval, Time& game_time_var) {
 
98
        const Time final_time = game_time_var + interval;
 
99
 
 
100
        while (game_time_var < final_time) {
 
101
                std::priority_queue<CmdItem>& current_cmds =
 
102
                   cmds_[game_time_var.get() % kCommandQueueBucketSize];
 
103
 
 
104
                while (!current_cmds.empty()) {
 
105
                        Command& c = *current_cmds.top().cmd;
 
106
                        if (game_time_var < c.duetime()) {
 
107
                                break;
 
108
                        }
 
109
 
 
110
                        current_cmds.pop();
 
111
                        --ncmds_;
 
112
                        assert(game_time_var == c.duetime());
 
113
 
 
114
                        if (dynamic_cast<GameLogicCommand*>(&c) != nullptr) {
 
115
                                StreamWrite& ss = game_.syncstream();
 
116
                                ss.unsigned_8(SyncEntry::kRunQueue);
 
117
                                ss.unsigned_32(c.duetime().get());
 
118
                                ss.unsigned_32(static_cast<uint32_t>(c.id()));
 
119
                        }
 
120
 
 
121
                        c.execute(game_);
 
122
 
 
123
                        delete &c;
 
124
                }
 
125
                game_time_var.increment();
 
126
        }
 
127
 
 
128
        assert(final_time == game_time_var);
 
129
}
 
130
 
 
131
constexpr uint16_t kCurrentPacketVersion = 1;
 
132
 
 
133
/**
 
134
 * Write variables from the base command to a file.
 
135
 *
 
136
 * \note This function must be called by deriving objects that override it.
 
137
 */
 
138
void GameLogicCommand::write(FileWrite& fw,
 
139
#ifndef NDEBUG
 
140
                             EditorGameBase& egbase,
 
141
#else
 
142
                             EditorGameBase&,
 
143
#endif
 
144
                             MapObjectSaver& /* mos */) {
 
145
        fw.unsigned_16(kCurrentPacketVersion);
 
146
 
 
147
        // Write duetime
 
148
        assert(egbase.get_gametime() <= duetime());
 
149
        duetime().save(fw);
 
150
}
 
151
 
 
152
/**
 
153
 * Read variables for the base command from a file.
 
154
 *
 
155
 * \note This function must be called by deriving objects that override it.
 
156
 */
 
157
void GameLogicCommand::read(FileRead& fr, EditorGameBase& egbase, MapObjectLoader& /* mol */) {
 
158
        try {
 
159
                uint16_t const packet_version = fr.unsigned_16();
 
160
                if (packet_version == kCurrentPacketVersion) {
 
161
                        set_duetime(Time(fr));
 
162
                        const Time& gametime = egbase.get_gametime();
 
163
                        if (duetime() < gametime) {
 
164
                                throw GameDataError("duetime (%u) < gametime (%u)", duetime().get(), gametime.get());
 
165
                        }
 
166
                } else {
 
167
                        throw UnhandledVersionError("GameLogicCommand", packet_version, kCurrentPacketVersion);
 
168
                }
 
169
        } catch (const WException& e) {
 
170
                throw GameDataError("game logic: %s", e.what());
 
171
        }
 
172
}
 
173
}  // namespace Widelands