source: Daodan/src/Flatline.c@ 876

Last change on this file since 876 was 876, checked in by gumby, 11 years ago

Daodan pass 1

File size: 16.4 KB
Line 
1#include "Flatline.h"
2#include "Oni_Character.h"
3#include "Oni_Gamestate.h"
4#include "Oni_Symbols.h"
5#include "Flatline_Client.h"
6#include "Flatline_Server.h"
7#include "Flatline_Events.h"
8#include "Daodan_Utility.h"
9#include <Windows.h>
10//#include <sys/time.h>
11#include <time.h>
12#include <float.h>
13#define isnan(x) ((x) != (x))
14uint32_t last1 = 0; uint32_t last2 = 0;
15player_info Players[MAX_PLAYERS] = {{0}, {0}, {0}, {0}};
16player_info * PlayerList[MAX_CONNECTIONS] = {0};
17multiplayer_status MultiplayerStatus;
18unsigned int lastPingTime;
19
20const char * Rejection_Messages[][255] = {
21 {"Server is full"},
22 {"-2"},
23 {"-3"},
24 {"-4"},
25 {"-5"},
26};
27
28#define BETTER_SYNC
29
30
31
32
33short TRrAnimation_GetType(char* anim)
34{
35 return *(short*)(anim + 0x15A);
36}
37
38void ONrCharacter_SetAnimationInternal(Character* Char, ActiveCharacter* AChar,
39 short inFromState, short inNextAnimType, const void *TRAM)
40{
41 ONCC *ONCC = Char->ONCC;
42 void *TRAC = ONCC->TRAC;
43 short index = Char->Number;
44 short animType;
45
46 if (TRAM == 0) return;
47
48 animType = TRrAnimation_GetType(TRAM);
49
50 AChar->Animation = TRAM;
51 AChar->Frame = 0;
52 AChar->AnimationFromState = inFromState;
53 AChar->AnimationType = animType;
54
55 AChar->NextAnimationType= inNextAnimType;
56 AChar->AnimationToState = TRrAnimation_GetTo(TRAM);
57
58 return;
59}
60
61
62
63
64
65
66//wtf, this needs cleaned up...
67player_info *FLr_FindEmptySlot() {
68 int j;
69 for(j = 0; j < MAX_PLAYERS; j++) {
70 if (Players[j].ip == 0) {
71 return &Players[j];
72 }
73 }
74 return 0;
75}
76
77extern uint16_t max_connections;
78uint16_t FLr_FindEmptyListSlot() {
79 int j;
80 for(j = 0; j < max_connections; j++) {
81 if (PlayerList[j] == 0) {
82 return j;
83 }
84 }
85 return -1;
86}
87typedef struct
88{
89 uint16_t x;
90 uint16_t y;
91} IMtPoint2D;
92static flatline_packet cache_input = {0};
93
94
95void * ONICALL FLrInput_Update_Keys(void)
96{
97 uint32_t i;
98 flatline_packet all_input = {0};
99 int16_t InputIndex = 0;
100
101 if(client_connected)
102 {
103 int sent_bytes;
104 flatline_packet input_packet = {0};
105
106 FLrClient_GetPackets();
107
108 input_packet.id = PLAYER_INPUT;
109// input_packet.input_struct.Time = ONgGameState->GameTime;
110 input_packet.input_struct.Actions1 = ONgGameState->Input.Current.Actions1;
111 input_packet.input_struct.Actions2 = ONgGameState->Input.Current.Actions2;
112 input_packet.input_struct.MouseDeltaX = ONgGameState->Input.MouseDeltaX;
113 input_packet.input_struct.MouseDeltaY = ONgGameState->Input.MouseDeltaY;
114 input_packet.input_struct.DesiredFacing = ONgGameState->PlayerCharacter->DesiredFacing;
115
116 sent_bytes = NetUDPSocket_Send(client_sock,(sockaddr *) &address, (char*)&input_packet, sizeof(input_struct) + FLATLINE_HEADER);
117
118 //return ONgGameState;
119 }
120
121
122 if(!(server_started || client_connected)) return ONgGameState;
123
124
125
126 for(i = 0; i < max_connections; i++) {
127 ActiveCharacter * Active_Player;
128 Character* Player;
129 GameInput * Active_Input;
130 if(PlayerList[i] == 0) continue;
131
132
133
134 Player = PlayerList[i]->Chr;
135 Active_Player = ONrGetActiveCharacter( PlayerList[i]->Chr);
136
137 if(!Player)
138 {
139 DDrConsole_Print("Warning, missing Character!");
140 continue;
141 }
142
143 if( server_started && i != 0 )
144 {
145 PlayerList[i]->Chr->DesiredFacing = PlayerList[i]->FacingFromClient;
146 }
147
148
149
150 //Set the health properly first.
151 //Always overridden by the server because of the chance of random damage and such
152 if( client_connected && DoWeUpdateThis( PlayerList[i]->UpdateFlags, PFlag_Health) )
153 {
154 PlayerList[i]->Chr->MaxHealth = PlayerList[i]->Health.MaxHealth;
155 ONrCharacter_SetHitPoints( PlayerList[i]->Chr, PlayerList[i]->Health.Health);
156 //PlayerList[i]->UpdateFlags &= ~( 1 << PFlag_Health );
157 }
158
159 //If the player is dead
160 if( PlayerList[i]->Chr->Health == 0 )
161
162 {
163 const short TicksToRespawn = 3 * 60;
164
165 //Permanently kill off dumb AI
166 if(PlayerList[i]->flags & PF_SCRIPTEDAI)
167 {
168 FLrPlayerDisconnect( i );
169 continue;
170 }
171
172 //Just to know if we have started counting off the respawn
173 if(PlayerList[i]->state != STATE_DEAD)
174 {
175 PlayerList[i]->state = STATE_DEAD;
176 PlayerList[i]->DeathTime = ONgGameState->GameTime;
177 if(i == client_slot)
178 {
179 ONrGameState_Timer_Start( "", TicksToRespawn );
180 }
181
182 if(server_started)
183 {
184 FLsPublic_Event( EV_KILLED, &i );
185 }
186
187 }
188
189 //Server respawning
190 if(server_started)
191 {
192 int Actions;
193 if(i == 0)
194 {
195 Actions = ONgGameState->Input.Current.Actions1;
196 }
197 else
198 {
199 Actions = PlayerList[i]->InputFromClient.Actions1;
200 }
201
202 if(ONgGameState->GameTime - PlayerList[i]->DeathTime > TicksToRespawn &&
203 (Actions & (Action_Punch | Action_Kick)) )
204 {
205 FLrPlayerRespawn( i );
206
207 FLsPublic_Event( EV_RESPAWN, &i );
208 }
209 else
210 {
211 continue;
212 }
213 }
214 else //clients?!
215 {
216 continue;
217 }
218 }
219
220 PlayerList[i]->state = STATE_ALIVE;
221
222 if( client_connected && DoWeUpdateThis( PlayerList[i]->UpdateFlags, PFlag_Class ) )
223 {
224 if(PlayerList[i]->Class)
225 {
226 ONrCharacter_SetCharacterClass( PlayerList[i]->Chr, PlayerList[i]->Class );
227 }
228 PlayerList[i]->UpdateFlags &= ~( 1 << PFlag_Class );
229 }
230
231 if( client_connected && DoWeUpdateThis( PlayerList[i]->UpdateFlags, PFlag_Facing ) )
232 {
233 PlayerList[i]->Chr->Facing = PlayerList[i]->Facings.Facing;
234 if(i != client_slot)
235 {
236 PlayerList[i]->Chr->DesiredFacing = PlayerList[i]->Facings.DesiredFacing;
237 }
238 PlayerList[i]->UpdateFlags &= ~( 1 << PFlag_Facing );
239 }
240
241 if(Active_Player == 0) continue;
242
243
244
245 // Active_Player->PlayingFilm.Flags = 1;
246
247 Active_Input = &(Active_Player->Input);
248
249
250 if( (server_started && i !=0) || !server_started )
251 {
252 Active_Input->Stop.Actions1 = ~PlayerList[i]->Input.Actions1 & Active_Input->Current.Actions1;
253 Active_Input->Stop.Actions2 = ~PlayerList[i]->Input.Actions2 & Active_Input->Current.Actions2;
254 Active_Input->Start.Actions1 = ~Active_Input->Current.Actions1 & PlayerList[i]->Input.Actions1;
255 Active_Input->Start.Actions2 = ~Active_Input->Current.Actions2 & PlayerList[i]->Input.Actions2;
256
257 Active_Input->Current.Actions1 = PlayerList[i]->Input.Actions1;
258 Active_Input->Current.Actions2 = PlayerList[i]->Input.Actions2;
259 Active_Input->Stopped.Actions1 = ~Active_Input->Current.Actions1;
260 Active_Input->Stopped.Actions2 = ~Active_Input->Current.Actions2;
261 if(client_connected && i == client_slot)
262 {
263 Active_Input->MouseDeltaX = ONgGameState->Input.MouseDeltaX;
264 Active_Input->MouseDeltaY = ONgGameState->Input.MouseDeltaY;
265 }
266 else
267 {
268 Active_Input->MouseDeltaX = PlayerList[i]->Input.MouseDeltaX;
269 Active_Input->MouseDeltaY = PlayerList[i]->Input.MouseDeltaY;
270 }
271 }
272
273 {
274 void* ConsoleAnimation = 0;
275 TMrInstance_GetDataPtr( 'TRAM', "KONOKOwatch_idle", &ConsoleAnimation);
276
277 if(!Active_Player->IsInAir && Active_Input->Current.Actions1 & (Action_Console | Action_PauseScreen)
278 && !(PlayerList[i]->Chr->Flags & ONcCharacterFlag_BeingThrown)
279 && Active_Player->ThrowTargetCharacter != -1)
280 {
281 if(ConsoleAnimation && ConsoleAnimation != Active_Player->Animation)
282 {
283 ONrCharacter_SetAnimationExternal(PlayerList[i]->Chr, Active_Player->AnimationFromState, ConsoleAnimation, 10);
284 Player->Flags |= 0x00200000;
285 Active_Player->ForcedAnimationFrames = -1;// TRrAnimation_GetDuration(ConsoleAnimation);
286 }
287 }
288 else if(Active_Input->Stopped.Actions1 & (Action_Console | Action_PauseScreen) )
289 {
290 Active_Player->ForcedAnimationFrames = 0;
291 }
292
293 }
294
295 //Check for character switching requests
296 if(server_started && PlayerList[i]->Chr->Health != 0
297 && PlayerList[i]->InputFromClient.Actions1 & Action_Block)
298 {
299 if( PlayerList[i]->ShapeshiftCooldown < ONgGameState->GameTime)
300 {
301 int error;
302
303
304
305 ONCC *newClass;
306 short numClasses = (short)TMrInstance_GetTagCount('ONCC');
307 /*
308 if(Active_Player->Input.Start.Actions1 & Action_Block)
309 {
310 //This might not be getting hit. Find out why, eh?
311 PlayerList[i]->ShapeshiftCooldown = ONgGameState->GameTime + 15;
312 }
313 else
314 {
315 PlayerList[i]->ShapeshiftCooldown = ONgGameState->GameTime + 5;
316 }
317 */
318
319 PlayerList[i]->ShapeshiftCooldown = ONgGameState->GameTime + 15;
320
321 if (PlayerList[i]->InputFromClient.Actions1 & Action_Crouch) {
322 Player->ONCCnumber += numClasses - 1;
323 }
324 else {
325 Player->ONCCnumber += 1;
326 }
327
328 if (numClasses > 0) {
329 Player->ONCCnumber = Player->ONCCnumber % numClasses;
330
331 error = TMrInstance_GetDataPtr_ByNumber('ONCC', Player->ONCCnumber, &newClass);
332
333 if ((newClass != NULL) && (!error)) {
334 ONrCharacter_SetCharacterClass(Player, newClass);
335 }
336 }
337
338 }
339 }
340 else
341 {
342 PlayerList[i]->ShapeshiftCooldown = 0;
343 }
344 if(client_connected) {
345
346 if( DoWeUpdateThis( PlayerList[i]->UpdateFlags, PFlag_Position) )
347 {
348 //Active_Player->PhyContext->Position = PlayerList[i]->Position;
349
350 Active_Player->PhyContext->Position.X =
351 (PlayerList[i]->Position.X + Active_Player->PhyContext->Position.X) / 2;
352
353 Active_Player->PhyContext->Position.Y =
354 (PlayerList[i]->Position.Y + Active_Player->PhyContext->Position.Y) / 2;
355
356 Active_Player->PhyContext->Position.Z =
357 (PlayerList[i]->Position.Z + Active_Player->PhyContext->Position.Z) / 2;
358
359 PlayerList[i]->UpdateFlags &= ~( 1 << PFlag_Position );
360 }
361
362
363
364
365 if (!(Player->Flags & ONcCharacterFlag_BeingThrown) &&
366 DoWeUpdateThis( PlayerList[i]->UpdateFlags, PFlag_Animation) && (PlayerList[i]->Animation))
367 {
368 // get a pointer to the animation
369
370
371 if (PlayerList[i]->Animation != Active_Player->Animation)
372 {
373
374 ///////////////////////////////////
375 //TODO: Check age of animation
376 ///////////////////////////////////
377 // set the characters animation
378 /*ONrCharacter_SetAnimationInternal(Player,
379 Active_Player,
380 Active_Player->AnimationToState,
381 0,
382 PlayerList[i]->Animation);*/
383 //ONrCharacter_NewAnimationHook(Player, Active_Player);
384 ONrCharacter_SetAnimationExternal(Player, TRrAnimation_GetFrom(PlayerList[i]->Animation), PlayerList[i]->Animation, 1);
385 //ONrCharacter_NewAnimationHook(Player, Active_Player);
386 }
387
388
389 }
390 PlayerList[i]->UpdateFlags &= ~( 1 << PFlag_Animation );
391
392 //Disabled Frame syncing for now. In most cases it won't be useful.
393 if(0 && DoWeUpdateThis( PlayerList[i]->UpdateFlags, PFlag_FramePing) && PlayerList[i]->Frame != -1
394 //&& !DoWeUpdateThis( PlayerList[i]->UpdateFlags, PFlag_Animation)
395 )
396 {
397 if( abs(PlayerList[i]->Frame - Active_Player->Frame) > 2 )
398 {
399 short AnimationLength;
400 AnimationLength = TRrAnimation_GetDuration(Active_Player->Animation);
401 if (PlayerList[i]->Frame >= AnimationLength)
402 {
403 Active_Player->Frame = AnimationLength - 1;
404 //Active_Player->Frame = 0;
405 }
406 else
407 {
408 Active_Player->Frame = PlayerList[i]->Frame;
409 }
410 }
411
412 }
413 PlayerList[i]->UpdateFlags &= ~( 1 << PFlag_FramePing );
414
415 //Increment frame in case we were waiting
416 PlayerList[i]->Frame++;
417
418 if (DoWeUpdateThis( PlayerList[i]->UpdateFlags, PFlag_Throws)
419 && PlayerList[i]->ThrowData.throwName[0] != 0)
420 {
421 if(PlayerList[PlayerList[i]->ThrowData.throwing])
422 {
423 short throwTarget = PlayerList[PlayerList[i]->ThrowData.throwing]->spawnnumber;
424 /*if ((throwTarget != Active_Player->throwing) &&
425 (PlayerList[i]->ThrowData.throwFrame < 10))*/
426 {
427 void *throw_animation;
428 ActiveCharacter* Target;
429 // get the animation
430
431 TMrInstance_GetDataPtr(
432 'TRAM',
433 PlayerList[i]->ThrowData.throwName,
434 &throw_animation);
435 //if (error) return;
436
437 // set the throw target
438 Active_Player->ThrowTargetCharacter = &ONgGameState->CharacterStorage[throwTarget];
439 Target = ONrGetActiveCharacter(Active_Player->ThrowTargetCharacter);
440 //if (/*(Target->Animation != throw_animation) &&*/
441 // (OldAnimation != Animation) &&
442 // !(Active_Player->ThrowTargetCharacter->Flags & ONcCharacterFlag_BeingThrown))
443 // Target->thrownBy == -
444 {
445 // set the throw variables
446 Active_Player->targetThrow = throw_animation;
447 Active_Player->throwing = throwTarget;
448
449 // run the throw
450 ONrCharacter_NewAnimationHook(Player, Active_Player);
451
452 //if (Active_Player->ThrowTargetCharacter)
453 {
454 // Target->Frame += 2;
455 //DDrConsole_PrintF("Thrown by player %hi", Player->Number );
456 //DDrStartupMessage("Thrown by player %hi", Player->Number );
457 Target->thrownBy = Player->Number & 0x00ff;
458 }
459 }
460 }
461 }
462 else
463 {
464 DDrConsole_PrintF("Warning, tried to throw nonexistant player %hi",
465 PlayerList[i]->ThrowData.throwing );
466 }
467 }
468
469 //Always discard old throw data, even if it isnt applied
470 PlayerList[i]->UpdateFlags &= ~( 1 << PFlag_Throws );
471 }
472
473
474 }
475
476 if(server_started)
477 {
478 if(ONgGameState->GameTime % 120 == 0)
479 {
480 FLsPingAll();
481 }
482
483 if(PlayerList[0])
484 {
485 PlayerList[0]->InputFromClient.Actions1 = ONgGameState->Input.Current.Actions1;
486 PlayerList[0]->InputFromClient.Actions2 = ONgGameState->Input.Current.Actions2;
487 PlayerList[0]->InputFromClient.MouseDeltaX = ONgGameState->Input.MouseDeltaX;
488 PlayerList[0]->InputFromClient.MouseDeltaY = ONgGameState->Input.MouseDeltaY;
489 }
490 FLsSendPlayerData();
491 }
492 MultiplayerStatus.PleaseUpdateAllPlayers = 0;
493 return ONgGameState;
494}
495
496void FLrPlayerDisconnect( int Player )
497{
498 if(server_started)
499 {
500 //FLsPublic_Event(EV_DISCONNECT, &Player );
501 MultiplayerStatus.PleaseUpdateAllPlayers = 1;
502 }
503 //Kill off the character in another function, please
504 //ONrCharacter_SetHitPoints( PlayerList[Player]->Chr, 0);
505
506 memset(PlayerList[Player], 0, sizeof(player_info));
507 PlayerList[Player] = 0;
508
509
510
511 return;
512}
513
514void FLrPlayerRespawn( int Player )
515{
516 PlayerList[Player]->state = STATE_ALIVE;
517 ONrCorpse_Create(PlayerList[Player]->Chr);
518 ONrCharacter_SetHitPoints( PlayerList[Player]->Chr, PlayerList[Player]->Chr->MaxHealth );
519}
520
521
522void* ScoreboardInstance = 0;
523void FLrRun_Scores()
524{
525 if(client_connected || server_started)
526 {
527 if(!ScoreboardInstance){
528 void* TSFFTahoma;
529 TMrInstance_GetDataPtr( 'TSFF', "Tahoma", &TSFFTahoma);
530 TSrContext_New( TSFFTahoma, 7, 1, 1, 0, &ScoreboardInstance);
531 }
532 if(ScoreboardInstance){
533 const int white = 0x00FFFFFF;
534 const int green = 0x0000FF00;
535 const int red = 0x00FF0000;
536 const int blue = 0x000000FF;
537 int i;
538 char DrawString[255];
539 const int LineHeight = 15;
540 IMtPoint2D DrawLocation = {25, 20};
541 TSrContext_SetShade(ScoreboardInstance, white);
542 TSrContext_DrawText(ScoreboardInstance, "Oni Flatline build " __DATE__ " " __TIME__, 255, 0, &DrawLocation);
543 TSrContext_SetShade(ScoreboardInstance, white);
544 DrawLocation.y += LineHeight;
545 DrawLocation.x = 25;
546 TSrContext_DrawText(ScoreboardInstance, "Name", 255, 0, &DrawLocation);
547 DrawLocation.x += 150;
548 TSrContext_DrawText(ScoreboardInstance, "Score", 255, 0, &DrawLocation);
549 DrawLocation.x += 50;
550 TSrContext_DrawText(ScoreboardInstance, "Ping", 255, 0, &DrawLocation);
551 for(i = 0; i <MAX_PLAYERS; i++)
552 {
553 if(PlayerList[i] == 0 || PlayerList[i]->Chr == 0) continue;
554
555 DrawLocation.x = 10;
556 DrawLocation.y += LineHeight;
557
558 sprintf(DrawString, "%i.", i );
559 TSrContext_DrawText(ScoreboardInstance, DrawString, 255, 0, &DrawLocation);
560 DrawLocation.x += 15;
561
562 if(PlayerList[i]->Chr && PlayerList[i]->Chr->Health == 0)
563 {
564 TSrContext_SetShade(ScoreboardInstance, red);
565 }
566 else if (i == client_slot)
567 {
568 TSrContext_SetShade(ScoreboardInstance, green);
569 }
570 TSrContext_DrawText(ScoreboardInstance, PlayerList[i]->name, 255, 0, &DrawLocation);
571 TSrContext_SetShade(ScoreboardInstance, white);
572 DrawLocation.x += 150;
573 sprintf(DrawString, "%i", PlayerList[i]->Chr->Damage);
574 TSrContext_DrawText(ScoreboardInstance, DrawString, 255, 0, &DrawLocation);
575 DrawLocation.x += 50;
576 sprintf(DrawString, "%i", PlayerList[i]->Ping);
577 TSrContext_DrawText(ScoreboardInstance, DrawString, 255, 0, &DrawLocation);
578 }
579 }
580 }
581}
582
583bool FlatlineInitialize()
584{
585 memset( Players, 0, sizeof( player_info ) * MAX_PLAYERS );
586 memset( PlayerList, 0, 4 * MAX_PLAYERS );
587 memset( &MultiplayerStatus, 0, sizeof( multiplayer_status ));
588 return 1;
589}
Note: See TracBrowser for help on using the repository browser.