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
|
/*
* Copyright (C) 2004-2024 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, see <https://www.gnu.org/licenses/>.
*
*/
#include "economy/wares_queue.h"
#include "base/wexception.h"
#include "economy/economy.h"
#include "economy/request.h"
#include "io/fileread.h"
#include "io/filewrite.h"
#include "logic/editor_game_base.h"
#include "logic/game.h"
#include "logic/map_objects/tribes/tribe_descr.h"
#include "logic/player.h"
#include "map_io/map_object_loader.h"
#include "map_io/map_object_saver.h"
namespace Widelands {
WaresQueue::WaresQueue(PlayerImmovable& init_owner,
DescriptionIndex const init_ware,
uint8_t const init_max_size)
: InputQueue(init_owner, init_ware, init_max_size, wwWARE) {
if (index_ != INVALID_INDEX) {
update();
}
}
void WaresQueue::cleanup() {
assert(index_ != INVALID_INDEX);
if ((filled_ != 0u) && (owner_.get_economy(wwWARE) != nullptr)) {
owner_.get_economy(wwWARE)->remove_wares_or_workers(index_, filled_);
}
filled_ = 0;
max_size_ = 0;
max_fill_ = 0;
update();
index_ = INVALID_INDEX;
}
void WaresQueue::entered(
#ifndef NDEBUG
DescriptionIndex index, Worker* worker
#else
DescriptionIndex, Worker*
#endif
) {
assert(worker == nullptr); // WaresQueue can't hold workers
assert(filled_ < max_size_);
assert(index_ == index);
// Update
set_filled(filled_ + 1);
}
void WaresQueue::remove_from_economy(Economy& e) {
if (index_ != INVALID_INDEX) {
e.remove_wares_or_workers(index_, filled_);
if (request_) {
request_->set_economy(nullptr);
}
}
}
void WaresQueue::add_to_economy(Economy& e) {
if (index_ != INVALID_INDEX) {
e.add_wares_or_workers(index_, filled_);
if (request_) {
request_->set_economy(&e);
}
}
}
void WaresQueue::set_filled(Quantity filled) {
if (filled > max_size_) {
filled = max_size_;
}
if (owner_.get_economy(wwWARE) != nullptr) {
if (filled > filled_) {
owner_.get_economy(wwWARE)->add_wares_or_workers(index_, filled - filled_);
} else if (filled < filled_) {
owner_.get_economy(wwWARE)->remove_wares_or_workers(index_, filled_ - filled);
}
}
filled_ = filled;
update();
}
/**
* Read and write
*/
constexpr uint16_t kCurrentPacketVersion = 3;
void WaresQueue::write_child(FileWrite& fw, Game& /*g*/, MapObjectSaver& /*s*/) {
fw.unsigned_16(kCurrentPacketVersion);
fw.signed_32(filled_);
}
void WaresQueue::read_child(FileRead& fr, Game& /*g*/, MapObjectLoader& /*mol*/) {
uint16_t const packet_version = fr.unsigned_16();
try {
if (packet_version == kCurrentPacketVersion) {
filled_ = fr.unsigned_32();
} else {
throw UnhandledVersionError("WaresQueue", packet_version, kCurrentPacketVersion);
}
} catch (const GameDataError& e) {
throw GameDataError("waresqueue: %s", e.what());
}
}
} // namespace Widelands
|