28 @synthesize blackView, savePath, port; |
28 @synthesize blackView, savePath, port; |
29 |
29 |
30 #pragma mark - |
30 #pragma mark - |
31 #pragma mark Instance methods for engine interaction |
31 #pragma mark Instance methods for engine interaction |
32 // prepares the controllers for hosting a game |
32 // prepares the controllers for hosting a game |
33 -(void) earlyEngineLaunch:(NSDictionary *)optionsOrNil { |
33 - (void)earlyEngineLaunch:(NSDictionary *)optionsOrNil { |
34 [self retain]; |
|
35 [[AudioManagerController mainManager] fadeOutBackgroundMusic]; |
34 [[AudioManagerController mainManager] fadeOutBackgroundMusic]; |
36 |
35 |
37 EngineProtocolNetwork *engineProtocol = [[EngineProtocolNetwork alloc] init]; |
36 EngineProtocolNetwork *engineProtocol = [[EngineProtocolNetwork alloc] init]; |
38 self.port = engineProtocol.enginePort; |
37 self.port = engineProtocol.enginePort; |
39 engineProtocol.delegate = self; |
38 engineProtocol.delegate = self; |
40 [engineProtocol spawnThread:self.savePath withOptions:optionsOrNil]; |
39 [engineProtocol spawnThread:self.savePath withOptions:optionsOrNil]; |
41 [engineProtocol release]; |
|
42 |
40 |
43 // add a black view hiding the background |
41 // add a black view hiding the background |
44 UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow]; |
42 UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow]; |
45 self.blackView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
43 self.blackView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
46 self.blackView.opaque = YES; |
44 self.blackView.opaque = YES; |
187 |
177 |
188 // moar cleanup |
178 // moar cleanup |
189 [self lateEngineLaunch]; |
179 [self lateEngineLaunch]; |
190 } |
180 } |
191 |
181 |
192 -(void) dealloc { |
|
193 releaseAndNil(blackView); |
|
194 releaseAndNil(savePath); |
|
195 [super dealloc]; |
|
196 } |
|
197 |
182 |
198 #pragma mark - |
183 #pragma mark - |
199 #pragma mark EngineProtocolDelegate methods |
184 #pragma mark EngineProtocolDelegate methods |
200 -(void) gameEndedWithStatistics:(NSArray *)stats { |
185 - (void)gameEndedWithStatistics:(NSArray *)stats { |
|
186 [self performSelectorOnMainThread:@selector(presentStats:) withObject:stats waitUntilDone:NO]; |
|
187 } |
|
188 |
|
189 - (void)presentStats:(NSArray *)stats { |
201 if (stats != nil) { |
190 if (stats != nil) { |
202 StatsPageViewController *statsPage = [[StatsPageViewController alloc] init]; |
191 StatsPageViewController *statsPage = [[StatsPageViewController alloc] init]; |
203 statsPage.statsArray = stats; |
192 statsPage.statsArray = stats; |
204 statsPage.modalTransitionStyle = UIModalTransitionStyleCoverVertical; |
193 statsPage.modalTransitionStyle = UIModalTransitionStyleCoverVertical; |
205 |
194 |
206 [callingController presentViewController:statsPage animated:YES completion:nil]; |
195 [callingController presentViewController:statsPage animated:YES completion:nil]; |
207 [statsPage release]; |
|
208 } |
196 } |
209 } |
197 } |
210 |
198 |
211 #pragma mark - |
199 #pragma mark - |
212 #pragma mark Class methods for setting up the engine from outsite |
200 #pragma mark Class methods for setting up the engine from outsite |
213 +(void) registerCallingController:(UIViewController *)controller { |
201 + (void)registerCallingController:(UIViewController *)controller { |
214 callingController = controller; |
202 callingController = controller; |
215 } |
203 } |
216 |
204 |
217 +(void) startGame:(TGameType) type atPath:(NSString *)path withOptions:(NSDictionary *)config { |
205 + (void)startGame:(TGameType)type atPath:(NSString *)path withOptions:(NSDictionary *)config { |
218 [HWUtils setGameType:type]; |
206 [HWUtils setGameType:type]; |
219 id bridge = [[self alloc] init]; |
207 id bridge = [[self alloc] init]; |
220 [bridge setSavePath:path]; |
208 [bridge setSavePath:path]; |
221 [bridge earlyEngineLaunch:config]; |
209 [bridge earlyEngineLaunch:config]; |
222 [bridge release]; |
210 } |
223 } |
211 |
224 |
212 + (void)startLocalGame:(NSDictionary *)withOptions { |
225 +(void) startLocalGame:(NSDictionary *)withOptions { |
|
226 NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; |
213 NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; |
227 [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"]; |
214 [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"]; |
228 NSString *savePath = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]]; |
215 NSString *savePath = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]]; |
229 [outputFormatter release]; |
|
230 |
216 |
231 // in the rare case in which a savefile with the same name exists the older one must be removed (otherwise it gets corrupted) |
217 // in the rare case in which a savefile with the same name exists the older one must be removed (otherwise it gets corrupted) |
232 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]) |
218 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]) |
233 [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil]; |
219 [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil]; |
234 |
220 |
235 [self startGame:gtLocal atPath:savePath withOptions:withOptions]; |
221 [self startGame:gtLocal atPath:savePath withOptions:withOptions]; |
236 [savePath release]; |
222 } |
237 } |
223 |
238 |
224 + (void)startSaveGame:(NSString *)atPath { |
239 +(void) startSaveGame:(NSString *)atPath { |
|
240 [self startGame:gtSave atPath:atPath withOptions:nil]; |
225 [self startGame:gtSave atPath:atPath withOptions:nil]; |
241 } |
226 } |
242 |
227 |
243 +(void) startMissionGame:(NSString *)withScript { |
228 + (void)startMissionGame:(NSString *)withSubPath { |
244 NSString *seedCmd = [self seedCommand]; |
229 NSString *seedCmd = [self seedCommand]; |
245 NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/Training/%@.lua",withScript]; |
230 NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/%@.lua", withSubPath]; |
246 NSDictionary *missionDict = [[NSDictionary alloc] initWithObjectsAndKeys:missionPath, @"mission_command", seedCmd, @"seed_command", nil]; |
231 NSDictionary *missionDict = [[NSDictionary alloc] initWithObjectsAndKeys:missionPath, @"mission_command", seedCmd, @"seed_command", nil]; |
247 [missionPath release]; |
|
248 [seedCmd release]; |
|
249 |
232 |
250 [self startGame:gtMission atPath:nil withOptions:missionDict]; |
233 [self startGame:gtMission atPath:nil withOptions:missionDict]; |
251 [missionDict release]; |
234 } |
252 } |
235 |
253 |
236 + (NSString *)seedCommand { |
254 +(NSString *) seedCommand { |
|
255 // generate a seed |
237 // generate a seed |
256 NSString *seed = [HWUtils seed]; |
238 NSString *seed = [HWUtils seed]; |
257 NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed]; |
239 NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed]; |
258 [seed release]; |
|
259 return seedCmd; |
240 return seedCmd; |
260 } |
241 } |
261 |
242 |
262 +(void) startCampaignMissionGameWithScript:(NSString *)missionScriptName forCampaign:(NSString *)campaignName { |
243 + (void)startCampaignMissionGameWithScript:(NSString *)missionScriptName forCampaign:(NSString *)campaignName { |
263 NSString *seedCmd = [self seedCommand]; |
244 NSString *seedCmd = [self seedCommand]; |
264 NSString *campaignMissionPath = [[NSString alloc] initWithFormat:@"escript Missions/Campaign/%@/%@", campaignName, missionScriptName]; |
245 NSString *campaignMissionPath = [[NSString alloc] initWithFormat:@"escript Missions/Campaign/%@/%@", campaignName, missionScriptName]; |
265 NSDictionary *campaignMissionDict = [[NSDictionary alloc] initWithObjectsAndKeys:campaignMissionPath, @"mission_command", seedCmd, @"seed_command", nil]; |
246 NSDictionary *campaignMissionDict = [[NSDictionary alloc] initWithObjectsAndKeys:campaignMissionPath, @"mission_command", seedCmd, @"seed_command", nil]; |
266 [campaignMissionPath release]; |
|
267 [seedCmd release]; |
|
268 |
247 |
269 [self startGame:gtCampaign atPath:nil withOptions:campaignMissionDict]; |
248 [self startGame:gtCampaign atPath:nil withOptions:campaignMissionDict]; |
270 [campaignMissionDict release]; |
249 } |
271 } |
250 |
272 |
251 + (void)startSimpleGame { |
273 +(void) startSimpleGame { |
|
274 NSString *seedCmd = [self seedCommand]; |
252 NSString *seedCmd = [self seedCommand]; |
275 |
253 |
276 // pick a random static map |
254 // pick a random static map |
277 NSArray *listOfMaps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL]; |
255 NSArray *listOfMaps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL]; |
278 NSString *mapName = [listOfMaps objectAtIndex:arc4random_uniform((int)[listOfMaps count])]; |
256 NSString *mapName = [listOfMaps objectAtIndex:arc4random_uniform((int)[listOfMaps count])]; |
279 NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg",MAPS_DIRECTORY(),mapName]; |
257 NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg",MAPS_DIRECTORY(),mapName]; |
280 NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL]; |
258 NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL]; |
281 [fileCfg release]; |
|
282 NSArray *split = [contents componentsSeparatedByString:@"\n"]; |
259 NSArray *split = [contents componentsSeparatedByString:@"\n"]; |
283 [contents release]; |
|
284 NSString *themeCommand = [[NSString alloc] initWithFormat:@"etheme %@", [split objectAtIndex:0]]; |
260 NSString *themeCommand = [[NSString alloc] initWithFormat:@"etheme %@", [split objectAtIndex:0]]; |
285 NSString *staticMapCommand = [[NSString alloc] initWithFormat:@"emap %@", mapName]; |
261 NSString *staticMapCommand = [[NSString alloc] initWithFormat:@"emap %@", mapName]; |
286 |
262 |
287 // select teams with two different colors |
263 // select teams with two different colors |
288 NSArray *colorArray = [HWUtils teamColors]; |
264 NSArray *colorArray = [HWUtils teamColors]; |
299 @"Ninjas.plist",@"team",nil]; |
275 @"Ninjas.plist",@"team",nil]; |
300 NSDictionary *secondTeam = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:4],@"number", |
276 NSDictionary *secondTeam = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:4],@"number", |
301 [NSNumber numberWithUnsignedInt:secondColor],@"color", |
277 [NSNumber numberWithUnsignedInt:secondColor],@"color", |
302 @"Robots.plist",@"team",nil]; |
278 @"Robots.plist",@"team",nil]; |
303 NSArray *listOfTeams = [[NSArray alloc] initWithObjects:firstTeam,secondTeam,nil]; |
279 NSArray *listOfTeams = [[NSArray alloc] initWithObjects:firstTeam,secondTeam,nil]; |
304 [firstTeam release]; |
|
305 [secondTeam release]; |
|
306 |
280 |
307 // create the configuration |
281 // create the configuration |
308 NSDictionary *gameDictionary = [[NSDictionary alloc] initWithObjectsAndKeys: |
282 NSDictionary *gameDictionary = [[NSDictionary alloc] initWithObjectsAndKeys: |
309 seedCmd,@"seed_command", |
283 seedCmd,@"seed_command", |
310 @"e$template_filter 0",@"templatefilter_command", |
284 @"e$template_filter 0",@"templatefilter_command", |