Changeset - r2092:297c1ed07fe8
[Not reviewed]
master
0 3 0
truelight - 19 years ago 2005-07-17 13:34:19
truelight@openttd.org
(svn r2602) -Codechange: removed unused variable (disable_computer)
-Fix: Changed some stuff to make server-side AIs possible in the far future
3 files changed with 5 insertions and 6 deletions:
0 comments (0 inline, 0 general)
main_gui.c
Show inline comments
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "spritecache.h"
 
#include "strings.h"
 
#include "table/sprites.h"
 
#include "table/strings.h"
 
#include "map.h"
 
#include "tile.h"
 
#include "window.h"
 
#include "gui.h"
 
#include "viewport.h"
 
#include "gfx.h"
 
#include "player.h"
 
#include "command.h"
 
#include "news.h"
 
#include "town.h"
 
#include "vehicle.h"
 
#include "console.h"
 
#include "sound.h"
 
#include "network.h"
 
#include "signs.h"
 
#include "waypoint.h"
 

	
 
#include "network_data.h"
 
#include "network_client.h"
 
#include "network_server.h"
 

	
 
#include "table/animcursors.h"
 

	
 

	
 
/* Min/Max date for scenario editor */
 
static const uint MinDate = 0;     // 1920-01-01 (MAX_YEAR_BEGIN_REAL)
 
static const uint MaxDate = 29220; // 2000-01-01
 

	
 
extern void DoTestSave(void);
 
extern void DoTestLoad(void);
 

	
 
extern bool disable_computer;
 

	
 
static int _rename_id;
 
static int _rename_what;
 

	
 
static byte _terraform_size = 1;
 
static byte _last_built_railtype;
 
extern void GenerateWorld(int mode, uint size_x, uint size_y);
 

	
 
extern void GenerateIndustries(void);
 
extern void GenerateTowns(void);
 

	
 
void HandleOnEditTextCancel(void)
 
{
 
	switch(_rename_what) {
 
#ifdef ENABLE_NETWORK
 
	case 4:
 
		NetworkDisconnect();
 
		ShowNetworkGameWindow();
 
		break;
 
#endif /* ENABLE_NETWORK */
 
	}
 
}
 

	
 
void HandleOnEditText(WindowEvent *e)
 
