2
* Copyright (C) 2002-4 by the Widelands Development Team
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20
#include "editor_increase_resources_tool.h"
23
#include "editorinteractive.h"
27
#include "overlay_manager.h"
30
=============================
32
class Editor_Increase_Resources_Tool
34
=============================
36
int Editor_Change_Resource_Tool_Callback(FCoords& f, void* data, int curres) {
37
Map* map=static_cast<Map*>(data);
40
Terrain_Descr* terr, *terd;
44
terr=f.field->get_terr();
45
terd=f.field->get_terd();
46
bool is_valid_d = curres==terd->get_default_resources() || terd->is_resource_valid(curres) ? 1 : 0;
47
bool is_valid_r = curres==terr->get_default_resources() || terr->is_resource_valid(curres) ? 1 : 0;
48
if(terd->get_is()&TERRAIN_UNPASSABLE && is_valid_d)
51
count+= curres==terd->get_default_resources() || terd->is_resource_valid(curres) ? 1 : -1;
52
if(terr->get_is()&TERRAIN_UNPASSABLE && is_valid_r)
55
count+= curres==terr->get_default_resources() || terr->is_resource_valid(curres) ? 1 : -1;
58
// If one of the neighbours is unpassable, count its resource stronger
60
map->get_neighbour(f, Map_Object::WALK_NW, &f1);
61
terr=f1.field->get_terr();
62
terd=f1.field->get_terd();
63
is_valid_d = curres==terd->get_default_resources() || terd->is_resource_valid(curres) ? 1 : 0;
64
is_valid_r = curres==terr->get_default_resources() || terr->is_resource_valid(curres) ? 1 : 0;
65
if(terd->get_is()&TERRAIN_UNPASSABLE && is_valid_d)
68
count+= curres==terd->get_default_resources() || terd->is_resource_valid(curres) ? 1 : -1;
69
if(terr->get_is()&TERRAIN_UNPASSABLE && is_valid_r)
72
count+= curres==terr->get_default_resources() || terr->is_resource_valid(curres) ? 1 : -1;
75
map->get_neighbour(f, Map_Object::WALK_NE, &f1);
76
terd=f1.field->get_terd();
77
is_valid_r = curres==terr->get_default_resources() || terr->is_resource_valid(curres) ? 1 : 0;
78
if(terd->get_is()&TERRAIN_UNPASSABLE && is_valid_r)
81
count+= curres==terd->get_default_resources() || terd->is_resource_valid(curres) ? 1 : -1;
84
map->get_neighbour(f, Map_Object::WALK_W, &f1);
85
terr=f1.field->get_terr();
86
is_valid_r = curres==terr->get_default_resources() || terr->is_resource_valid(curres) ? 1 : 0;
87
if(terr->get_is()&TERRAIN_UNPASSABLE && is_valid_r)
90
count+= curres==terr->get_default_resources() || terr->is_resource_valid(curres) ? 1 : -1;
95
return f.field->get_caps();
100
Editor_Increase_Resources_Tool::handle_click_impl()
102
decrease the resources of the current field by one if
103
there is not already another resource there.
106
int Editor_Increase_Resources_Tool::handle_click_impl(FCoords& fc, Map* map, Editor_Interactive* parent) {
107
MapRegion mrc(map, fc, parent->get_fieldsel_radius());
110
while(mrc.next(&c)) {
111
Field* f=map->get_field(c);
113
int res=f->get_resources();
114
int amount=f->get_resources_amount();
115
int max_amount=map->get_world()->get_resource(m_cur_res)->get_max_amount();
117
amount+=m_changed_by;
118
if(amount>max_amount) amount=max_amount;
121
if((res==m_cur_res || !res) && Editor_Change_Resource_Tool_Callback(c,map,m_cur_res)) {
122
// Ok, we're doing something. First remove the current overlays
124
str=map->get_world()->get_resource(res)->get_editor_pic(f->get_resources_amount());
125
int picid=g_gr->get_picture(PicMod_Menu, str.c_str(), true);
126
map->get_overlay_manager()->remove_overlay(c,picid);
129
f->set_resources(0,0);
130
f->set_starting_res_amount(0);
132
f->set_resources(m_cur_res,amount);
133
f->set_starting_res_amount(amount);
135
str=map->get_world()->get_resource(m_cur_res)->get_editor_pic(amount);
136
int picid=g_gr->get_picture(PicMod_Menu, str.c_str(), true);
137
map->get_overlay_manager()->register_overlay(c,picid,4);
138
map->recalc_for_field_area(c,0);
142
return parent->get_fieldsel_radius();