1 /*
2  * Copyright (C) 2002-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/tribes/ware_descr.h"
21 
22 #include <memory>
23 
24 #include "base/i18n.h"
25 #include "logic/game_data_error.h"
26 #include "logic/map_objects/tribes/tribe_descr.h"
27 
28 namespace Widelands {
29 
30 /**
31  * The contents of 'table' are documented in
32  * /data/tribes/wares/armor/init.lua
33  */
WareDescr(const std::string & init_descname,const LuaTable & table)34 WareDescr::WareDescr(const std::string& init_descname, const LuaTable& table)
35    : MapObjectDescr(MapObjectType::WARE, table.get_string("name"), init_descname, table),
36      ai_hints_(new WareHints(table.get_string("name"), *table.get_table("preciousness"))) {
37 	if (helptext_script().empty()) {
38 		throw GameDataError("Ware %s has no helptext script", name().c_str());
39 	}
40 	if (!is_animation_known("idle")) {
41 		throw GameDataError("Ware %s has no idle animation", name().c_str());
42 	}
43 	if (icon_filename().empty()) {
44 		throw GameDataError("Ware %s has no menu icon", name().c_str());
45 	}
46 	i18n::Textdomain td("tribes");
47 
48 	std::unique_ptr<LuaTable> items_table = table.get_table("default_target_quantity");
49 	for (const std::string& key : items_table->keys<std::string>()) {
50 		default_target_quantities_.emplace(key, items_table->get_int(key));
51 	}
52 }
53 
default_target_quantity(const std::string & tribename) const54 DescriptionIndex WareDescr::default_target_quantity(const std::string& tribename) const {
55 	if (default_target_quantities_.count(tribename) > 0) {
56 		return default_target_quantities_.at(tribename);
57 	}
58 	return kInvalidWare;
59 }
60 
has_demand_check(const std::string & tribename) const61 bool WareDescr::has_demand_check(const std::string& tribename) const {
62 	return (default_target_quantity(tribename) != 0 &&
63 	        default_target_quantity(tribename) != kInvalidWare);
64 }
65 
set_has_demand_check(const std::string & tribename)66 void WareDescr::set_has_demand_check(const std::string& tribename) {
67 	if (default_target_quantities_.count(tribename) > 0 &&
68 	    default_target_quantities_.at(tribename) == kInvalidWare) {
69 		default_target_quantities_.at(tribename) = 0;
70 	}
71 }
72 
add_consumer(const DescriptionIndex & building_index)73 void WareDescr::add_consumer(const DescriptionIndex& building_index) {
74 	consumers_.insert(building_index);
75 }
76 
add_producer(const DescriptionIndex & building_index)77 void WareDescr::add_producer(const DescriptionIndex& building_index) {
78 	producers_.insert(building_index);
79 }
80 
consumers() const81 const std::set<DescriptionIndex>& WareDescr::consumers() const {
82 	return consumers_;
83 }
84 
producers() const85 const std::set<DescriptionIndex>& WareDescr::producers() const {
86 	return producers_;
87 }
88 }  // namespace Widelands
89