~widelands-dev/widelands/trunk

« back to all changes in this revision

Viewing changes to src/logic/map_objects/tribes/market.cc

  • Committer: The Widelands Bunnybot
  • Date: 2025-06-03 10:08:25 UTC
  • Revision ID: bunnybot@widelands.org-20250603100825-154ypyg1m77dtv9d
Sanity-check trade batch counts and batch sizes (CB #5129 / GH #6769)

Co-authored-by: Benedikt Straub <benedikt-straub@web.de>
Co-authored-by: Widelands Bunnybot <bunnybot@widelands.org>
Co-authored-by: Benedikt Straub <nordfriese@noreply.codeberg.org>
Co-committed-by: Benedikt Straub <nordfriese@noreply.codeberg.org>

(by bunnybot)
24ab248a2b11aaa396d89c6791ac5c385be67bc6

Show diffs side-by-side

added added

removed removed

Lines of Context:
687
687
        }
688
688
}
689
689
 
 
690
std::string TradeInstance::check_illegal() const {
 
691
        if (num_batches > kMaxWaresPerBatch) {
 
692
                return format("Too many batches (found %d, limit %d)", num_batches, kMaxWaresPerBatch);
 
693
        }
 
694
        if (num_batches < 1 && num_batches != kInfiniteTrade) {
 
695
                return format("Bad number of batches %d", num_batches);
 
696
        }
 
697
 
 
698
        int32_t c = 0;
 
699
        for (const WareAmount& pair : items_to_send) {
 
700
                if (pair.second < 1 || pair.second > kMaxWaresPerBatch) {
 
701
                        return format("Sending %u items of ware %d", pair.second, pair.first);
 
702
                }
 
703
                c += pair.second;
 
704
        }
 
705
        if (c < 1) {
 
706
                return "Not sending any items";
 
707
        }
 
708
        if (c > kMaxWaresPerBatch) {
 
709
                return format("Sending too many items (found %d, limit %d)", c, kMaxWaresPerBatch);
 
710
        }
 
711
 
 
712
        c = 0;
 
713
        for (const WareAmount& pair : items_to_receive) {
 
714
                if (pair.second < 1 || pair.second > kMaxWaresPerBatch) {
 
715
                        return format("Receiving %u items of ware %d", pair.second, pair.first);
 
716
                }
 
717
                c += pair.second;
 
718
        }
 
719
        if (c < 1) {
 
720
                return "Not receiving any items";
 
721
        }
 
722
        if (c > kMaxWaresPerBatch) {
 
723
                return format("Receiving too many items (found %d, limit %d)", c, kMaxWaresPerBatch);
 
724
        }
 
725
 
 
726
        return std::string();
 
727
}
 
728
 
690
729
std::string TradeInstance::format_richtext(const TradeID id,
691
730
                                           const EditorGameBase& egbase,
692
731
                                           const PlayerNumber iplayer,