dedicated.c
author Darkvater
Mon, 18 Jul 2005 00:17:19 +0000
changeset 2118 aec8042f000b
parent 1959 c2c3a9850c2e
child 2159 f6284cf5fab0
permissions -rw-r--r--
(svn r2628) - Fix: Planting trees does not result in a MapSize() assertion anymore; introduced in r2598
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     1
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1829
diff changeset
     2
#include "openttd.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1290
diff changeset
     3
#include "debug.h"
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     4
#include "network.h"
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     5
#include "hal.h"
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     6
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     7
#ifdef ENABLE_NETWORK
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     8
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     9
#include "gfx.h"
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    10
#include "window.h"
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    11
#include "command.h"
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    12
#include "console.h"
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    13
#ifdef WIN32
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    14
#	include <windows.h> /* GetTickCount */
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    15
#	include <conio.h>
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    16
#endif
781
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    17
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    18
#ifdef __OS2__
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    19
#	include <sys/time.h> /* gettimeofday */
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    20
#	include <sys/types.h>
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    21
#	include <unistd.h>
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    22
#	include <conio.h>
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    23
#	define STDIN 0  /* file descriptor for standard input */
810
a1494b19bd2a (svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents: 796
diff changeset
    24
a1494b19bd2a (svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents: 796
diff changeset
    25
	extern void OS2_SwitchToConsoleMode();
781
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    26
#endif
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    27
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    28
#ifdef UNIX
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    29
#	include <sys/time.h> /* gettimeofday */
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    30
#	include <sys/types.h>
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    31
#	include <unistd.h>
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
    32
#	include <signal.h>
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    33
#	define STDIN 0  /* file descriptor for standard input */
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    34
#endif
770
c2eacca29601 (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 736
diff changeset
    35
#ifdef __MORPHOS__
1290
7fbc2fdfa203 (svn r1794) Make the dedicated server compile again under MorphOS (tokai)
tron
parents: 1114
diff changeset
    36
/* Voids the fork, option will be disabled for MorphOS build anyway, because
7fbc2fdfa203 (svn r1794) Make the dedicated server compile again under MorphOS (tokai)
tron
parents: 1114
diff changeset
    37
 * MorphOS doesn't support forking (could only implemented with lots of code
7fbc2fdfa203 (svn r1794) Make the dedicated server compile again under MorphOS (tokai)
tron
parents: 1114
diff changeset
    38
 * changes here). */
7fbc2fdfa203 (svn r1794) Make the dedicated server compile again under MorphOS (tokai)
tron
parents: 1114
diff changeset
    39
int fork(void) { return -1; }
7fbc2fdfa203 (svn r1794) Make the dedicated server compile again under MorphOS (tokai)
tron
parents: 1114
diff changeset
    40
int dup2(int oldd, int newd) { return -1; }
770
c2eacca29601 (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 736
diff changeset
    41
#endif
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    42
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    43
// This file handles all dedicated-server in- and outputs
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    44
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    45
static void *_dedicated_video_mem;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    46
1414
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
    47
extern bool SafeSaveOrLoad(const char *filename, int mode, int newgm);
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
    48
extern void SwitchMode(int new_mode);
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
    49
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
    50
#ifdef UNIX
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    51
/* We want to fork our dedicated server */
774
bb9ec520a1b1 (svn r1240) -Fix: OpenTTD once again compiles if ENABLE_NETWORK is disabled.
darkvater
parents: 770
diff changeset
    52
void DedicatedFork(void)
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    53
{
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    54
	/* Fork the program */
1406
f5da270a654f (svn r1910) Move two variables out of variables.h which are only used locally
tron
parents: 1301
diff changeset
    55
	pid_t pid = fork();
f5da270a654f (svn r1910) Move two variables out of variables.h which are only used locally
tron
parents: 1301
diff changeset
    56
	switch (pid) {
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    57
		case -1:
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    58
			perror("Unable to fork");
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    59
			exit(1);
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    60
		case 0:
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    61
			// We're the child
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    62
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    63
			/* Open the log-file to log all stuff too */
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    64
			_log_file_fd = fopen(_log_file, "a");
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    65
			if (!_log_file_fd) {
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    66
				perror("Unable to open logfile");
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    67
				exit(1);
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    68
			}
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    69
			/* Redirect stdout and stderr to log-file */
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    70
			if (dup2(fileno(_log_file_fd), fileno(stdout)) == -1) {
1626
b0cda32b478a (svn r2130) Various spelling fixes in messages.
pasky
parents: 1614
diff changeset
    71
				perror("Rerouting stdout");
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    72
				exit(1);
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    73
			}
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    74
			if (dup2(fileno(_log_file_fd), fileno(stderr)) == -1) {
1626
b0cda32b478a (svn r2130) Various spelling fixes in messages.
pasky
parents: 1614
diff changeset
    75
				perror("Rerouting stderr");
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    76
				exit(1);
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    77
			}
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    78
			break;
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    79
		default:
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    80
			// We're the parent
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    81
			printf("Loading dedicated server...\n");
1406
f5da270a654f (svn r1910) Move two variables out of variables.h which are only used locally
tron
parents: 1301
diff changeset
    82
			printf("  - Forked to background with pid %d\n", pid);
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    83
			exit(0);
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    84
	}
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    85
}
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    86
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
    87
/* Signal handlers */
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    88
static void DedicatedSignalHandler(int sig)
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
    89
{
736
75bcef85daeb (svn r1188) -Fix: [Network] Fixed that CTRL+<key> could sometimes hang a dedicated
truelight
parents: 721
diff changeset
    90
	_exit_game = true;
75bcef85daeb (svn r1188) -Fix: [Network] Fixed that CTRL+<key> could sometimes hang a dedicated
truelight
parents: 721
diff changeset
    91
	signal(sig, DedicatedSignalHandler);
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
    92
}
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
    93
#endif
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
    94
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
    95
#ifdef WIN32
1114
74e602e41692 (svn r1615) -Fix: [1107350] console ignoring return character occasionally. For everyone that is running 2 dedicated servers on 1 windows machine, console input is now correct (event was the same so it was random which console received the input)
darkvater
parents: 1109
diff changeset
    96
#include <time.h>
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
    97
HANDLE hEvent;
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
    98
static HANDLE hThread; // Thread to close
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
    99
static char _win_console_thread_buffer[200];
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   100
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   101
/* Windows Console thread. Just loop and signal when input has been received */
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   102
void WINAPI CheckForConsoleInput(void)
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   103
{
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   104
	while (true) {
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   105
		fgets(_win_console_thread_buffer, lengthof(_win_console_thread_buffer), stdin);
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   106
		SetEvent(hEvent); // signal input waiting that the line is ready
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   107
	}
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   108
}
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   109
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   110
void CreateWindowsConsoleThread(void)
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   111
{
1114
74e602e41692 (svn r1615) -Fix: [1107350] console ignoring return character occasionally. For everyone that is running 2 dedicated servers on 1 windows machine, console input is now correct (event was the same so it was random which console received the input)
darkvater
parents: 1109
diff changeset
   112
	static char tbuffer[9];
1762
2441d8946237 (svn r2266) - Feature: it is now possible to start a dedicated server on Win98/95, for the few sadistic geeks that really want to do this (thanks for testing Hackykid). Also fix up another glitch in console output
Darkvater
parents: 1626
diff changeset
   113
	DWORD dwThreadId;
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   114
	/* Create event to signal when console input is ready */
1114
74e602e41692 (svn r1615) -Fix: [1107350] console ignoring return character occasionally. For everyone that is running 2 dedicated servers on 1 windows machine, console input is now correct (event was the same so it was random which console received the input)
darkvater
parents: 1109
diff changeset
   115
	hEvent = CreateEvent(NULL, false, false, _strtime(tbuffer));
74e602e41692 (svn r1615) -Fix: [1107350] console ignoring return character occasionally. For everyone that is running 2 dedicated servers on 1 windows machine, console input is now correct (event was the same so it was random which console received the input)
darkvater
parents: 1109
diff changeset
   116
	if (hEvent == NULL)
74e602e41692 (svn r1615) -Fix: [1107350] console ignoring return character occasionally. For everyone that is running 2 dedicated servers on 1 windows machine, console input is now correct (event was the same so it was random which console received the input)
darkvater
parents: 1109
diff changeset
   117
		error("Cannot create console event!");
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   118
1762
2441d8946237 (svn r2266) - Feature: it is now possible to start a dedicated server on Win98/95, for the few sadistic geeks that really want to do this (thanks for testing Hackykid). Also fix up another glitch in console output
Darkvater
parents: 1626
diff changeset
   119
	hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, NULL, 0, &dwThreadId);
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   120
	if (hThread == NULL)
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   121
		error("Cannot create console thread!");
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   122
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   123
	DEBUG(misc, 0) ("Windows console thread started...");
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   124
}
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   125
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   126
void CloseWindowsConsoleThread(void)
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   127
{
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   128
	CloseHandle(hThread);
1114
74e602e41692 (svn r1615) -Fix: [1107350] console ignoring return character occasionally. For everyone that is running 2 dedicated servers on 1 windows machine, console input is now correct (event was the same so it was random which console received the input)
darkvater
parents: 1109
diff changeset
   129
	CloseHandle(hEvent);
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   130
	DEBUG(misc, 0) ("Windows console thread shut down...");
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   131
}
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   132
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   133
#endif
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   134
1301
bf64ba5b6774 (svn r1805) Teach the driver layer a few things about const correctness
tron
parents: 1299
diff changeset
   135
static const char *DedicatedVideoStart(const char * const *parm)
bf64ba5b6774 (svn r1805) Teach the driver layer a few things about const correctness
tron
parents: 1299
diff changeset
   136
{
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   137
	_screen.width = _screen.pitch = _cur_resolution[0];
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   138
	_screen.height = _cur_resolution[1];
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   139
	_dedicated_video_mem = malloc(_cur_resolution[0]*_cur_resolution[1]);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   140
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   141
	_debug_net_level = 6;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   142
	_debug_misc_level = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   143
626
35294912464a (svn r1056) -Fix: [Network] Give the dedicated-server always a console in windows (sign_de)
truelight
parents: 543
diff changeset
   144
#ifdef WIN32
35294912464a (svn r1056) -Fix: [Network] Give the dedicated-server always a console in windows (sign_de)
truelight
parents: 543
diff changeset
   145
	// For win32 we need to allocate an console (debug mode does the same)
35294912464a (svn r1056) -Fix: [Network] Give the dedicated-server always a console in windows (sign_de)
truelight
parents: 543
diff changeset
   146
	CreateConsole();
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   147
	CreateWindowsConsoleThread();
626
35294912464a (svn r1056) -Fix: [Network] Give the dedicated-server always a console in windows (sign_de)
truelight
parents: 543
diff changeset
   148
	SetConsoleTitle("OpenTTD Dedicated Server");
35294912464a (svn r1056) -Fix: [Network] Give the dedicated-server always a console in windows (sign_de)
truelight
parents: 543
diff changeset
   149
#endif
35294912464a (svn r1056) -Fix: [Network] Give the dedicated-server always a console in windows (sign_de)
truelight
parents: 543
diff changeset
   150
810
a1494b19bd2a (svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents: 796
diff changeset
   151
#ifdef __OS2__
a1494b19bd2a (svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents: 796
diff changeset
   152
	// For OS/2 we also need to switch to console mode instead of PM mode
a1494b19bd2a (svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents: 796
diff changeset
   153
	OS2_SwitchToConsoleMode();
a1494b19bd2a (svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents: 796
diff changeset
   154
#endif
a1494b19bd2a (svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents: 796
diff changeset
   155
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   156
	DEBUG(misc,0)("Loading dedicated server...");
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   157
	return NULL;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   158
}
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   159
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   160
static void DedicatedVideoStop(void)
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   161
{
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   162
#ifdef WIN32
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   163
	CloseWindowsConsoleThread();
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   164
#endif
1109
ecb98f43ba2c (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1090
diff changeset
   165
	free(_dedicated_video_mem);
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   166
}
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   167
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   168
static void DedicatedVideoMakeDirty(int left, int top, int width, int height) {}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   169
static bool DedicatedVideoChangeRes(int w, int h) { return false; }
1829
e90fe433fa7d (svn r2334) - Fix (regression): moved togglefullscreen into the video-driver, now windows works, dedicated works and sdl works. Also reverted the change to the makefile.
Darkvater
parents: 1794
diff changeset
   170
static void DedicatedVideoFullScreen(bool fs) {}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   171
1090
5d378bd5fcbc (svn r1591) Make dedicated servers on OS/2 check for input the UNIX way
tron
parents: 1046
diff changeset
   172
#if defined(UNIX) || defined(__OS2__)
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   173
static bool InputWaiting(void)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   174
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   175
	struct timeval tv;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   176
	fd_set readfds;
736
75bcef85daeb (svn r1188) -Fix: [Network] Fixed that CTRL+<key> could sometimes hang a dedicated
truelight
parents: 721
diff changeset
   177
	byte ret;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   178
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   179
	tv.tv_sec = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   180
	tv.tv_usec = 1;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   181
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   182
	FD_ZERO(&readfds);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   183
	FD_SET(STDIN, &readfds);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   184
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   185
	/* don't care about writefds and exceptfds: */
736
75bcef85daeb (svn r1188) -Fix: [Network] Fixed that CTRL+<key> could sometimes hang a dedicated
truelight
parents: 721
diff changeset
   186
	ret = select(STDIN + 1, &readfds, NULL, NULL, &tv);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   187
736
75bcef85daeb (svn r1188) -Fix: [Network] Fixed that CTRL+<key> could sometimes hang a dedicated
truelight
parents: 721
diff changeset
   188
	if (ret > 0)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   189
		return true;
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   190
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   191
	return false;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   192
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   193
#else
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   194
static bool InputWaiting(void)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   195
{
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   196
	if (WaitForSingleObject(hEvent, 1) == WAIT_OBJECT_0)
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   197
		return true;
1109
ecb98f43ba2c (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1090
diff changeset
   198
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   199
	return false;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   200
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   201
#endif
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   202
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   203
static void DedicatedHandleKeyInput(void)
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   204
{
721
617a397f8b4b (svn r1173) -Fix: [Network] Console input in dedicated server for windows was not
truelight
parents: 720
diff changeset
   205
	static char input_line[200] = "";
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   206
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   207
	if (!InputWaiting())
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   208
		return;
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   209
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   210
	if (_exit_game)
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   211
		return;
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   212
781
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
   213
#if defined(UNIX) || defined(__OS2__)
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   214
		fgets(input_line, lengthof(input_line), stdin);
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   215
#else
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   216
		strncpy(input_line, _win_console_thread_buffer, lengthof(input_line));
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   217
#endif
736
75bcef85daeb (svn r1188) -Fix: [Network] Fixed that CTRL+<key> could sometimes hang a dedicated
truelight
parents: 721
diff changeset
   218
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   219
	/* XXX - strtok() does not 'forget' \n\r if it is the first character! */
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   220
	strtok(input_line, "\r\n"); // Forget about the final \n (or \r)
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   221
	{ /* Remove any special control characters */
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   222
		uint i;
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   223
		for (i = 0; i < lengthof(input_line); i++) {
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   224
			if (input_line[i] == '\n' || input_line[i] == '\r') // cut missed beginning '\0'
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   225
				input_line[i] = '\0';
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   226
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   227
			if (input_line[i] == '\0')
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   228
				break;
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   229
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   230
			if (!IS_INT_INSIDE(input_line[i], ' ', 256))
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   231
				input_line[i] = ' ';
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   232
		}
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   233
	}
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   234
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   235
	IConsoleCmdExec(input_line); // execute command
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   236
}
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   237
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   238
static int DedicatedVideoMainLoop(void)
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   239
{
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   240
#ifndef WIN32
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   241
	struct timeval tim;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   242
#endif
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   243
	uint32 next_tick;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   244
	uint32 cur_ticks;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   245
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   246
#ifdef WIN32
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   247
	next_tick = GetTickCount() + 30;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   248
#else
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   249
	gettimeofday(&tim, NULL);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   250
	next_tick = (tim.tv_usec / 1000) + 30 + (tim.tv_sec * 1000);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   251
#endif
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   252
770
c2eacca29601 (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 736
diff changeset
   253
	/* Signal handlers */
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
   254
#ifdef UNIX
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
   255
	signal(SIGTERM, DedicatedSignalHandler);
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
   256
	signal(SIGINT, DedicatedSignalHandler);
736
75bcef85daeb (svn r1188) -Fix: [Network] Fixed that CTRL+<key> could sometimes hang a dedicated
truelight
parents: 721
diff changeset
   257
	signal(SIGQUIT, DedicatedSignalHandler);
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
   258
#endif
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
   259
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   260
	// Load the dedicated server stuff
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   261
	_is_network_server = true;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   262
	_network_dedicated = true;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   263
	_network_playas = OWNER_SPECTATOR;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   264
	_local_player = OWNER_SPECTATOR;
1414
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   265
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   266
	/* If SwitchMode is SM_LOAD, it means that the user used the '-g' options */
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   267
	if (_switch_mode != SM_LOAD) {
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   268
		_switch_mode = SM_NONE;
1794
24b4239f2090 (svn r2298) - CodeChange: removed CmdAbuses: CmdSetTownNameType(), CmdStartNewGame(), CmdCreateScenario(), CmdSetNewLandscapeType() and CmdGenRandomNewGame().
Darkvater
parents: 1762
diff changeset
   269
		GenRandomNewGame(Random(), InteractiveRandom());
1414
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   270
	} else {
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   271
		_switch_mode = SM_NONE;
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   272
		/* First we need to test if the savegame can be loaded, else we will end up playing the
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   273
		 *  intro game... */
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   274
		if (!SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL)) {
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   275
			/* Loading failed, pop out.. */
1626
b0cda32b478a (svn r2130) Various spelling fixes in messages.
pasky
parents: 1614
diff changeset
   276
			DEBUG(net, 0)("Loading requested map failed. Aborting.");
1414
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   277
			_networking = false;
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   278
		} else {
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   279
			/* We can load this game, so go ahead */
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   280
			SwitchMode(SM_LOAD);
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   281
		}
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   282
	}
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   283
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   284
	// Done loading, start game!
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   285
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   286
	if (!_networking) {
1626
b0cda32b478a (svn r2130) Various spelling fixes in messages.
pasky
parents: 1614
diff changeset
   287
		DEBUG(net, 1)("Dedicated server could not be launched. Aborting.");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   288
		return ML_QUIT;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   289
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   290
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   291
	while (true) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   292
		InteractiveRandom(); // randomness
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   293
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
   294
		if (_exit_game) return ML_QUIT;
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
   295
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   296
		if (!_dedicated_forks)
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   297
			DedicatedHandleKeyInput();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   298
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   299
#ifdef WIN32
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   300
		cur_ticks = GetTickCount();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   301
#else
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   302
		gettimeofday(&tim, NULL);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   303
		cur_ticks = (tim.tv_usec / 1000) + (tim.tv_sec * 1000);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   304
#endif
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   305
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   306
		if (cur_ticks >= next_tick) {
1543
713bc7d94d4b (svn r2047) -Fix: never commit your own personal changes... :(
truelight
parents: 1542
diff changeset
   307
			next_tick += 30;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   308
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   309
			GameLoop();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   310
			_screen.dst_ptr = _dedicated_video_mem;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   311
			UpdateWindows();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   312
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   313
		CSleep(1);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   314
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   315
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   316
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   317
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   318
const HalVideoDriver _dedicated_video_driver = {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   319
	DedicatedVideoStart,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   320
	DedicatedVideoStop,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   321
	DedicatedVideoMakeDirty,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   322
	DedicatedVideoMainLoop,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   323
	DedicatedVideoChangeRes,
1829
e90fe433fa7d (svn r2334) - Fix (regression): moved togglefullscreen into the video-driver, now windows works, dedicated works and sdl works. Also reverted the change to the makefile.
Darkvater
parents: 1794
diff changeset
   324
	DedicatedVideoFullScreen,
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   325
};
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   326
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   327
#else
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   328
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   329
static void *_dedicated_video_mem;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   330
1614
f6f2c918356d (svn r2118) - Fix: Fix compilation with network disabled, and comment out some of the warnings (unused function)
Darkvater
parents: 1543
diff changeset
   331
static const char *DedicatedVideoStart(const char * const *parm)
1301
bf64ba5b6774 (svn r1805) Teach the driver layer a few things about const correctness
tron
parents: 1299
diff changeset
   332
{
1626
b0cda32b478a (svn r2130) Various spelling fixes in messages.
pasky
parents: 1614
diff changeset
   333
	DEBUG(misc, 0) ("OpenTTD compiled without network support, exiting.");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   334
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   335
	return NULL;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   336
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   337
774
bb9ec520a1b1 (svn r1240) -Fix: OpenTTD once again compiles if ENABLE_NETWORK is disabled.
darkvater
parents: 770
diff changeset
   338
void DedicatedFork(void) {}
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   339
static void DedicatedVideoStop(void) { free(_dedicated_video_mem); }
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   340
static void DedicatedVideoMakeDirty(int left, int top, int width, int height) {}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   341
static bool DedicatedVideoChangeRes(int w, int h) { return false; }
1829
e90fe433fa7d (svn r2334) - Fix (regression): moved togglefullscreen into the video-driver, now windows works, dedicated works and sdl works. Also reverted the change to the makefile.
Darkvater
parents: 1794
diff changeset
   342
static void DedicatedVideoFullScreen(bool fs) {}
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   343
static int DedicatedVideoMainLoop(void) { return ML_QUIT; }
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   344
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   345
const HalVideoDriver _dedicated_video_driver = {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   346
	DedicatedVideoStart,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   347
	DedicatedVideoStop,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   348
	DedicatedVideoMakeDirty,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   349
	DedicatedVideoMainLoop,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   350
	DedicatedVideoChangeRes,
1829
e90fe433fa7d (svn r2334) - Fix (regression): moved togglefullscreen into the video-driver, now windows works, dedicated works and sdl works. Also reverted the change to the makefile.
Darkvater
parents: 1794
diff changeset
   351
	DedicatedVideoFullScreen,
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   352
};
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   353
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   354
#endif /* ENABLE_NETWORK */