1 /*
2 Copyright (C) 2005 David Kamphausen <david.kamphausen@web.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 "MainLincity.hpp"
21
22 #include <SDL.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #include <iostream>
26 #include <stdio.h>
27 #include <physfs.h>
28
29 #include "lincity/init_game.h"
30 #include "lincity/simulate.h"
31 #include "lincity/lin-city.h"
32 #include "lincity/lc_locale.h"
33 #include "lincity/fileutil.h"
34 #include "lincity/loadsave.h"
35
36 #include "gui_interface/screen_interface.h"
37 #include "gui_interface/mps.h"
38 #include "gui_interface/shared_globals.h"
39
40 #include "TimerInterface.hpp"
41
42 #include "GameView.hpp"
43 #include "ScreenInterface.hpp"
44 #include "Dialog.hpp"
45 #include "Config.hpp"
46
47 extern void print_total_money(void);
48 extern void init_types(void);
49
50 int lincitySpeed = MED_TIME_FOR_YEAR;
51 /******************************************/
52
setLincitySpeed(int speed)53 void setLincitySpeed( int speed )
54 {
55 lincitySpeed = speed;
56 }
57
execute_timestep()58 void execute_timestep ()
59 {
60
61 if( lincitySpeed == 0 || blockingDialogIsOpen ) {
62 SDL_Delay(10); //don't burn cpu in active loop
63 return;
64 } else if ( lincitySpeed == fast_time_for_year) {
65 if ( (total_time % (10 - fast_time_for_year)) == 0 ) {
66 SDL_Delay(10);
67 /* fast = 1 => wait once for each 9 loop => 1.3 real second / game year
68 * beware it can warm hardware (nearly always active).
69 *
70 * fast = 9 = default => wait at each step => 12 real seconds / game year
71 * = nearly old behavior, except we no more skeep frames.
72 *
73 * On athlon-xp 2200+ (1600MHz) 750 MB + Nvidia geforce 420 MX (16 MB)
74 * this is the limiting factor for max_speed
75 * Removing it gives approximately the same speed as old-ng = 4.0 s/year
76 * instead of 24s/year with delay 10 (default fast = 9)
77 *
78 * SDL doc says to rely on at least 10 ms granurality on all OS without
79 * real time ability (Windows, Linux, MacOS X...) hence the trick
80 * of waiting 1/n loop.
81 */
82 }
83 } else
84 SDL_Delay(lincitySpeed);
85
86 // Do the simulation. Remember 1 month = 100 days, only the display fits real life :)
87 do_time_step();
88
89 //draw the updated city
90 //in FAST-Mode, update at the last day in Month, so print_stats will work.
91 if( ( lincitySpeed != fast_time_for_year ) ||
92 ( total_time % ( NUMOF_DAYS_IN_MONTH * getConfig()->skipMonthsFast ) ) == NUMOF_DAYS_IN_MONTH - 1 ){
93 print_stats ();
94 updateDate();
95 print_total_money();
96 getGameView()->requestRedraw();
97 } else if (fast_time_for_year != FAST_TIME_FOR_YEAR) { // The point of fast mode is to be really fast. So skip frames for speed by default.
98 getGameView()->requestRedraw(); // Users with fast machines who prefer nice animations in fast mode can set fast speed manually to get them back.
99 }
100 }
101
102 /*
103 * get Data form Lincity NG and Save City
104 */
saveCityNG(std::string newFilename)105 void saveCityNG( std::string newFilename ){
106 GameView* gv = getGameView();
107 if( gv ){ gv->writeOrigin(); }
108 save_city(const_cast<char*>( newFilename.c_str() ) );
109 }
110
111 /*
112 * Load City and do setup for Lincity NG.
113 */
loadCityNG(std::string filename)114 bool loadCityNG( std::string filename ){
115 /* TODO use PhysFS directly to load file instead of getRealDir hack */
116 const char* directory = PHYSFS_getRealDir(filename.c_str());
117 if( !directory ){
118 return false;
119 }
120 std::string dir = directory;
121 filename = dir + PHYSFS_getDirSeparator() + filename;
122 if( file_exists( const_cast<char*>( filename.c_str()) ) ){
123 load_city_2(const_cast<char*>(filename.c_str()));
124 update_avail_modules(0);
125 GameView* gv = getGameView();
126 if( gv ){ gv->readOrigin(); }
127 return true;
128 }
129 return false;
130 }
131
initLCengine()132 void initLCengine()
133 {
134 /* I18n */
135 lincity_set_locale ();
136
137 /* Set up the paths to certain files and directories */
138 init_path_strings ();
139
140 /* Make sure that things are installed where they should be */
141 // verify_package ();
142
143 /* Make sure the save directory exists */
144 check_savedir ();
145
146 /* Load preferences */
147 //load_lincityrc (); //oldgui stuff, unused in NG
148
149 /* Initialize random number generator */
150 srand (time (0));
151
152 /* Save preferences */
153 //save_lincityrc (); //oldgui stuff, unused in NG
154 // init_fonts (); // unsused in NG
155
156 initialize_monthgraph ();
157 // init_mouse_registry ();
158 // init_mini_map_mouse ();
159 mps_init();
160
161 // setcustompalette ();
162 // draw_background ();
163 // prog_box (_("Loading the game"), 1);
164 init_types ();
165 // means "init buttons" init_modules();
166
167 }
168
169
initLincity()170 void initLincity()
171 {
172 initLCengine();
173
174 // animation time
175 reset_start_time ();
176
177 screen_full_refresh ();
178
179 //load current game if it exists
180 if( ! loadCityNG( std::string( "9_currentGameNG.scn" ) ) ) {
181 //create a new City with village just in case
182 new_city( &main_screen_originx, &main_screen_originy, 1 );
183 }
184 }
185
186
doLincityStep()187 void doLincityStep()
188 {
189 /* Get timestamp for this iteration */
190 get_real_time();
191
192 execute_timestep ();
193
194 // return true;
195 }
196