author | koda |
Sun, 22 Aug 2010 22:08:39 +0200 | |
changeset 3752 | 73c2d7d5643b |
parent 3697 | d5b30d6373fc |
child 3779 | 3351a017d4ad |
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 |
#import "GameSetup.h" |
|
10 |
#import "SDL_uikitappdelegate.h" |
|
11 |
#import "SDL_net.h" |
|
12 |
#import "PascalImports.h" |
|
13 |
#import "CommodityFunctions.h" |
|
14 |
||
15 |
#define BUFFER_SIZE 256 |
|
16 |
||
17 |
@implementation GameSetup |
|
18 |
||
19 |
@synthesize systemSettings, gameConfig; |
|
20 |
||
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
|
21 |
-(id) initWithDictionary:(NSDictionary *)gameDictionary { |
3547 | 22 |
if (self = [super init]) { |
23 |
ipcPort = randomPort(); |
|
3697 | 24 |
|
3547 | 25 |
// should check they exist and throw and exection if not |
26 |
NSDictionary *dictSett = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()]; |
|
27 |
self.systemSettings = dictSett; |
|
28 |
[dictSett release]; |
|
3697 | 29 |
|
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
|
30 |
self.gameConfig = gameDictionary; |
3697 | 31 |
} |
3547 | 32 |
return self; |
33 |
} |
|
34 |
||
35 |
-(void) dealloc { |
|
36 |
[gameConfig release]; |
|
37 |
[systemSettings release]; |
|
38 |
[super dealloc]; |
|
39 |
} |
|
40 |
||
41 |
#pragma mark - |
|
42 |
#pragma mark Provider functions |
|
43 |
// unpacks team data from the selected team.plist to a sequence of engine commands |
|
44 |
-(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor { |
|
45 |
/* |
|
46 |
addteam <32charsMD5hash> <color> <team name> |
|
47 |
addhh <level> <health> <hedgehog name> |
|
48 |
<level> is 0 for human, 1-5 for bots (5 is the most stupid) |
|
49 |
*/ |
|
3697 | 50 |
|
3547 | 51 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), teamName]; |
52 |
NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile]; |
|
53 |
[teamFile release]; |
|
3697 | 54 |
|
55 |
NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@", |
|
3660 | 56 |
[teamData objectForKey:@"hash"], [teamColor stringValue], [teamName stringByDeletingPathExtension]]; |
3547 | 57 |
[self sendToEngine: teamHashColorAndName]; |
58 |
[teamHashColorAndName release]; |
|
3697 | 59 |
|
3547 | 60 |
NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]]; |
61 |
[self sendToEngine: grave]; |
|
62 |
[grave release]; |
|
3697 | 63 |
|
3547 | 64 |
NSString *fort = [[NSString alloc] initWithFormat:@"efort %@", [teamData objectForKey:@"fort"]]; |
65 |
[self sendToEngine: fort]; |
|
66 |
[fort release]; |
|
3697 | 67 |
|
3547 | 68 |
NSString *voicepack = [[NSString alloc] initWithFormat:@"evoicepack %@", [teamData objectForKey:@"voicepack"]]; |
69 |
[self sendToEngine: voicepack]; |
|
70 |
[voicepack release]; |
|
3697 | 71 |
|
3547 | 72 |
NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]]; |
73 |
[self sendToEngine: flag]; |
|
74 |
[flag release]; |
|
3697 | 75 |
|
3547 | 76 |
NSArray *hogs = [teamData objectForKey:@"hedgehogs"]; |
77 |
for (int i = 0; i < numberOfPlayingHogs; i++) { |
|
78 |
NSDictionary *hog = [hogs objectAtIndex:i]; |
|
3697 | 79 |
|
80 |
NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %d %@", |
|
3547 | 81 |
[hog objectForKey:@"level"], initialHealth, [hog objectForKey:@"hogname"]]; |
82 |
[self sendToEngine: hogLevelHealthAndName]; |
|
83 |
[hogLevelHealthAndName release]; |
|
3697 | 84 |
|
3547 | 85 |
NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]]; |
86 |
[self sendToEngine: hogHat]; |
|
87 |
[hogHat release]; |
|
88 |
} |
|
3697 | 89 |
|
3547 | 90 |
[teamData release]; |
91 |
} |
|
92 |
||
93 |
// unpacks ammostore data from the selected ammo.plist to a sequence of engine commands |
|
94 |
-(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams { |
|
95 |
NSString *weaponPath = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),ammostoreName]; |
|
96 |
NSDictionary *ammoData = [[NSDictionary alloc] initWithContentsOfFile:weaponPath]; |
|
97 |
[weaponPath release]; |
|
3621 | 98 |
NSString *update = @""; |
3697 | 99 |
|
3621 | 100 |
// if we're loading an older version of ammos fill the engine message with 0s |
101 |
int diff = CURRENT_AMMOSIZE - [[ammoData objectForKey:@"version"] intValue]; |
|
102 |
if (diff != 0) |
|
103 |
update = [NSString stringWithCharacters:(const unichar*)"0000000000000000000000000000000000" length:diff]; |
|
3697 | 104 |
|
3621 | 105 |
NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@%@", [ammoData objectForKey:@"ammostore_initialqt"], update]; |
3547 | 106 |
[self sendToEngine: ammloadt]; |
107 |
[ammloadt release]; |
|
3697 | 108 |
|
3621 | 109 |
NSString *ammprob = [[NSString alloc] initWithFormat:@"eammprob %@%@", [ammoData objectForKey:@"ammostore_probability"], update]; |
3547 | 110 |
[self sendToEngine: ammprob]; |
111 |
[ammprob release]; |
|
3697 | 112 |
|
3621 | 113 |
NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammdelay %@%@", [ammoData objectForKey:@"ammostore_delay"], update]; |
3547 | 114 |
[self sendToEngine: ammdelay]; |
115 |
[ammdelay release]; |
|
3697 | 116 |
|
3621 | 117 |
NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@%@", [ammoData objectForKey:@"ammostore_crate"], update]; |
3547 | 118 |
[self sendToEngine: ammreinf]; |
119 |
[ammreinf release]; |
|
3697 | 120 |
|
3547 | 121 |
// sent twice so it applies to both teams |
122 |
NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"]; |
|
123 |
for (int i = 0; i < numberOfTeams; i++) |
|
124 |
[self sendToEngine: ammstore]; |
|
125 |
[ammstore release]; |
|
3697 | 126 |
|
3547 | 127 |
[ammoData release]; |
128 |
} |
|
129 |
||
130 |
// unpacks scheme data from the selected scheme.plist to a sequence of engine commands |
|
131 |
-(NSInteger) provideScheme:(NSString *)schemeName { |
|
132 |
NSString *schemePath = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),schemeName]; |
|
133 |
NSArray *scheme = [[NSArray alloc] initWithContentsOfFile:schemePath]; |
|
134 |
[schemePath release]; |
|
135 |
int result = 0; |
|
136 |
int i = 0; |
|
137 |
||
138 |
if ([[scheme objectAtIndex:i++] boolValue]) |
|
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
139 |
result |= 0x00000001; |
3547 | 140 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
141 |
result |= 0x00000010; |
3547 | 142 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
143 |
result |= 0x00000004; |
3547 | 144 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
145 |
result |= 0x00000008; |
3547 | 146 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
147 |
result |= 0x00000020; |
3547 | 148 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
149 |
result |= 0x00000040; |
3547 | 150 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
151 |
result |= 0x00000080; |
3547 | 152 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
153 |
result |= 0x00000100; |
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
154 |
if ([[scheme objectAtIndex:i++] boolValue]) |
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
155 |
result |= 0x00000200; |
3547 | 156 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
157 |
result |= 0x00000400; |
3547 | 158 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
159 |
result |= 0x00000800; |
3547 | 160 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
161 |
result |= 0x00002000; |
3547 | 162 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
163 |
result |= 0x00004000; |
3547 | 164 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
165 |
result |= 0x00008000; |
3547 | 166 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
167 |
result |= 0x00010000; |
3547 | 168 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
169 |
result |= 0x00020000; |
3547 | 170 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
171 |
result |= 0x00080000; |
3547 | 172 |
if ([[scheme objectAtIndex:i++] boolValue]) |
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
173 |
result |= 0x00100000; |
3547 | 174 |
|
175 |
NSString *flags = [[NSString alloc] initWithFormat:@"e$gmflags %d",result]; |
|
176 |
[self sendToEngine:flags]; |
|
177 |
[flags release]; |
|
3697 | 178 |
|
3547 | 179 |
NSString *dmgMod = [[NSString alloc] initWithFormat:@"e$damagepct %d",[[scheme objectAtIndex:i++] intValue]]; |
180 |
[self sendToEngine:dmgMod]; |
|
181 |
[dmgMod release]; |
|
3697 | 182 |
|
3547 | 183 |
NSString *turnTime = [[NSString alloc] initWithFormat:@"e$turntime %d",[[scheme objectAtIndex:i++] intValue] * 1000]; |
184 |
[self sendToEngine:turnTime]; |
|
185 |
[turnTime release]; |
|
3697 | 186 |
|
3547 | 187 |
result = [[scheme objectAtIndex:i++] intValue]; // initial health |
3697 | 188 |
|
3547 | 189 |
NSString *sdTime = [[NSString alloc] initWithFormat:@"e$sd_turns %d",[[scheme objectAtIndex:i++] intValue]]; |
190 |
[self sendToEngine:sdTime]; |
|
191 |
[sdTime release]; |
|
3697 | 192 |
|
3547 | 193 |
NSString *crateDrops = [[NSString alloc] initWithFormat:@"e$casefreq %d",[[scheme objectAtIndex:i++] intValue]]; |
194 |
[self sendToEngine:crateDrops]; |
|
195 |
[crateDrops release]; |
|
3697 | 196 |
|
3547 | 197 |
NSString *minesTime = [[NSString alloc] initWithFormat:@"e$minestime %d",[[scheme objectAtIndex:i++] intValue] * 1000]; |
198 |
[self sendToEngine:minesTime]; |
|
199 |
[minesTime release]; |
|
3697 | 200 |
|
3547 | 201 |
NSString *minesNumber = [[NSString alloc] initWithFormat:@"e$landadds %d",[[scheme objectAtIndex:i++] intValue]]; |
202 |
[self sendToEngine:minesNumber]; |
|
203 |
[minesNumber release]; |
|
3697 | 204 |
|
3752
73c2d7d5643b
add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents:
3697
diff
changeset
|
205 |
|
3547 | 206 |
NSString *dudMines = [[NSString alloc] initWithFormat:@"e$minedudpct %d",[[scheme objectAtIndex:i++] intValue]]; |
207 |
[self sendToEngine:dudMines]; |
|
208 |
[dudMines release]; |
|
3697 | 209 |
|
3547 | 210 |
NSString *explosives = [[NSString alloc] initWithFormat:@"e$explosives %d",[[scheme objectAtIndex:i++] intValue]]; |
211 |
[self sendToEngine:explosives]; |
|
212 |
[explosives release]; |
|
3697 | 213 |
|
3547 | 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]; |
|
3697 | 229 |
|
3547 | 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; |
|
3697 | 244 |
|
3548
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 |
} |
3697 | 252 |
|
3547 | 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 |
} |
3697 | 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 |
} |
3697 | 264 |
|
3547 | 265 |
DLog(@"Waiting for a client on port %d", ipcPort); |
3697 | 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); |
3697 | 269 |
|
3548
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; |
3697 | 277 |
|
3548
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); |
3697 | 281 |
|
3547 | 282 |
// local game |
283 |
[self sendToEngine:@"TL"]; |
|
3697 | 284 |
|
3547 | 285 |
// seed info |
286 |
[self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]]; |
|
3697 | 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"]]; |
|
3697 | 292 |
|
3642 | 293 |
// static land (if set) |
294 |
NSString *staticMap = [self.gameConfig objectForKey:@"staticmap_command"]; |
|
295 |
if ([staticMap length] != 0) |
|
296 |
[self sendToEngine:staticMap]; |
|
3697 | 297 |
|
3547 | 298 |
// theme info |
299 |
[self sendToEngine:[self.gameConfig objectForKey:@"theme_command"]]; |
|
3697 | 300 |
|
3547 | 301 |
// scheme (returns initial health) |
302 |
NSInteger health = [self provideScheme:[self.gameConfig objectForKey:@"scheme"]]; |
|
3697 | 303 |
|
3547 | 304 |
NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"]; |
305 |
for (NSDictionary *teamData in teamsConfig) { |
|
3697 | 306 |
[self provideTeamData:[teamData objectForKey:@"team"] |
3547 | 307 |
forHogs:[[teamData objectForKey:@"number"] intValue] |
308 |
withHealth:health |
|
309 |
ofColor:[teamData objectForKey:@"color"]]; |
|
310 |
} |
|
3697 | 311 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
312 |
[self provideAmmoData:[self.gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
313 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
314 |
case '?': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
315 |
DLog(@"Ping? Pong!"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
316 |
[self sendToEngine:@"!"]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
317 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
318 |
case 'E': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
319 |
DLog(@"ERROR - last console line: [%s]", &buffer[1]); |
3547 | 320 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
321 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
322 |
case 'e': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
323 |
sscanf(buffer, "%*s %d", &eProto); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
324 |
short int netProto = 0; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
325 |
char *versionStr; |
3697 | 326 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
327 |
HW_versionInfo(&netProto, &versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
328 |
if (netProto == eProto) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
329 |
DLog(@"Setting protocol version %d (%s)", eProto, versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
330 |
} else { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
331 |
DLog(@"ERROR - wrong protocol number: [%s] - expecting %d", &buffer[1], eProto); |
3547 | 332 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
333 |
} |
3697 | 334 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
335 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
336 |
case 'i': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
337 |
switch (buffer[1]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
338 |
case 'r': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
339 |
NSLog(@"Winning team: %s", &buffer[2]); |
3547 | 340 |
break; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
341 |
case 'k': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
342 |
NSLog(@"Best Hedgehog: %s", &buffer[2]); |
3547 | 343 |
break; |
344 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
345 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
346 |
default: |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
347 |
// 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
|
348 |
gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
349 |
//DLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
350 |
break; |
3547 | 351 |
} |
352 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
353 |
DLog(@"Engine exited, closing server"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
354 |
// wait a little to let the client close cleanly |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
355 |
[NSThread sleepForTimeInterval:2]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
356 |
// Close the client socket |
3697 | 357 |
SDLNet_TCP_Close(csd); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
358 |
SDLNet_Quit(); |
3697 | 359 |
|
3547 | 360 |
[pool release]; |
361 |
//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. |
|
362 |
//[NSThread exit]; |
|
363 |
} |
|
364 |
||
365 |
#pragma mark - |
|
366 |
#pragma mark Setting methods |
|
367 |
// returns an array of c-strings that are read by engine at startup |
|
368 |
-(const char **)getSettings { |
|
369 |
NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", ipcPort]; |
|
370 |
NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]; |
|
371 |
CGRect screenBounds = [[UIScreen mainScreen] bounds]; |
|
372 |
NSString *wSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.width]; |
|
373 |
NSString *hSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.height]; |
|
3613 | 374 |
const char **gameArgs = (const char**) malloc(sizeof(char *) * 10); |
3634 | 375 |
NSInteger tmpQuality; |
3697 | 376 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3660
diff
changeset
|
377 |
NSString *modelId = modelType(); |
3613 | 378 |
if ([modelId hasPrefix:@"iPhone1"] || // = iPhone or iPhone 3G |
379 |
[modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"]) // = iPod Touch or iPod Touch 2G |
|
3634 | 380 |
tmpQuality = 0x00000001 | 0x00000002 | 0x00000040; // rqLowRes | rqBlurryLand | rqKillFlakes |
3613 | 381 |
else if ([modelId hasPrefix:@"iPhone2"] || // = iPhone 3GS |
382 |
[modelId hasPrefix:@"iPod3"]) // = iPod Touch 3G |
|
3697 | 383 |
tmpQuality = 0x00000002 | 0x00000040; // rqBlurryLand | rqKillFlakes |
3624 | 384 |
else if ([modelId hasPrefix:@"iPad1"]) // = iPad |
3634 | 385 |
tmpQuality = 0x00000002; // rqBlurryLand |
3624 | 386 |
else // = everything else |
3634 | 387 |
tmpQuality = 0; // full quality |
388 |
if (![modelId hasPrefix:@"iPad"]) // = disable tooltips unless iPad |
|
389 |
tmpQuality = tmpQuality | 0x00000400; |
|
3697 | 390 |
|
3634 | 391 |
gameArgs[9] = [[[NSNumber numberWithInteger:tmpQuality] stringValue] UTF8String]; |
392 |
||
3547 | 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]; |
|
3697 | 400 |
|
3547 | 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 |
|
3697 | 410 |
|
3547 | 411 |
[wSize release]; |
412 |
[hSize release]; |
|
413 |
[localeString release]; |
|
414 |
[ipcString release]; |
|
415 |
[username release]; |
|
416 |
return gameArgs; |
|
417 |
} |
|
418 |
||
419 |
||
420 |
@end |