1 /*
2 * Copyright (C) 2010-2020 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 */
19
20 #include "logic/map_objects/buildcost.h"
21
22 #include <memory>
23
24 #include "base/wexception.h"
25 #include "logic/game_data_error.h"
26 #include "logic/map_objects/tribes/tribe_descr.h"
27 #include "logic/map_objects/tribes/tribes.h"
28
29 namespace Widelands {
30
Buildcost()31 Buildcost::Buildcost() : std::map<DescriptionIndex, uint8_t>() {
32 }
33
Buildcost(std::unique_ptr<LuaTable> table,const Tribes & tribes)34 Buildcost::Buildcost(std::unique_ptr<LuaTable> table, const Tribes& tribes)
35 : std::map<DescriptionIndex, uint8_t>() {
36 for (const std::string& warename : table->keys<std::string>()) {
37 // Check ware name
38 if (!tribes.ware_exists(warename)) {
39 throw GameDataError("Buildcost: Unknown ware: %s", warename.c_str());
40 }
41
42 // Read value
43 const int32_t value = table->get_int(warename);
44 if (value < 1) {
45 throw GameDataError("Buildcost: Ware count needs to be > 0 in \"%s=%d\".\nEmpty buildcost "
46 "tables are allowed if you wish to have an amount of 0.",
47 warename.c_str(), value);
48 } else if (value > 255) {
49 throw GameDataError(
50 "Buildcost: Ware count needs to be <= 255 in \"%s=%d\".", warename.c_str(), value);
51 }
52
53 // Add
54 insert(std::pair<DescriptionIndex, uint8_t>(tribes.safe_ware_index(warename), value));
55 }
56 }
57
58 /**
59 * Compute the total buildcost.
60 */
total() const61 Widelands::Quantity Buildcost::total() const {
62 Widelands::Quantity sum = 0;
63 for (const_iterator it = begin(); it != end(); ++it) {
64 sum += it->second;
65 }
66 return sum;
67 }
68
save(FileWrite & fw,const Widelands::TribeDescr & tribe) const69 void Buildcost::save(FileWrite& fw, const Widelands::TribeDescr& tribe) const {
70 for (const_iterator it = begin(); it != end(); ++it) {
71 fw.c_string(tribe.get_ware_descr(it->first)->name());
72 fw.unsigned_8(it->second);
73 }
74 fw.c_string("");
75 }
76
load(FileRead & fr,const Widelands::TribeDescr & tribe)77 void Buildcost::load(FileRead& fr, const Widelands::TribeDescr& tribe) {
78 clear();
79
80 for (;;) {
81 std::string name = fr.c_string();
82 if (name.empty()) {
83 break;
84 }
85
86 DescriptionIndex index = tribe.ware_index(name);
87 if (!tribe.has_ware(index)) {
88 log("buildcost: tribe %s does not define ware %s", tribe.name().c_str(), name.c_str());
89 fr.unsigned_8();
90 } else {
91 (*this)[index] = fr.unsigned_8();
92 }
93 }
94 }
95
96 } // namespace Widelands
97