{
 
	const char *b = e->edittext.str;
 
	int id;
 

	
 
	_cmd_text = b;
 

	
 
	id = _rename_id;
 

	
 
	switch (_rename_what) {
 
	case 0: /* Rename a s sign, if string is empty, delete sign */
 
		DoCommandP(0, id, 0, NULL, CMD_RENAME_SIGN | CMD_MSG(STR_280C_CAN_T_CHANGE_SIGN_NAME));
 
		break;
 
	case 1: /* Rename a waypoint */
 
		if (*b == 0) return;
 
		DoCommandP(0, id, 0, NULL, CMD_RENAME_WAYPOINT | CMD_MSG(STR_CANT_CHANGE_WAYPOINT_NAME));
 
		break;
 
#ifdef ENABLE_NETWORK
 
	case 2: /* Speak to.. */
 
		if (!_network_server)
 
			SEND_COMMAND(PACKET_CLIENT_CHAT)(NETWORK_ACTION_CHAT + (id & 0xFF), id & 0xFF, (id >> 8) & 0xFF, e->edittext.str);
 
		else
 
			NetworkServer_HandleChat(NETWORK_ACTION_CHAT + (id & 0xFF), id & 0xFF, (id >> 8) & 0xFF, e->edittext.str, NETWORK_SERVER_INDEX);
 
		break;
 
	case 3: { /* Give money, you can only give money in excess of loan */
 
		const Player *p = GetPlayer(_current_player);
openttd.c
Show inline comments
 
@@ -17,97 +17,96 @@
 
#include "viewport.h"
 
#include "window.h"
 
#include "player.h"
 
#include "command.h"
 
#include "town.h"
 
#include "industry.h"
 
#include "news.h"
 
#include "engine.h"
 
#include "sound.h"
 
#include "economy.h"
 
#include "fileio.h"
 
#include "hal.h"
 
#include "airport.h"
 
#include "ai.h"
 
#include "console.h"
 
#include "screenshot.h"
 
#include "network.h"
 
#include "signs.h"
 
#include "depot.h"
 
#include "waypoint.h"
 

	
 
#include <stdarg.h>
 

	
 
#ifdef GPMI
 
#include <gpmi.h>
 
#include <gpmi/packages/paths.h>
 
#endif /* GPMI */
 

	
 
void GenerateWorld(int mode, uint size_x, uint size_y);
 
void CallLandscapeTick(void);
 
void IncreaseDate(void);
 
void RunOtherPlayersLoop(void);
 
void DoPaletteAnimations(void);
 
void MusicLoop(void);
 
void ResetMusic(void);
 
void InitializeStations(void);
 
void DeleteAllPlayerStations(void);
 

	
 
extern void SetDifficultyLevel(int mode, GameOptions *gm_opt);
 
extern void DoStartupNewPlayer(bool is_ai);
 
extern void ShowOSErrorBox(const char *buf);
 

	
 
bool LoadSavegame(const char *filename);
 

	
 
extern void HalGameLoop(void);
 

	
 
uint32 _pixels_redrawn;
 
bool _dbg_screen_rect;
 
bool disable_computer; // We should get ride of this thing.. is only used for a debug-cheat
 
static byte _os_version = 0;
 

	
 
/* TODO: usrerror() for errors which are not of an internal nature but
 
 * caused by the user, i.e. missing files or fatal configuration errors.
 
 * Post-0.4.0 since Celestar doesn't want this in SVN before. --pasky */
 

	
 
void CDECL error(const char *s, ...) {
 
	va_list va;
 
	char buf[512];
 
	va_start(va, s);
 
	vsprintf(buf, s, va);
 
	va_end(va);
 

	
 
	ShowOSErrorBox(buf);
 
	if (_video_driver)
 
		_video_driver->stop();
 

	
 
	assert(0);
 
	exit(1);
 
}
 

	
 
void CDECL ShowInfoF(const char *str, ...)
 
{
 
	va_list va;
 
	char buf[1024];
 
	va_start(va, str);
 
	vsprintf(buf, str, va);
 
	va_end(va);
 
	ShowInfo(buf);
 
}
 

	
 
char * CDECL str_fmt(const char *str, ...)
 
{
 
	char buf[4096];
 
	va_list va;
 
	int len;
 
	char *p;
 

	
 
	va_start(va, str);
 
	len = vsprintf(buf, str, va);
 
	va_end(va);
 
	p = malloc(len + 1);
 
	if (p)
 
		memcpy(p, buf, len + 1);
 
	return p;
 
}
 

	
 

	
 
@@ -987,97 +986,97 @@ void SwitchMode(int new_mode)
 

	
 
	_in_state_game_loop = false;
 
}
 

	
 

	
 
// State controlling game loop.
 
// The state must not be changed from anywhere
 
// but here.
 
// That check is enforced in DoCommand.
 
void StateGameLoop(void)
 
{
 
	// dont execute the state loop during pause
 
	if (_pause) return;
 

	
 
	_in_state_game_loop = true;
 
	// _frame_counter is increased somewhere else when in network-mode
 
	//  Sidenote: _frame_counter is ONLY used for _savedump in non-MP-games
 
	//    Should that not be deleted? If so, the next 2 lines can also be deleted
 
	if (!_networking)
 
		_frame_counter++;
 

	
 
	if (_savedump_path[0] && (uint)_frame_counter >= _savedump_first && (uint)(_frame_counter -_savedump_first) % _savedump_freq == 0 ) {
 
		char buf[100];
 
		sprintf(buf, "%s%.5d.sav", _savedump_path, _frame_counter);
 
		SaveOrLoad(buf, SL_SAVE);
 
		if ((uint)_frame_counter >= _savedump_last) exit(1);
 
	}
 

	
 
	if (_game_mode == GM_EDITOR) {
 
		RunTileLoop();
 
		CallVehicleTicks();
 
		CallLandscapeTick();
 
		CallWindowTickEvent();
 
		NewsLoop();
 
	} else {
 
		// All these actions has to be done from OWNER_NONE
 
		//  for multiplayer compatibility
 
		uint p = _current_player;
 
		_current_player = OWNER_NONE;
 

	
 
		AnimateAnimatedTiles();
 
		IncreaseDate();
 
		RunTileLoop();
 
		CallVehicleTicks();
 
		CallLandscapeTick();
 

	
 
		// To bad the AI does not work in multiplayer, because states are not saved
 
		//  perfectly
 
		if (!disable_computer && !_networking)
 
		if (!_networking)
 
			RunOtherPlayersLoop();
 

	
 
		CallWindowTickEvent();
 
		NewsLoop();
 
		_current_player = p;
 
	}
 

	
 
	_in_state_game_loop = false;
 
}
 

	
 
static void DoAutosave(void)
 
{
 
	char buf[200];
 

	
 
	if (_patches.keep_all_autosave && _local_player != OWNER_SPECTATOR) {
 
		const Player *p = GetPlayer(_local_player);
 
		char *s;
 
		sprintf(buf, "%s%s", _path.autosave_dir, PATHSEP);
 

	
 
		SetDParam(0, p->name_1);
 
		SetDParam(1, p->name_2);
 
		SetDParam(2, _date);
 
		s = (char*)GetString(buf + strlen(_path.autosave_dir) + strlen(PATHSEP), STR_4004);
 
		strcpy(s, ".sav");
 
	} else { /* Save a maximum of 15 autosaves */
 
		int n = _autosave_ctr;
 
		_autosave_ctr = (_autosave_ctr + 1) & 15;
 
		sprintf(buf, "%s%sautosave%d.sav", _path.autosave_dir, PATHSEP, n);
 
	}
 

	
 
	DEBUG(misc, 2) ("Autosaving to %s", buf);
 
	if (SaveOrLoad(buf, SL_SAVE) != SL_OK)
 
		ShowErrorMessage(INVALID_STRING_ID, STR_AUTOSAVE_FAILED, 0, 0);
 
}
 

	
 
static void ScrollMainViewport(int x, int y)
 
{
 
	if (_game_mode != GM_MENU) {
 
		Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
		assert(w);
 

	
 
		WP(w,vp_d).scrollpos_x += x << w->viewport->zoom;
 
		WP(w,vp_d).scrollpos_y += y << w->viewport->zoom;
 
	}
 
}
 

	
 
static const int8 scrollamt[16][2] = {
 
	{ 0, 0},
players.c
Show inline comments
 
@@ -476,101 +476,103 @@ Player *DoStartupNewPlayer(bool is_ai)
 
	p = AllocatePlayer();
 
	if (p == NULL) return NULL;
 

	
 
	index = p->index;
 

	
 
	// Make a color
 
	p->player_color = GeneratePlayerColor();
 
	_player_colors[index] = p->player_color;
 
	p->name_1 = STR_SV_UNNAMED;
 
	p->is_active = true;
 

	
 
	p->money64 = p->player_money = p->current_loan = 100000;
 

	
 
	p->is_ai = is_ai;
 
	p->ai.state = 5; /* AIS_WANT_NEW_ROUTE */
 
	p->share_owners[0] = p->share_owners[1] = p->share_owners[2] = p->share_owners[3] = 0xFF;
 

	
 
	p->max_railtype = GetPlayerMaxRailtype(index);
 
	p->inaugurated_year = _cur_year;
 
	p->face = Random();
 

	
 
	GeneratePresidentName(p);
 

	
 
	InvalidateWindow(WC_GRAPH_LEGEND, 0);
 
	InvalidateWindow(WC_TOOLBAR_MENU, 0);
 
	InvalidateWindow(WC_CLIENT_LIST, 0);
 

	
 
	return p;
 
}
 

	
 
void StartupPlayers(void)
 
{
 
	// The AI starts like in the setting with +2 month max
 
	_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + RandomRange(60 * DAY_TICKS) + 1;
 
}
 

	
 
static void MaybeStartNewPlayer(void)
 
{
 
	uint n;
 
	Player *p;
 

	
 
	// count number of competitors
 
	n = 0;
 
	for(p=_players; p!=endof(_players); p++)
 
		if (p->is_active && p->is_ai)
 
			n++;
 

	
 
	// when there's a lot of computers in game, the probability that a new one starts is lower
 
	if (n < (uint)_opt.diff.max_no_competitors && n < RandomRange(_opt.diff.max_no_competitors + 2))
 
	if (n < (uint)_opt.diff.max_no_competitors)
 
		if (n < (!_network_server) ? RandomRange(_opt.diff.max_no_competitors + 2) : InteractiveRandomRange(_opt.diff.max_no_competitors + 2))
 
		DoStartupNewPlayer(true);
 

	
 
	// The next AI starts like the difficulty setting said, with +2 month max
 
	_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + RandomRange(60 * DAY_TICKS) + 1;
 
	_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + 1;
 
	_next_competitor_start += (!_network_server) ? RandomRange(60 * DAY_TICKS) : InteractiveRandomRange(60 * DAY_TICKS);
 
}
 

	
 
void InitializePlayers(void)
 
{
 
	int i;
 
	memset(_players, 0, sizeof(_players));
 
	for(i = 0; i != MAX_PLAYERS; i++)
 
		_players[i].index=i;
 
	_cur_player_tick_index = 0;
 
}
 

	
 
void OnTick_Players(void)
 
{
 
	Player *p;
 

	
 
	if (_game_mode == GM_EDITOR)
 
		return;
 

	
 
	p = GetPlayer(_cur_player_tick_index);
 
	_cur_player_tick_index = (_cur_player_tick_index + 1) % MAX_PLAYERS;
 
	if (p->name_1 != 0) GenerateCompanyName(p);
 

	
 
	if (!_networking && _game_mode != GM_MENU && !--_next_competitor_start) {
 
		MaybeStartNewPlayer();
 
	}
 
}
 

	
 
void RunOtherPlayersLoop(void)
 
{
 
	Player *p;
 

	
 
	_is_ai_player = true;
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active && p->is_ai) {
 
			_current_player = p->index;
 
			if (_patches.ainew_active)
 
				AiNewDoGameLoop(p);
 
			else
 
				AiDoGameLoop(p);
 
		}
 
	}
 

	
 
	_is_ai_player = false;
 
	_current_player = OWNER_NONE;
 
}
 

	
 
// index is the next parameter in _decode_parameters to set up
0 comments (0 inline, 0 general)