1 /*
2  * Seven Kingdoms: Ancient Adversaries
3  *
4  * Copyright 1997,1998 Enlight Software Ltd.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 //Filename    : OCONFIG.CPP
22 //Description : Config Object
23 
24 #include <OSYS.h>
25 #include <OHELP.h>
26 #include <ONATION.h>
27 #include <OUNITRES.h>
28 #include <OFIRMRES.h>
29 #include <OCONFIG.h>
30 #include <dbglog.h>
31 #include "gettext.h"
32 #include <FilePath.h>
33 
34 DBGLOG_DEFAULT_CHANNEL(Config);
35 
36 
37 // ------- define difficult table ---------//
38 
39 static char  table_ai_nation_count[5] = { 2, 4, 6, 6, 6 };
40 static short table_start_up_cash[5] = { OPTION_HIGH, OPTION_HIGH, OPTION_MODERATE, OPTION_MODERATE, OPTION_LOW };
41 static short table_ai_start_up_cash[5] = { OPTION_LOW, OPTION_MODERATE, OPTION_MODERATE, OPTION_HIGH, OPTION_VERY_HIGH };
42 static char  table_ai_aggressiveness[5] = { OPTION_LOW, OPTION_LOW, OPTION_MODERATE, OPTION_HIGH, OPTION_VERY_HIGH };
43 static short table_start_up_independent_town[5] = { 30, 30, 15, 15, 7 };
44 static short table_start_up_raw_site[5] = { 7, 6, 6, 4, 3, };
45 static char  table_explore_whole_map[5] = { 1, 1, 1, 1, 0 };
46 static char  table_fog_of_war[5] = { 0, 0, 0, 1, 1 };
47 static char  table_new_independent_town_emerge[5] = { 1, 1, 1, 1, 1 };
48 static char  table_independent_town_resistance[5] = { OPTION_LOW, OPTION_LOW, OPTION_MODERATE, OPTION_HIGH, OPTION_HIGH };
49 static char  table_random_event_frequency[5] = { OPTION_NONE, OPTION_LOW, OPTION_MODERATE, OPTION_MODERATE, OPTION_MODERATE };
50 static char  table_new_nation_emerge[5] = { 0, 0, 0, 1, 1 };
51 static char  table_monster_type[5] = { OPTION_MONSTER_DEFENSIVE, OPTION_MONSTER_DEFENSIVE, OPTION_MONSTER_DEFENSIVE, OPTION_MONSTER_OFFENSIVE, OPTION_MONSTER_OFFENSIVE };
52 static char  table_start_up_has_mine_nearby[5] = { 1, 1, 0, 0, 0 };
53 static char  table_random_start_up[5] = { 0, 0, 0, 1, 1 };
54 
55 //--------- Begin of function Config::init -----------//
56 
init()57 void Config::init()
58 {
59 	default_game_setting();
60 	default_cheat_setting();
61 	default_local_game_setting();
62 	default_preference();
63 }
64 //--------- End of function Config::init --------//
65 
66 
67 //--------- Begin of function Config::deinit -----------//
68 
deinit()69 void Config::deinit()
70 {
71 	config.save("CONFIG.DAT");		// save the config when the game quits
72 
73 	default_game_setting();
74 	default_local_game_setting();
75 	default_preference();
76 }
77 //--------- End of function Config::deinit --------//
78 
79 
80 //--------- Begin of function Config::default_game_setting ---------//
default_game_setting()81 void Config::default_game_setting()
82 {
83 	// -------- GLOBAL GAME SETTING -------- //
84 
85 	ai_nation_count = 6 ;
86 	start_up_cash = OPTION_MODERATE;
87 //	start_up_food = MEDIUM_STARTUP_RESOURCE;
88 	ai_start_up_cash = OPTION_MODERATE;
89 //	ai_start_up_food = MEDIUM_STARTUP_RESOURCE;
90 	ai_aggressiveness = OPTION_MODERATE;
91 	start_up_independent_town = 15;
92 	start_up_raw_site = 6;
93 	difficulty_level = OPTION_CUSTOM;
94 
95 	explore_whole_map = 1;
96 	fog_of_war   = 0;
97 
98 	terrain_set = 1;
99 	latitude = 45;
100 	weather_effect = 1;		// damage done by weather
101 	land_mass = OPTION_MODERATE;
102 
103 	new_independent_town_emerge = 1;
104 	independent_town_resistance = OPTION_MODERATE;
105 	random_event_frequency = OPTION_NONE;
106 	monster_type = OPTION_MONSTER_DEFENSIVE;
107 	new_nation_emerge = 1;
108 	start_up_has_mine_nearby = 0;
109    random_start_up = 0;
110 
111 	change_difficulty(OPTION_VERY_EASY);
112 
113 	//-------- goal --------//
114 
115 	goal_destroy_monster = 0;
116 	goal_population_flag = 0;
117 	goal_economic_score_flag = 0;
118 	goal_total_score_flag = 0;
119 	goal_year_limit_flag = 0;
120 
121 	goal_population = 300;
122 	goal_economic_score = 300;
123 	goal_total_score = 600;
124 	goal_year_limit = 20;
125 
126 	// game setting on fire
127 	fire_spread_rate = 0;          // 0 to disable, 10 for normal
128 	wind_spread_fire_rate = 5;     // 0 to disable, 5 for normal
129 	fire_fade_rate = 2;            // 1 for slow, 2 for fast
130 	fire_restore_prob = 80;        // 0 to 100, 5 for normal (with spreading)
131 	rain_reduce_fire_rate = 5;     // 0 to 20, 5 for normal
132 	fire_damage = 2;               // 0 to disable 2 for normal
133 }
134 //--------- End of function Config::default_game_setting ---------//
135 
136 
137 //--------- Begin of function Config::default_game_setting ---------//
default_cheat_setting()138 void Config::default_cheat_setting()
139 {
140 	show_ai_info = sys.debug_session;
141 	fast_build = sys.debug_session;
142 	disable_ai_flag = 0;
143 	king_undie_flag = sys.testing_session;
144 }
145 //--------- End of function Config::default_game_setting ---------//
146 
147 
148 //--------- Begin of function Config::default_local_game_setting ---------//
default_local_game_setting()149 void Config::default_local_game_setting()
150 {
151 	race_id = 1;
152 	strcpy(player_name, "New Player");
153 	player_nation_color = 1;
154 	expired_flag = 0;
155 }
156 //--------- End of function Config::default_local_game_setting ---------//
157 
158 
159 //--------- Begin of function Config::default_preference ---------//
160 
default_preference()161 void Config::default_preference()
162 {
163 	opaque_report = 0;			// opaque report instead of transparent report
164 	disp_news_flag = OPTION_DISPLAY_ALL_NEWS;
165 
166 	scroll_speed = 5;
167 	frame_speed	 = 12;
168 
169 	help_mode = DETAIL_HELP;
170 	disp_town_name = 1;
171 	disp_spy_sign = 1;
172 	show_all_unit_icon = 1;		// 0:show icon when pointed, 1:always
173 	show_unit_path = 3;			// bit 0 show unit path on ZoomMatrix, bit 1 for MapMatrix
174 
175 	// music setting
176 	music_flag = 1;
177 	cd_music_volume = 100;
178 	wav_music_volume = 100;
179 
180 	// sound effect setting
181 	sound_effect_flag = 1;
182 	sound_effect_volume = 100;
183 	pan_control = 1;
184 
185 	// weather visual effect flags
186 	lightning_visual = 1;
187 	earthquake_visual = 1;
188 	rain_visual = 1;
189 	snow_visual = 1;
190 	snow_ground = 0;			// 0=disable, 1=i_snow, 2=snow_res
191 
192 	// weather audio effect flags
193 	lightning_audio = 1;
194 	earthquake_audio = 1;
195 	rain_audio = 1;
196 	snow_audio = 0;				// not used
197 	wind_audio = 1;
198 
199 	// weather visual effect parameters
200 	lightning_brightness = 20;
201 	cloud_darkness = 5;
202 
203 	// weather audio effect parameters
204 	lightning_volume = 100;
205 	earthquake_volume = 100;
206 	rain_volume = 90;
207 	snow_volume = 100;
208 	wind_volume = 70;
209 
210 	// other
211 	blacken_map  = 1;
212 	explore_mask_method = 2;
213 	fog_mask_method = 2;
214 
215 	enable_weather_visual();
216 	enable_weather_audio();
217 	cloud_darkness = 0;
218 }
219 //--------- End of function Config::default_preference ---------//
220 
221 
222 //--------- Begin of function Config::change_game_setting --------//
223 // for synchronize the game setting with the host before a multiplayer game
change_game_setting(Config & c)224 void Config::change_game_setting( Config &c )
225 {
226 	//-------- game settings  ---------//
227 	ai_nation_count        = c.ai_nation_count;
228 	start_up_cash          = c.start_up_cash;
229 	// start_up_food          = c.start_up_food;
230 	ai_start_up_cash       = c.ai_start_up_cash;
231 	// ai_start_up_food       = c.ai_start_up_food;
232 	ai_aggressiveness      = c.ai_aggressiveness;
233 	start_up_independent_town = c.start_up_independent_town;
234 	start_up_raw_site      = c.start_up_raw_site;
235 	difficulty_level       = c.difficulty_level;
236 
237 	explore_whole_map      = c.explore_whole_map;
238 	fog_of_war             = c.fog_of_war;
239 
240 	terrain_set            = c.terrain_set;
241 	latitude               = c.latitude;
242 	weather_effect         = c.weather_effect;
243 	land_mass              = c.land_mass;
244 
245 	new_independent_town_emerge = c.new_independent_town_emerge;
246 	independent_town_resistance = c.independent_town_resistance;
247 	random_event_frequency = c.random_event_frequency;
248 	monster_type           = c.monster_type;
249 	new_nation_emerge      = c.new_nation_emerge;
250 	start_up_has_mine_nearby = c.start_up_has_mine_nearby;
251 	random_start_up 		  = c.random_start_up;
252 
253 	// --------- goal ---------//
254 
255 	goal_destroy_monster      = c.goal_destroy_monster;
256 	goal_population_flag      = c.goal_population_flag;
257 	goal_economic_score_flag  = c.goal_economic_score_flag;
258 	goal_total_score_flag  	  = c.goal_total_score_flag;
259 	goal_year_limit_flag   	  = c.goal_year_limit_flag;
260 	goal_population        	  = c.goal_population;
261 	goal_economic_score       = c.goal_economic_score;
262 	goal_total_score       	  = c.goal_total_score;
263 	goal_year_limit        	  = c.goal_year_limit;
264 
265 	// ------- game setting on fire ---------//
266 
267 	fire_spread_rate       = c.fire_spread_rate;
268 	wind_spread_fire_rate  = c.wind_spread_fire_rate;
269 	fire_fade_rate         = c.fire_fade_rate;
270 	fire_restore_prob      = c.fire_restore_prob;
271 	rain_reduce_fire_rate  = c.rain_reduce_fire_rate;
272 	fire_damage            = c.fire_damage;
273 }
274 //--------- End of function Config::change_game_setting --------//
275 
276 
277 //--------- Begin of function Config::change_game_setting --------//
278 // for saving config after changing option
change_preference(Config & c)279 void Config::change_preference( Config &c )
280 {
281 	opaque_report         = c.opaque_report;
282 	disp_news_flag        = c.disp_news_flag;
283 
284 	scroll_speed          = c.scroll_speed;
285 	frame_speed           = c.frame_speed;
286 
287 	help_mode             = c.help_mode;
288 	disp_town_name        = c.disp_town_name;
289 	disp_spy_sign         = c.disp_spy_sign;
290 	show_all_unit_icon    = c.show_all_unit_icon;
291 	show_unit_path        = c.show_unit_path;
292 
293 	//------- sound effect --------//
294 
295 	music_flag            = c.music_flag;
296 	cd_music_volume       = c.cd_music_volume;
297 	wav_music_volume      = c.wav_music_volume;
298 	sound_effect_flag     = c.sound_effect_flag;
299 	sound_effect_volume   = c.sound_effect_volume;
300 	pan_control           = c.pan_control;
301 
302 	//------- weather visual effect flags -------//
303 
304 	lightning_visual      = c.lightning_visual;
305 	earthquake_visual     = c.earthquake_visual;
306 	rain_visual           = c.rain_visual;
307 	snow_visual           = c.snow_visual;
308 	snow_ground           = c.snow_ground;
309 
310 	//-------- weather audio effect flags -------//
311 
312 	lightning_audio       = c.lightning_audio;
313 	earthquake_audio      = c.earthquake_audio;
314 	rain_audio            = c.rain_audio;
315 	snow_audio            = c.snow_audio;
316 	wind_audio            = c.wind_audio;
317 
318 	//--------- weather visual effect parameters --------//
319 
320 	lightning_brightness  = c.lightning_brightness;
321 	cloud_darkness        = c.cloud_darkness;
322 
323 	//-------- weather audio effect parameters ----------//
324 
325 	lightning_volume = c.lightning_volume;
326 	earthquake_volume = c.earthquake_volume;
327 	rain_volume = c.rain_volume;
328 	snow_volume = c.snow_volume;
329 	wind_volume = c.wind_volume;
330 
331 	//------------ map prefernce -------------//
332 
333 	blacken_map = c.blacken_map;
334 	explore_mask_method = c.explore_mask_method;
335 	fog_mask_method = c.fog_mask_method;
336 }
337 
338 
339 //--------- Begin of function Config::enable_weather_visual --------//
enable_weather_visual()340 void Config::enable_weather_visual()
341 {
342 	lightning_visual = 1;
343 	earthquake_visual = 1;
344 	rain_visual = 1;
345 	snow_visual = 1;
346 	snow_ground = 0;			// 0=disable, 1=i_snow, 2=snow_res
347 	cloud_darkness = 1;
348 }
349 //--------- End of function Config::enable_weather_visual --------//
350 
351 
352 //--------- Begin of function Config::disable_weather_visual --------//
disable_weather_visual()353 void Config::disable_weather_visual()
354 {
355 	lightning_visual = 0;
356 	earthquake_visual = 0;
357 	rain_visual = 0;
358 	snow_visual = 0;
359 	snow_ground = 0;			// 0=disable, 1=i_snow, 2=snow_res
360 	cloud_darkness = 0;
361 }
362 //--------- End of function Config::disable_weather_visual --------//
363 
364 
365 //--------- Begin of function Config::enable_weather_audio --------//
enable_weather_audio()366 void Config::enable_weather_audio()
367 {
368 	lightning_audio = 1;
369 	earthquake_audio = 1;
370 	rain_audio = 1;
371 	snow_audio = 1;				// not used
372 	wind_audio = 1;
373 }
374 //--------- End of function Config::enable_weather_audio --------//
375 
376 
377 //--------- Begin of function Config::disable_weather_audio --------//
disable_weather_audio()378 void Config::disable_weather_audio()
379 {
380 	lightning_audio = 0;
381 	earthquake_audio = 0;
382 	rain_audio = 0;
383 	snow_audio = 0;				// not used
384 	wind_audio = 0;
385 }
386 //--------- End of function Config::disable_weather_audio --------//
387 
388 
389 //--------- Begin of function Config::save -------------//
save(const char * filename)390 int Config::save(const char *filename)
391 {
392 	FilePath full_path(sys.dir_config);
393 	File configFile;
394 
395 	full_path += filename;
396 	if( full_path.error_flag )
397 		return 0;
398 
399 	if( !configFile.file_create(full_path) )
400 		return 0;
401 
402 	int retFlag = write_file(&configFile);
403 
404 	configFile.file_close();
405 
406 	return retFlag;
407 
408 }
409 //--------- End of function Config::save -------------//
410 
411 
412 //--------- Begin of function Config::load -------------//
413 //
414 // if load() fails, call init to re-initialize it
415 //
load(const char * filename)416 int Config::load(const char *filename)
417 {
418 	FilePath full_path(sys.dir_config);
419 	File configFile;
420 
421 	full_path += filename;
422 	if( full_path.error_flag )
423 		return 0;
424 
425 	if( !misc.is_file_exist(full_path) || !configFile.file_open(full_path) )
426 		return 0;
427 
428 	int retFlag = 0;
429 
430 	// check file size is the same
431 	if( configFile.file_size() == 144 )
432 	{
433 		retFlag = read_file(&configFile);
434 	}
435 
436 	configFile.file_close();
437 
438 	return retFlag;
439 }
440 //--------- End of function Config::load -------------//
441 
442 
443 //--------- Begin of function Config::reset_game_setting ---------//
reset_cheat_setting()444 void Config::reset_cheat_setting()
445 {
446 	show_ai_info = 0;
447 	fast_build = 0;
448 	disable_ai_flag = 0;
449 	king_undie_flag = 0;
450 }
451 //--------- End of function Config::reset_game_setting ---------//
452 
453 
454 //------- Begin of function Config::single_player_difficulty --------//
single_player_difficulty()455 int Config::single_player_difficulty()
456 {
457 	int score = 10;
458 	score += ai_nation_count * 6;
459 	if( !explore_whole_map)
460 		score += 7;
461 	if( fog_of_war )
462 		score += 7;
463 	score += (7 - start_up_raw_site) * 5;
464 
465 //	if( start_up_cash <= SMALL_STARTUP_RESOURCE )
466 //		score += 16;
467 //	else if( start_up_cash < LARGE_STARTUP_RESOURCE )
468 //		score += 8;
469 //	else
470 //		score += 0;
471 	score += (4 - start_up_cash) * 6;
472 
473 //	if( ai_start_up_cash <= SMALL_STARTUP_RESOURCE )
474 //		score += 0;
475 //	else if( ai_start_up_cash < LARGE_STARTUP_RESOURCE )
476 //		score += 8;
477 //	else
478 //		score += 16;
479 	score += (ai_start_up_cash - 1 ) * 6;
480 
481 	score += (ai_aggressiveness -1) * 10;
482 	score += (independent_town_resistance -1) * 5;
483 	if( new_nation_emerge )
484 		score += 6;
485 	score += random_event_frequency * 2;
486 	switch( monster_type )
487 	{
488 	case OPTION_MONSTER_NONE:
489 		break;
490 	case OPTION_MONSTER_DEFENSIVE:
491 		score += 6;
492 		break;
493 	case OPTION_MONSTER_OFFENSIVE:
494 		score += 16;
495 		break;
496 	default:
497 		err_here();
498 	}
499 
500 	if( !start_up_has_mine_nearby )
501 		score += 6;
502 	return score;
503 }
504 //------- End of function Config::single_player_difficulty --------//
505 
506 
507 //------- Begin of function Config::multi_player_difficulty --------//
multi_player_difficulty(int remotePlayers)508 int Config::multi_player_difficulty(int remotePlayers)
509 {
510 	short totalOpp = ai_nation_count + remotePlayers;
511 	if( totalOpp > MAX_NATION-1 )
512 		totalOpp = MAX_NATION-1;
513 	return single_player_difficulty() + (totalOpp - ai_nation_count) * 6;
514 }
515 //------- End of function Config::multi_player_difficulty --------//
516 
517 
518 //------- Begin of function Config::multi_player_difficulty --------//
change_difficulty(int difficulty)519 void Config::change_difficulty(int difficulty)
520 {
521 	err_when( difficulty < OPTION_VERY_EASY || difficulty > OPTION_VERY_HARD );
522 
523 	difficulty_level = difficulty;
524 
525 	ai_nation_count             = table_ai_nation_count[difficulty];
526 	start_up_cash               = table_start_up_cash[difficulty];
527 	// start_up_food               = table_start_up_food[difficulty];
528 	ai_start_up_cash            = table_ai_start_up_cash[difficulty];
529 	// ai_start_up_food            = table_ai_start_up_food[difficulty];
530 	ai_aggressiveness           = table_ai_aggressiveness[difficulty];
531 	start_up_independent_town   = table_start_up_independent_town[difficulty];
532 	start_up_raw_site           = table_start_up_raw_site[difficulty];
533 	explore_whole_map           = table_explore_whole_map[difficulty];
534 	fog_of_war                  = table_fog_of_war[difficulty];
535 
536 	new_independent_town_emerge = table_new_independent_town_emerge[difficulty];
537 	independent_town_resistance = table_independent_town_resistance[difficulty];
538 	random_event_frequency      = table_random_event_frequency[difficulty];
539 	new_nation_emerge           = table_new_nation_emerge[difficulty];
540 	monster_type                = table_monster_type[difficulty];
541 	start_up_has_mine_nearby    = table_start_up_has_mine_nearby[difficulty];
542 	random_start_up    			 = table_random_start_up[difficulty];
543 }
544 //------- End of function Config::change_difficulty --------//
545 
546