author | koda |
Sun, 19 Sep 2010 22:56:53 +0200 | |
changeset 3889 | f7d6834a54fe |
parent 3829 | 81db3c85784b |
child 3891 | f8f0d0ceb19c |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 |
* Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
* |
|
18 |
* File created on 10/01/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "GameSetup.h" |
|
23 |
#import "SDL_uikitappdelegate.h" |
|
24 |
#import "SDL_net.h" |
|
25 |
#import "PascalImports.h" |
|
26 |
#import "CommodityFunctions.h" |
|
27 |
||
28 |
#define BUFFER_SIZE 256 |
|
29 |
||
30 |
@implementation GameSetup |
|
31 |
||
32 |
@synthesize systemSettings, gameConfig; |
|
33 |
||
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
|
34 |
-(id) initWithDictionary:(NSDictionary *)gameDictionary { |
3547 | 35 |
if (self = [super init]) { |
36 |
ipcPort = randomPort(); |
|
3697 | 37 |
|
3547 | 38 |
// should check they exist and throw and exection if not |
39 |
NSDictionary *dictSett = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()]; |
|
40 |
self.systemSettings = dictSett; |
|
41 |
[dictSett release]; |
|
3697 | 42 |
|
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
|
43 |
self.gameConfig = gameDictionary; |
3697 | 44 |
} |
3547 | 45 |
return self; |
46 |
} |
|
47 |
||
48 |
-(void) dealloc { |
|
49 |
[gameConfig release]; |
|
50 |
[systemSettings release]; |
|
51 |
[super dealloc]; |
|
52 |
} |
|
53 |
||
54 |
#pragma mark - |
|
55 |
#pragma mark Provider functions |
|
56 |
// unpacks team data from the selected team.plist to a sequence of engine commands |
|
57 |
-(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor { |
|
58 |
/* |
|
59 |
addteam <32charsMD5hash> <color> <team name> |
|
60 |
addhh <level> <health> <hedgehog name> |
|
61 |
<level> is 0 for human, 1-5 for bots (5 is the most stupid) |
|
62 |
*/ |
|
3697 | 63 |
|
3547 | 64 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), teamName]; |
65 |
NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile]; |
|
66 |
[teamFile release]; |
|
3697 | 67 |
|
68 |
NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@", |
|
3660 | 69 |
[teamData objectForKey:@"hash"], [teamColor stringValue], [teamName stringByDeletingPathExtension]]; |
3547 | 70 |
[self sendToEngine: teamHashColorAndName]; |
71 |
[teamHashColorAndName release]; |
|
3697 | 72 |
|
3547 | 73 |
NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]]; |
74 |
[self sendToEngine: grave]; |
|
75 |
[grave release]; |
|
3697 | 76 |
|
3547 | 77 |
NSString *fort = [[NSString alloc] initWithFormat:@"efort %@", [teamData objectForKey:@"fort"]]; |
78 |
[self sendToEngine: fort]; |
|
79 |
[fort release]; |
|
3697 | 80 |
|
3547 | 81 |
NSString *voicepack = [[NSString alloc] initWithFormat:@"evoicepack %@", [teamData objectForKey:@"voicepack"]]; |
82 |
[self sendToEngine: voicepack]; |
|
83 |
[voicepack release]; |
|
3697 | 84 |
|
3547 | 85 |
NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]]; |
86 |
[self sendToEngine: flag]; |
|
87 |
[flag release]; |
|
3697 | 88 |
|
3547 | 89 |
NSArray *hogs = [teamData objectForKey:@"hedgehogs"]; |
90 |
for (int i = 0; i < numberOfPlayingHogs; i++) { |
|
91 |
NSDictionary *hog = [hogs objectAtIndex:i]; |
|
3697 | 92 |
|
93 |
NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %d %@", |
|
3547 | 94 |
[hog objectForKey:@"level"], initialHealth, [hog objectForKey:@"hogname"]]; |
95 |
[self sendToEngine: hogLevelHealthAndName]; |
|
96 |
[hogLevelHealthAndName release]; |
|
3697 | 97 |
|
3547 | 98 |
NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]]; |
99 |
[self sendToEngine: hogHat]; |
|
100 |
[hogHat release]; |
|
101 |
} |
|
3697 | 102 |
|
3547 | 103 |
[teamData release]; |
104 |
} |
|
105 |
||
106 |
// unpacks ammostore data from the selected ammo.plist to a sequence of engine commands |
|
107 |
-(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams { |
|
108 |
NSString *weaponPath = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),ammostoreName]; |
|
109 |
NSDictionary *ammoData = [[NSDictionary alloc] initWithContentsOfFile:weaponPath]; |
|
110 |
[weaponPath release]; |
|
3621 | 111 |
NSString *update = @""; |
3697 | 112 |
|
3621 | 113 |
// if we're loading an older version of ammos fill the engine message with 0s |
114 |
int diff = CURRENT_AMMOSIZE - [[ammoData objectForKey:@"version"] intValue]; |
|
115 |
if (diff != 0) |
|
116 |
update = [NSString stringWithCharacters:(const unichar*)"0000000000000000000000000000000000" length:diff]; |
|
3697 | 117 |
|
3621 | 118 |
NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@%@", [ammoData objectForKey:@"ammostore_initialqt"], update]; |
3547 | 119 |
[self sendToEngine: ammloadt]; |
120 |
[ammloadt release]; |
|
3697 | 121 |
|
3621 | 122 |
NSString *ammprob = [[NSString alloc] initWithFormat:@"eammprob %@%@", [ammoData objectForKey:@"ammostore_probability"], update]; |
3547 | 123 |
[self sendToEngine: ammprob]; |
124 |
[ammprob release]; |
|
3697 | 125 |
|
3621 | 126 |
NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammdelay %@%@", [ammoData objectForKey:@"ammostore_delay"], update]; |
3547 | 127 |
[self sendToEngine: ammdelay]; |
128 |
[ammdelay release]; |
|
3697 | 129 |
|
3621 | 130 |
NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@%@", [ammoData objectForKey:@"ammostore_crate"], update]; |
3547 | 131 |
[self sendToEngine: ammreinf]; |
132 |
[ammreinf release]; |
|
3697 | 133 |
|
3547 | 134 |
// sent twice so it applies to both teams |
135 |
NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"]; |
|
136 |
for (int i = 0; i < numberOfTeams; i++) |
|
137 |
[self sendToEngine: ammstore]; |
|
138 |
[ammstore release]; |
|
3697 | 139 |
|
3547 | 140 |
[ammoData release]; |
141 |
} |
|
142 |
||
143 |
// unpacks scheme data from the selected scheme.plist to a sequence of engine commands |
|
144 |
-(NSInteger) provideScheme:(NSString *)schemeName { |
|
145 |
NSString *schemePath = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),schemeName]; |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
146 |
NSDictionary *schemeDictionary = [[NSDictionary alloc] initWithContentsOfFile:schemePath]; |
3547 | 147 |
[schemePath release]; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
148 |
NSArray *basicArray = [schemeDictionary objectForKey:@"basic"]; |
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
149 |
NSArray *gamemodArray = [schemeDictionary objectForKey:@"gamemod"]; |
3547 | 150 |
int result = 0; |
151 |
int i = 0; |
|
152 |
||
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
153 |
if ([[gamemodArray 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
|
154 |
result |= 0x00000001; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
155 |
if ([[gamemodArray 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
|
156 |
result |= 0x00000010; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
157 |
if ([[gamemodArray 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
|
158 |
result |= 0x00000004; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
159 |
if ([[gamemodArray 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
|
160 |
result |= 0x00000008; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
161 |
if ([[gamemodArray 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
|
162 |
result |= 0x00000020; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
163 |
if ([[gamemodArray 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
|
164 |
result |= 0x00000040; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
165 |
if ([[gamemodArray 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
|
166 |
result |= 0x00000080; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
167 |
if ([[gamemodArray 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
|
168 |
result |= 0x00000100; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
169 |
if ([[gamemodArray 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
|
170 |
result |= 0x00000200; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
171 |
if ([[gamemodArray 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
|
172 |
result |= 0x00000400; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
173 |
if ([[gamemodArray 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
|
174 |
result |= 0x00000800; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
175 |
if ([[gamemodArray 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
|
176 |
result |= 0x00002000; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
177 |
if ([[gamemodArray 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
|
178 |
result |= 0x00004000; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
179 |
if ([[gamemodArray 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
|
180 |
result |= 0x00008000; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
181 |
if ([[gamemodArray 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
|
182 |
result |= 0x00010000; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
183 |
if ([[gamemodArray 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
|
184 |
result |= 0x00020000; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
185 |
if ([[gamemodArray 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
|
186 |
result |= 0x00080000; |
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
187 |
if ([[gamemodArray objectAtIndex:i++] boolValue]) |
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
188 |
result |= 0x00100000; |
3547 | 189 |
|
190 |
NSString *flags = [[NSString alloc] initWithFormat:@"e$gmflags %d",result]; |
|
191 |
[self sendToEngine:flags]; |
|
192 |
[flags release]; |
|
3697 | 193 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
194 |
i = 0; |
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
195 |
NSString *dmgMod = [[NSString alloc] initWithFormat:@"e$damagepct %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 196 |
[self sendToEngine:dmgMod]; |
197 |
[dmgMod release]; |
|
3697 | 198 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
199 |
NSString *turnTime = [[NSString alloc] initWithFormat:@"e$turntime %d",[[basicArray objectAtIndex:i++] intValue] * 1000]; |
3547 | 200 |
[self sendToEngine:turnTime]; |
201 |
[turnTime release]; |
|
3697 | 202 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
203 |
result = [[basicArray objectAtIndex:i++] intValue]; // initial health |
3697 | 204 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
205 |
NSString *sdTime = [[NSString alloc] initWithFormat:@"e$sd_turns %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 206 |
[self sendToEngine:sdTime]; |
207 |
[sdTime release]; |
|
3697 | 208 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
209 |
NSString *crateDrops = [[NSString alloc] initWithFormat:@"e$casefreq %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 210 |
[self sendToEngine:crateDrops]; |
211 |
[crateDrops release]; |
|
3697 | 212 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
213 |
NSString *minesTime = [[NSString alloc] initWithFormat:@"e$minestime %d",[[basicArray objectAtIndex:i++] intValue] * 1000]; |
3547 | 214 |
[self sendToEngine:minesTime]; |
215 |
[minesTime release]; |
|
3697 | 216 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
217 |
NSString *minesNumber = [[NSString alloc] initWithFormat:@"e$landadds %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 218 |
[self sendToEngine:minesNumber]; |
219 |
[minesNumber release]; |
|
3697 | 220 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
221 |
NSString *dudMines = [[NSString alloc] initWithFormat:@"e$minedudpct %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 222 |
[self sendToEngine:dudMines]; |
223 |
[dudMines release]; |
|
3697 | 224 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
225 |
NSString *explosives = [[NSString alloc] initWithFormat:@"e$explosives %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 226 |
[self sendToEngine:explosives]; |
227 |
[explosives release]; |
|
3697 | 228 |
|
3781
2bfda544ae48
modified file format for schemes files (which is going to introduce a lot of pre-release bugs, i'm sure)
koda
parents:
3779
diff
changeset
|
229 |
[schemeDictionary release]; |
3547 | 230 |
return result; |
231 |
} |
|
232 |
||
233 |
#pragma mark - |
|
234 |
#pragma mark Thread/Network relevant code |
|
235 |
// select one of GameSetup method and execute it in a seprate thread |
|
236 |
-(void) startThread: (NSString *) selector { |
|
237 |
SEL usage = NSSelectorFromString(selector); |
|
238 |
[NSThread detachNewThreadSelector:usage toTarget:self withObject:nil]; |
|
239 |
} |
|
240 |
||
241 |
// wrapper that computes the length of the message and then sends the command string |
|
242 |
-(int) sendToEngine: (NSString *)string { |
|
243 |
uint8_t length = [string length]; |
|
3697 | 244 |
|
3547 | 245 |
SDLNet_TCP_Send(csd, &length , 1); |
246 |
return SDLNet_TCP_Send(csd, [string UTF8String], length); |
|
247 |
} |
|
248 |
||
249 |
// method that handles net setup with engine and keeps connection alive |
|
250 |
-(void) engineProtocol { |
|
251 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
252 |
TCPsocket sd; |
3547 | 253 |
IPaddress ip; |
254 |
int eProto; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
255 |
BOOL clientQuit; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
256 |
char buffer[BUFFER_SIZE]; |
3547 | 257 |
uint8_t msgSize; |
258 |
uint16_t gameTicks; |
|
3697 | 259 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
260 |
clientQuit = NO; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
261 |
csd = NULL; |
3547 | 262 |
|
263 |
if (SDLNet_Init() < 0) { |
|
264 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
265 |
clientQuit = YES; |
3547 | 266 |
} |
3697 | 267 |
|
3547 | 268 |
// Resolving the host using NULL make network interface to listen |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
269 |
if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0 && !clientQuit) { |
3547 | 270 |
DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
271 |
clientQuit = YES; |
3547 | 272 |
} |
3697 | 273 |
|
274 |
// 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
|
275 |
if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) { |
3547 | 276 |
DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
277 |
clientQuit = YES; |
3547 | 278 |
} |
3697 | 279 |
|
3547 | 280 |
DLog(@"Waiting for a client on port %d", ipcPort); |
3697 | 281 |
while (csd == NULL) |
3547 | 282 |
csd = SDLNet_TCP_Accept(sd); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
283 |
SDLNet_TCP_Close(sd); |
3697 | 284 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
285 |
while (!clientQuit) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
286 |
msgSize = 0; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
287 |
memset(buffer, 0, BUFFER_SIZE); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
288 |
if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0) |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
289 |
clientQuit = YES; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
290 |
if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0) |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
291 |
clientQuit = YES; |
3697 | 292 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
293 |
switch (buffer[0]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
294 |
case 'C': |
3626 | 295 |
DLog(@"sending game config...\n%@",self.gameConfig); |
3697 | 296 |
|
3547 | 297 |
// local game |
298 |
[self sendToEngine:@"TL"]; |
|
3697 | 299 |
|
3547 | 300 |
// seed info |
301 |
[self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]]; |
|
3697 | 302 |
|
3547 | 303 |
// dimension of the map |
304 |
[self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]]; |
|
305 |
[self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]]; |
|
306 |
[self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]]; |
|
3697 | 307 |
|
3642 | 308 |
// static land (if set) |
309 |
NSString *staticMap = [self.gameConfig objectForKey:@"staticmap_command"]; |
|
310 |
if ([staticMap length] != 0) |
|
311 |
[self sendToEngine:staticMap]; |
|
3697 | 312 |
|
3547 | 313 |
// theme info |
314 |
[self sendToEngine:[self.gameConfig objectForKey:@"theme_command"]]; |
|
3697 | 315 |
|
3547 | 316 |
// scheme (returns initial health) |
317 |
NSInteger health = [self provideScheme:[self.gameConfig objectForKey:@"scheme"]]; |
|
3697 | 318 |
|
3547 | 319 |
NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"]; |
320 |
for (NSDictionary *teamData in teamsConfig) { |
|
3697 | 321 |
[self provideTeamData:[teamData objectForKey:@"team"] |
3547 | 322 |
forHogs:[[teamData objectForKey:@"number"] intValue] |
323 |
withHealth:health |
|
324 |
ofColor:[teamData objectForKey:@"color"]]; |
|
325 |
} |
|
3697 | 326 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
327 |
[self provideAmmoData:[self.gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
328 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
329 |
case '?': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
330 |
DLog(@"Ping? Pong!"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
331 |
[self sendToEngine:@"!"]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
332 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
333 |
case 'E': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
334 |
DLog(@"ERROR - last console line: [%s]", &buffer[1]); |
3547 | 335 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
336 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
337 |
case 'e': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
338 |
sscanf(buffer, "%*s %d", &eProto); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
339 |
short int netProto = 0; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
340 |
char *versionStr; |
3697 | 341 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
342 |
HW_versionInfo(&netProto, &versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
343 |
if (netProto == eProto) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
344 |
DLog(@"Setting protocol version %d (%s)", eProto, versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
345 |
} else { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
346 |
DLog(@"ERROR - wrong protocol number: [%s] - expecting %d", &buffer[1], eProto); |
3547 | 347 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
348 |
} |
3697 | 349 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
350 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
351 |
case 'i': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
352 |
switch (buffer[1]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
353 |
case 'r': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
354 |
NSLog(@"Winning team: %s", &buffer[2]); |
3547 | 355 |
break; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
356 |
case 'k': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
357 |
NSLog(@"Best Hedgehog: %s", &buffer[2]); |
3547 | 358 |
break; |
359 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
360 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
361 |
default: |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
362 |
// 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
|
363 |
gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
364 |
//DLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
365 |
break; |
3547 | 366 |
} |
367 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
368 |
DLog(@"Engine exited, closing server"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
369 |
// wait a little to let the client close cleanly |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
370 |
[NSThread sleepForTimeInterval:2]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
371 |
// Close the client socket |
3697 | 372 |
SDLNet_TCP_Close(csd); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
373 |
SDLNet_Quit(); |
3697 | 374 |
|
3547 | 375 |
[pool release]; |
376 |
//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. |
|
377 |
//[NSThread exit]; |
|
378 |
} |
|
379 |
||
380 |
#pragma mark - |
|
381 |
#pragma mark Setting methods |
|
382 |
// returns an array of c-strings that are read by engine at startup |
|
383 |
-(const char **)getSettings { |
|
384 |
NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", ipcPort]; |
|
385 |
NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]; |
|
386 |
CGRect screenBounds = [[UIScreen mainScreen] bounds]; |
|
387 |
NSString *wSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.width]; |
|
388 |
NSString *hSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.height]; |
|
3613 | 389 |
const char **gameArgs = (const char**) malloc(sizeof(char *) * 10); |
3634 | 390 |
NSInteger tmpQuality; |
3697 | 391 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3660
diff
changeset
|
392 |
NSString *modelId = modelType(); |
3613 | 393 |
if ([modelId hasPrefix:@"iPhone1"] || // = iPhone or iPhone 3G |
394 |
[modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"]) // = iPod Touch or iPod Touch 2G |
|
3634 | 395 |
tmpQuality = 0x00000001 | 0x00000002 | 0x00000040; // rqLowRes | rqBlurryLand | rqKillFlakes |
3613 | 396 |
else if ([modelId hasPrefix:@"iPhone2"] || // = iPhone 3GS |
397 |
[modelId hasPrefix:@"iPod3"]) // = iPod Touch 3G |
|
3697 | 398 |
tmpQuality = 0x00000002 | 0x00000040; // rqBlurryLand | rqKillFlakes |
3624 | 399 |
else if ([modelId hasPrefix:@"iPad1"]) // = iPad |
3634 | 400 |
tmpQuality = 0x00000002; // rqBlurryLand |
3624 | 401 |
else // = everything else |
3634 | 402 |
tmpQuality = 0; // full quality |
403 |
if (![modelId hasPrefix:@"iPad"]) // = disable tooltips unless iPad |
|
404 |
tmpQuality = tmpQuality | 0x00000400; |
|
3697 | 405 |
|
3547 | 406 |
// prevents using an empty nickname |
407 |
NSString *username; |
|
408 |
NSString *originalUsername = [self.systemSettings objectForKey:@"username"]; |
|
409 |
if ([originalUsername length] == 0) |
|
410 |
username = [[NSString alloc] initWithFormat:@"MobileUser-%@",ipcString]; |
|
411 |
else |
|
412 |
username = [[NSString alloc] initWithString:originalUsername]; |
|
3697 | 413 |
|
3779
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
414 |
gameArgs[ 0] = [ipcString UTF8String]; //ipcPort |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
415 |
gameArgs[ 1] = [wSize UTF8String]; //cScreenHeight |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
416 |
gameArgs[ 2] = [hSize UTF8String]; //cScreenWidth |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
417 |
gameArgs[ 3] = [[[NSNumber numberWithInteger:tmpQuality] stringValue] UTF8String]; //quality |
3825 | 418 |
gameArgs[ 4] = "en.txt";//[localeString UTF8String]; //cLocaleFName |
3779
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
419 |
gameArgs[ 5] = [username UTF8String]; //UserNick |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
420 |
gameArgs[ 6] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String]; //isSoundEnabled |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
421 |
gameArgs[ 7] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String]; //isMusicEnabled |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
422 |
gameArgs[ 8] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String]; //cAltDamage |
3803 | 423 |
gameArgs[ 9] = NULL; //unused |
3779
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
424 |
gameArgs[10] = NULL; //recordFileName |
3697 | 425 |
|
3547 | 426 |
[wSize release]; |
427 |
[hSize release]; |
|
428 |
[localeString release]; |
|
429 |
[ipcString release]; |
|
430 |
[username release]; |
|
431 |
return gameArgs; |
|
432 |
} |
|
433 |
||
434 |
||
435 |
@end |