|
1 #include "stdafx.h" |
|
2 #include "ttd.h" |
|
3 #include "network.h" |
|
4 #include "hal.h" |
|
5 |
|
6 #ifdef ENABLE_NETWORK |
|
7 |
|
8 #include "gfx.h" |
|
9 #include "window.h" |
|
10 #include "command.h" |
|
11 #include "console.h" |
|
12 #ifdef WIN32 |
|
13 # include <windows.h> /* GetTickCount */ |
|
14 # include <conio.h> |
|
15 #endif |
|
16 #ifdef UNIX |
|
17 # include <sys/time.h> /* gettimeofday */ |
|
18 # include <sys/types.h> |
|
19 # include <unistd.h> |
|
20 # define STDIN 0 /* file descriptor for standard input */ |
|
21 #endif |
|
22 |
|
23 // This file handles all dedicated-server in- and outputs |
|
24 |
|
25 static void *_dedicated_video_mem; |
|
26 |
|
27 static const char *DedicatedVideoStart(char **parm) { |
|
28 _screen.width = _screen.pitch = _cur_resolution[0]; |
|
29 _screen.height = _cur_resolution[1]; |
|
30 _dedicated_video_mem = malloc(_cur_resolution[0]*_cur_resolution[1]); |
|
31 |
|
32 _debug_net_level = 6; |
|
33 _debug_misc_level = 0; |
|
34 |
|
35 DEBUG(misc,0)("Loading dedicated server..."); |
|
36 return NULL; |
|
37 } |
|
38 static void DedicatedVideoStop() { free(_dedicated_video_mem); } |
|
39 static void DedicatedVideoMakeDirty(int left, int top, int width, int height) {} |
|
40 static bool DedicatedVideoChangeRes(int w, int h) { return false; } |
|
41 |
|
42 #ifdef UNIX |
|
43 |
|
44 bool InputWaiting() |
|
45 { |
|
46 struct timeval tv; |
|
47 fd_set readfds; |
|
48 |
|
49 tv.tv_sec = 0; |
|
50 tv.tv_usec = 1; |
|
51 |
|
52 FD_ZERO(&readfds); |
|
53 FD_SET(STDIN, &readfds); |
|
54 |
|
55 /* don't care about writefds and exceptfds: */ |
|
56 select(STDIN+1, &readfds, NULL, NULL, &tv); |
|
57 |
|
58 if (FD_ISSET(STDIN, &readfds)) |
|
59 return true; |
|
60 else |
|
61 return false; |
|
62 } |
|
63 #else |
|
64 bool InputWaiting() |
|
65 { |
|
66 return kbhit(); |
|
67 } |
|
68 #endif |
|
69 |
|
70 static int DedicatedVideoMainLoop() { |
|
71 #ifndef WIN32 |
|
72 struct timeval tim; |
|
73 #else |
|
74 char input; |
|
75 #endif |
|
76 uint32 next_tick; |
|
77 uint32 cur_ticks; |
|
78 char input_line[200]; |
|
79 |
|
80 #ifdef WIN32 |
|
81 next_tick = GetTickCount() + 30; |
|
82 #else |
|
83 gettimeofday(&tim, NULL); |
|
84 next_tick = (tim.tv_usec / 1000) + 30 + (tim.tv_sec * 1000); |
|
85 #endif |
|
86 |
|
87 // Load the dedicated server stuff |
|
88 _is_network_server = true; |
|
89 _network_dedicated = true; |
|
90 _switch_mode = SM_NONE; |
|
91 _network_playas = OWNER_SPECTATOR; |
|
92 _local_player = OWNER_SPECTATOR; |
|
93 DoCommandP(0, Random(), InteractiveRandom(), NULL, CMD_GEN_RANDOM_NEW_GAME); |
|
94 // Done loading, start game! |
|
95 |
|
96 if (!_networking) { |
|
97 DEBUG(net, 1)("Dedicated server could not be launced. Aborting.."); |
|
98 return ML_QUIT; |
|
99 } |
|
100 |
|
101 while (true) { |
|
102 InteractiveRandom(); // randomness |
|
103 |
|
104 #ifdef UNIX |
|
105 if (InputWaiting()) { |
|
106 fgets(input_line, 200, stdin); |
|
107 // Forget about the final \n (or \r) |
|
108 strtok(input_line, "\r\n"); |
|
109 IConsoleCmdExec(input_line); |
|
110 } |
|
111 #else |
|
112 if (InputWaiting()) { |
|
113 input = getch(); |
|
114 printf("%c", input); |
|
115 if (input != '\r') |
|
116 snprintf(input_line, 200, "%s%c", input_line, input); |
|
117 else { |
|
118 printf("\n"); |
|
119 IConsoleCmdExec(input_line); |
|
120 sprintf(input_line, ""); |
|
121 } |
|
122 } |
|
123 #endif |
|
124 |
|
125 if (_exit_game) return ML_QUIT; |
|
126 |
|
127 #ifdef WIN32 |
|
128 cur_ticks = GetTickCount(); |
|
129 #else |
|
130 gettimeofday(&tim, NULL); |
|
131 cur_ticks = (tim.tv_usec / 1000) + (tim.tv_sec * 1000); |
|
132 #endif |
|
133 |
|
134 if (cur_ticks >= next_tick) { |
|
135 next_tick += 30; |
|
136 |
|
137 GameLoop(); |
|
138 _screen.dst_ptr = _dedicated_video_mem; |
|
139 UpdateWindows(); |
|
140 } |
|
141 CSleep(1); |
|
142 } |
|
143 |
|
144 return ML_QUIT; |
|
145 } |
|
146 |
|
147 |
|
148 const HalVideoDriver _dedicated_video_driver = { |
|
149 DedicatedVideoStart, |
|
150 DedicatedVideoStop, |
|
151 DedicatedVideoMakeDirty, |
|
152 DedicatedVideoMainLoop, |
|
153 DedicatedVideoChangeRes, |
|
154 }; |
|
155 |
|
156 #else |
|
157 |
|
158 static void *_dedicated_video_mem; |
|
159 |
|
160 static const char *DedicatedVideoStart(char **parm) { |
|
161 DEBUG(misc,0)("OpenTTD compiled without network-support, quiting..."); |
|
162 |
|
163 return NULL; |
|
164 } |
|
165 |
|
166 static void DedicatedVideoStop() { free(_dedicated_video_mem); } |
|
167 static void DedicatedVideoMakeDirty(int left, int top, int width, int height) {} |
|
168 static bool DedicatedVideoChangeRes(int w, int h) { return false; } |
|
169 static int DedicatedVideoMainLoop() { return ML_QUIT; } |
|
170 |
|
171 const HalVideoDriver _dedicated_video_driver = { |
|
172 DedicatedVideoStart, |
|
173 DedicatedVideoStop, |
|
174 DedicatedVideoMakeDirty, |
|
175 DedicatedVideoMainLoop, |
|
176 DedicatedVideoChangeRes, |
|
177 }; |
|
178 |
|
179 #endif /* ENABLE_NETWORK */ |