1 /*
2 Copyright (C) 2005 Wolfgang Becker <uafr@gmx.de>
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 2 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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18 #include <config.h>
19 
20 #include "Dialog.hpp"
21 
22 #include <iostream>
23 #include <fstream>
24 #include <sstream>
25 #include <stdexcept>
26 
27 #include "GameView.hpp"
28 #include "Util.hpp"
29 #include "MapEdit.hpp"
30 #include "CheckButton.hpp"
31 #include "lincity/engine.h"
32 #include "lincity/fileutil.h"
33 #include "lincity/simulate.h"
34 #include "lincity/lclib.h"
35 #include "lincity/loadsave.h"
36 
37 #include "gui_interface/shared_globals.h"
38 
39 #include "gui/ComponentLoader.hpp"
40 #include "gui/Button.hpp"
41 #include "gui/callback/Callback.hpp"
42 #include "gui/Paragraph.hpp"
43 
44 #include "tinygettext/gettext.hpp"
45 
46 bool blockingDialogIsOpen = false;
47 
48 std::vector<Dialog*> dialogVector;
49 
closeAllDialogs()50 void closeAllDialogs(){
51     std::vector<Dialog*>::iterator iter;
52     while( !dialogVector.empty() ){
53         iter = dialogVector.begin();
54         (*iter)->closeDialog();
55     }
56 }
57 
Dialog(int type)58 Dialog::Dialog( int type ){
59     initDialog();
60     switch( type ){
61         case ASK_COAL_SURVEY:
62             coalSurvey();
63             break;
64         case GAME_STATS:
65             gameStats();
66             break;
67         default:
68             std::stringstream msg;
69             msg <<"Can't open Dialog type " << type << " without additional parameters.";
70             throw std::runtime_error(msg.str());
71     }
72 }
73 
Dialog(int type,std::string message,std::string extraString)74 Dialog::Dialog( int type, std::string message, std::string extraString){
75     initDialog();
76     switch( type ){
77         case MSG_DIALOG:
78             msgDialog( message, extraString );
79             break;
80         default:
81             std::stringstream msg;
82             msg <<"Can't open Dialog type " << type << " with String parameters.";
83             throw std::runtime_error(msg.str());
84     }
85 }
86 
Dialog(int type,int x,int y)87 Dialog::Dialog( int type, int x, int y ){
88     initDialog( x, y );
89     switch( type ) {
90         case BULLDOZE_MONUMENT:
91             askBulldozeMonument();
92             break;
93         case BULLDOZE_RIVER:
94             askBulldozeRiver();
95             break;
96         case BULLDOZE_SHANTY:
97             askBulldozeShanty();
98             break;
99         case EDIT_MARKET:
100             editMarket();
101             break;
102         case EDIT_PORT:
103             editPort();
104             break;
105         case ASK_LAUNCH_ROCKET:
106             askRocket();
107             break;
108         default:
109             std::stringstream msg;
110             msg <<"Can't open Dialog type " << type << " with coordinates.";
111             throw std::runtime_error(msg.str());
112     }
113 }
114 
initDialog(int x,int y)115 void Dialog::initDialog( int x /*= -1*/, int y /*= -1*/ ){
116     Component* root = getGameView();
117     desktop = 0;
118     myDialogComponent = 0;
119     pointX = x;
120     pointY = y;
121     iAmBlocking = false;
122     if( root ) {
123         while( root->getParent() )
124             root = root->getParent();
125         desktop = dynamic_cast<Desktop*> (root);
126         if(!desktop)
127             std::cerr << "Root not a desktop!?!\n";
128     } else {
129         std::cerr << "Dialog: Root not found.\n";
130     }
131 }
132 
~Dialog()133 Dialog::~Dialog(){
134 }
135 
registerDialog()136 void Dialog::registerDialog(){
137     dialogVector.push_back( this );
138     desktop->addChildComponent( myDialogComponent );
139 }
140 
unRegisterDialog()141 void Dialog::unRegisterDialog(){
142     std::vector<Dialog*>::iterator iter = dialogVector.begin();
143     std::vector<Dialog*>::iterator del;
144     while( iter <= dialogVector.end() ){
145         if ( *iter == this ){
146             del = iter;
147             iter++;
148             dialogVector.erase( del );
149         } else {
150             iter++;
151         }
152     }
153     delete( this );
154 }
155 
askRocket()156 void Dialog::askRocket(){
157     if( !desktop ) {
158         std::cerr << "No desktop found.\n";
159         return;
160     }
161     try {
162         myDialogComponent = loadGUIFile( "gui/launch_rocket_yn.xml" );
163         assert( myDialogComponent != 0);
164         registerDialog();
165         blockingDialogIsOpen = true;
166         iAmBlocking = true;
167     } catch(std::exception& e) {
168         std::cerr << "Couldn't display message 'launch_rocket_yn': "
169             << e.what() << "\n";
170         return;
171     }
172     Paragraph* p = getParagraph( *myDialogComponent, "DialogTitle" );
173     std::stringstream title;
174 	title << _("Launchsite") << " ( " << pointX <<" , " << pointY << " )";
175     p->setText( title.str() );
176     // connect signals
177     Button* yesButton = getButton( *myDialogComponent, "Yes" );
178     yesButton->clicked.connect( makeCallback(*this, &Dialog::okayLaunchRocketButtonClicked ) );
179     Button* noButton = getButton( *myDialogComponent, "No" );
180     noButton->clicked.connect( makeCallback( *this, &Dialog::closeDialogButtonClicked ) );
181     Button* gotoButton = getButton( *myDialogComponent, "goto" );
182     gotoButton->clicked.connect( makeCallback( *this, &Dialog::gotoButtonClicked ) );
183 }
184 
185 //no Signals caught here, so ScreenInterface has to catch them.
msgDialog(std::string message,std::string extraString)186 void Dialog::msgDialog( std::string message, std::string extraString){
187     if( !desktop ) {
188         std::cerr << "No desktop found.\n";
189         return;
190     }
191     //generate filename. foo.mes => gui/foo.xml
192     std::string filename = "gui/";
193     filename += message;
194     std::string::size_type pos = filename.rfind( ".mes" );
195     if( pos != std::string::npos ){
196         filename.replace( pos, 4 ,".xml");
197     }
198     std::auto_ptr<Component> myDialogComponent (loadGUIFile( filename ));
199 
200     //set Extra-String
201     getParagraph( *myDialogComponent, "ExtraText" )->setText( extraString );
202 
203     // connect signals
204     Button* noButton = getButton( *myDialogComponent, "Ok" );
205     noButton->clicked.connect( makeCallback( *this, &Dialog::closeDialogButtonClicked ) );
206 
207     this->myDialogComponent = myDialogComponent.release();
208     registerDialog();
209 }
210 
askBulldozeMonument()211 void Dialog::askBulldozeMonument() {
212     if( !desktop ) {
213         std::cerr << "No desktop found.\n";
214         return;
215     }
216     try {
217         myDialogComponent = loadGUIFile( "gui/bulldoze_monument_yn.xml" );
218         assert( myDialogComponent != 0);
219         registerDialog();
220         blockingDialogIsOpen = true;
221         iAmBlocking = true;
222     } catch(std::exception& e) {
223         std::cerr << "Couldn't display message 'bulldoze_monument_yn': "
224             << e.what() << "\n";
225         return;
226     }
227     // connect signals
228     Button* yesButton = getButton( *myDialogComponent, "Yes" );
229     yesButton->clicked.connect( makeCallback(*this, &Dialog::okayBulldozeMonumentButtonClicked ) );
230     Button* noButton = getButton( *myDialogComponent, "No" );
231     noButton->clicked.connect( makeCallback( *this, &Dialog::closeDialogButtonClicked ) );
232 }
233 
askBulldozeRiver()234 void Dialog::askBulldozeRiver() {
235     if( !desktop ) {
236         std::cerr << "No desktop found.\n";
237         return;
238     }
239     try {
240         myDialogComponent = loadGUIFile( "gui/bulldoze_river_yn.xml" );
241         assert( myDialogComponent != 0);
242         registerDialog();
243         blockingDialogIsOpen = true;
244         iAmBlocking = true;
245     } catch(std::exception& e) {
246         std::cerr << "Couldn't display message 'bulldoze_river_yn.xml': "
247             << e.what() << "\n";
248         return;
249     }
250     // connect signals
251     Button* yesButton = getButton( *myDialogComponent, "Yes" );
252     yesButton->clicked.connect( makeCallback(*this, &Dialog::okayBulldozeRiverButtonClicked ) );
253     Button* noButton = getButton( *myDialogComponent, "No" );
254     noButton->clicked.connect( makeCallback( *this, &Dialog::closeDialogButtonClicked ) );
255 }
256 
askBulldozeShanty()257 void Dialog::askBulldozeShanty() {
258     if( !desktop ) {
259         std::cerr << "No desktop found.\n";
260         return;
261     }
262     try {
263         myDialogComponent = loadGUIFile( "gui/bulldoze_shanty_yn.xml" );
264         assert( myDialogComponent != 0);
265         registerDialog();
266         blockingDialogIsOpen = true;
267         iAmBlocking = true;
268     } catch(std::exception& e) {
269         std::cerr << "Couldn't display message 'bulldoze_shanty_yn': "
270             << e.what() << "\n";
271         return;
272     }
273     // connect signals
274     Button* yesButton = getButton( *myDialogComponent, "Yes" );
275     yesButton->clicked.connect( makeCallback(*this, &Dialog::okayBulldozeShantyButtonClicked ) );
276     Button* noButton = getButton( *myDialogComponent, "No" );
277     noButton->clicked.connect( makeCallback( *this, &Dialog::closeDialogButtonClicked ) );
278 }
279 
coalSurvey()280 void Dialog::coalSurvey(){
281     if( !desktop ) {
282         std::cerr << "No desktop found.\n";
283         return;
284     }
285     try {
286         myDialogComponent = loadGUIFile( "gui/coal_survey_yn.xml" );
287         assert( myDialogComponent != 0);
288         registerDialog();
289         blockingDialogIsOpen = true;
290         iAmBlocking = true;
291     } catch(std::exception& e) {
292         std::cerr << "Couldn't display message 'coal_survey_yn': "
293             << e.what() << "\n";
294         return;
295     }
296     // connect signals
297     Button* yesButton = getButton( *myDialogComponent, "Yes" );
298     yesButton->clicked.connect( makeCallback(*this, &Dialog::okayCoalSurveyButtonClicked ) );
299     Button* noButton = getButton( *myDialogComponent, "No" );
300     noButton->clicked.connect( makeCallback( *this, &Dialog::closeDialogButtonClicked ) );
301 }
302 
setParagraphN(const std::string basename,const int number,const std::string text)303 void Dialog::setParagraphN( const std::string basename, const int number, const std::string text ){
304     std::stringstream paragraphName;
305     paragraphName << basename << number;
306     setParagraph( paragraphName.str(), text );
307 }
308 
setTableRC(const std::string basename,const int row,const int column,const std::string text,T value)309 template<typename T> void Dialog::setTableRC( const std::string basename, const int row, const int column, const std::string text, T value ){
310     std::stringstream paragraphName;
311     paragraphName << basename << "_text_" << row << "_" << column;
312     setParagraph( paragraphName.str(), text );
313     paragraphName.str("");
314     paragraphName << basename << "_value_" << row << "_" << column;
315     std::stringstream stringvalue;
316     stringvalue << value;
317     setParagraph( paragraphName.str(), stringvalue.str() );
318 }
319 
setParagraph(const std::string paragraphName,const std::string text)320 void Dialog::setParagraph( const std::string paragraphName, const std::string text ){
321     Paragraph* p;
322     try{
323         p = getParagraph( *myDialogComponent, paragraphName );
324         p->setText( text );
325     } catch(std::exception& e) {
326         std::cerr << "Couldn't set " << paragraphName << " to '" << text << "': "
327             << e.what() << "\n";
328     }
329 }
330 
331 /*
332  * Show game statistics in dialog and write them to RESULTS_FILENAME.
333  *
334  * gamestats.xml has 11 lines
335  * statistic_text1
336  * [...]
337  * statistic_text11
338  * and 15 lines in a Table with 6 columns
339  * statistic_text_1_1  statistic_number_1_1  statistic_text_1_2  statistic_number_1_2  statistic_text_1_3  statistic_number_1_3
340  * [...]
341 //  * statistic_text_15_1 statistic_number_15_1 statistic_text_15_2 statistic_number_15_2 statistic_text_15_3 statistic_number_15_3
342  */
gameStats()343 void Dialog::gameStats(){
344      saveGameStats();
345      if( !desktop ) {
346         std::cerr << "No desktop found.\n";
347         return;
348     }
349     bool useExisting = false;
350     myDialogComponent = desktop->findComponent("GameStats");
351     if( myDialogComponent == 0){
352         try {
353             myDialogComponent = loadGUIFile( "gui/gamestats.xml" );
354             assert( myDialogComponent != 0);
355             registerDialog();
356         } catch(std::exception& e) {
357             std::cerr << "Couldn't display message 'gamestats': "
358                 << e.what() << "\n";
359             return;
360         }
361     } else {
362         useExisting = true;
363     }
364 
365     // Fill in Fields.
366     int line = 1;
367     int maxlength = 567;
368     char* outf = (char *) malloc ( maxlength );
369     int group_count[NUM_OF_GROUPS];
370     count_all_groups (group_count);
371     if (cheat_flag){
372         setParagraphN( "statistic_text", line++, _("----- IN TEST MODE -------") );
373     }
374     snprintf (outf, maxlength, _("Game statistics from LinCity-NG Version %s"), PACKAGE_VERSION);
375     setParagraphN( "statistic_text", line++, outf );
376     if (strlen (given_scene) > 3){
377 	    snprintf (outf, maxlength, _("Initial loaded scene - %s"), given_scene);
378         setParagraphN( "statistic_text", line++, outf );
379     }
380     if (sustain_flag){
381 	    snprintf (outf, maxlength, _("Economy is sustainable"));
382         setParagraphN( "statistic_text", line++, outf );
383     }
384     snprintf (outf, maxlength, _("Population  %d  of which  %d  are not housed.")
385 	     ,housed_population + people_pool, people_pool);
386     setParagraphN( "statistic_text", line++, outf );
387     snprintf (outf, maxlength,
388 	     _("Max population %d  Number evacuated %d Total births %d")
389 	     ,max_pop_ever, total_evacuated, total_births);
390     setParagraphN( "statistic_text", line++, outf );
391     snprintf (outf, maxlength,
392 	     _("Date %s %04d  Money %8d   Tech-level %5.1f (%5.1f)"),
393 	     current_month(total_time), current_year(total_time), total_money,
394 	     (float) tech_level * 100.0 / MAX_TECH_LEVEL,
395 	     (float) highest_tech_level * 100.0 / MAX_TECH_LEVEL);
396     setParagraphN( "statistic_text", line++, outf );
397     snprintf (outf, maxlength,
398 	     _("Deaths by starvation %7d   History %8.3f"),
399 	     total_starve_deaths, starve_deaths_history);
400     setParagraphN( "statistic_text", line++, outf );
401     snprintf (outf, maxlength,
402 	     _("Deaths from pollution %7d   History %8.3f"),
403 	     total_pollution_deaths, pollution_deaths_history);
404     setParagraphN( "statistic_text", line++, outf );
405     snprintf (outf, maxlength, _("Years of unemployment %7d   History %8.3f"),
406 	     total_unemployed_years, unemployed_history);
407     setParagraphN( "statistic_text", line++, outf );
408     snprintf (outf, maxlength, _("Rockets launched %2d  Successful launches %2d"),
409 	     rockets_launched, rockets_launched_success);
410     setParagraphN( "statistic_text", line++, outf );
411 
412     while( line <= 11 ){ //clear remaining lines
413         setParagraphN( "statistic_text", line++, "" );
414     }
415     free( outf );
416 
417     setTableRC("statistic", 1, 1, _("Residences"),
418 	     group_count[GROUP_RESIDENCE_LL] +
419 	     group_count[GROUP_RESIDENCE_ML] +
420 	     group_count[GROUP_RESIDENCE_HL] +
421 	     group_count[GROUP_RESIDENCE_LH] +
422 	     group_count[GROUP_RESIDENCE_MH] +
423 	     group_count[GROUP_RESIDENCE_HH]);
424     setTableRC("statistic", 1, 2, _("Markets"), group_count[GROUP_MARKET] );
425     setTableRC("statistic", 1, 3, _("Farms"), group_count[GROUP_ORGANIC_FARM]);
426 
427     setTableRC("statistic", 2, 1, _("Water wells"), group_count[GROUP_WATERWELL]);
428     setTableRC("statistic", 2, 2, "", "");
429     setTableRC("statistic", 2, 3, "", "");
430 
431     setTableRC("statistic", 3, 1, "", "");
432     setTableRC("statistic", 3, 2, "", "");
433     setTableRC("statistic", 3, 3, "", "");
434 
435     setTableRC("statistic", 4, 1, _("Monuments"), group_count[GROUP_MONUMENT]);
436     setTableRC("statistic", 4, 2, _("Schools"),  group_count[GROUP_SCHOOL]);
437     setTableRC("statistic", 4, 3, _("Universities"), group_count[GROUP_UNIVERSITY]);
438 
439     setTableRC("statistic", 5, 1, _("Fire stations"), group_count[GROUP_FIRESTATION]);
440     setTableRC("statistic", 5, 2, _("Parks"), group_count[GROUP_PARKLAND]);
441     setTableRC("statistic", 5, 3, _("Sports fields"), group_count[GROUP_CRICKET]);
442 
443     setTableRC("statistic", 6, 1, _("Health centres"), group_count[GROUP_HEALTH]);
444     setTableRC("statistic", 6, 2, _("Tips"), group_count[GROUP_TIP]);
445     setTableRC("statistic", 6, 3, _("Shanties"), group_count[GROUP_SHANTY]);
446 
447     setTableRC("statistic", 7, 1, "", "");
448     setTableRC("statistic", 7, 2, "", "");
449     setTableRC("statistic", 7, 3, "", "");
450 
451     setTableRC("statistic", 8, 1, _("Windmills"), group_count[GROUP_WINDMILL]);
452     setTableRC("statistic", 8, 2, _("Coal powers"), group_count[GROUP_COAL_POWER]);
453     setTableRC("statistic", 8, 3, _("Solar powers"), group_count[GROUP_SOLAR_POWER]);
454 
455     setTableRC("statistic", 9, 1, _("Substations"), group_count[GROUP_SUBSTATION]);
456     setTableRC("statistic", 9, 2, _("Power lines"), group_count[GROUP_POWER_LINE]);
457     setTableRC("statistic", 9, 3, _("Ports"), group_count[GROUP_PORT]);
458 
459     setTableRC("statistic", 10, 1, _("Tracks"), group_count[GROUP_TRACK]);
460     setTableRC("statistic", 10, 2, _("Roads"), group_count[GROUP_ROAD]);
461     setTableRC("statistic", 10, 3, _("Rail"), group_count[GROUP_RAIL]);
462 
463     setTableRC("statistic", 11, 1, "", "");
464     setTableRC("statistic", 11, 2, "", "");
465     setTableRC("statistic", 11, 3, "", "");
466 
467     setTableRC("statistic", 12, 1, _("Potteries"), group_count[GROUP_POTTERY]);
468     setTableRC("statistic", 12, 2, _("Blacksmiths"), group_count[GROUP_BLACKSMITH]);
469     setTableRC("statistic", 12, 3, _("Mills"), group_count[GROUP_MILL]);
470 
471     setTableRC("statistic", 13, 1, _("Light inds"), group_count[GROUP_INDUSTRY_L]);
472     setTableRC("statistic", 13, 2, _("Heavy inds"), group_count[GROUP_INDUSTRY_H]);
473     setTableRC("statistic", 13, 3, _("Recyclers"), group_count[GROUP_RECYCLE]);
474 
475     setTableRC("statistic", 14, 1, _("Coal mines"), group_count[GROUP_COALMINE]);
476     setTableRC("statistic", 14, 2, _("Ore mines"), group_count[GROUP_OREMINE]);
477     setTableRC("statistic", 14, 3, _("Communes"), group_count[GROUP_COMMUNE]);
478 
479     setTableRC("statistic", 15, 1, "", "");
480     setTableRC("statistic", 15, 2, "", "");
481     setTableRC("statistic", 15, 3, "", "");
482 
483     if( !useExisting ){
484         // connect signals
485         Button* noButton = getButton( *myDialogComponent, "Okay" );
486         noButton->clicked.connect( makeCallback( *this, &Dialog::closeDialogButtonClicked ) );
487     }
488 }
489 
490 /*
491  * Save game statistics to RESULTS_FILENAME. Do NOT translate text here!
492  * That way the files can be used for highscores.
493  */
saveGameStats()494 void Dialog::saveGameStats(){
495     //open File
496     char *s;
497     if ((s = (char *) malloc (lc_save_dir_len + strlen (LC_SAVE_DIR)
498 			      + strlen (RESULTS_FILENAME) + 64)) == 0)
499 	malloc_failure ();
500     sprintf (s, "%s%c%s", lc_save_dir, PATH_SLASH, RESULTS_FILENAME);
501 
502     std::ofstream results( s );
503     free( s );
504 
505     // Fill in Fields.
506     int maxlength = 567;
507     char* outf = (char *) malloc ( maxlength );
508     int group_count[NUM_OF_GROUPS];
509     count_all_groups (group_count);
510     if (cheat_flag){
511         results << "----- IN TEST MODE -------"  << std::endl;
512     }
513     snprintf (outf, maxlength, "Game statistics from LinCity-NG Version %s", PACKAGE_VERSION);
514     results << outf << std::endl;
515     if (strlen (given_scene) > 3){
516 	    snprintf (outf, maxlength, "Initial loaded scene - %s", given_scene);
517         results << outf << std::endl;
518     }
519     if (sustain_flag){
520 	    snprintf (outf, maxlength, "Economy is sustainable");
521         results << outf << std::endl;
522     }
523     snprintf (outf, maxlength, "Population  %d  of which  %d  are not housed."
524 	     ,housed_population + people_pool, people_pool);
525     results << outf << std::endl;
526     snprintf (outf, maxlength,
527 	     "Max population %d  Number evacuated %d Total births %d"
528 	     ,max_pop_ever, total_evacuated, total_births);
529     results << outf << std::endl;
530     snprintf (outf, maxlength,
531 	     "Date %02d/%04d  Money %8d   Tech-level %5.1f (%5.1f)",
532 	     1 + ((total_time % NUMOF_DAYS_IN_YEAR) / NUMOF_DAYS_IN_MONTH), current_year(total_time), total_money,
533 	     (float) tech_level * 100.0 / MAX_TECH_LEVEL,
534 	     (float) highest_tech_level * 100.0 / MAX_TECH_LEVEL);
535     results << outf << std::endl;
536     snprintf (outf, maxlength,
537 	     " Deaths by starvation %7d   History %8.3f",
538 	     total_starve_deaths, starve_deaths_history);
539     results << outf << std::endl;
540     snprintf (outf, maxlength,
541 	     "Deaths from pollution %7d   History %8.3f",
542 	     total_pollution_deaths, pollution_deaths_history);
543     results << outf << std::endl;
544     snprintf (outf, maxlength, "Years of unemployment %7d   History %8.3f",
545 	     total_unemployed_years, unemployed_history);
546     results << outf << std::endl;
547     snprintf (outf, maxlength, "Rockets launched %2d  Successful launches %2d",
548 	     rockets_launched, rockets_launched_success);
549     results << outf << std::endl;
550     results << "" << std::endl;
551 
552     snprintf (outf, maxlength, "    Residences %4d         Markets %4d            Farms %4d",
553 	     group_count[GROUP_RESIDENCE_LL] +
554 	     group_count[GROUP_RESIDENCE_ML] +
555 	     group_count[GROUP_RESIDENCE_HL] +
556 	     group_count[GROUP_RESIDENCE_LH] +
557 	     group_count[GROUP_RESIDENCE_MH] +
558 	     group_count[GROUP_RESIDENCE_HH],
559 	     group_count[GROUP_MARKET],
560 	     group_count[GROUP_ORGANIC_FARM]);
561     results << outf << std::endl;
562     snprintf (outf, maxlength, "   Water wells %4d", group_count[GROUP_WATERWELL]);
563     results << outf << std::endl;
564     snprintf (outf, maxlength, "     Monuments %4d         Schools %4d     Universities %4d"
565 	     ,group_count[GROUP_MONUMENT], group_count[GROUP_SCHOOL]
566 	     ,group_count[GROUP_UNIVERSITY]);
567     results << outf << std::endl;
568     snprintf (outf, maxlength, " Fire stations %4d           Parks %4d    Sports fields %4d"
569 	     ,group_count[GROUP_FIRESTATION], group_count[GROUP_PARKLAND]
570 	     ,group_count[GROUP_CRICKET]);
571     results << outf << std::endl;
572     snprintf (outf, maxlength, "Health centres %4d            Tips %4d         Shanties %4d",
573 	     group_count[GROUP_HEALTH], group_count[GROUP_TIP],
574 	     group_count[GROUP_SHANTY]);
575     results << outf << std::endl;
576     results << "" << std::endl;
577 
578     snprintf (outf, maxlength, "     Windmills %4d     Coal powers %4d     Solar powers %4d",
579 	     group_count[GROUP_WINDMILL],
580 	     group_count[GROUP_COAL_POWER],
581 	     group_count[GROUP_SOLAR_POWER]);
582     results << outf << std::endl;
583     snprintf (outf, maxlength, "   Substations %4d     Power lines %4d            Ports %4d"
584 	     ,group_count[GROUP_SUBSTATION], group_count[GROUP_POWER_LINE]
585 	     ,group_count[GROUP_PORT]);
586     results << outf << std::endl;
587     snprintf (outf, maxlength, "        Tracks %4d           Roads %4d             Rail %4d"
588 	     ,group_count[GROUP_TRACK], group_count[GROUP_ROAD]
589 	     ,group_count[GROUP_RAIL]);
590     results << outf << std::endl;
591     results << "" << std::endl;
592 
593     snprintf (outf, maxlength, "     Potteries %4d     Blacksmiths %4d            Mills %4d"
594 	     ,group_count[GROUP_POTTERY], group_count[GROUP_BLACKSMITH]
595 	     ,group_count[GROUP_MILL]);
596     results << outf << std::endl;
597     snprintf (outf, maxlength, "    Light inds %4d      Heavy inds %4d        Recyclers %4d"
598 	     ,group_count[GROUP_INDUSTRY_L], group_count[GROUP_INDUSTRY_H]
599 	     ,group_count[GROUP_RECYCLE]);
600     results << outf << std::endl;
601     snprintf (outf, maxlength, "    Coal mines %4d       Ore mines %4d         Communes %4d"
602 	     ,group_count[GROUP_COALMINE], group_count[GROUP_OREMINE]
603 	     ,group_count[GROUP_COMMUNE]);
604     results << outf << std::endl;
605     results << "" << std::endl;
606 
607     //close File
608     results.close();
609 
610     free( outf );
611 }
612 
editMarket()613 void Dialog::editMarket(){
614     if( !desktop ) {
615         std::cerr << "No desktop found.\n";
616         return;
617     }
618     try {
619         myDialogComponent = loadGUIFile( "gui/tradedialog.xml" );
620         assert( myDialogComponent != 0);
621         registerDialog();
622         blockingDialogIsOpen = true;
623         iAmBlocking = true;
624     } catch(std::exception& e) {
625         std::cerr << "Couldn't display dialog 'tradedialog.xml': "
626             << e.what() << "\n";
627         return;
628     }
629     // set Dialog to Market-Data
630     Paragraph* p = getParagraph( *myDialogComponent, "DialogTitle" );
631     std::stringstream title;
632 	title << _("Market") << " ( " << pointX <<" , " << pointY << " )";
633     p->setText( title.str() );
634 
635     CheckButton* cb;
636     cb = getCheckButton( *myDialogComponent, "BuyJobs" );
637     if( MP_INFO( pointX,pointY ).flags & FLAG_MB_JOBS ) cb->check(); else cb->uncheck();
638     cb = getCheckButton( *myDialogComponent, "SellJobs" );
639     if( MP_INFO( pointX,pointY ).flags & FLAG_MS_JOBS ) cb->check(); else cb->uncheck();
640     cb = getCheckButton( *myDialogComponent, "BuyFood" );
641     if( MP_INFO( pointX,pointY ).flags & FLAG_MB_FOOD) cb->check(); else cb->uncheck();
642     cb = getCheckButton( *myDialogComponent, "SellFood" );
643     if( MP_INFO( pointX,pointY ).flags & FLAG_MS_FOOD) cb->check(); else cb->uncheck();
644     cb = getCheckButton( *myDialogComponent, "BuyCoal" );
645     if( MP_INFO( pointX,pointY ).flags & FLAG_MB_COAL) cb->check(); else cb->uncheck();
646     cb = getCheckButton( *myDialogComponent, "SellCoal" );
647     if( MP_INFO( pointX,pointY ).flags & FLAG_MS_COAL) cb->check(); else cb->uncheck();
648     cb = getCheckButton( *myDialogComponent, "BuyOre" );
649     if( MP_INFO( pointX,pointY ).flags & FLAG_MB_ORE) cb->check(); else cb->uncheck();
650     cb = getCheckButton( *myDialogComponent, "SellOre" );
651     if( MP_INFO( pointX,pointY ).flags & FLAG_MS_ORE) cb->check(); else cb->uncheck();
652     cb = getCheckButton( *myDialogComponent, "BuyGoods" );
653     if( MP_INFO( pointX,pointY ).flags & FLAG_MB_GOODS) cb->check(); else cb->uncheck();
654     cb = getCheckButton( *myDialogComponent, "SellGoods" );
655     if( MP_INFO( pointX,pointY ).flags & FLAG_MS_GOODS) cb->check(); else cb->uncheck();
656     cb = getCheckButton( *myDialogComponent, "BuySteel" );
657     if( MP_INFO( pointX,pointY ).flags & FLAG_MB_STEEL) cb->check(); else cb->uncheck();
658     cb = getCheckButton( *myDialogComponent, "SellSteel" );
659     if( MP_INFO( pointX,pointY ).flags & FLAG_MS_STEEL) cb->check(); else cb->uncheck();
660     // connect signals
661     Button* applyButton = getButton( *myDialogComponent, "Apply" );
662     applyButton->clicked.connect( makeCallback(*this, &Dialog::applyMarketButtonClicked ) );
663     Button* gotoButton = getButton( *myDialogComponent, "goto" );
664     gotoButton->clicked.connect( makeCallback( *this, &Dialog::gotoButtonClicked ) );
665 }
666 
editPort()667 void Dialog::editPort(){
668     if( !desktop ) {
669         std::cerr << "No desktop found.\n";
670         return;
671     }
672     try {
673         myDialogComponent = loadGUIFile( "gui/portdialog.xml" );
674         assert( myDialogComponent != 0);
675         registerDialog();
676         blockingDialogIsOpen = true;
677         iAmBlocking = true;
678     } catch(std::exception& e) {
679         std::cerr << "Couldn't display dialog 'tradedialog.xml': "
680             << e.what() << "\n";
681         return;
682     }
683     // set Dialog to Port-Data
684     Paragraph* p = getParagraph( *myDialogComponent, "DialogTitle" );
685     std::stringstream title;
686 	title << _("Port") << " ( " << pointX <<" , " << pointY << " )";
687     p->setText( title.str() );
688 
689     CheckButton* cb;
690     cb = getCheckButton( *myDialogComponent, "BuyFood" );
691     if( MP_INFO( pointX,pointY ).flags & FLAG_MB_FOOD) cb->check(); else cb->uncheck();
692     cb = getCheckButton( *myDialogComponent, "SellFood" );
693     if( MP_INFO( pointX,pointY ).flags & FLAG_MS_FOOD) cb->check(); else cb->uncheck();
694     cb = getCheckButton( *myDialogComponent, "BuyCoal" );
695     if( MP_INFO( pointX,pointY ).flags & FLAG_MB_COAL) cb->check(); else cb->uncheck();
696     cb = getCheckButton( *myDialogComponent, "SellCoal" );
697     if( MP_INFO( pointX,pointY ).flags & FLAG_MS_COAL) cb->check(); else cb->uncheck();
698     cb = getCheckButton( *myDialogComponent, "BuyOre" );
699     if( MP_INFO( pointX,pointY ).flags & FLAG_MB_ORE) cb->check(); else cb->uncheck();
700     cb = getCheckButton( *myDialogComponent, "SellOre" );
701     if( MP_INFO( pointX,pointY ).flags & FLAG_MS_ORE) cb->check(); else cb->uncheck();
702     cb = getCheckButton( *myDialogComponent, "BuyGoods" );
703     if( MP_INFO( pointX,pointY ).flags & FLAG_MB_GOODS) cb->check(); else cb->uncheck();
704     cb = getCheckButton( *myDialogComponent, "SellGoods" );
705     if( MP_INFO( pointX,pointY ).flags & FLAG_MS_GOODS) cb->check(); else cb->uncheck();
706     cb = getCheckButton( *myDialogComponent, "BuySteel" );
707     if( MP_INFO( pointX,pointY ).flags & FLAG_MB_STEEL) cb->check(); else cb->uncheck();
708     cb = getCheckButton( *myDialogComponent, "SellSteel" );
709     if( MP_INFO( pointX,pointY ).flags & FLAG_MS_STEEL) cb->check(); else cb->uncheck();
710     // connect signals
711     Button* applyButton = getButton( *myDialogComponent, "Apply" );
712     applyButton->clicked.connect( makeCallback(*this, &Dialog::applyPortButtonClicked ) );
713     Button* gotoButton = getButton( *myDialogComponent, "goto" );
714     gotoButton->clicked.connect( makeCallback( *this, &Dialog::gotoButtonClicked ) );
715 }
716 
applyMarketButtonClicked(Button * b)717 void Dialog::applyMarketButtonClicked( Button* b ){
718     CheckButton* cb;
719     cb = getCheckButton( *myDialogComponent, "BuyJobs" );
720     if( cb->state == CheckButton::STATE_CHECKED ){
721         MP_INFO( pointX,pointY ).flags |= FLAG_MB_JOBS;
722     } else {
723         MP_INFO( pointX,pointY ).flags &= ~FLAG_MB_JOBS;
724     }
725     cb = getCheckButton( *myDialogComponent, "SellJobs" );
726     if( cb->state == CheckButton::STATE_CHECKED ){
727         MP_INFO( pointX,pointY ).flags |= FLAG_MS_JOBS;
728     } else {
729         MP_INFO( pointX,pointY ).flags &= ~FLAG_MS_JOBS;
730     }
731     applyPortButtonClicked( b );
732 }
733 
applyPortButtonClicked(Button *)734 void Dialog::applyPortButtonClicked( Button* ){
735     CheckButton* cb;
736     cb = getCheckButton( *myDialogComponent, "BuyFood" );
737     if( cb->state == CheckButton::STATE_CHECKED ){
738         MP_INFO( pointX,pointY ).flags |= FLAG_MB_FOOD;
739     } else {
740         MP_INFO( pointX,pointY ).flags &= ~FLAG_MB_FOOD;
741     }
742     cb = getCheckButton( *myDialogComponent, "SellFood" );
743     if( cb->state == CheckButton::STATE_CHECKED ){
744         MP_INFO( pointX,pointY ).flags |= FLAG_MS_FOOD;
745     } else {
746         MP_INFO( pointX,pointY ).flags &= ~FLAG_MS_FOOD;
747     }
748     cb = getCheckButton( *myDialogComponent, "BuyCoal" );
749     if( cb->state == CheckButton::STATE_CHECKED ){
750         MP_INFO( pointX,pointY ).flags |= FLAG_MB_COAL;
751     } else {
752         MP_INFO( pointX,pointY ).flags &= ~FLAG_MB_COAL;
753     }
754     cb = getCheckButton( *myDialogComponent, "SellCoal" );
755     if( cb->state == CheckButton::STATE_CHECKED ){
756         MP_INFO( pointX,pointY ).flags |= FLAG_MS_COAL;
757     } else {
758         MP_INFO( pointX,pointY ).flags &= ~FLAG_MS_COAL;
759     }
760     cb = getCheckButton( *myDialogComponent, "BuyOre" );
761     if( cb->state == CheckButton::STATE_CHECKED ){
762         MP_INFO( pointX,pointY ).flags |= FLAG_MB_ORE;
763     } else {
764         MP_INFO( pointX,pointY ).flags &= ~FLAG_MB_ORE;
765     }
766     cb = getCheckButton( *myDialogComponent, "SellOre" );
767     if( cb->state == CheckButton::STATE_CHECKED ){
768         MP_INFO( pointX,pointY ).flags |= FLAG_MS_ORE;
769     } else {
770         MP_INFO( pointX,pointY ).flags &= ~FLAG_MS_ORE;
771     }
772     cb = getCheckButton( *myDialogComponent, "BuyGoods" );
773     if( cb->state == CheckButton::STATE_CHECKED ){
774         MP_INFO( pointX,pointY ).flags |= FLAG_MB_GOODS;
775     } else {
776         MP_INFO( pointX,pointY ).flags &= ~FLAG_MB_GOODS;
777     }
778     cb = getCheckButton( *myDialogComponent, "SellGoods" );
779     if( cb->state == CheckButton::STATE_CHECKED ){
780         MP_INFO( pointX,pointY ).flags |= FLAG_MS_GOODS;
781     } else {
782         MP_INFO( pointX,pointY ).flags &= ~FLAG_MS_GOODS;
783     }
784     cb = getCheckButton( *myDialogComponent, "BuySteel" );
785     if( cb->state == CheckButton::STATE_CHECKED ){
786         MP_INFO( pointX,pointY ).flags |= FLAG_MB_STEEL;
787     } else {
788         MP_INFO( pointX,pointY ).flags &= ~FLAG_MB_STEEL;
789     }
790     cb = getCheckButton( *myDialogComponent, "SellSteel" );
791     if( cb->state == CheckButton::STATE_CHECKED ){
792         MP_INFO( pointX,pointY ).flags |= FLAG_MS_STEEL;
793     } else {
794         MP_INFO( pointX,pointY ).flags &= ~FLAG_MS_STEEL;
795     }
796     desktop->remove( myDialogComponent );
797     blockingDialogIsOpen = false;
798     unRegisterDialog();
799 }
800 
okayLaunchRocketButtonClicked(Button *)801 void Dialog::okayLaunchRocketButtonClicked( Button* ){
802     launch_rocket( pointX, pointY );
803     desktop->remove( myDialogComponent );
804     blockingDialogIsOpen = false;
805     unRegisterDialog();
806 }
807 
808 
okayCoalSurveyButtonClicked(Button *)809 void Dialog::okayCoalSurveyButtonClicked( Button* ){
810     do_coal_survey();
811     desktop->remove( myDialogComponent );
812     blockingDialogIsOpen = false;
813     unRegisterDialog();
814 }
815 
okayBulldozeRiverButtonClicked(Button *)816 void Dialog::okayBulldozeRiverButtonClicked( Button* ){
817     river_bul_flag = 1;
818     desktop->remove( myDialogComponent );
819     check_bulldoze_area( pointX, pointY );
820     blockingDialogIsOpen = false;
821     unRegisterDialog();
822 }
823 
okayBulldozeShantyButtonClicked(Button *)824 void Dialog::okayBulldozeShantyButtonClicked( Button* ){
825     shanty_bul_flag = 1;
826     desktop->remove( myDialogComponent );
827     check_bulldoze_area( pointX, pointY );
828     blockingDialogIsOpen = false;
829     unRegisterDialog();
830 }
831 
okayBulldozeMonumentButtonClicked(Button *)832 void Dialog::okayBulldozeMonumentButtonClicked( Button* ){
833     monument_bul_flag = 1;
834     desktop->remove( myDialogComponent );
835     check_bulldoze_area( pointX, pointY );
836     blockingDialogIsOpen = false;
837     unRegisterDialog();
838 }
839 
gotoButtonClicked(Button *)840 void Dialog::gotoButtonClicked( Button* ){
841     getGameView()->show( MapPoint( pointX, pointY ) );
842 }
843 
closeDialogButtonClicked(Button *)844 void Dialog::closeDialogButtonClicked( Button* ){
845     closeDialog();
846 }
847 
closeDialog()848 void Dialog::closeDialog(){
849     desktop->remove( myDialogComponent );
850     if( iAmBlocking ){
851         blockingDialogIsOpen = false;
852     }
853     unRegisterDialog();
854 }
855