ba8b79990fdeff629c06cc01a2ea92ff65637cca
[lordsawar] / src / game.h
1 // Copyright (C) 2006, 2007, 2008, 2009 Ben Asselstine
2 // Copyright (C) 2007, 2008 Ole Laursen
3 //
4 //  This program is free software; you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation; either version 3 of the License, or
7 //  (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 Library 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 
17 //  02110-1301, USA.
18
19 #ifndef GAME_H
20 #define GAME_H
21
22 #include <sigc++/signal.h>
23 #include <glibmm/ustring.h>
24 #include <sigc++/connection.h>
25 #include <memory>
26 #include <list>
27 #include <string>
28
29 #include "rectangle.h"
30 #include "sidebar-stats.h"
31 #include "map-tip-position.h"
32 #include "callback-enums.h"
33 #include "army.h"
34 #include "fight.h"
35 #include "game-parameters.h"
36 #include "player.h"
37 #include "defs.h"
38
39 class NextTurn;
40 class GameBigMap;
41 class SmallMap;
42 class GameScenario;
43 class Hero;
44 class City;
45 class Stack;
46 class Player;
47 class Temple;
48 class Ruin;
49 class Signpost;
50 class Fight;
51 class Quest;
52 class Stack;
53 class Reward;
54 class StackTile;
55 class Sage;
56
57 /** Controls a game.
58   * 
59   * Manages the big and small map, the game scenario and who's turn it is, etc.
60   * It's mostly a puppeteer class that connects the various other classes with
61   * signals and callbacks. 
62   *
63   */
64 class Game
65 {
66  public:
67     Game(GameScenario* gameScenario, NextTurn *nextTurn);
68     ~Game();
69
70     void redraw();
71     void blank(bool on);
72
73     void select_next_movable_stack();
74     void center_selected_stack();
75     void defend_selected_stack();
76     void park_selected_stack();
77     void deselect_selected_stack();
78     void search_selected_stack();
79     void search_stack(Stack *stack);
80     void move_selected_stack_along_path();
81     void move_selected_stack_northwest();
82     void move_selected_stack_north();
83     void move_selected_stack_northeast();
84     void move_selected_stack_east();
85     void move_selected_stack_west();
86     void move_selected_stack_southwest();
87     void move_selected_stack_south();
88     void move_selected_stack_southeast();
89     void move_all_stacks();
90     void end_turn();
91     void recalculate_moves_for_stack(Stack *s);
92
93     void startGame(); // initiate game flow
94     void loadGame();
95     void stopGame(); // stop game flow, clean up
96     // save current game, returns true if successful
97     bool saveGame(std::string file);
98     bool saveTurnFile(std::string file);
99
100     static GameScenario *getScenario();
101     GameBigMap &get_bigmap();
102     SmallMap &get_smallmap();
103     
104     // signals
105     sigc::signal<void, Vector<int> > current_map_position;
106     sigc::signal<void, Glib::RefPtr<Gdk::Pixmap>, Gdk::Rectangle> smallmap_changed;
107     sigc::signal<void, Glib::RefPtr<Gdk::Pixmap> > bigmap_changed;
108     sigc::signal<void, SidebarStats> sidebar_stats_changed;
109     sigc::signal<void, std::string> progress_status_changed;
110     sigc::signal<void> progress_changed;
111     sigc::signal<void, bool>
112         can_select_next_movable_stack,
113         can_center_selected_stack,
114         can_defend_selected_stack,
115         can_park_selected_stack,
116         can_deselect_selected_stack,
117         can_search_selected_stack,
118         can_plant_standard_selected_stack,
119         can_move_selected_stack_along_path,
120         can_move_selected_stack,
121         can_group_ungroup_selected_stack,
122         can_move_all_stacks,
123         can_disband_stack,
124         can_change_signpost,
125         can_see_history,
126         can_see_diplomacy,
127         received_diplomatic_proposal,
128         city_too_poor_to_produce,
129         can_end_turn;
130     sigc::signal<void, Stack *> stack_info_changed;
131     sigc::signal<void, Glib::ustring, MapTipPosition> map_tip_changed;
132     sigc::signal<void, StackTile *, MapTipPosition> stack_tip_changed;
133     sigc::signal<void,  City *, MapTipPosition> city_tip_changed;
134     sigc::signal<void, Ruin*, Stack*, Reward*> ruin_searched;
135     sigc::signal<Reward*, Ruin*, Sage*, Stack*> sage_visited;
136     sigc::signal<void, LocationBox, Fight &> fight_started;
137     sigc::signal<void, LocationBox> abbreviated_fight_started;
138     sigc::signal<void, Stack *, Stack *> ruinfight_started;
139     sigc::signal<void, float> advice_asked;
140     sigc::signal<void, Fight::Result> ruinfight_finished;
141     sigc::signal<bool, Player *, HeroProto *, City *, int> hero_offers_service;
142     sigc::signal<bool, int > enemy_offers_surrender;
143     sigc::signal<void, bool> surrender_answered;
144     sigc::signal<bool, Player *, Stack *, Player *, Vector<int> > stack_considers_treachery;
145     sigc::signal<bool, Hero *, Temple *, int> temple_searched;
146     sigc::signal<void, Hero *, Quest *> quest_assigned;
147     sigc::signal<CityDefeatedAction, City *, int> city_defeated;
148     sigc::signal<void, City *, int, unsigned int> city_pillaged;
149     sigc::signal<void, City *, int, std::list<guint32> > city_sacked;
150     sigc::signal<void, City *> city_razed;
151     sigc::signal<void, City *> city_visited;
152     sigc::signal<void, Ruin *> ruin_visited;
153     sigc::signal<void, Temple *> temple_visited;
154     sigc::signal<void, Player *, unsigned int> next_player_turn;
155     sigc::signal<void> remote_next_player_turn;
156     sigc::signal<void, int> hero_arrives;
157     sigc::signal<void, Army *, int> medal_awarded_to_army;
158     sigc::signal<Army::Stat, Hero *> hero_gains_level;
159     sigc::signal<void, Player *> game_loaded;
160     sigc::signal<void, Player *> game_over;
161     sigc::signal<void, Player *> player_died;
162     sigc::signal<void> game_stopped;
163     sigc::signal<void, std::string> commentator_comments;
164     sigc::signal<void, Stack*, Vector<int> > stack_moves;
165     
166     void addPlayer(Player *p);
167
168     void inhibitAutosaveRemoval(bool inhibit);
169
170     void endOfGameRoaming(Player *winner);
171  private:
172     static Game *current_game;
173
174     // move the selected stack one tile in a given direction
175     void move_selected_stack_dir(int diffx, int diffy);
176     void move_map_dir(int diffx, int diffy);
177
178     // centers the map on a city of the active player
179     void center_view_on_city();
180
181     void update_control_panel();
182     void update_sidebar_stats();
183     void update_stack_info();   // emit stack_info_changed
184     void clear_stack_info();
185
186     // locks/unlocks the input widgets during computer turns
187     void lock_inputs();
188     void unlock_inputs();
189
190     //! Maybe peform treachery
191     bool maybeTreachery(Stack *stack, Player *them, Vector<int> pos);
192
193     // bigmap callbacks
194     void on_stack_selected(Stack* s);
195     void on_stack_grouped_or_ungrouped(Stack *s);
196     void on_city_visted (City* c);
197     void on_ruin_queried (Ruin* r, bool brief);
198     void on_temple_queried (Temple* t, bool brief);
199     void on_signpost_queried (Signpost* s);
200     void on_stack_queried (Vector<int> tile);
201     void on_stack_unqueried ();
202     void on_city_visited(City *city); // for city window
203     void on_city_queried (Vector<int>, City *city); // for city info tip
204     void on_city_unqueried ();
205
206     // smallmap callbacks
207     void on_smallmap_changed(Glib::RefPtr<Gdk::Pixmap> map);
208     void on_bigmap_changed(Glib::RefPtr<Gdk::Pixmap> map);
209     
210     // misc. callbacks
211     void invading_city(City* city, int gold);
212     void init_turn_for_player(Player* p);
213     void on_player_died(Player *p);
214     void stack_searches_ruin(Ruin *ruin, Stack *stack);
215     void stack_searches_temple(Temple *temple, Stack *stack);
216
217     //! Callback when the army of a human player reaches a new level.
218     Army::Stat heroGainsLevel(Hero * a);
219     //! Callback when an army gets a new medal.
220     void newMedalArmy(Army* a, int medaltype);
221     //! Called whenever a stack has changed, updates the map etc.
222     void stackUpdate(Stack* s);
223     //! Called whenever players fight
224     void on_fight_started(Fight &fight);
225     //! Called whenever a player receives an offer of surrender
226     void on_surrender_offered(Player *recipient);
227     void nextRound();
228     //! Called after a player's stack attacks a city
229     void on_city_fight_finished(City *city, Fight::Result result);
230     
231     void looting_city(City *city, int &gold);
232     void unselect_active_stack();
233     void select_active_stack();
234     bool recruitHero(HeroProto *hero, City *city, int gold);
235
236     void on_stack_grouped(Stack *stack, bool grouped);
237     void stack_arrives_on_tile(Stack *stack, Vector<int> tile);
238     void stack_leaves_tile(Stack *stack, Vector<int> tile);
239     void on_stack_halted(Stack *stack);
240
241
242     GameScenario* d_gameScenario;
243     NextTurn* d_nextTurn;
244     std::auto_ptr<GameBigMap> bigmap;
245     std::auto_ptr<SmallMap> smallmap;
246
247
248     bool input_locked;
249
250     std::list<sigc::connection> connections[MAX_PLAYERS + 1];
251 };
252
253 #endif