00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "ClientController.h"
00010
00011
00012 @implementation ClientController
00013
00014
00015 bool ghostStart = NO;
00016
00017 - (id) initWithUniverse:(Universe*)newUniverse {
00018 self = [super init];
00019 if (self != nil) {
00020 universe = newUniverse;
00021 communication = [[Communication alloc] initWithUniverse:universe baseUdpPort:0];
00022
00023
00024 [notificationCenter addObserver:self selector:@selector(checkForDeath:) name:@"SP_PSTATUS"
00025 object:nil useLocks:NO useMainRunLoop:YES];
00026 }
00027 return self;
00028 }
00029
00030 - (void) checkForDeath:(Player *)player {
00031
00032 if ([player isMe]) {
00033 NSLog(@"ClientController.checkForDeath status = %@", [player statusString]);
00034 if (([player status] == PLAYER_EXPLODE) ) {
00035 NSLog(@"ClientController.checkForDeath: today was a good day to die");
00036
00037
00038
00039 [self performSelector: @selector(iDied:) withObject:player afterDelay: 1.0];
00040
00041 [self iDied:player];
00042 }
00043 }
00044 }
00045
00046 - (void) iDied:(Player *)me {
00047
00048 NSLog(@"ClientController.iDied: today was a good day to die");
00049 [me setStatus:PLAYER_OUTFIT];
00050 [notificationCenter postNotificationName:@"CC_GO_OUTFIT"
00051 object:self
00052 userInfo:nil];
00053 }
00054
00055 - (bool) slotObtained {
00056
00057
00058 Player *me = [universe playerThatIsMe];
00059 if (me == nil) {
00060 NSLog(@"ClientController.slotObtained checking... nope");
00061 return NO;
00062 } else {
00063 NSLog(@"ClientController.slotObtained checking... YES");
00064 return YES;
00065 }
00066 }
00067
00068 - (void) setupSlot {
00069
00070
00071 if (![self slotObtained]) {
00072
00073 NSLog(@"ClientController.setupSlot called, but not a player");
00074 return;
00075 }
00076
00077 Player *me = [universe playerThatIsMe];
00078
00079
00080 NSLog(@"ClientController.setupSlot started");
00081 [notificationCenter postNotificationName:@"CC_SLOT_FOUND"
00082 object:self
00083 userInfo:nil];
00084
00085 if (ghostStart) {
00086
00087
00088 if ([me damage] > [[me ship] maxDamage]) {
00089 [me setStatus:PLAYER_OUTFIT];
00090
00091 [notificationCenter postNotificationName:@"CC_GO_OUTFIT"
00092 object:self
00093 userInfo:nil];
00094 } else {
00095 [me setStatus:PLAYER_ALIVE];
00096
00097 }
00098 }
00099
00100 return;
00101 }
00102
00103
00104
00105 - (void) findSlot {
00107
00108
00109
00110
00111
00112 if (![self slotObtained]) {
00113
00114 [self performSelector: @selector(findSlot) withObject: self afterDelay: 0.1];
00115 return;
00116 }
00117 [self setupSlot];
00118 }
00119
00120 - (bool)sendSlotSettingsToServer {
00121
00122
00123
00124 [notificationCenter postNotificationName:@"COMM_SEND_SHORT_REQ" object:self userInfo:[NSNumber numberWithInt:COMM_UDP]];
00125
00126
00127 [notificationCenter postNotificationName:@"COMM_SEND_SHORT_REQ" object:self userInfo:[NSNumber numberWithInt:SPK_VON]];
00128
00129
00130 [notificationCenter postNotificationName:@"COMM_SEND_OPTIONS_PACKET" object:self userInfo:nil];
00131
00132
00133 [notificationCenter postNotificationName:@"COMM_SEND_PING_REQ" object:self userInfo:[NSNumber numberWithBool:NO]];
00134
00135
00136 [notificationCenter postNotificationName:@"COMM_SEND_UPDATE_PACKET" object:self userInfo:[NSNumber numberWithInt:COMM_UPDATES_PER_SECOND]];
00137 return YES;
00138 }
00139
00140 - (void)singleReadFromServer {
00141 [communication readFromServer];
00142 }
00143
00144 - (bool)startClientAt:(NSString *)server port:(int)port seperate:(bool)multiThread {
00145
00146 NSLog(@"ClientController.startClientAt: %@ port %d", server, port);
00147
00148
00149 if ([communication callServer:server port:port]) {
00150 [notificationCenter postNotificationName:@"CC_SERVER_CALL_SUCCESS"];
00151
00152
00153 [communication setMultiThreaded:multiThread];
00154 if (multiThread) {
00155
00156
00157 if ([communication startCommunicationThread]) {
00158
00159 [self findSlot];
00160 return YES;
00161 } else {
00162 NSLog(@"ClientController.startClientAt: ERROR starting thread");
00163 return NO;
00164 }
00165 } else {
00166
00167 while (![self slotObtained]) {
00168 [communication readFromServer];
00169 }
00170
00171 [self setupSlot];
00172 return YES;
00173 }
00174
00175 } else {
00176 NSLog(@"ClientController.startClientAt: %@ port %d NO CONNECTION", server, port);
00177 [notificationCenter postNotificationName:@"CC_SERVER_CALL_FAILED"];
00178 return NO;
00179 }
00180 }
00181
00182 - (void)stop {
00183 NSLog(@"ClientController.stop stoping thread and cleaning self");
00184 [communication stopCommunicationThread];
00185 [communication cleanUp:self];
00186
00187 [[universe playerThatIsMe] setIsMe:NO];
00188 }
00189
00190 @end