author | koda |
Wed, 22 Sep 2010 02:22:15 +0200 | |
changeset 3893 | 568bfd083465 |
parent 3891 | f8f0d0ceb19c |
child 3895 | e7c202c08ac1 |
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" |
|
3891 | 27 |
#import "NSStringExtra.h" |
3547 | 28 |
|
3891 | 29 |
#define BUFFER_SIZE 128 |
3547 | 30 |
|
31 |
@implementation GameSetup |
|
3891 | 32 |
@synthesize systemSettings, gameConfig, savePath; |
3547 | 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 |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
43 |
self.gameConfig = [gameDictionary objectForKey:@"game_dictionary"]; |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
44 |
isNetGame = [[gameDictionary objectForKey:@"netgame"] boolValue]; |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
45 |
NSString *path = [gameDictionary objectForKey:@"savefile"]; |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
46 |
// if path is empty it means i have to create a new file, otherwise i read from that file |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
47 |
if ([path isEqualToString:@""] == YES) |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
48 |
self.savePath = [SAVES_DIRECTORY() stringByAppendingFormat:@"%@.hws", [[NSDate date] description]]; |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
49 |
else |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
50 |
self.savePath = [SAVES_DIRECTORY() stringByAppendingString:path]; |
3697 | 51 |
} |
3547 | 52 |
return self; |
53 |
} |
|
54 |
||
55 |
-(void) dealloc { |
|
56 |
[gameConfig release]; |
|
57 |
[systemSettings release]; |
|
3891 | 58 |
[savePath release]; |
3547 | 59 |
[super dealloc]; |
60 |
} |
|
61 |
||
62 |
#pragma mark - |
|
63 |
#pragma mark Provider functions |
|
64 |
// unpacks team data from the selected team.plist to a sequence of engine commands |
|
65 |
-(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor { |
|
66 |
/* |
|
67 |
addteam <32charsMD5hash> <color> <team name> |
|
68 |
addhh <level> <health> <hedgehog name> |
|
69 |
<level> is 0 for human, 1-5 for bots (5 is the most stupid) |
|
70 |
*/ |
|
3697 | 71 |
|
3547 | 72 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), teamName]; |
73 |
NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile]; |
|
74 |
[teamFile release]; |
|
3697 | 75 |
|
76 |
NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@", |
|
3660 | 77 |
[teamData objectForKey:@"hash"], [teamColor stringValue], [teamName stringByDeletingPathExtension]]; |
3547 | 78 |
[self sendToEngine: teamHashColorAndName]; |
79 |
[teamHashColorAndName release]; |
|
3697 | 80 |
|
3547 | 81 |
NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]]; |
82 |
[self sendToEngine: grave]; |
|
83 |
[grave release]; |
|
3697 | 84 |
|
3547 | 85 |
NSString *fort = [[NSString alloc] initWithFormat:@"efort %@", [teamData objectForKey:@"fort"]]; |
86 |
[self sendToEngine: fort]; |
|
87 |
[fort release]; |
|
3697 | 88 |
|
3547 | 89 |
NSString *voicepack = [[NSString alloc] initWithFormat:@"evoicepack %@", [teamData objectForKey:@"voicepack"]]; |
90 |
[self sendToEngine: voicepack]; |
|
91 |
[voicepack release]; |
|
3697 | 92 |
|
3547 | 93 |
NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]]; |
94 |
[self sendToEngine: flag]; |
|
95 |
[flag release]; |
|
3697 | 96 |
|
3547 | 97 |
NSArray *hogs = [teamData objectForKey:@"hedgehogs"]; |
98 |
for (int i = 0; i < numberOfPlayingHogs; i++) { |
|
99 |
NSDictionary *hog = [hogs objectAtIndex:i]; |
|
3697 | 100 |
|
101 |
NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %d %@", |
|
3547 | 102 |
[hog objectForKey:@"level"], initialHealth, [hog objectForKey:@"hogname"]]; |
103 |
[self sendToEngine: hogLevelHealthAndName]; |
|
104 |
[hogLevelHealthAndName release]; |
|
3697 | 105 |
|
3547 | 106 |
NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]]; |
107 |
[self sendToEngine: hogHat]; |
|
108 |
[hogHat release]; |
|
109 |
} |
|
3697 | 110 |
|
3547 | 111 |
[teamData release]; |
112 |
} |
|
113 |
||
114 |
// unpacks ammostore data from the selected ammo.plist to a sequence of engine commands |
|
115 |
-(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams { |
|
116 |
NSString *weaponPath = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),ammostoreName]; |
|
117 |
NSDictionary *ammoData = [[NSDictionary alloc] initWithContentsOfFile:weaponPath]; |
|
118 |
[weaponPath release]; |
|
3621 | 119 |
NSString *update = @""; |
3697 | 120 |
|
3621 | 121 |
// if we're loading an older version of ammos fill the engine message with 0s |
122 |
int diff = CURRENT_AMMOSIZE - [[ammoData objectForKey:@"version"] intValue]; |
|
123 |
if (diff != 0) |
|
124 |
update = [NSString stringWithCharacters:(const unichar*)"0000000000000000000000000000000000" length:diff]; |
|
3697 | 125 |
|
3621 | 126 |
NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@%@", [ammoData objectForKey:@"ammostore_initialqt"], update]; |
3547 | 127 |
[self sendToEngine: ammloadt]; |
128 |
[ammloadt release]; |
|
3697 | 129 |
|
3621 | 130 |
NSString *ammprob = [[NSString alloc] initWithFormat:@"eammprob %@%@", [ammoData objectForKey:@"ammostore_probability"], update]; |
3547 | 131 |
[self sendToEngine: ammprob]; |
132 |
[ammprob release]; |
|
3697 | 133 |
|
3621 | 134 |
NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammdelay %@%@", [ammoData objectForKey:@"ammostore_delay"], update]; |
3547 | 135 |
[self sendToEngine: ammdelay]; |
136 |
[ammdelay release]; |
|
3697 | 137 |
|
3621 | 138 |
NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@%@", [ammoData objectForKey:@"ammostore_crate"], update]; |
3547 | 139 |
[self sendToEngine: ammreinf]; |
140 |
[ammreinf release]; |
|
3697 | 141 |
|
3547 | 142 |
// sent twice so it applies to both teams |
143 |
NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"]; |
|
144 |
for (int i = 0; i < numberOfTeams; i++) |
|
145 |
[self sendToEngine: ammstore]; |
|
146 |
[ammstore release]; |
|
3697 | 147 |
|
3547 | 148 |
[ammoData release]; |
149 |
} |
|
150 |
||
151 |
// unpacks scheme data from the selected scheme.plist to a sequence of engine commands |
|
152 |
-(NSInteger) provideScheme:(NSString *)schemeName { |
|
153 |
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
|
154 |
NSDictionary *schemeDictionary = [[NSDictionary alloc] initWithContentsOfFile:schemePath]; |
3547 | 155 |
[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
|
156 |
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
|
157 |
NSArray *gamemodArray = [schemeDictionary objectForKey:@"gamemod"]; |
3547 | 158 |
int result = 0; |
159 |
int i = 0; |
|
160 |
||
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 |= 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
|
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 |= 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
|
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 |= 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
|
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 |= 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
|
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 |= 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
|
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 |= 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
|
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 |= 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
|
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 |= 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
|
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 |= 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
|
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 |= 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
|
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 |= 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
|
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 |= 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
|
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 |= 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
|
187 |
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
|
188 |
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
|
189 |
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
|
190 |
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
|
191 |
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
|
192 |
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
|
193 |
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
|
194 |
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
|
195 |
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
|
196 |
result |= 0x00100000; |
3547 | 197 |
|
198 |
NSString *flags = [[NSString alloc] initWithFormat:@"e$gmflags %d",result]; |
|
199 |
[self sendToEngine:flags]; |
|
200 |
[flags release]; |
|
3697 | 201 |
|
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
|
202 |
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
|
203 |
NSString *dmgMod = [[NSString alloc] initWithFormat:@"e$damagepct %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 204 |
[self sendToEngine:dmgMod]; |
205 |
[dmgMod release]; |
|
3697 | 206 |
|
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
|
207 |
NSString *turnTime = [[NSString alloc] initWithFormat:@"e$turntime %d",[[basicArray objectAtIndex:i++] intValue] * 1000]; |
3547 | 208 |
[self sendToEngine:turnTime]; |
209 |
[turnTime release]; |
|
3697 | 210 |
|
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
|
211 |
result = [[basicArray objectAtIndex:i++] intValue]; // initial health |
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 *sdTime = [[NSString alloc] initWithFormat:@"e$sd_turns %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 214 |
[self sendToEngine:sdTime]; |
215 |
[sdTime 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 *crateDrops = [[NSString alloc] initWithFormat:@"e$casefreq %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 218 |
[self sendToEngine:crateDrops]; |
219 |
[crateDrops 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 *minesTime = [[NSString alloc] initWithFormat:@"e$minestime %d",[[basicArray objectAtIndex:i++] intValue] * 1000]; |
3547 | 222 |
[self sendToEngine:minesTime]; |
223 |
[minesTime 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 *minesNumber = [[NSString alloc] initWithFormat:@"e$landadds %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 226 |
[self sendToEngine:minesNumber]; |
227 |
[minesNumber 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 |
NSString *dudMines = [[NSString alloc] initWithFormat:@"e$minedudpct %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 230 |
[self sendToEngine:dudMines]; |
231 |
[dudMines release]; |
|
3697 | 232 |
|
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
|
233 |
NSString *explosives = [[NSString alloc] initWithFormat:@"e$explosives %d",[[basicArray objectAtIndex:i++] intValue]]; |
3547 | 234 |
[self sendToEngine:explosives]; |
235 |
[explosives release]; |
|
3697 | 236 |
|
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
|
237 |
[schemeDictionary release]; |
3547 | 238 |
return result; |
239 |
} |
|
240 |
||
241 |
#pragma mark - |
|
242 |
#pragma mark Thread/Network relevant code |
|
243 |
// select one of GameSetup method and execute it in a seprate thread |
|
244 |
-(void) startThread: (NSString *) selector { |
|
245 |
SEL usage = NSSelectorFromString(selector); |
|
246 |
[NSThread detachNewThreadSelector:usage toTarget:self withObject:nil]; |
|
247 |
} |
|
248 |
||
3891 | 249 |
// wrapper that computes the length of the message and then sends the command string, saving the command on a file |
3547 | 250 |
-(int) sendToEngine: (NSString *)string { |
251 |
uint8_t length = [string length]; |
|
3697 | 252 |
|
3891 | 253 |
[[NSString stringWithFormat:@"%c%@",length,string] appendToFile:savePath]; |
254 |
SDLNet_TCP_Send(csd, &length , 1); |
|
255 |
return SDLNet_TCP_Send(csd, [string UTF8String], length); |
|
256 |
} |
|
257 |
||
258 |
// wrapper that computes the length of the message and then sends the command string, skipping file writing |
|
259 |
-(int) sendToEngineNoSave: (NSString *)string { |
|
260 |
uint8_t length = [string length]; |
|
261 |
||
3547 | 262 |
SDLNet_TCP_Send(csd, &length , 1); |
263 |
return SDLNet_TCP_Send(csd, [string UTF8String], length); |
|
264 |
} |
|
265 |
||
266 |
// method that handles net setup with engine and keeps connection alive |
|
267 |
-(void) engineProtocol { |
|
268 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
269 |
TCPsocket sd; |
3547 | 270 |
IPaddress ip; |
271 |
int eProto; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
272 |
BOOL clientQuit; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
273 |
char buffer[BUFFER_SIZE]; |
3547 | 274 |
uint8_t msgSize; |
275 |
uint16_t gameTicks; |
|
3697 | 276 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
277 |
clientQuit = NO; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
278 |
csd = NULL; |
3547 | 279 |
|
280 |
if (SDLNet_Init() < 0) { |
|
281 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
282 |
clientQuit = YES; |
3547 | 283 |
} |
3697 | 284 |
|
3547 | 285 |
// Resolving the host using NULL make network interface to listen |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
286 |
if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0 && !clientQuit) { |
3547 | 287 |
DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
288 |
clientQuit = YES; |
3547 | 289 |
} |
3697 | 290 |
|
291 |
// 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
|
292 |
if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) { |
3547 | 293 |
DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
294 |
clientQuit = YES; |
3547 | 295 |
} |
3697 | 296 |
|
3547 | 297 |
DLog(@"Waiting for a client on port %d", ipcPort); |
3697 | 298 |
while (csd == NULL) |
3547 | 299 |
csd = SDLNet_TCP_Accept(sd); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
300 |
SDLNet_TCP_Close(sd); |
3697 | 301 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
302 |
while (!clientQuit) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
303 |
msgSize = 0; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
304 |
memset(buffer, 0, BUFFER_SIZE); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
305 |
if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0) |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
306 |
clientQuit = YES; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
307 |
if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0) |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
308 |
clientQuit = YES; |
3697 | 309 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
310 |
switch (buffer[0]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
311 |
case 'C': |
3626 | 312 |
DLog(@"sending game config...\n%@",self.gameConfig); |
3697 | 313 |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
314 |
if (isNetGame == YES) |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
315 |
[self sendToEngineNoSave:@"TN"]; |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
316 |
else |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
317 |
[self sendToEngineNoSave:@"TL"]; |
3891 | 318 |
NSString *saveHeader = @"TS"; |
319 |
[[NSString stringWithFormat:@"%c%@",[saveHeader length], saveHeader] appendToFile:savePath]; |
|
3697 | 320 |
|
3547 | 321 |
// seed info |
322 |
[self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]]; |
|
3697 | 323 |
|
3547 | 324 |
// dimension of the map |
325 |
[self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]]; |
|
326 |
[self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]]; |
|
327 |
[self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]]; |
|
3697 | 328 |
|
3642 | 329 |
// static land (if set) |
330 |
NSString *staticMap = [self.gameConfig objectForKey:@"staticmap_command"]; |
|
331 |
if ([staticMap length] != 0) |
|
332 |
[self sendToEngine:staticMap]; |
|
3697 | 333 |
|
3547 | 334 |
// theme info |
335 |
[self sendToEngine:[self.gameConfig objectForKey:@"theme_command"]]; |
|
3697 | 336 |
|
3547 | 337 |
// scheme (returns initial health) |
338 |
NSInteger health = [self provideScheme:[self.gameConfig objectForKey:@"scheme"]]; |
|
3697 | 339 |
|
3547 | 340 |
NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"]; |
341 |
for (NSDictionary *teamData in teamsConfig) { |
|
3697 | 342 |
[self provideTeamData:[teamData objectForKey:@"team"] |
3547 | 343 |
forHogs:[[teamData objectForKey:@"number"] intValue] |
344 |
withHealth:health |
|
345 |
ofColor:[teamData objectForKey:@"color"]]; |
|
346 |
} |
|
3697 | 347 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
348 |
[self provideAmmoData:[self.gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
349 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
350 |
case '?': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
351 |
DLog(@"Ping? Pong!"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
352 |
[self sendToEngine:@"!"]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
353 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
354 |
case 'E': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
355 |
DLog(@"ERROR - last console line: [%s]", &buffer[1]); |
3547 | 356 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
357 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
358 |
case 'e': |
3891 | 359 |
buffer[msgSize] = '\0'; |
360 |
[[NSString stringWithUTF8String:buffer] appendToFile:savePath]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
361 |
sscanf(buffer, "%*s %d", &eProto); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
362 |
short int netProto = 0; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
363 |
char *versionStr; |
3697 | 364 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
365 |
HW_versionInfo(&netProto, &versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
366 |
if (netProto == eProto) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
367 |
DLog(@"Setting protocol version %d (%s)", eProto, versionStr); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
368 |
} else { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
369 |
DLog(@"ERROR - wrong protocol number: [%s] - expecting %d", &buffer[1], eProto); |
3547 | 370 |
clientQuit = YES; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
371 |
} |
3697 | 372 |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
373 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
374 |
case 'i': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
375 |
switch (buffer[1]) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
376 |
case 'r': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
377 |
NSLog(@"Winning team: %s", &buffer[2]); |
3547 | 378 |
break; |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
379 |
case 'k': |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
380 |
NSLog(@"Best Hedgehog: %s", &buffer[2]); |
3547 | 381 |
break; |
382 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
383 |
break; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
384 |
default: |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
385 |
// 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
|
386 |
gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]); |
3891 | 387 |
DLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer); |
388 |
buffer[msgSize] = '\0'; |
|
389 |
[[NSString stringWithUTF8String:buffer] appendToFile:savePath]; |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
390 |
break; |
3547 | 391 |
} |
392 |
} |
|
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
393 |
DLog(@"Engine exited, closing server"); |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
394 |
// wait a little to let the client close cleanly |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
395 |
[NSThread sleepForTimeInterval:2]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
396 |
// Close the client socket |
3697 | 397 |
SDLNet_TCP_Close(csd); |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
398 |
SDLNet_Quit(); |
3697 | 399 |
|
3547 | 400 |
[pool release]; |
401 |
//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. |
|
402 |
//[NSThread exit]; |
|
403 |
} |
|
404 |
||
405 |
#pragma mark - |
|
406 |
#pragma mark Setting methods |
|
407 |
// returns an array of c-strings that are read by engine at startup |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
408 |
-(const char **)getSettings: (NSString *)recordFile { |
3547 | 409 |
NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", ipcPort]; |
410 |
NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]; |
|
411 |
CGRect screenBounds = [[UIScreen mainScreen] bounds]; |
|
412 |
NSString *wSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.width]; |
|
413 |
NSString *hSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.height]; |
|
3613 | 414 |
const char **gameArgs = (const char**) malloc(sizeof(char *) * 10); |
3634 | 415 |
NSInteger tmpQuality; |
3697 | 416 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3660
diff
changeset
|
417 |
NSString *modelId = modelType(); |
3613 | 418 |
if ([modelId hasPrefix:@"iPhone1"] || // = iPhone or iPhone 3G |
419 |
[modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"]) // = iPod Touch or iPod Touch 2G |
|
3634 | 420 |
tmpQuality = 0x00000001 | 0x00000002 | 0x00000040; // rqLowRes | rqBlurryLand | rqKillFlakes |
3613 | 421 |
else if ([modelId hasPrefix:@"iPhone2"] || // = iPhone 3GS |
422 |
[modelId hasPrefix:@"iPod3"]) // = iPod Touch 3G |
|
3697 | 423 |
tmpQuality = 0x00000002 | 0x00000040; // rqBlurryLand | rqKillFlakes |
3624 | 424 |
else if ([modelId hasPrefix:@"iPad1"]) // = iPad |
3634 | 425 |
tmpQuality = 0x00000002; // rqBlurryLand |
3624 | 426 |
else // = everything else |
3634 | 427 |
tmpQuality = 0; // full quality |
428 |
if (![modelId hasPrefix:@"iPad"]) // = disable tooltips unless iPad |
|
429 |
tmpQuality = tmpQuality | 0x00000400; |
|
3697 | 430 |
|
3547 | 431 |
// prevents using an empty nickname |
432 |
NSString *username; |
|
433 |
NSString *originalUsername = [self.systemSettings objectForKey:@"username"]; |
|
434 |
if ([originalUsername length] == 0) |
|
435 |
username = [[NSString alloc] initWithFormat:@"MobileUser-%@",ipcString]; |
|
436 |
else |
|
437 |
username = [[NSString alloc] initWithString:originalUsername]; |
|
3697 | 438 |
|
3779
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
439 |
gameArgs[ 0] = [ipcString UTF8String]; //ipcPort |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
440 |
gameArgs[ 1] = [wSize UTF8String]; //cScreenHeight |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
441 |
gameArgs[ 2] = [hSize UTF8String]; //cScreenWidth |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
442 |
gameArgs[ 3] = [[[NSNumber numberWithInteger:tmpQuality] stringValue] UTF8String]; //quality |
3825 | 443 |
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
|
444 |
gameArgs[ 5] = [username UTF8String]; //UserNick |
3351a017d4ad
tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents:
3752
diff
changeset
|
445 |
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
|
446 |
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
|
447 |
gameArgs[ 8] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String]; //cAltDamage |
3803 | 448 |
gameArgs[ 9] = NULL; //unused |
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
3891
diff
changeset
|
449 |
gameArgs[10] = [recordFile UTF8String]; //recordFileName |
3697 | 450 |
|
3547 | 451 |
[wSize release]; |
452 |
[hSize release]; |
|
453 |
[localeString release]; |
|
454 |
[ipcString release]; |
|
455 |
[username release]; |
|
456 |
return gameArgs; |
|
457 |
} |
|
458 |
||
459 |
||
460 |
@end |