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 "editor/tools/set_resources_tool.h"
21
22 #include "editor/editorinteractive.h"
23 #include "editor/tools/decrease_resources_tool.h"
24 #include "editor/tools/increase_resources_tool.h"
25 #include "logic/field.h"
26 #include "logic/map_objects/world/resource_description.h"
27 #include "logic/map_objects/world/world.h"
28 #include "logic/mapregion.h"
29
handle_click_impl(const Widelands::NodeAndTriangle<> & center,EditorInteractive & eia,EditorActionArgs * args,Widelands::Map * map)30 int32_t EditorSetResourcesTool::handle_click_impl(const Widelands::NodeAndTriangle<>& center,
31 EditorInteractive& eia,
32 EditorActionArgs* args,
33 Widelands::Map* map) {
34 const Widelands::World& world = eia.egbase().world();
35 Widelands::MapRegion<Widelands::Area<Widelands::FCoords>> mr(
36 *map, Widelands::Area<Widelands::FCoords>(map->get_fcoords(center.node), args->sel_radius));
37 do {
38 Widelands::ResourceAmount amount = args->set_to;
39 Widelands::ResourceAmount max_amount =
40 args->current_resource != Widelands::kNoResource ?
41 world.get_resource(args->current_resource)->max_amount() :
42 0;
43
44 if (amount > max_amount)
45 amount = max_amount;
46
47 if (map->is_resource_valid(world, mr.location(), args->current_resource)) {
48
49 args->original_resource.push_back(
50 EditorActionArgs::ResourceState{mr.location(), mr.location().field->get_resources(),
51 mr.location().field->get_resources_amount()});
52
53 map->initialize_resources(mr.location(), args->current_resource, amount);
54 }
55 } while (mr.advance(*map));
56 return mr.radius();
57 }
58
handle_undo_impl(const Widelands::NodeAndTriangle<Widelands::Coords> &,EditorInteractive & eia,EditorActionArgs * args,Widelands::Map * map)59 int32_t EditorSetResourcesTool::handle_undo_impl(
60 const Widelands::NodeAndTriangle<Widelands::Coords>& /* center */,
61 EditorInteractive& eia,
62 EditorActionArgs* args,
63 Widelands::Map* map) {
64 for (const auto& res : args->original_resource) {
65 Widelands::ResourceAmount amount = res.amount;
66 Widelands::ResourceAmount max_amount =
67 eia.egbase().world().get_resource(args->current_resource)->max_amount();
68
69 if (amount > max_amount) {
70 amount = max_amount;
71 }
72
73 map->initialize_resources(map->get_fcoords(res.location), res.idx, amount);
74 }
75
76 args->original_resource.clear();
77 return args->sel_radius;
78 }
79
format_args_impl(EditorInteractive & parent)80 EditorActionArgs EditorSetResourcesTool::format_args_impl(EditorInteractive& parent) {
81 EditorActionArgs a(parent);
82 a.current_resource = cur_res_;
83 a.set_to = set_to_;
84 return a;
85 }
86
resource_tools_nodecaps(const Widelands::FCoords & fcoords,const Widelands::EditorGameBase & egbase,Widelands::DescriptionIndex resource)87 Widelands::NodeCaps resource_tools_nodecaps(const Widelands::FCoords& fcoords,
88 const Widelands::EditorGameBase& egbase,
89 Widelands::DescriptionIndex resource) {
90 if (egbase.map().is_resource_valid(egbase.world(), fcoords, resource)) {
91 return fcoords.field->nodecaps();
92 }
93 return Widelands::NodeCaps::CAPS_NONE;
94 }
95