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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
|
/*
* Copyright (C) 2006-2025 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 "logic/map_objects/tribes/market.h"
#include <memory>
#include "base/i18n.h"
#include "economy/flag.h"
#include "graphic/style_manager.h"
#include "graphic/text_layout.h"
#include "logic/map_objects/descriptions.h"
#include "logic/map_objects/tribes/productionsite.h"
#include "logic/player.h"
namespace Widelands {
MarketDescr::MarketDescr(const std::string& init_descname,
const LuaTable& table,
Descriptions& descriptions)
: BuildingDescr(init_descname, MapObjectType::MARKET, table, descriptions),
local_carrier(descriptions.worker_index(table.get_string("local_carrier"))),
trade_carrier(descriptions.worker_index(table.get_string("trade_carrier"))) {
}
Building& MarketDescr::create_object() const {
return *new Market(*this);
}
int Market::TradeOrder::num_wares_per_batch() const {
int sum = 0;
for (const auto& item_pair : items) {
sum += item_pair.second;
}
return sum;
}
bool Market::TradeOrder::fulfilled() const {
return num_shipped_batches == initial_num_batches;
}
Market::Market(const MarketDescr& the_descr) : Building(the_descr) {
}
bool Market::init(EditorGameBase& egbase) {
Building::init(egbase);
market_name_ = get_owner()->pick_warehousename(Player::WarehouseNameType::kMarket);
carrier_request_.reset(
new Request(*this, descr().local_carrier, Market::carrier_callback, wwWORKER));
if (upcast(Game, game, &egbase); game != nullptr) {
send_message(*game, Message::Type::kTrading, descr().descname(), descr().icon_filename(),
descr().descname(), _("A new market was added to your economy."), true);
}
return true;
}
void Market::cleanup(EditorGameBase& egbase) {
if (upcast(Game, game, &egbase); game != nullptr) {
std::vector<TradeID> to_cancel;
std::vector<TradeID> to_retract;
for (const auto& pair : game->all_trade_agreements()) {
if (pair.second.initiator == this || pair.second.receiver == this) {
if (pair.second.state == TradeInstance::State::kProposed) {
to_retract.push_back(pair.first);
} else {
to_cancel.push_back(pair.first);
}
}
}
for (TradeID id : to_cancel) {
game->cancel_trade(id, false, get_owner());
}
for (TradeID id : to_retract) {
game->retract_trade(id);
}
}
assert(trade_orders_.empty());
if (Worker* carrier = carrier_.get(egbase); carrier != nullptr) {
carrier->set_location(nullptr);
}
Building::cleanup(egbase);
}
void Market::TradeOrder::cleanup() {
upcast(Game, game, &market->get_owner()->egbase());
while (carriers_queue_->get_filled() > 0) {
Worker* worker = carriers_queue_->extract_worker();
if (worker->get_state(Worker::taskCarryTradeItem) != nullptr) {
worker->send_signal(*game, "trade");
} else {
worker->set_location(&market->base_flag());
worker->reset_tasks(*game);
worker->start_task_return(*game, true);
}
}
carriers_queue_->cleanup();
for (auto& pair : wares_queues_) {
for (int i = pair.second->get_filled(); i > 0; --i) {
market->pending_dropout_wares_.push_back(pair.second->get_index());
market->get_economy(wwWARE)->add_wares_or_workers(pair.second->get_index(), 1);
}
pair.second->cleanup();
}
if (Worker* carrier = market->carrier_.get(*game); carrier != nullptr) {
carrier->update_task_buildingwork(*game);
}
}
void Market::kickout_worker_from_queue(Game& game, Worker& worker) {
if (worker.get_state(Worker::taskCarryTradeItem) != nullptr) {
worker.send_signal(game, "trade");
} else {
Building::kickout_worker_from_queue(game, worker);
}
}
std::string Market::market_census_string() const {
static const std::string fmt = "⇵ %s ⇵";
return named_building_census_string(fmt, get_market_name());
}
void Market::update_statistics_string(std::string* str) {
const unsigned nr_trades = trade_orders_.size();
*str = StyleManager::color_tag(format(ngettext("%u trade", "%u trades", nr_trades), nr_trades),
g_style_manager->building_statistics_style().neutral_color());
}
void Market::remove_worker(Worker& worker) {
Building::remove_worker(worker);
if (carrier_.serial() == worker.serial()) {
carrier_ = nullptr;
return;
}
for (auto& pair : trade_orders_) {
if (pair.second->carriers_queue_->remove_if_present(worker)) {
return;
}
}
}
void Market::set_economy(Economy* const e, WareWorker type) {
Economy* const old = get_economy(type);
if (old == e) {
return;
}
Building::set_economy(e, type);
if (Worker* carrier = carrier_.get(get_owner()->egbase()); carrier != nullptr) {
carrier->set_economy(e, type);
}
if (type == wwWARE) {
for (DescriptionIndex ware : pending_dropout_wares_) {
if (old != nullptr) {
old->remove_wares_or_workers(ware, 1);
}
if (e != nullptr) {
e->add_wares_or_workers(ware, 1);
}
}
} else {
if (carrier_request_ != nullptr) {
carrier_request_->set_economy(e);
}
}
for (auto& pair : trade_orders_) {
if (type == wwWARE) {
if (old != nullptr) {
for (auto& q : pair.second->wares_queues_) {
q.second->remove_from_economy(*old);
}
}
if (e != nullptr) {
for (auto& q : pair.second->wares_queues_) {
q.second->add_to_economy(*e);
}
}
} else {
if (old != nullptr) {
pair.second->carriers_queue_->remove_from_economy(*old);
}
if (e != nullptr) {
pair.second->carriers_queue_->add_to_economy(*old);
}
}
}
}
void Market::set_market_name(const std::string& name) {
market_name_ = name;
get_owner()->reserve_warehousename(name);
}
void Market::notify_trade_extended(const TradeID trade_id, const int new_total_batches) {
trade_orders_.at(trade_id)->initial_num_batches = new_total_batches;
}
void Market::new_trade(const TradeID trade_id,
const BillOfMaterials& items,
const int32_t num_batches,
OPtr<Market> other_side) {
MutexLock m(MutexLock::ID::kObjects);
assert(trade_orders_.count(trade_id) == 0);
std::unique_ptr<TradeOrder>& trade_order = trade_orders_[trade_id];
trade_order.reset(new TradeOrder);
trade_order->market = this;
trade_order->items = items;
trade_order->initial_num_batches = num_batches;
trade_order->other_side = other_side;
trade_order->carriers_queue_.reset(
new WorkersQueue(*this, descr().trade_carrier, trade_order->num_wares_per_batch()));
trade_order->carriers_queue_->set_callback(Market::ware_arrived_callback, this);
for (const auto& entry : items) {
auto& queue = trade_order->wares_queues_[entry.first];
queue.reset(new WaresQueue(*this, entry.first, entry.second));
queue->set_callback(Market::ware_arrived_callback, this);
}
molog(owner().egbase().get_gametime(), "Enqueuing new trade #%u with %d batches to %u", trade_id,
num_batches, other_side.serial());
Notifications::publish(NoteBuilding(serial(), NoteBuilding::Action::kChanged));
}
void Market::cancel_trade(Game& game,
const TradeID trade_id,
const bool reached_regular_end,
const bool send_msg) {
MutexLock m(MutexLock::ID::kObjects);
if (auto it = trade_orders_.find(trade_id); it != trade_orders_.end()) {
molog(owner().egbase().get_gametime(),
reached_regular_end ? "Completed trade #%u" : "Cancelling trade #%u", trade_id);
if (send_msg) {
send_message(
game, Message::Type::kTrading,
reached_regular_end ? _("Trade Complete") : _("Trade Cancelled"),
descr().icon_filename(),
reached_regular_end ? _("Trade agreement complete") : _("Trade agreement cancelled"),
format(reached_regular_end ? _("Your trade agreement at %s has been completed.") :
_("Your trade agreement at %s has been cancelled."),
get_market_name()),
false);
}
it->second->cleanup();
trade_orders_.erase(trade_id);
Notifications::publish(NoteBuilding(serial(), NoteBuilding::Action::kChanged));
} else {
molog(owner().egbase().get_gametime(), "cancel_trade: trade #%u not found", trade_id);
}
}
void Market::move_trade_to(Game& game, const TradeID trade_id, Market& dest) {
const auto it = trade_orders_.find(trade_id);
if (it == trade_orders_.end()) {
return; // Trade no longer exists
}
Market* other_market = it->second->other_side.get(game);
if (other_market == nullptr) {
return; // Other side no longer exists
}
const auto other_it = other_market->trade_orders_.find(trade_id);
if (other_it == other_market->trade_orders_.end() ||
other_it->second->other_side.serial() != serial()) {
return; // Trade no longer exists
}
// Clear the old queues and dismiss the trade's carriers
it->second->cleanup();
// Move the trade
assert(dest.trade_orders_.count(trade_id) == 0);
TradeOrder* order = it->second.release();
trade_orders_.erase(it);
order->market = &dest;
dest.trade_orders_.emplace(trade_id, order);
assert(dest.trade_orders_.count(trade_id) == 1);
other_it->second->other_side = &dest;
// Create the new queues
order->carriers_queue_.reset(
new WorkersQueue(dest, descr().trade_carrier, order->num_wares_per_batch()));
order->carriers_queue_->set_callback(Market::ware_arrived_callback, &dest);
for (const auto& entry : order->items) {
auto& queue = order->wares_queues_[entry.first];
queue.reset(new WaresQueue(dest, entry.first, entry.second));
queue->set_callback(Market::ware_arrived_callback, &dest);
}
}
void Market::carrier_callback(Game& game,
Request& /* rq */,
DescriptionIndex /* widx */,
Worker* carrier,
PlayerImmovable& target) {
Market& market = dynamic_cast<Market&>(target);
market.carrier_request_.reset();
market.carrier_ = carrier;
assert(carrier != nullptr);
assert(carrier->get_location(game) == &market);
carrier->reset_tasks(game);
carrier->start_task_buildingwork(game);
}
void Market::ware_arrived_callback(Game& g,
InputQueue* /* queue */,
DescriptionIndex /* index */,
Worker* /* worker */,
void* data) {
Market& market = *static_cast<Market*>(data);
market.try_launching_batch(&g);
}
void Market::try_launching_batch(Game* game) {
for (auto& pair : trade_orders_) {
if (!is_ready_to_launch_batch(pair.first)) {
continue;
}
Market* other_market = pair.second->other_side.get(*game);
if (other_market == nullptr) {
// TODO(sirver,trading): Can this even happen? Where is this function called from?
// The other market seems to have vanished. The game tracks this and
// should soon delete this trade request from us. We just ignore it.
continue;
}
if (!other_market->is_ready_to_launch_batch(pair.first)) {
continue;
}
launch_batch(pair.first, game);
other_market->launch_batch(pair.first, game);
break;
}
}
bool Market::is_ready_to_launch_batch(const TradeID trade_id) const {
const auto it = trade_orders_.find(trade_id);
if (it == trade_orders_.end()) {
return false;
}
const TradeOrder& trade_order = *it->second;
assert(!trade_order.fulfilled());
if (trade_order.paused) {
return false;
}
// Do we have all necessary carriers and wares for a batch?
if (static_cast<int>(trade_order.carriers_queue_->get_filled()) <
trade_order.num_wares_per_batch()) {
return false;
}
for (Worker* carrier : trade_order.carriers_queue_->workers_in_queue()) {
if (carrier->get_position() != get_position() || !carrier->is_idle()) {
return false;
}
}
return std::all_of(trade_order.items.begin(), trade_order.items.end(),
[&trade_order](const WareAmount& ware_amount) {
const auto wares_it = trade_order.wares_queues_.find(ware_amount.first);
return wares_it != trade_order.wares_queues_.end() &&
wares_it->second->get_filled() >= ware_amount.second;
});
}
void Market::launch_batch(const TradeID trade_id, Game* game) {
assert(is_ready_to_launch_batch(trade_id));
TradeOrder& trade_order = *trade_orders_.at(trade_id);
molog(game->get_gametime(), "Launching batch for trade #%u", trade_id);
// Do we have all necessary wares for a batch?
Worker** next_carrier = trade_order.carriers_queue_->workers_in_queue().data();
for (const auto& item_pair : trade_order.items) {
for (size_t i = 0; i < item_pair.second; ++i) {
Worker* carrier = *next_carrier++;
// Give the carrier a ware.
WareInstance* ware =
new WareInstance(item_pair.first, game->descriptions().get_ware_descr(item_pair.first));
ware->init(*game);
carrier->set_carried_ware(*game, ware);
// We have to remove this item from our economy. Otherwise it would be
// considered idle (since it has no transport associated with it) and
// the engine would want to transfer it to the next warehouse.
ware->set_economy(nullptr);
trade_order.wares_queues_.at(item_pair.first)
->set_filled(trade_order.wares_queues_.at(item_pair.first)->get_filled() - 1);
// Send the carrier going.
carrier->reset_tasks(*game);
carrier->start_task_carry_trade_item(*game, trade_id, trade_order.other_side.get(*game));
}
}
}
InputQueue& Market::inputqueue(const DescriptionIndex index,
const WareWorker ware_worker,
const Request* r,
const uint32_t disambiguator_id) {
std::pair<InputQueue*, TradeID> pair = find_inputqueue(index, ware_worker, r, disambiguator_id);
if (pair.first != nullptr) {
return *pair.first;
}
// The parent will throw an exception.
return Building::inputqueue(index, ware_worker, r, disambiguator_id);
}
std::pair<InputQueue*, TradeID> Market::find_inputqueue(const DescriptionIndex index,
const WareWorker ware_worker,
const Request* r,
const uint32_t disambiguator_id) {
if (r != nullptr) {
for (auto& pair : trade_orders_) {
if (ware_worker == wwWARE) {
auto it = pair.second->wares_queues_.find(index);
if (it != pair.second->wares_queues_.end() && it->second->matches(*r)) {
assert(it->second->get_index() == index);
return {it->second.get(), pair.first};
}
} else if (pair.second->carriers_queue_->matches(*r)) {
return {pair.second->carriers_queue_.get(), pair.first};
}
}
} else {
if (auto pair = trade_orders_.find(disambiguator_id); pair != trade_orders_.end()) {
if (ware_worker == wwWARE) {
auto it = pair->second->wares_queues_.find(index);
if (it != pair->second->wares_queues_.end()) {
assert(it->second->get_index() == index);
return {it->second.get(), pair->first};
}
} else if (pair->second->carriers_queue_->get_index() == index) {
return {pair->second->carriers_queue_.get(), pair->first};
}
}
}
return {nullptr, kInvalidTrade};
}
bool Market::can_change_max_fill(const DescriptionIndex index,
const WareWorker ware_worker,
const Request* r,
const uint32_t disambiguator_id) {
const std::pair<InputQueue*, TradeID> pair =
find_inputqueue(index, ware_worker, r, disambiguator_id);
const auto order = trade_orders_.find(pair.second);
return order != trade_orders_.end() && order->second->paused &&
Building::can_change_max_fill(index, ware_worker, r, disambiguator_id);
}
uint32_t Market::get_priority_disambiguator_id(const Request* req) const {
if (req != nullptr) {
for (const auto& pair : trade_orders_) {
for (const auto& queue : pair.second->wares_queues_) {
if (queue.second->matches(*req)) {
return pair.first;
}
}
if (pair.second->carriers_queue_->matches(*req)) {
return pair.first;
}
}
}
return Building::get_priority_disambiguator_id(req);
}
bool Market::is_paused(const TradeID id) const {
const auto trade_order = trade_orders_.find(id);
return trade_order != trade_orders_.end() && trade_order->second->paused;
}
bool Market::can_resume(const TradeID id) const {
if (!is_paused(id)) {
return false;
}
const TradeOrder& order = *trade_orders_.at(id);
for (const auto& pair : order.wares_queues_) {
if (pair.second->get_max_fill() < pair.second->get_max_size()) {
return false;
}
}
return order.carriers_queue_->get_max_fill() >= order.carriers_queue_->get_max_size();
}
void Market::set_paused(Game& game, const TradeID id, const bool pause) {
auto trade_order = trade_orders_.find(id);
if (trade_order == trade_orders_.end() || trade_order->second->paused == pause) {
return;
}
if (!pause && !can_resume(id)) {
molog(game.get_gametime(), "Attempt to resume but cannot resume");
return;
}
trade_order->second->paused = pause;
if (Market* other = trade_order->second->other_side.get(game); other != nullptr) {
if (pause) {
other->send_message(game, Message::Type::kTrading, _("Trade Paused"),
other->descr().icon_filename(), _("Trade agreement paused"),
format_l(_("%1$s paused the trade with you at %2$s."),
other->owner().get_name(), other->get_market_name()),
true);
} else {
other->send_message(game, Message::Type::kTrading, _("Trade Resumed"),
other->descr().icon_filename(), _("Trade agreement resumed"),
format_l(_("%1$s resumed the trade with you at %2$s."),
other->owner().get_name(), other->get_market_name()),
true);
}
}
Notifications::publish(NoteTradeChanged(
id, pause ? NoteTradeChanged::Action::kPaused : NoteTradeChanged::Action::kUnpaused));
if (!pause) {
try_launching_batch(&game);
}
}
InputQueue* Market::find_overfull_input_queue() {
for (auto& order : trade_orders_) {
for (auto& pair : order.second->wares_queues_) {
if (pair.second->get_type() == wwWARE &&
pair.second->get_filled() > pair.second->get_max_fill()) {
return pair.second.get();
}
}
}
return nullptr;
}
void Market::inputqueue_max_fill_changed() {
Building::inputqueue_max_fill_changed();
if (find_overfull_input_queue() != nullptr) {
if (Worker* carrier = carrier_.get(get_owner()->egbase()); carrier != nullptr) {
carrier->update_task_buildingwork(dynamic_cast<Game&>(get_owner()->egbase()));
}
}
}
bool Market::fetch_from_flag(Game& game) {
++fetchfromflag_;
if (Worker* carrier = carrier_.get(game); carrier != nullptr) {
carrier->update_task_buildingwork(game);
}
return true;
}
bool Market::get_building_work(Game& game, Worker& worker, bool /* success */) {
if (fetchfromflag_ > 0) {
--fetchfromflag_;
worker.start_task_fetchfromflag(game);
return true;
}
if (!pending_dropout_wares_.empty()) {
get_economy(wwWARE)->remove_wares_or_workers(pending_dropout_wares_.front(), 1);
WareInstance& ware =
*new WareInstance(pending_dropout_wares_.front(),
owner().tribe().get_ware_descr(pending_dropout_wares_.front()));
ware.init(game);
worker.start_task_dropoff(game, ware);
pending_dropout_wares_.pop_front();
return true;
}
if (InputQueue* queue = find_overfull_input_queue(); queue != nullptr) {
queue->set_filled(queue->get_filled() - 1);
const WareDescr& wd = *owner().tribe().get_ware_descr(queue->get_index());
WareInstance& ware = *new WareInstance(queue->get_index(), &wd);
ware.init(game);
worker.start_task_dropoff(game, ware);
return true;
}
return false;
}
void Market::traded_ware_arrived(const TradeID trade_id,
const DescriptionIndex ware_index,
Game* game) {
auto trade_order = trade_orders_.find(trade_id);
if (trade_order != trade_orders_.end()) {
++trade_order->second->received_traded_wares_in_this_batch;
}
pending_dropout_wares_.push_back(ware_index);
get_economy(wwWARE)->add_wares_or_workers(ware_index, 1);
get_owner()->ware_produced(ware_index);
if (Worker* carrier = carrier_.get(*game); carrier != nullptr) {
carrier->update_task_buildingwork(*game);
}
if (trade_order != trade_orders_.end()) {
if (Market* other_market = trade_order->second->other_side.get(*game);
other_market != nullptr) {
other_market->get_owner()->ware_consumed(ware_index, 1);
auto other_trade_order = other_market->trade_orders_.find(trade_id);
if (other_trade_order != other_market->trade_orders_.end() &&
trade_order->second->received_traded_wares_in_this_batch ==
other_trade_order->second->num_wares_per_batch() &&
other_trade_order->second->received_traded_wares_in_this_batch ==
trade_order->second->num_wares_per_batch()) {
// This batch is completed.
++trade_order->second->num_shipped_batches;
trade_order->second->received_traded_wares_in_this_batch = 0;
++other_trade_order->second->num_shipped_batches;
other_trade_order->second->received_traded_wares_in_this_batch = 0;
if (trade_order->second->fulfilled()) {
assert(other_trade_order->second->fulfilled());
game->cancel_trade(trade_id, true, get_owner());
}
}
}
}
Notifications::publish(NoteTradeChanged(trade_id, NoteTradeChanged::Action::kWareArrived));
}
void Market::log_general_info(const EditorGameBase& egbase) const {
molog(egbase.get_gametime(), "Market '%s'", market_name_.c_str());
Building::log_general_info(egbase);
molog(egbase.get_gametime(), "%" PRIuS " trade orders", trade_orders_.size());
for (const auto& pair : trade_orders_) {
molog(egbase.get_gametime(), " - #%6u: %3d/%3d to %6u, received %4d", pair.first,
pair.second->num_shipped_batches, pair.second->initial_num_batches,
pair.second->other_side.serial(), pair.second->received_traded_wares_in_this_batch);
for (const auto& ware_amount : pair.second->items) {
molog(egbase.get_gametime(), " - %3u x %s", ware_amount.second,
owner().tribe().get_ware_descr(ware_amount.first)->name().c_str());
}
}
}
std::string TradeInstance::check_illegal() const {
if (num_batches > kMaxWaresPerBatch) {
return format("Too many batches (found %d, limit %d)", num_batches, kMaxWaresPerBatch);
}
if (num_batches < 1 && num_batches != kInfiniteTrade) {
return format("Bad number of batches %d", num_batches);
}
int32_t c = 0;
for (const WareAmount& pair : items_to_send) {
if (pair.second < 1 || pair.second > kMaxWaresPerBatch) {
return format("Sending %u items of ware %d", pair.second, pair.first);
}
c += pair.second;
}
if (c < 1) {
return "Not sending any items";
}
if (c > kMaxWaresPerBatch) {
return format("Sending too many items (found %d, limit %d)", c, kMaxWaresPerBatch);
}
c = 0;
for (const WareAmount& pair : items_to_receive) {
if (pair.second < 1 || pair.second > kMaxWaresPerBatch) {
return format("Receiving %u items of ware %d", pair.second, pair.first);
}
c += pair.second;
}
if (c < 1) {
return "Not receiving any items";
}
if (c > kMaxWaresPerBatch) {
return format("Receiving too many items (found %d, limit %d)", c, kMaxWaresPerBatch);
}
return std::string();
}
std::string TradeInstance::format_richtext(const TradeID id,
const EditorGameBase& egbase,
const PlayerNumber iplayer,
const bool can_act) const {
constexpr int16_t kSpacing = 4;
assert((iplayer == receiving_player) ^ (iplayer == sending_player));
const bool is_receiver = iplayer == receiving_player;
Widelands::Market* own_market = initiator.get(egbase);
Widelands::Market* other_market = receiver.get(egbase);
if (is_receiver) {
std::swap(own_market, other_market);
}
std::string infotext("<rt><p>");
if (state == State::kRunning) { // Active trade
if (own_market == nullptr || other_market == nullptr) {
return std::string();
}
infotext += as_font_tag(
UI::FontStyle::kWuiInfoPanelHeading,
format_l(
/** TRANSLATORS: "At" is the market's name, and "with" is the receiving player's name */
_("Trade agreement at %1$s with %2$s"), own_market->get_market_name(),
other_market->owner().get_name()));
} else if (is_receiver) { // Offered trade
if (other_market == nullptr) {
return std::string();
}
infotext += as_font_tag(UI::FontStyle::kWuiInfoPanelHeading,
format_l(_("Trade offer from %s"), other_market->owner().get_name()));
} else { // Proposed trade
if (own_market == nullptr) {
return std::string();
}
infotext += as_font_tag(
UI::FontStyle::kWuiInfoPanelHeading,
format_l(
/** TRANSLATORS: "From" is the market's name, and "to" is the receiving player's name */
_("Trade offer from %1$s to %2$s"), own_market->get_market_name(),
egbase.player(receiving_player).get_name()));
}
infotext += format("</p><div padding_l=%d padding_b=%d><p>",
g_style_manager->styled_size(UI::StyledSize::kUIDefaultIndent),
g_style_manager->styled_size(UI::StyledSize::kWuiTextDefaultGap));
if (num_batches != kInfiniteTrade) {
infotext +=
as_font_tag(UI::FontStyle::kWuiInfoPanelParagraph,
format_l(ngettext("%d batch", "%d batches", num_batches), num_batches));
} else {
infotext += as_font_tag(UI::FontStyle::kWuiInfoPanelParagraph, _("Trade runs indefinitely"));
}
if (state == State::kRunning) {
const auto trade = own_market->trade_orders().find(id);
if (trade == own_market->trade_orders().end()) {
return std::string();
}
infotext += "</p><p>";
infotext += as_font_tag(UI::FontStyle::kWuiInfoPanelParagraph,
format_l(ngettext("%d batch delivered", "%d batches delivered",
trade->second->num_shipped_batches),
trade->second->num_shipped_batches));
if (num_batches != kInfiniteTrade) {
infotext += "</p><p>";
infotext +=
as_font_tag(UI::FontStyle::kWuiInfoPanelParagraph,
format_l(ngettext("%d batch remaining", "%d batches remaining",
num_batches - trade->second->num_shipped_batches),
num_batches - trade->second->num_shipped_batches));
}
bool other_paused = false;
if (const auto other_trade = other_market->trade_orders().find(id);
other_trade != other_market->trade_orders().end()) {
other_paused = other_trade->second->paused;
}
std::string paused_text;
if (trade->second->paused) {
if (other_paused) {
paused_text = _("Paused by both players");
} else {
paused_text = can_act ? _("Paused by you") :
format_l(_("Paused by %s"), own_market->owner().get_name());
}
} else {
if (other_paused) {
paused_text = format_l(_("Paused by %s"), other_market->owner().get_name());
} else {
paused_text = _("Active");
}
}
infotext += "</p><p>";
infotext += as_font_tag(UI::FontStyle::kWuiInfoPanelParagraph, paused_text);
}
infotext += "</p>";
infotext += as_vspace(kSpacing);
infotext += "<p>";
infotext += as_font_tag(
UI::FontStyle::kWuiInfoPanelHeading, can_act ? _("You send:") : _("Player sends:"));
for (const auto& pair : is_receiver ? items_to_receive : items_to_send) {
infotext +=
as_listitem(format_l(_("%1$i× %2$s"), pair.second,
egbase.descriptions().get_ware_descr(pair.first)->descname()),
UI::FontStyle::kWuiInfoPanelParagraph);
}
infotext += "</p>";
infotext += as_vspace(kSpacing);
infotext += "<p>";
infotext += as_font_tag(
UI::FontStyle::kWuiInfoPanelHeading, can_act ? _("You receive:") : _("Player receives:"));
for (const auto& pair : is_receiver ? items_to_send : items_to_receive) {
infotext +=
as_listitem(format_l(_("%1$i× %2$s"), pair.second,
egbase.descriptions().get_ware_descr(pair.first)->descname()),
UI::FontStyle::kWuiInfoPanelParagraph);
}
infotext += "</p></div></rt>";
return infotext;
}
} // namespace Widelands
|