author | koda |
Tue, 12 Jan 2010 07:32:15 +0000 | |
changeset 2692 | ce9992075118 |
parent 2691 | c0da3a98c01c |
permissions | -rw-r--r-- |
2691 | 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 |
||
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
20 |
// |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
21 |
TCPsocket sd, csd; /* Socket descriptor, Client socket descriptor */ |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
22 |
|
2691 | 23 |
@implementation gameSetup |
24 |
||
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
25 |
int sendToEngine(NSString *string) { |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
26 |
Uint8 length = [string length]; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
27 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
28 |
SDLNet_TCP_Send(csd, &length , 1); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
29 |
return SDLNet_TCP_Send(csd, [string UTF8String], length); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
30 |
} |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
31 |
|
2691 | 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; |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
39 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
2691 | 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"); |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
78 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
79 |
// send config data data |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
80 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
81 |
// local game |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
82 |
sendToEngine(@"TL"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
83 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
84 |
// seed info |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
85 |
sendToEngine(@"eseed {232c1b42-7d39-4ee6-adf8-4240e1f1efb8}"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
86 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
87 |
// various flags |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
88 |
sendToEngine(@"e$gmflags 256"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
89 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
90 |
// various flags |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
91 |
sendToEngine(@"e$damagepct 100"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
92 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
93 |
// various flags |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
94 |
sendToEngine(@"e$turntime 45000"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
95 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
96 |
// various flags |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
97 |
sendToEngine(@"e$minestime 3000"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
98 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
99 |
// various flags |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
100 |
sendToEngine(@"e$landadds 4"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
101 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
102 |
// various flags |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
103 |
sendToEngine(@"e$sd_turns 15"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
104 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
105 |
// various flags |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
106 |
sendToEngine(@"e$casefreq 5"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
107 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
108 |
// various flags |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
109 |
sendToEngine(@"e$template_filter 1"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
110 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
111 |
// theme info |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
112 |
sendToEngine(@"etheme Freeway"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
113 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
114 |
// team 1 info |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
115 |
sendToEngine(@"eaddteam 4421353 System Cats"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
116 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
117 |
// team 1 grave info |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
118 |
sendToEngine(@"egrave star"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
119 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
120 |
// team 1 fort info |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
121 |
sendToEngine(@"efort Earth"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
122 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
123 |
// team 1 voicepack info |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
124 |
sendToEngine(@"evoicepack Classic"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
125 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
126 |
// team 1 binds (skipped) |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
127 |
// team 1 members info |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
128 |
//for (int i=0; i<4; i++) { |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
129 |
sendToEngine(@"eaddhh 0 100 Snow Leopard"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
130 |
sendToEngine(@"ehat NoHat"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
131 |
//} |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
132 |
// team 1 ammostore |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
133 |
sendToEngine(@"eammstore 93919294221991210322351110012010000002110404000441400444645644444774776112211144"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
134 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
135 |
// team 2 info |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
136 |
sendToEngine(@"eaddteam 4100897 Poke-MAN"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
137 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
138 |
// team 2 grave info |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
139 |
sendToEngine(@"egrave Badger"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
140 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
141 |
// team 2 fort info |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
142 |
sendToEngine(@"efort UFO"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
143 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
144 |
// team 2 voicepack info |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
145 |
sendToEngine(@"evoicepack Classic"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
146 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
147 |
// team 2 binds (skipped) |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
148 |
// team 2 members info |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
149 |
// for (int i=0; i<4; i++) { |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
150 |
sendToEngine(@"eaddhh 0 100 Raichu"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
151 |
sendToEngine(@"ehat Bunny"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
152 |
// } |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
153 |
|
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
154 |
// team 2 ammostore |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
155 |
sendToEngine(@"eammstore 93919294221991210322351110012010000002110404000441400444645644444774776112211144"); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
156 |
|
2691 | 157 |
clientQuit = NO; |
158 |
} else { |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
159 |
NSLog(@"engineProtocolThread - wrong message, closing connection"); |
2691 | 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); |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
170 |
if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(Uint8)) <= 0) |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
171 |
clientQuit = YES; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
172 |
if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0) |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
173 |
clientQuit = YES; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
174 |
|
2691 | 175 |
gameTicks = SDLNet_Read16(&buffer[msgSize - 2]); |
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
176 |
//NSLog(@"engineProtocolThread - %d: received [%s]", gameTicks, buffer); |
2691 | 177 |
|
178 |
switch (buffer[0]) { |
|
179 |
case '?': |
|
180 |
NSLog(@"Ping? Pong!"); |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
181 |
sendToEngine(@"!"); |
2691 | 182 |
break; |
183 |
case 'E': |
|
184 |
NSLog(@"ERROR - last console line: [%s]", buffer); |
|
185 |
clientQuit = YES; |
|
186 |
break; |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
187 |
case 'e': |
2691 | 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 |
} |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
195 |
break; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
196 |
default: |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
197 |
// empty packet or just statistics |
2691 | 198 |
break; |
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
199 |
case 'i': |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
200 |
switch (buffer[1]) { |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
201 |
case 'r': |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
202 |
NSLog(@"Winning team: %s", &buffer[2]); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
203 |
break; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
204 |
case 'k': |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
205 |
NSLog(@"Best Hedgehog: %s", &buffer[2]); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
206 |
break; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
207 |
} |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
208 |
break; |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
209 |
// missing case for exiting |
2691 | 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 |
||
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
235 |
[pool release]; |
2691 | 236 |
pthread_exit(NULL); |
237 |
} |
|
238 |
||
239 |
void setupArgsForLocalPlay() { |
|
2692
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
240 |
memset(forward_argv, 0, forward_argc); |
ce9992075118
better network support + initial work for returning to frontend
koda
parents:
2691
diff
changeset
|
241 |
|
2691 | 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 |