author | koda |
Sat, 03 Jul 2010 04:42:29 +0200 | |
changeset 3616 | 85d69ddb41b6 |
parent 3613 | cfd3c6536a09 |
child 3617 | 1df21e06b8ba |
permissions | -rw-r--r-- |
3547 | 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 |
#include <sys/types.h> |
|
10 |
#include <sys/sysctl.h> |
|
11 |
||
12 |
#import "GameSetup.h" |
|
13 |
#import "SDL_uikitappdelegate.h" |
|
14 |
#import "SDL_net.h" |
|
15 |
#import "PascalImports.h" |
|
16 |
#import "CommodityFunctions.h" |
|
17 |
||
18 |
#define BUFFER_SIZE 256 |
|
19 |
||
20 |
@implementation GameSetup |
|
21 |
||
22 |
@synthesize systemSettings, gameConfig; |
|
23 |
||
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3613
diff
changeset
|
24 |
-(id) initWithDictionary:(NSDictionary *)gameDictionary { |
3547 | 25 |
if (self = [super init]) { |
26 |
ipcPort = randomPort(); |
|
27 |
||
28 |
// should check they exist and throw and exection if not |
|
29 |
NSDictionary *dictSett = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()]; |
|
30 |
self.systemSettings = dictSett; |
|
31 |
[dictSett release]; |
|
32 |
||
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3613
diff
changeset
|
33 |
self.gameConfig = gameDictionary; |
3547 | 34 |
} |
35 |
return self; |
|
36 |
} |
|
37 |
||
38 |
-(void) dealloc { |
|
39 |
[gameConfig release]; |
|
40 |
[systemSettings release]; |
|
41 |
[super dealloc]; |
|
42 |
} |
|
43 |
||
44 |
#pragma mark - |
|
45 |
#pragma mark Provider functions |
|
46 |
// unpacks team data from the selected team.plist to a sequence of engine commands |
|
47 |
-(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor { |
|
48 |
/* |
|
49 |
addteam <32charsMD5hash> <color> <team name> |
|
50 |
addhh <level> <health> <hedgehog name> |
|
51 |
<level> is 0 for human, 1-5 for bots (5 is the most stupid) |
|
52 |
*/ |
|
53 |
||
54 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), teamName]; |
|
55 |
NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile]; |
|
56 |
[teamFile release]; |
|
57 |
||
58 |
NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@", |
|
59 |
[teamData objectForKey:@"hash"], [teamColor stringValue], [teamData objectForKey:@"teamname"]]; |
|
60 |
[self sendToEngine: teamHashColorAndName]; |
|
61 |
[teamHashColorAndName release]; |
|
62 |
||
63 |
NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]]; |
|
64 |
[self sendToEngine: grave]; |
|
65 |
[grave release]; |
|
66 |
||
67 |
NSString *fort = [[NSString alloc] initWithFormat:@"efort %@", [teamData objectForKey:@"fort"]]; |
|
68 |
[self sendToEngine: fort]; |
|
69 |
[fort release]; |
|
70 |
||
71 |
NSString *voicepack = [[NSString alloc] initWithFormat:@"evoicepack %@", [teamData objectForKey:@"voicepack"]]; |
|
72 |
[self sendToEngine: voicepack]; |
|
73 |
[voicepack release]; |
|
74 |
||
75 |
NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]]; |
|
76 |
[self sendToEngine: flag]; |
|
77 |
[flag release]; |
|
78 |
||
79 |
NSArray *hogs = [teamData objectForKey:@"hedgehogs"]; |
|
80 |
for (int i = 0; i < numberOfPlayingHogs; i++) { |
|
81 |
NSDictionary *hog = [hogs objectAtIndex:i]; |
|
82 |
||
83 |
NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %d %@", |
|
84 |
[hog objectForKey:@"level"], initialHealth, [hog objectForKey:@"hogname"]]; |
|
85 |
[self sendToEngine: hogLevelHealthAndName]; |
|
86 |
[hogLevelHealthAndName release]; |
|
87 |
||
88 |
NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]]; |
|
89 |
[self sendToEngine: hogHat]; |
|
90 |
[hogHat release]; |
|
91 |
} |
|
92 |
||
93 |
[teamData release]; |
|
94 |
} |
|
95 |
||
96 |
// unpacks ammostore data from the selected ammo.plist to a sequence of engine commands |
|
97 |
-(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams { |
|
98 |
NSString *weaponPath = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),ammostoreName]; |
|
99 |
NSDictionary *ammoData = [[NSDictionary alloc] initWithContentsOfFile:weaponPath]; |
|
100 |
[weaponPath release]; |
|
101 |
||
102 |
NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@", [ammoData objectForKey:@"ammostore_initialqt"]]; |
|
103 |
[self sendToEngine: ammloadt]; |
|
104 |
[ammloadt release]; |
|
105 |
||
106 |
NSString *ammprob = [[NSString alloc] initWithFormat:@"eammprob %@", [ammoData objectForKey:@"ammostore_probability"]]; |
|
107 |
[self sendToEngine: ammprob]; |
|
108 |
[ammprob release]; |
|
109 |
||
110 |
NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammdelay %@", [ammoData objectForKey:@"ammostore_delay"]]; |
|
111 |
[self sendToEngine: ammdelay]; |
|
112 |
[ammdelay release]; |
|
113 |
||
114 |
NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@", [ammoData objectForKey:@"ammostore_crate"]]; |
|
115 |
[self sendToEngine: ammreinf]; |
|
116 |
[ammreinf release]; |
|
117 |
||
118 |
// sent twice so it applies to both teams |
|
119 |
NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"]; |
|
120 |
for (int i = 0; i < numberOfTeams; i++) |
|
121 |
[self sendToEngine: ammstore]; |
|
122 |
[ammstore release]; |
|
123 |
||
124 |
[ammoData release]; |
|
125 |
} |
|
126 |
||
127 |
// unpacks scheme data from the selected scheme.plist to a sequence of engine commands |
|
128 |
-(NSInteger) provideScheme:(NSString *)schemeName { |
|
129 |
NSString *schemePath = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),schemeName]; |
|
130 |
NSArray *scheme = [[NSArray alloc] initWithContentsOfFile:schemePath]; |
|
131 |
[schemePath release]; |
|
132 |
int result = 0; |
|
133 |
int i = 0; |
|
134 |
||
135 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
136 |
result |= 0x01; |
|
137 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
138 |
result |= 0x10; |
|
139 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
140 |
result |= 0x04; |
|
141 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
142 |
result |= 0x08; |
|
143 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
144 |
result |= 0x20; |
|
145 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
146 |
result |= 0x40; |
|
147 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
148 |
result |= 0x80; |
|
149 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
150 |
result |= 0x100; |
|
151 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
152 |
result |= 0x200; |
|
153 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
154 |
result |= 0x400; |
|
155 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
156 |
result |= 0x800; |
|
157 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
158 |
result |= 0x2000; |
|
159 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
160 |
result |= 0x4000; |
|
161 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
162 |
result |= 0x8000; |
|
163 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
164 |
result |= 0x10000; |
|
165 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
166 |
result |= 0x20000; |
|
167 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
168 |
result |= 0x80000; |
|
169 |
||
170 |
NSString *flags = [[NSString alloc] initWithFormat:@"e$gmflags %d",result]; |
|
171 |
[self sendToEngine:flags]; |
|
172 |
[flags release]; |
|
173 |
||
174 |
NSString *dmgMod = [[NSString alloc] initWithFormat:@"e$damagepct %d",[[scheme objectAtIndex:i++] intValue]]; |
|
175 |
[self sendToEngine:dmgMod]; |
|
176 |
[dmgMod release]; |
|
177 |
||
178 |
NSString *turnTime = [[NSString alloc] initWithFormat:@"e$turntime %d",[[scheme objectAtIndex:i++] intValue] * 1000]; |
|
179 |
[self sendToEngine:turnTime]; |
|
180 |
[turnTime release]; |
|
181 |
||
182 |
result = [[scheme objectAtIndex:i++] intValue]; // initial health |
|
183 |
||
184 |
NSString *sdTime = [[NSString alloc] initWithFormat:@"e$sd_turns %d",[[scheme objectAtIndex:i++] intValue]]; |
|
185 |
[self sendToEngine:sdTime]; |
|
186 |
[sdTime release]; |
|
187 |
||
188 |
NSString *crateDrops = [[NSString alloc] initWithFormat:@"e$casefreq %d",[[scheme objectAtIndex:i++] intValue]]; |
|
189 |
[self sendToEngine:crateDrops]; |
|
190 |
[crateDrops release]; |
|
191 |
||
192 |
NSString *minesTime = [[NSString alloc] initWithFormat:@"e$minestime %d",[[scheme objectAtIndex:i++] intValue] * 1000]; |
|
193 |
[self sendToEngine:minesTime]; |
|
194 |
[minesTime release]; |
|
195 |
||
196 |
NSString *minesNumber = [[NSString alloc] initWithFormat:@"e$landadds %d",[[scheme objectAtIndex:i++] intValue]]; |
|
197 |
[self sendToEngine:minesNumber]; |
|
198 |
[minesNumber release]; |
|
199 |
||
200 |
NSString *dudMines = [[NSString alloc] initWithFormat:@"e$minedudpct %d",[[scheme objectAtIndex:i++] intValue]]; |
|
201 |
[self sendToEngine:dudMines]; |
|
202 |
[dudMines release]; |
|
203 |
||
204 |
NSString *explosives = [[NSString alloc] initWithFormat:@"e$explosives %d",[[scheme objectAtIndex:i++] intValue]]; |
|
205 |
[self sendToEngine:explosives]; |
|
206 |
[explosives release]; |
|
207 |
||
208 |
[scheme release]; |
|
209 |
return result; |
|
210 |
} |
|
211 |
||
212 |
#pragma mark - |
|
213 |
#pragma mark Thread/Network relevant code |
|
214 |
// select one of GameSetup method and execute it in a seprate thread |
|
215 |
-(void) startThread: (NSString *) selector { |
|
216 |
SEL usage = NSSelectorFromString(selector); |
|
217 |
[NSThread detachNewThreadSelector:usage toTarget:self withObject:nil]; |
|
218 |
} |
|
219 |
||
220 |
// wrapper that computes the length of the message and then sends the command string |
|
221 |
-(int) sendToEngine: (NSString *)string { |
|
222 |
uint8_t length = [string length]; |
|
223 |
||
224 |
SDLNet_TCP_Send(csd, &length , 1); |
|
225 |
return SDLNet_TCP_Send(csd, [string UTF8String], length); |
|
226 |
} |
|
227 |
||
228 |
// method that handles net setup with engine and keeps connection alive |
|
229 |
-(void) engineProtocol { |
|
230 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
231 |
TCPsocket sd; |
3547 | 232 |
IPaddress ip; |
233 |
int eProto; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
234 |
BOOL clientQuit; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
235 |
char buffer[BUFFER_SIZE]; |
3547 | 236 |
uint8_t msgSize; |
237 |
uint16_t gameTicks; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
238 |
|
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
239 |
clientQuit = NO; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
240 |
csd = NULL; |
3547 | 241 |
|
242 |
if (SDLNet_Init() < 0) { |
|
243 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
244 |
clientQuit = YES; |
3547 | 245 |
} |
246 |
||
247 |
// Resolving the host using NULL make network interface to listen |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
248 |
if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0 && !clientQuit) { |
3547 | 249 |
DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
250 |
clientQuit = YES; |
3547 | 251 |
} |
252 |
||
253 |
// Open a connection with the IP provided (listen on the host's port) |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
254 |
if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) { |
3547 | 255 |
DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
256 |
clientQuit = YES; |
3547 | 257 |
} |
258 |
||
259 |
DLog(@"Waiting for a client on port %d", ipcPort); |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
260 |
while (csd == NULL) |
3547 | 261 |
csd = SDLNet_TCP_Accept(sd); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
262 |
SDLNet_TCP_Close(sd); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
263 |
|
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
264 |
while (!clientQuit) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
265 |
msgSize = 0; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
266 |
memset(buffer, 0, BUFFER_SIZE); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
267 |
if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0) |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
268 |
clientQuit = YES; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
269 |
if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0) |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
270 |
clientQuit = YES; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
271 |
|
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
272 |
switch (buffer[0]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
273 |
case 'C': |
3598 | 274 |
DLog(@"sending game config\n%@",self.gameConfig); |
3547 | 275 |
|
276 |
// local game |
|
277 |
[self sendToEngine:@"TL"]; |
|
278 |
||
279 |
// seed info |
|
280 |
[self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
281 |
|
3547 | 282 |
// dimension of the map |
283 |
[self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]]; |
|
284 |
[self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]]; |
|
285 |
[self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
286 |
|
3547 | 287 |
// theme info |
288 |
[self sendToEngine:[self.gameConfig objectForKey:@"theme_command"]]; |
|
289 |
||
290 |
// scheme (returns initial health) |
|
291 |
NSInteger health = [self provideScheme:[self.gameConfig objectForKey:@"scheme"]]; |
|
292 |
||
293 |
NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"]; |
|
294 |
for (NSDictionary *teamData in teamsConfig) { |
|
295 |
[self provideTeamData:[teamData objectForKey:@"team"] |
|
296 |
forHogs:[[teamData objectForKey:@"number"] intValue] |
|
297 |
withHealth:health |
|
298 |
ofColor:[teamData objectForKey:@"color"]]; |
|
299 |
} |
|
300 |
||
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
301 |
[self provideAmmoData:[self.gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
302 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
303 |
case '?': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
304 |
DLog(@"Ping? Pong!"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
305 |
[self sendToEngine:@"!"]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
306 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
307 |
case 'E': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
308 |
DLog(@"ERROR - last console line: [%s]", &buffer[1]); |
3547 | 309 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
310 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
311 |
case 'e': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
312 |
sscanf(buffer, "%*s %d", &eProto); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
313 |
short int netProto = 0; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
314 |
char *versionStr; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
315 |
|
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
316 |
HW_versionInfo(&netProto, &versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
317 |
if (netProto == eProto) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
318 |
DLog(@"Setting protocol version %d (%s)", eProto, versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
319 |
} else { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
320 |
DLog(@"ERROR - wrong protocol number: [%s] - expecting %d", &buffer[1], eProto); |
3547 | 321 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
322 |
} |
3547 | 323 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
324 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
325 |
case 'i': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
326 |
switch (buffer[1]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
327 |
case 'r': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
328 |
NSLog(@"Winning team: %s", &buffer[2]); |
3547 | 329 |
break; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
330 |
case 'k': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
331 |
NSLog(@"Best Hedgehog: %s", &buffer[2]); |
3547 | 332 |
break; |
333 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
334 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
335 |
default: |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
336 |
// empty packet or just statistics -- in either cases gameTicks is sent |
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3613
diff
changeset
|
337 |
gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
338 |
//DLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
339 |
break; |
3547 | 340 |
} |
341 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
342 |
DLog(@"Engine exited, closing server"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
343 |
// wait a little to let the client close cleanly |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
344 |
[NSThread sleepForTimeInterval:2]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
345 |
// Close the client socket |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
346 |
SDLNet_TCP_Close(csd); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
347 |
SDLNet_Quit(); |
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3613
diff
changeset
|
348 |
|
3547 | 349 |
[pool release]; |
350 |
//Invoking this method should be avoided as it does not give your thread a chance to clean up any resources it allocated during its execution. |
|
351 |
//[NSThread exit]; |
|
352 |
} |
|
353 |
||
354 |
#pragma mark - |
|
355 |
#pragma mark Setting methods |
|
356 |
// returns an array of c-strings that are read by engine at startup |
|
357 |
-(const char **)getSettings { |
|
358 |
NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", ipcPort]; |
|
359 |
NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]; |
|
360 |
CGRect screenBounds = [[UIScreen mainScreen] bounds]; |
|
361 |
NSString *wSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.width]; |
|
362 |
NSString *hSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.height]; |
|
3613 | 363 |
const char **gameArgs = (const char**) malloc(sizeof(char *) * 10); |
3547 | 364 |
|
365 |
size_t size; |
|
366 |
// Set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space |
|
367 |
sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
|
3613 | 368 |
char *name = (char *)malloc(sizeof(char) * size); |
3547 | 369 |
// Get the platform name |
370 |
sysctlbyname("hw.machine", name, &size, NULL, 0); |
|
3613 | 371 |
NSString *modelId = [[NSString alloc] initWithUTF8String:name]; |
3547 | 372 |
free(name); |
373 |
||
3613 | 374 |
if ([modelId hasPrefix:@"iPhone1"] || // = iPhone or iPhone 3G |
375 |
[modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"]) // = iPod Touch or iPod Touch 2G |
|
376 |
gameArgs[9] = "2"; // rqLowRes & rqBlurryLand |
|
377 |
else if ([modelId hasPrefix:@"iPhone2"] || // = iPhone 3GS |
|
378 |
[modelId hasPrefix:@"iPad1"] || // = iPad |
|
379 |
[modelId hasPrefix:@"iPod3"]) // = iPod Touch 3G |
|
380 |
gameArgs[9] = "1"; // rqBlurryLand |
|
381 |
else // = everything else |
|
382 |
gameArgs[9] = "0"; // full quality |
|
383 |
[modelId release]; |
|
384 |
||
3547 | 385 |
|
386 |
// prevents using an empty nickname |
|
387 |
NSString *username; |
|
388 |
NSString *originalUsername = [self.systemSettings objectForKey:@"username"]; |
|
389 |
if ([originalUsername length] == 0) |
|
390 |
username = [[NSString alloc] initWithFormat:@"MobileUser-%@",ipcString]; |
|
391 |
else |
|
392 |
username = [[NSString alloc] initWithString:originalUsername]; |
|
393 |
||
394 |
gameArgs[0] = [username UTF8String]; //UserNick |
|
395 |
gameArgs[1] = [ipcString UTF8String]; //ipcPort |
|
396 |
gameArgs[2] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String]; //isSoundEnabled |
|
397 |
gameArgs[3] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String]; //isMusicEnabled |
|
398 |
gameArgs[4] = [localeString UTF8String]; //cLocaleFName |
|
399 |
gameArgs[5] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String]; //cAltDamage |
|
400 |
gameArgs[6] = [wSize UTF8String]; //cScreenHeight |
|
401 |
gameArgs[7] = [hSize UTF8String]; //cScreenWidth |
|
402 |
gameArgs[8] = NULL; //recordFileName |
|
403 |
||
404 |
[wSize release]; |
|
405 |
[hSize release]; |
|
406 |
[localeString release]; |
|
407 |
[ipcString release]; |
|
408 |
[username release]; |
|
409 |
return gameArgs; |
|
410 |
} |
|
411 |
||
412 |
||
413 |
@end |