1 // |
|
2 // gameSetup.m |
|
3 // hwengine |
|
4 // |
|
5 // Created by Vittorio on 10/01/10. |
|
6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 // |
|
8 |
|
9 #import <pthread.h> |
|
10 #import "SDL_uikitappdelegate.h" |
|
11 #import "gameSetup.h" |
|
12 #import "SDL_net.h" |
|
13 #import "PascalImports.h" |
|
14 |
|
15 #define IPC_PORT 51342 |
|
16 #define IPC_PORT_STR "51342" |
|
17 #define BUFFER_SIZE 256 |
|
18 |
|
19 |
|
20 // |
|
21 TCPsocket sd, csd; /* Socket descriptor, Client socket descriptor */ |
|
22 |
|
23 @implementation gameSetup |
|
24 |
|
25 int sendToEngine(NSString *string) { |
|
26 Uint8 length = [string length]; |
|
27 |
|
28 SDLNet_TCP_Send(csd, &length , 1); |
|
29 return SDLNet_TCP_Send(csd, [string UTF8String], length); |
|
30 } |
|
31 |
|
32 void engineProtocolThread () { |
|
33 IPaddress ip; |
|
34 int idx, eProto; |
|
35 BOOL serverQuit, clientQuit; |
|
36 char buffer[BUFFER_SIZE], string[BUFFER_SIZE]; |
|
37 Uint8 msgSize; |
|
38 Uint16 gameTicks; |
|
39 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
40 |
|
41 if (SDLNet_Init() < 0) { |
|
42 fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError()); |
|
43 exit(EXIT_FAILURE); |
|
44 } |
|
45 |
|
46 /* Resolving the host using NULL make network interface to listen */ |
|
47 if (SDLNet_ResolveHost(&ip, NULL, IPC_PORT) < 0) { |
|
48 fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
|
49 exit(EXIT_FAILURE); |
|
50 } |
|
51 |
|
52 /* Open a connection with the IP provided (listen on the host's port) */ |
|
53 if (!(sd = SDLNet_TCP_Open(&ip))) { |
|
54 fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError()); |
|
55 exit(EXIT_FAILURE); |
|
56 } |
|
57 |
|
58 NSLog(@"engineProtocolThread - Waiting for a client"); |
|
59 |
|
60 serverQuit = NO; |
|
61 while (!serverQuit) { |
|
62 |
|
63 /* This check the sd if there is a pending connection. |
|
64 * If there is one, accept that, and open a new socket for communicating */ |
|
65 if ((csd = SDLNet_TCP_Accept(sd))) { |
|
66 |
|
67 NSLog(@"engineProtocolThread - Client found"); |
|
68 |
|
69 //first byte of the command alwayas contain the size of the command |
|
70 SDLNet_TCP_Recv(csd, &msgSize, sizeof(Uint8)); |
|
71 |
|
72 SDLNet_TCP_Recv(csd, buffer, msgSize); |
|
73 gameTicks = SDLNet_Read16(&buffer[msgSize - 2]); |
|
74 NSLog(@"engineProtocolThread - %d: received [%s]", gameTicks, buffer); |
|
75 |
|
76 if ('C' == buffer[0]) { |
|
77 NSLog(@"engineProtocolThread - Client found and connected"); |
|
78 |
|
79 // send config data data |
|
80 |
|
81 // local game |
|
82 sendToEngine(@"TL"); |
|
83 |
|
84 // seed info |
|
85 sendToEngine(@"eseed {232c1b42-7d39-4ee6-adf8-4240e1f1efb8}"); |
|
86 |
|
87 // various flags |
|
88 sendToEngine(@"e$gmflags 256"); |
|
89 |
|
90 // various flags |
|
91 sendToEngine(@"e$damagepct 100"); |
|
92 |
|
93 // various flags |
|
94 sendToEngine(@"e$turntime 45000"); |
|
95 |
|
96 // various flags |
|
97 sendToEngine(@"e$minestime 3000"); |
|
98 |
|
99 // various flags |
|
100 sendToEngine(@"e$landadds 4"); |
|
101 |
|
102 // various flags |
|
103 sendToEngine(@"e$sd_turns 15"); |
|
104 |
|
105 // various flags |
|
106 sendToEngine(@"e$casefreq 5"); |
|
107 |
|
108 // various flags |
|
109 sendToEngine(@"e$template_filter 1"); |
|
110 |
|
111 // theme info |
|
112 sendToEngine(@"etheme Freeway"); |
|
113 |
|
114 // team 1 info |
|
115 sendToEngine(@"eaddteam 4421353 System Cats"); |
|
116 |
|
117 // team 1 grave info |
|
118 sendToEngine(@"egrave star"); |
|
119 |
|
120 // team 1 fort info |
|
121 sendToEngine(@"efort Earth"); |
|
122 |
|
123 // team 1 voicepack info |
|
124 sendToEngine(@"evoicepack Classic"); |
|
125 |
|
126 // team 1 binds (skipped) |
|
127 // team 1 members info |
|
128 //for (int i=0; i<4; i++) { |
|
129 sendToEngine(@"eaddhh 0 100 Snow Leopard"); |
|
130 sendToEngine(@"ehat NoHat"); |
|
131 //} |
|
132 // team 1 ammostore |
|
133 sendToEngine(@"eammstore 93919294221991210322351110012010000002110404000441400444645644444774776112211144"); |
|
134 |
|
135 // team 2 info |
|
136 sendToEngine(@"eaddteam 4100897 Poke-MAN"); |
|
137 |
|
138 // team 2 grave info |
|
139 sendToEngine(@"egrave Badger"); |
|
140 |
|
141 // team 2 fort info |
|
142 sendToEngine(@"efort UFO"); |
|
143 |
|
144 // team 2 voicepack info |
|
145 sendToEngine(@"evoicepack Classic"); |
|
146 |
|
147 // team 2 binds (skipped) |
|
148 // team 2 members info |
|
149 // for (int i=0; i<4; i++) { |
|
150 sendToEngine(@"eaddhh 0 100 Raichu"); |
|
151 sendToEngine(@"ehat Bunny"); |
|
152 // } |
|
153 |
|
154 // team 2 ammostore |
|
155 sendToEngine(@"eammstore 93919294221991210322351110012010000002110404000441400444645644444774776112211144"); |
|
156 |
|
157 clientQuit = NO; |
|
158 } else { |
|
159 NSLog(@"engineProtocolThread - wrong message, closing connection"); |
|
160 clientQuit = YES; |
|
161 } |
|
162 |
|
163 while (!clientQuit){ |
|
164 /* Now we can communicate with the client using csd socket |
|
165 * sd will remain opened waiting other connections */ |
|
166 idx = 0; |
|
167 msgSize = 0; |
|
168 memset(buffer, 0, BUFFER_SIZE); |
|
169 memset(string, 0, BUFFER_SIZE); |
|
170 if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(Uint8)) <= 0) |
|
171 clientQuit = YES; |
|
172 if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0) |
|
173 clientQuit = YES; |
|
174 |
|
175 gameTicks = SDLNet_Read16(&buffer[msgSize - 2]); |
|
176 //NSLog(@"engineProtocolThread - %d: received [%s]", gameTicks, buffer); |
|
177 |
|
178 switch (buffer[0]) { |
|
179 case '?': |
|
180 NSLog(@"Ping? Pong!"); |
|
181 sendToEngine(@"!"); |
|
182 break; |
|
183 case 'E': |
|
184 NSLog(@"ERROR - last console line: [%s]", buffer); |
|
185 clientQuit = YES; |
|
186 break; |
|
187 case 'e': |
|
188 sscanf(buffer, "%*s %d", &eProto); |
|
189 if (HW_protoVer() == eProto) { |
|
190 NSLog(@"Setting protocol version %s", buffer); |
|
191 } else { |
|
192 NSLog(@"ERROR - wrong protocol number: [%s] - expecting %d", buffer, eProto); |
|
193 clientQuit = YES; |
|
194 } |
|
195 break; |
|
196 default: |
|
197 // empty packet or just statistics |
|
198 break; |
|
199 case 'i': |
|
200 switch (buffer[1]) { |
|
201 case 'r': |
|
202 NSLog(@"Winning team: %s", &buffer[2]); |
|
203 break; |
|
204 case 'k': |
|
205 NSLog(@"Best Hedgehog: %s", &buffer[2]); |
|
206 break; |
|
207 } |
|
208 break; |
|
209 // missing case for exiting |
|
210 } |
|
211 |
|
212 /* |
|
213 // Terminate this connection |
|
214 if(strcmp(buffer, "exit") == 0) { |
|
215 quit2 = 1; |
|
216 printf("Terminate connection\n"); |
|
217 } |
|
218 // Quit the thread |
|
219 if(strcmp(buffer, "quit") == 0) { |
|
220 quit2 = 1; |
|
221 quit = 1; |
|
222 printf("Quit program\n"); |
|
223 } |
|
224 */ |
|
225 } |
|
226 } |
|
227 |
|
228 /* Close the client socket */ |
|
229 SDLNet_TCP_Close(csd); |
|
230 } |
|
231 |
|
232 SDLNet_TCP_Close(sd); |
|
233 SDLNet_Quit(); |
|
234 |
|
235 [pool release]; |
|
236 pthread_exit(NULL); |
|
237 } |
|
238 |
|
239 void setupArgsForLocalPlay() { |
|
240 memset(forward_argv, 0, forward_argc); |
|
241 |
|
242 forward_argc = 18; |
|
243 forward_argv = (char **)realloc(forward_argv, forward_argc * sizeof(char *)); |
|
244 //forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char)); |
|
245 forward_argv[ 1] = forward_argv[0]; // (UNUSED) |
|
246 forward_argv[ 2] = "320"; // cScreenWidth (NO EFFECT) |
|
247 forward_argv[ 3] = "480"; // cScreenHeight (NO EFFECT) |
|
248 forward_argv[ 4] = "32"; // cBitsStr |
|
249 forward_argv[ 5] = IPC_PORT_STR; // ipcPort; <- (MAIN TODO) |
|
250 forward_argv[ 6] = "1"; // cFullScreen (NO EFFECT) |
|
251 forward_argv[ 7] = "0"; // isSoundEnabled (TOSET) |
|
252 forward_argv[ 8] = "1"; // cVSyncInUse (UNUSED) |
|
253 forward_argv[ 9] = "en.txt"; // cLocaleFName (TOSET) |
|
254 forward_argv[10] = "100"; // cInitVolume (TOSET) |
|
255 forward_argv[11] = "8"; // cTimerInterval |
|
256 forward_argv[12] = "Data"; // PathPrefix |
|
257 forward_argv[13] = "1"; // cShowFPS (TOSET?) |
|
258 forward_argv[14] = "0"; // cAltDamage (TOSET) |
|
259 forward_argv[15] = "Koda"; // UserNick (DecodeBase64(ParamStr(15)) FTW) <- TODO |
|
260 forward_argv[16] = "0"; // isMusicEnabled (TOSET) |
|
261 forward_argv[17] = "0"; // cReducedQuality |
|
262 |
|
263 return; |
|
264 } |
|
265 |
|
266 @end |
|