author | koda |
Wed, 07 Jul 2010 02:52:11 +0200 | |
changeset 3626 | 19f78afa0188 |
parent 3624 | 304c6d32383a |
child 3634 | 93d260c96635 |
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]; |
|
3621 | 101 |
NSString *update = @""; |
3547 | 102 |
|
3621 | 103 |
// if we're loading an older version of ammos fill the engine message with 0s |
104 |
int diff = CURRENT_AMMOSIZE - [[ammoData objectForKey:@"version"] intValue]; |
|
105 |
if (diff != 0) |
|
106 |
update = [NSString stringWithCharacters:(const unichar*)"0000000000000000000000000000000000" length:diff]; |
|
107 |
||
108 |
NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@%@", [ammoData objectForKey:@"ammostore_initialqt"], update]; |
|
3547 | 109 |
[self sendToEngine: ammloadt]; |
110 |
[ammloadt release]; |
|
111 |
||
3621 | 112 |
NSString *ammprob = [[NSString alloc] initWithFormat:@"eammprob %@%@", [ammoData objectForKey:@"ammostore_probability"], update]; |
3547 | 113 |
[self sendToEngine: ammprob]; |
114 |
[ammprob release]; |
|
115 |
||
3621 | 116 |
NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammdelay %@%@", [ammoData objectForKey:@"ammostore_delay"], update]; |
3547 | 117 |
[self sendToEngine: ammdelay]; |
118 |
[ammdelay release]; |
|
119 |
||
3621 | 120 |
NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@%@", [ammoData objectForKey:@"ammostore_crate"], update]; |
3547 | 121 |
[self sendToEngine: ammreinf]; |
122 |
[ammreinf release]; |
|
123 |
||
124 |
// sent twice so it applies to both teams |
|
125 |
NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"]; |
|
126 |
for (int i = 0; i < numberOfTeams; i++) |
|
127 |
[self sendToEngine: ammstore]; |
|
128 |
[ammstore release]; |
|
129 |
||
130 |
[ammoData release]; |
|
131 |
} |
|
132 |
||
133 |
// unpacks scheme data from the selected scheme.plist to a sequence of engine commands |
|
134 |
-(NSInteger) provideScheme:(NSString *)schemeName { |
|
135 |
NSString *schemePath = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),schemeName]; |
|
136 |
NSArray *scheme = [[NSArray alloc] initWithContentsOfFile:schemePath]; |
|
137 |
[schemePath release]; |
|
138 |
int result = 0; |
|
139 |
int i = 0; |
|
140 |
||
141 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
142 |
result |= 0x01; |
|
143 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
144 |
result |= 0x10; |
|
145 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
146 |
result |= 0x04; |
|
147 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
148 |
result |= 0x08; |
|
149 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
150 |
result |= 0x20; |
|
151 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
152 |
result |= 0x40; |
|
153 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
154 |
result |= 0x80; |
|
155 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
156 |
result |= 0x100; |
|
157 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
158 |
result |= 0x200; |
|
159 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
160 |
result |= 0x400; |
|
161 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
162 |
result |= 0x800; |
|
163 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
164 |
result |= 0x2000; |
|
165 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
166 |
result |= 0x4000; |
|
167 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
168 |
result |= 0x8000; |
|
169 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
170 |
result |= 0x10000; |
|
171 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
172 |
result |= 0x20000; |
|
173 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
174 |
result |= 0x80000; |
|
175 |
||
176 |
NSString *flags = [[NSString alloc] initWithFormat:@"e$gmflags %d",result]; |
|
177 |
[self sendToEngine:flags]; |
|
178 |
[flags release]; |
|
179 |
||
180 |
NSString *dmgMod = [[NSString alloc] initWithFormat:@"e$damagepct %d",[[scheme objectAtIndex:i++] intValue]]; |
|
181 |
[self sendToEngine:dmgMod]; |
|
182 |
[dmgMod release]; |
|
183 |
||
184 |
NSString *turnTime = [[NSString alloc] initWithFormat:@"e$turntime %d",[[scheme objectAtIndex:i++] intValue] * 1000]; |
|
185 |
[self sendToEngine:turnTime]; |
|
186 |
[turnTime release]; |
|
187 |
||
188 |
result = [[scheme objectAtIndex:i++] intValue]; // initial health |
|
189 |
||
190 |
NSString *sdTime = [[NSString alloc] initWithFormat:@"e$sd_turns %d",[[scheme objectAtIndex:i++] intValue]]; |
|
191 |
[self sendToEngine:sdTime]; |
|
192 |
[sdTime release]; |
|
193 |
||
194 |
NSString *crateDrops = [[NSString alloc] initWithFormat:@"e$casefreq %d",[[scheme objectAtIndex:i++] intValue]]; |
|
195 |
[self sendToEngine:crateDrops]; |
|
196 |
[crateDrops release]; |
|
197 |
||
198 |
NSString *minesTime = [[NSString alloc] initWithFormat:@"e$minestime %d",[[scheme objectAtIndex:i++] intValue] * 1000]; |
|
199 |
[self sendToEngine:minesTime]; |
|
200 |
[minesTime release]; |
|
201 |
||
202 |
NSString *minesNumber = [[NSString alloc] initWithFormat:@"e$landadds %d",[[scheme objectAtIndex:i++] intValue]]; |
|
203 |
[self sendToEngine:minesNumber]; |
|
204 |
[minesNumber release]; |
|
205 |
||
206 |
NSString *dudMines = [[NSString alloc] initWithFormat:@"e$minedudpct %d",[[scheme objectAtIndex:i++] intValue]]; |
|
207 |
[self sendToEngine:dudMines]; |
|
208 |
[dudMines release]; |
|
209 |
||
210 |
NSString *explosives = [[NSString alloc] initWithFormat:@"e$explosives %d",[[scheme objectAtIndex:i++] intValue]]; |
|
211 |
[self sendToEngine:explosives]; |
|
212 |
[explosives release]; |
|
213 |
||
214 |
[scheme release]; |
|
215 |
return result; |
|
216 |
} |
|
217 |
||
218 |
#pragma mark - |
|
219 |
#pragma mark Thread/Network relevant code |
|
220 |
// select one of GameSetup method and execute it in a seprate thread |
|
221 |
-(void) startThread: (NSString *) selector { |
|
222 |
SEL usage = NSSelectorFromString(selector); |
|
223 |
[NSThread detachNewThreadSelector:usage toTarget:self withObject:nil]; |
|
224 |
} |
|
225 |
||
226 |
// wrapper that computes the length of the message and then sends the command string |
|
227 |
-(int) sendToEngine: (NSString *)string { |
|
228 |
uint8_t length = [string length]; |
|
229 |
||
230 |
SDLNet_TCP_Send(csd, &length , 1); |
|
231 |
return SDLNet_TCP_Send(csd, [string UTF8String], length); |
|
232 |
} |
|
233 |
||
234 |
// method that handles net setup with engine and keeps connection alive |
|
235 |
-(void) engineProtocol { |
|
236 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
237 |
TCPsocket sd; |
3547 | 238 |
IPaddress ip; |
239 |
int eProto; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
240 |
BOOL clientQuit; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
241 |
char buffer[BUFFER_SIZE]; |
3547 | 242 |
uint8_t msgSize; |
243 |
uint16_t gameTicks; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
244 |
|
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
245 |
clientQuit = NO; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
246 |
csd = NULL; |
3547 | 247 |
|
248 |
if (SDLNet_Init() < 0) { |
|
249 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
250 |
clientQuit = YES; |
3547 | 251 |
} |
252 |
||
253 |
// Resolving the host using NULL make network interface to listen |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
254 |
if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0 && !clientQuit) { |
3547 | 255 |
DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
256 |
clientQuit = YES; |
3547 | 257 |
} |
258 |
||
259 |
// 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
|
260 |
if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) { |
3547 | 261 |
DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
262 |
clientQuit = YES; |
3547 | 263 |
} |
264 |
||
265 |
DLog(@"Waiting for a client on port %d", ipcPort); |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
266 |
while (csd == NULL) |
3547 | 267 |
csd = SDLNet_TCP_Accept(sd); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
268 |
SDLNet_TCP_Close(sd); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
269 |
|
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
270 |
while (!clientQuit) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
271 |
msgSize = 0; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
272 |
memset(buffer, 0, BUFFER_SIZE); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
273 |
if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0) |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
274 |
clientQuit = YES; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
275 |
if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0) |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
276 |
clientQuit = YES; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
277 |
|
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
278 |
switch (buffer[0]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
279 |
case 'C': |
3626 | 280 |
DLog(@"sending game config...\n%@",self.gameConfig); |
3547 | 281 |
|
282 |
// local game |
|
283 |
[self sendToEngine:@"TL"]; |
|
284 |
||
285 |
// seed info |
|
286 |
[self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
287 |
|
3547 | 288 |
// dimension of the map |
289 |
[self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]]; |
|
290 |
[self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]]; |
|
291 |
[self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
292 |
|
3547 | 293 |
// theme info |
294 |
[self sendToEngine:[self.gameConfig objectForKey:@"theme_command"]]; |
|
295 |
||
296 |
// scheme (returns initial health) |
|
297 |
NSInteger health = [self provideScheme:[self.gameConfig objectForKey:@"scheme"]]; |
|
298 |
||
299 |
NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"]; |
|
300 |
for (NSDictionary *teamData in teamsConfig) { |
|
301 |
[self provideTeamData:[teamData objectForKey:@"team"] |
|
302 |
forHogs:[[teamData objectForKey:@"number"] intValue] |
|
303 |
withHealth:health |
|
304 |
ofColor:[teamData objectForKey:@"color"]]; |
|
305 |
} |
|
306 |
||
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
307 |
[self provideAmmoData:[self.gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
308 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
309 |
case '?': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
310 |
DLog(@"Ping? Pong!"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
311 |
[self sendToEngine:@"!"]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
312 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
313 |
case 'E': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
314 |
DLog(@"ERROR - last console line: [%s]", &buffer[1]); |
3547 | 315 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
316 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
317 |
case 'e': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
318 |
sscanf(buffer, "%*s %d", &eProto); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
319 |
short int netProto = 0; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
320 |
char *versionStr; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
321 |
|
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
322 |
HW_versionInfo(&netProto, &versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
323 |
if (netProto == eProto) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
324 |
DLog(@"Setting protocol version %d (%s)", eProto, versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
325 |
} else { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
326 |
DLog(@"ERROR - wrong protocol number: [%s] - expecting %d", &buffer[1], eProto); |
3547 | 327 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
328 |
} |
3547 | 329 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
330 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
331 |
case 'i': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
332 |
switch (buffer[1]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
333 |
case 'r': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
334 |
NSLog(@"Winning team: %s", &buffer[2]); |
3547 | 335 |
break; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
336 |
case 'k': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
337 |
NSLog(@"Best Hedgehog: %s", &buffer[2]); |
3547 | 338 |
break; |
339 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
340 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
341 |
default: |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
342 |
// 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
|
343 |
gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
344 |
//DLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
345 |
break; |
3547 | 346 |
} |
347 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
348 |
DLog(@"Engine exited, closing server"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
349 |
// wait a little to let the client close cleanly |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
350 |
[NSThread sleepForTimeInterval:2]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
351 |
// Close the client socket |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
352 |
SDLNet_TCP_Close(csd); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
353 |
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
|
354 |
|
3547 | 355 |
[pool release]; |
356 |
//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. |
|
357 |
//[NSThread exit]; |
|
358 |
} |
|
359 |
||
360 |
#pragma mark - |
|
361 |
#pragma mark Setting methods |
|
362 |
// returns an array of c-strings that are read by engine at startup |
|
363 |
-(const char **)getSettings { |
|
364 |
NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", ipcPort]; |
|
365 |
NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]; |
|
366 |
CGRect screenBounds = [[UIScreen mainScreen] bounds]; |
|
367 |
NSString *wSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.width]; |
|
368 |
NSString *hSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.height]; |
|
3613 | 369 |
const char **gameArgs = (const char**) malloc(sizeof(char *) * 10); |
3547 | 370 |
|
371 |
size_t size; |
|
372 |
// Set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space |
|
373 |
sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
|
3613 | 374 |
char *name = (char *)malloc(sizeof(char) * size); |
3547 | 375 |
// Get the platform name |
376 |
sysctlbyname("hw.machine", name, &size, NULL, 0); |
|
3613 | 377 |
NSString *modelId = [[NSString alloc] initWithUTF8String:name]; |
3547 | 378 |
free(name); |
379 |
||
3613 | 380 |
if ([modelId hasPrefix:@"iPhone1"] || // = iPhone or iPhone 3G |
381 |
[modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"]) // = iPod Touch or iPod Touch 2G |
|
3624 | 382 |
gameArgs[9] = "3"; // rqLowRes & rqBlurryLand & rqKillFlakes |
3613 | 383 |
else if ([modelId hasPrefix:@"iPhone2"] || // = iPhone 3GS |
384 |
[modelId hasPrefix:@"iPod3"]) // = iPod Touch 3G |
|
3624 | 385 |
gameArgs[9] = "2"; // rqBlurryLand & rqKillFlakes |
386 |
else if ([modelId hasPrefix:@"iPad1"]) // = iPad |
|
387 |
gameArgs[9] = "1"; // rqBlurryLand |
|
388 |
else // = everything else |
|
389 |
gameArgs[9] = "0"; // full quality |
|
3613 | 390 |
[modelId release]; |
391 |
||
3547 | 392 |
|
393 |
// prevents using an empty nickname |
|
394 |
NSString *username; |
|
395 |
NSString *originalUsername = [self.systemSettings objectForKey:@"username"]; |
|
396 |
if ([originalUsername length] == 0) |
|
397 |
username = [[NSString alloc] initWithFormat:@"MobileUser-%@",ipcString]; |
|
398 |
else |
|
399 |
username = [[NSString alloc] initWithString:originalUsername]; |
|
400 |
||
401 |
gameArgs[0] = [username UTF8String]; //UserNick |
|
402 |
gameArgs[1] = [ipcString UTF8String]; //ipcPort |
|
403 |
gameArgs[2] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String]; //isSoundEnabled |
|
404 |
gameArgs[3] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String]; //isMusicEnabled |
|
405 |
gameArgs[4] = [localeString UTF8String]; //cLocaleFName |
|
406 |
gameArgs[5] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String]; //cAltDamage |
|
407 |
gameArgs[6] = [wSize UTF8String]; //cScreenHeight |
|
408 |
gameArgs[7] = [hSize UTF8String]; //cScreenWidth |
|
409 |
gameArgs[8] = NULL; //recordFileName |
|
410 |
||
411 |
[wSize release]; |
|
412 |
[hSize release]; |
|
413 |
[localeString release]; |
|
414 |
[ipcString release]; |
|
415 |
[username release]; |
|
416 |
return gameArgs; |
|
417 |
} |
|
418 |
||
419 |
||
420 |
@end |