25 #import "StatsPageViewController.h" |
25 #import "StatsPageViewController.h" |
26 #import "AudioManagerController.h" |
26 #import "AudioManagerController.h" |
27 #import "ObjcExports.h" |
27 #import "ObjcExports.h" |
28 |
28 |
29 @implementation GameInterfaceBridge |
29 @implementation GameInterfaceBridge |
30 @synthesize savePath, engineProtocol, ipcPort; |
30 @synthesize ipcPort; |
31 |
31 |
32 -(id) initWithController:(id) viewController { |
32 -(id) initWithController:(id) viewController { |
33 if (self = [super init]) { |
33 if (self = [super init]) { |
34 self.ipcPort = [HWUtils randomPort]; |
34 self.ipcPort = [HWUtils randomPort]; |
35 self.savePath = nil; |
|
36 |
|
37 self.engineProtocol = [[EngineProtocolNetwork alloc] initOnPort:self.ipcPort]; |
|
38 } |
35 } |
39 return self; |
36 return self; |
40 } |
37 } |
41 |
38 |
42 -(void) dealloc { |
39 -(void) dealloc { |
43 releaseAndNil(engineProtocol); |
|
44 releaseAndNil(savePath); |
|
45 [super dealloc]; |
40 [super dealloc]; |
46 } |
41 } |
47 |
42 |
48 #pragma mark - |
43 #pragma mark - |
49 // main routine for calling the actual game engine |
44 // main routine for calling the actual game engine |
50 -(void) engineLaunch { |
45 -(void) engineLaunch:(NSString *)path { |
51 const char *gameArgs[11]; |
46 const char *gameArgs[11]; |
52 CGFloat width, height; |
47 CGFloat width, height; |
53 CGFloat screenScale = [[UIScreen mainScreen] safeScale]; |
48 CGFloat screenScale = [[UIScreen mainScreen] safeScale]; |
54 NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", self.ipcPort]; |
49 NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", self.ipcPort]; |
55 NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]; |
50 NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]; |
97 gameArgs[ 5] = [username UTF8String]; //UserNick |
92 gameArgs[ 5] = [username UTF8String]; //UserNick |
98 gameArgs[ 6] = [[[settings objectForKey:@"sound"] stringValue] UTF8String]; //isSoundEnabled |
93 gameArgs[ 6] = [[[settings objectForKey:@"sound"] stringValue] UTF8String]; //isSoundEnabled |
99 gameArgs[ 7] = [[[settings objectForKey:@"music"] stringValue] UTF8String]; //isMusicEnabled |
94 gameArgs[ 7] = [[[settings objectForKey:@"music"] stringValue] UTF8String]; //isMusicEnabled |
100 gameArgs[ 8] = [[[settings objectForKey:@"alternate"] stringValue] UTF8String]; //cAltDamage |
95 gameArgs[ 8] = [[[settings objectForKey:@"alternate"] stringValue] UTF8String]; //cAltDamage |
101 gameArgs[ 9] = [rotation UTF8String]; //rotateQt |
96 gameArgs[ 9] = [rotation UTF8String]; //rotateQt |
102 gameArgs[10] = ([HWUtils gameType] == gtSave) ? [self.savePath UTF8String] : NULL; //recordFileName |
97 gameArgs[10] = ([HWUtils gameType] == gtSave) ? [path UTF8String] : NULL; //recordFileName |
103 |
98 |
104 [verticalSize release]; |
99 [verticalSize release]; |
105 [horizontalSize release]; |
100 [horizontalSize release]; |
106 [rotation release]; |
101 [rotation release]; |
107 [localeString release]; |
102 [localeString release]; |
112 // this is the pascal fuction that starts the game |
107 // this is the pascal fuction that starts the game |
113 Game(gameArgs); |
108 Game(gameArgs); |
114 } |
109 } |
115 |
110 |
116 // prepares the controllers for hosting a game |
111 // prepares the controllers for hosting a game |
117 -(void) prepareEngineLaunch { |
112 -(void) prepareEngineOn:(NSString *)pathOrNil withOptions:(NSDictionary *)optionsOrNil { |
|
113 EngineProtocolNetwork *proto = [[EngineProtocolNetwork alloc] initOnPort:self.ipcPort]; |
|
114 [proto spawnThread:pathOrNil withOptions:optionsOrNil]; |
|
115 |
118 CGRect theFrame = [[UIScreen mainScreen] bounds]; |
116 CGRect theFrame = [[UIScreen mainScreen] bounds]; |
119 UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow]; |
117 UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow]; |
120 // we add a black view hiding the background |
118 // we add a black view hiding the background |
121 UIView *blackView = [[UIView alloc] initWithFrame:theFrame]; |
119 UIView *blackView = [[UIView alloc] initWithFrame:theFrame]; |
122 [thisWindow addSubview:blackView]; |
120 [thisWindow addSubview:blackView]; |
133 } else |
131 } else |
134 blackView.alpha = 1; |
132 blackView.alpha = 1; |
135 |
133 |
136 // keep track of uncompleted games |
134 // keep track of uncompleted games |
137 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; |
135 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; |
138 [userDefaults setObject:self.savePath forKey:@"savedGamePath"]; |
136 [userDefaults setObject:pathOrNil forKey:@"savedGamePath"]; |
139 [userDefaults synchronize]; |
137 [userDefaults synchronize]; |
140 |
138 |
141 [AudioManagerController pauseBackgroundMusic]; |
139 [AudioManagerController pauseBackgroundMusic]; |
142 |
140 |
143 // SYSTEMS ARE GO!! |
141 // SYSTEMS ARE GO!! |
144 [self engineLaunch]; |
142 [self engineLaunch:pathOrNil]; |
145 |
143 |
146 // remove completed games notification |
144 // remove completed games notification |
147 [userDefaults setObject:@"" forKey:@"savedGamePath"]; |
145 [userDefaults setObject:@"" forKey:@"savedGamePath"]; |
148 [userDefaults synchronize]; |
146 [userDefaults synchronize]; |
149 |
147 |
167 -(void) startLocalGame:(NSDictionary *)withOptions { |
165 -(void) startLocalGame:(NSDictionary *)withOptions { |
168 [HWUtils setGameType:gtLocal]; |
166 [HWUtils setGameType:gtLocal]; |
169 |
167 |
170 NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; |
168 NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; |
171 [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"]; |
169 [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"]; |
172 NSString *path = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]]; |
170 NSString *savePath = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]]; |
173 [outputFormatter release]; |
171 [outputFormatter release]; |
174 self.savePath = path; |
|
175 [path release]; |
|
176 |
172 |
177 // in the rare case in which a savefile with the same name exists the older one must be removed (or it gets corrupted) |
173 // in the rare case in which a savefile with the same name exists the older one must be removed (or it gets corrupted) |
178 if ([[NSFileManager defaultManager] fileExistsAtPath:self.savePath]) |
174 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]) |
179 [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil]; |
175 [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil]; |
180 |
176 |
181 [self.engineProtocol spawnThread:self.savePath withOptions:withOptions]; |
177 [self prepareEngineOn:savePath withOptions:withOptions]; |
182 [self prepareEngineLaunch]; |
178 [savePath release]; |
183 } |
179 } |
184 |
180 |
185 // set up variables for a save game |
181 // set up variables for a save game |
186 -(void) startSaveGame:(NSString *)atPath { |
182 -(void) startSaveGame:(NSString *)atPath { |
187 self.savePath = atPath; |
|
188 [HWUtils setGameType:gtSave]; |
183 [HWUtils setGameType:gtSave]; |
189 |
184 [self prepareEngineOn:atPath withOptions:nil]; |
190 [self.engineProtocol spawnThread:self.savePath]; |
|
191 [self prepareEngineLaunch]; |
|
192 } |
185 } |
193 |
186 |
194 -(void) startMissionGame:(NSString *)withScript { |
187 -(void) startMissionGame:(NSString *)withScript { |
195 self.savePath = nil; |
|
196 [HWUtils setGameType:gtMission]; |
188 [HWUtils setGameType:gtMission]; |
197 |
189 |
198 NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/Training/%@.lua",withScript]; |
190 NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/Training/%@.lua",withScript]; |
199 NSDictionary *config = [NSDictionary dictionaryWithObject:missionPath forKey:@"mission_command"]; |
191 NSDictionary *missionLine = [[NSDictionary alloc] initWithObjectsAndKeys:missionPath,@"mission_command",nil]; |
200 [missionPath release]; |
192 [missionPath release]; |
201 [self.engineProtocol spawnThread:nil withOptions:config]; |
193 |
202 [self prepareEngineLaunch]; |
194 [self prepareEngineOn:nil withOptions:missionLine]; |
|
195 [missionLine release]; |
203 } |
196 } |
204 |
197 |
205 /* |
198 /* |
206 -(void) gameHasEndedWithStats:(NSArray *)stats { |
199 -(void) gameHasEndedWithStats:(NSArray *)stats { |
207 // wrap this around a retain/realse to prevent being deallocated too soon |
200 // wrap this around a retain/realse to prevent being deallocated too soon |