initial commit, lordsawar source, slightly modified
[lordsawar] / src / ruinmap.cpp
1 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
16 //  02110-1301, USA.
17
18 #include "ruinmap.h"
19
20 #include "gui/image-helpers.h"
21 #include "playerlist.h"
22 #include "GraphicsCache.h"
23 #include "stacklist.h"
24 #include "playerlist.h"
25 #include "player.h"
26 #include "maptile.h"
27 #include "ruinlist.h"
28 #include "templelist.h"
29 #include "citylist.h"
30 #include "GameMap.h"
31
32 RuinMap::RuinMap(NamedLocation *r)
33 {
34   ruin = r;
35 }
36
37
38 void RuinMap::draw_ruins (bool show_selected)
39 {
40   GraphicsCache *gc = GraphicsCache::getInstance();
41
42   // Draw all ruins as pictures over their location -- showing them as
43   // explored/unexplored
44   for (Ruinlist::iterator it = Ruinlist::getInstance()->begin();
45       it != Ruinlist::getInstance()->end(); it++)
46   {
47       if ((*it)->isHidden() == true && 
48           (*it)->getOwner() != Playerlist::getInstance()->getViewingplayer())
49         continue;
50       if ((*it)->isVisible(Playerlist::getViewingplayer()) == false)
51         continue;
52       PixMask *tmp;
53       if ((*it)->isSearched())
54         tmp = gc->getSmallRuinExploredPic();
55       else
56         {
57           if ((*it)->getType() == Ruin::STRONGHOLD)
58             tmp = gc->getSmallStrongholdUnexploredPic();
59           else
60             tmp = gc->getSmallRuinUnexploredPic();
61         }
62   
63       Vector<int> pos = (*it)->getPos();
64       pos = mapToSurface(pos);
65       tmp->blit_centered(surface, pos);
66       if (show_selected)
67         {
68           if ((*it)->getId() == ruin->getId()) //is this the selected ruin?
69             {
70               Gdk::Color box_color = Gdk::Color();
71               box_color.set_rgb_p(100,100,100);
72               draw_rect(pos.x - (tmp->get_width()/2), 
73                         pos.y - (tmp->get_height()/2), 
74                         tmp->get_width(), tmp->get_height(), box_color);
75             }
76         }
77   }
78 }
79
80 void RuinMap::draw_temples (bool show_selected)
81 {
82   GraphicsCache *gc = GraphicsCache::getInstance();
83
84   // Draw all temples as pictures over their location
85   for (Templelist::iterator it = Templelist::getInstance()->begin();
86       it != Templelist::getInstance()->end(); it++)
87   {
88       if ((*it)->isVisible(Playerlist::getViewingplayer()) == false)
89         continue;
90   
91       Vector<int> pos = (*it)->getPos();
92       pos = mapToSurface(pos);
93       PixMask *templepic = gc->getSmallTemplePic();
94       templepic->blit_centered(surface, pos);
95       if (show_selected)
96         {
97           if ((*it)->getId() == ruin->getId()) //is this the selected ruin?
98             {
99               Gdk::Color box_color = Gdk::Color();
100               box_color.set_rgb_p(100,100,100);
101               draw_rect(pos.x - (templepic->get_width()/2), 
102                         pos.y - (templepic->get_height()/2), 
103                         templepic->get_width(), templepic->get_height(), 
104                         box_color);
105             }
106         }
107   }
108 }
109
110 void RuinMap::after_draw()
111 {
112   draw_cities(true);
113   bool show_selected = true;
114   if (ruin == NULL)
115     show_selected = false;
116   draw_ruins (show_selected);
117   draw_temples (show_selected);
118   map_changed.emit(surface);
119 }
120
121 void RuinMap::mouse_button_event(MouseButtonEvent e)
122 {
123   if (e.button == MouseButtonEvent::LEFT_BUTTON && 
124       e.state == MouseButtonEvent::PRESSED)
125     {
126       Ruinlist *rl = Ruinlist::getInstance();
127       Ruin *nearestRuin;
128       Templelist *tl = Templelist::getInstance();
129       Temple *nearestTemple;
130       Vector<int> dest;
131       dest = mapFromScreen(e.pos);
132
133       nearestRuin = rl->getNearestVisibleRuin(dest, 4);
134       if (nearestRuin)
135         {
136           ruin = nearestRuin;
137           location_changed.emit (ruin);
138           draw(Playerlist::getViewingplayer());
139         }
140       else
141         {
142           nearestTemple = tl->getNearestVisibleTemple(dest, 4);
143           if (nearestTemple)
144             {
145               ruin = nearestTemple;
146               location_changed.emit (ruin);
147               draw(Playerlist::getViewingplayer());
148             }
149         }
150     }
151 }