~ubuntu-branches/ubuntu/precise/widelands/precise-backports

« back to all changes in this revision

Viewing changes to src/editor/tools/editor_increase_resources_tool.cc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Quinson
  • Date: 2005-02-14 10:41:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050214104112-6v08iux9fptxpva9
Tags: upstream-build9
Import upstream version build9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002-4 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 *
 
18
 */
 
19
 
 
20
#include "editor_increase_resources_tool.h"
 
21
#include "map.h"
 
22
#include "field.h"
 
23
#include "editorinteractive.h"
 
24
#include "world.h"
 
25
#include "map.h"
 
26
#include "error.h"
 
27
#include "overlay_manager.h"
 
28
 
 
29
/*
 
30
=============================
 
31
 
 
32
class Editor_Increase_Resources_Tool
 
33
 
 
34
=============================
 
35
*/
 
36
int Editor_Change_Resource_Tool_Callback(FCoords& f, void* data, int curres) {
 
37
   Map* map=static_cast<Map*>(data);
 
38
 
 
39
   FCoords f1;
 
40
   Terrain_Descr* terr, *terd;
 
41
   int count=0;
 
42
 
 
43
   // This field
 
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)
 
49
      count+=8;
 
50
   else
 
51
      count+= curres==terd->get_default_resources() || terd->is_resource_valid(curres) ? 1 : -1;
 
52
   if(terr->get_is()&TERRAIN_UNPASSABLE && is_valid_r)
 
53
      count+=8;
 
54
   else
 
55
      count+= curres==terr->get_default_resources() || terr->is_resource_valid(curres) ? 1 : -1;
 
56
 
 
57
 
 
58
   // If one of the neighbours is unpassable, count its resource stronger
 
59
   // top left neigbour
 
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)
 
66
      count+=8;
 
67
   else
 
68
      count+= curres==terd->get_default_resources() || terd->is_resource_valid(curres) ? 1 : -1;
 
69
   if(terr->get_is()&TERRAIN_UNPASSABLE && is_valid_r)
 
70
      count+=8;
 
71
   else
 
72
      count+= curres==terr->get_default_resources() || terr->is_resource_valid(curres) ? 1 : -1;
 
73
 
 
74
   // top right neigbour
 
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)
 
79
      count+=8;
 
80
   else
 
81
      count+= curres==terd->get_default_resources() || terd->is_resource_valid(curres) ? 1 : -1;
 
82
 
 
83
   // left neighbour
 
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)
 
88
      count+=8;
 
89
   else
 
90
      count+= curres==terr->get_default_resources() || terr->is_resource_valid(curres) ? 1 : -1;
 
91
 
 
92
   if(count<=3)
 
93
      return 0;
 
94
   else
 
95
      return f.field->get_caps();
 
96
}
 
97
 
 
98
/*
 
99
===========
 
100
Editor_Increase_Resources_Tool::handle_click_impl()
 
101
 
 
102
decrease the resources of the current field by one if
 
103
there is not already another resource there.
 
104
===========
 
105
*/
 
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());
 
108
   FCoords c;
 
109
 
 
110
   while(mrc.next(&c)) {
 
111
      Field* f=map->get_field(c);
 
112
 
 
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();
 
116
 
 
117
      amount+=m_changed_by;
 
118
      if(amount>max_amount) amount=max_amount;
 
119
 
 
120
 
 
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
 
123
         std::string str;
 
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);
 
127
 
 
128
         if(!amount) {
 
129
            f->set_resources(0,0);
 
130
            f->set_starting_res_amount(0);
 
131
         } else {
 
132
            f->set_resources(m_cur_res,amount);
 
133
            f->set_starting_res_amount(amount);
 
134
            // set new overlay
 
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);
 
139
         }
 
140
      }
 
141
   }
 
142
   return parent->get_fieldsel_radius();
 
143
}