[567] | 1 | #include "Flatline.h"
|
---|
| 2 | #include "Flatline_Server.h"
|
---|
| 3 |
|
---|
[578] | 4 | //I hereby apologize for the uglyness of the below code.
|
---|
| 5 | //It was never intended to be "final" code, much less shared with anyone
|
---|
| 6 |
|
---|
[567] | 7 | int total_players = 0;
|
---|
| 8 | uint16_t max_connections = MAX_CONNECTIONS;
|
---|
| 9 |
|
---|
[573] | 10 | player_info* FLrServer_AddPlayer(int ip, char* name, bool is_server, bool is_bot) {
|
---|
[567] | 11 | flatline_packet new_char = {0};
|
---|
| 12 | CharacterObject* Char;
|
---|
| 13 | uint32_t player_slot = 0;
|
---|
| 14 | int playerlist_slot = 0;
|
---|
[578] | 15 |
|
---|
| 16 | if(is_server || total_players < max_connections) {
|
---|
| 17 |
|
---|
[567] | 18 | int i = 0;
|
---|
| 19 | int k = 0;
|
---|
| 20 |
|
---|
[578] | 21 | //Skip for the host
|
---|
| 22 | if(!is_server)
|
---|
| 23 | {
|
---|
| 24 | char* zero = strchr(name, 0);
|
---|
| 25 | playerlist_slot = FLr_FindEmptyListSlot();
|
---|
[567] | 26 |
|
---|
[578] | 27 | total_players++;
|
---|
[567] | 28 |
|
---|
[578] | 29 | //checks to see if a name exists or not
|
---|
| 30 | //then appends [#] on the end of it if it does
|
---|
| 31 | //May be buggy, come back to this.
|
---|
| 32 | if(zero - name > 28) zero = name + 28;
|
---|
| 33 | for(i = 0; i < max_connections; i++) {
|
---|
| 34 | if(PlayerList[i] != 0 && !strcmp(name, PlayerList[i]->name)) {
|
---|
| 35 | k++;
|
---|
| 36 | sprintf(zero, "[%i]", k);
|
---|
| 37 | i = 0;
|
---|
| 38 | }
|
---|
[567] | 39 | }
|
---|
| 40 | }
|
---|
[578] | 41 |
|
---|
| 42 | new_char.new_player.Playernumber = playerlist_slot;
|
---|
[567] | 43 |
|
---|
[578] | 44 | //Set up a new Character structure to be spawned as the new player.
|
---|
| 45 | //Can this code be surrounded with if(!is_server){}?
|
---|
[567] | 46 | Char = &new_char.new_player.Character;
|
---|
| 47 | memset(Char, 0, sizeof(CharacterObject));
|
---|
| 48 | Char->Header.Type = 'CHAR';
|
---|
| 49 | sprintf(Char->OSD.Name,"%s",name);
|
---|
| 50 | sprintf(Char->OSD.Class, "%s", "konoko_generic");
|
---|
[573] | 51 | if(is_bot) {
|
---|
| 52 | Char->OSD.MeleeID = 22;
|
---|
| 53 | Char->OSD.JobID = 1;
|
---|
| 54 | Char->OSD.MinimalAlertLevel = 4;
|
---|
| 55 | Char->OSD.InvestigatingAlertLevel = 4;
|
---|
| 56 | Char->OSD.InitialAlertLevel = 4;
|
---|
| 57 | Char->OSD.StartJobAlertLevel = 4;
|
---|
| 58 | }
|
---|
| 59 | else if( !is_server )
|
---|
| 60 | {
|
---|
| 61 | Char->Header.Position.X = PlayerList[0]->Chr->Position.X;
|
---|
| 62 | Char->Header.Position.Y = PlayerList[0]->Chr->Position.Y;
|
---|
| 63 | Char->Header.Position.Z = PlayerList[0]->Chr->Position.Z;
|
---|
| 64 | }
|
---|
[567] | 65 |
|
---|
| 66 | //TMrInstance_GetDataPtr('ONCC', "striker_easy_1", PlayerList[playerlist_slot]->Chr->ONCC);
|
---|
| 67 |
|
---|
| 68 | new_char.id = NEW_PLAYER;
|
---|
| 69 | if(!is_server) {
|
---|
| 70 | ONrGameState_NewCharacter(Char, NULL, NULL, &(player_slot));
|
---|
[568] | 71 | //move this to a set up characters function...
|
---|
[573] | 72 | if(!is_bot) ONgGameState->CharacterStorage[player_slot].charType = 0;
|
---|
[568] | 73 |
|
---|
[567] | 74 | PlayerList[playerlist_slot] = Players+player_slot;
|
---|
[568] | 75 | PlayerList[playerlist_slot]->spawnnumber = player_slot;
|
---|
[578] | 76 | PlayerList[playerlist_slot]->Chr = &(ONgGameState->CharacterStorage)[player_slot];
|
---|
| 77 | //PlayerList[playerlist_slot]->Chr->Flags = chr_dontaim | chr_unkillable; //&= 0xFFBFFFFF; //WTF
|
---|
| 78 | if(!is_bot) PlayerList[playerlist_slot]->Chr->Flags &= 0xFFBFFFFF; //WTF, magic number.
|
---|
[567] | 79 | sprintf(PlayerList[playerlist_slot]->Chr->Name, "%.31s", name);
|
---|
| 80 | UDPServer_SendToAll( (char*)&new_char, sizeof(new_player) + FLATLINE_HEADER );
|
---|
| 81 |
|
---|
| 82 | }
|
---|
| 83 | else {
|
---|
| 84 | PlayerList[0] = Players;
|
---|
| 85 | PlayerList[0]->Chr = (Character *)(((GameState * )(ONgGameState))->CharacterStorage);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | //add player to list
|
---|
| 89 |
|
---|
| 90 | PlayerList[playerlist_slot]->ip = ip;
|
---|
| 91 | PlayerList[playerlist_slot]->list_slot = playerlist_slot;
|
---|
| 92 | sprintf(PlayerList[playerlist_slot]->name,"%.31s",name);
|
---|
| 93 |
|
---|
| 94 | return &Players[player_slot];
|
---|
| 95 | }
|
---|
| 96 | return (player_info*)(-1);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | void FLrServer_Initialize(){
|
---|
[573] | 100 | FLrServer_AddPlayer(inet_addr("127.0.0.1"), "host", 1, 0);
|
---|
[568] | 101 | }
|
---|
[567] | 102 |
|
---|
[568] | 103 | //UDPServer_SendToAll
|
---|
| 104 | //Sends a packet to all the clients currently connected.
|
---|
| 105 | //Returns the number of players sent to.
|
---|
| 106 | int UDPServer_SendToAll(void* packet, int size) {
|
---|
| 107 | int j;
|
---|
| 108 | int players = 0;
|
---|
| 109 | sockaddr_in address;
|
---|
| 110 | memset(&address, 0, sizeof(sockaddr_in));
|
---|
| 111 | address.sin_family = AF_INET;
|
---|
| 112 | address.sin_addr.s_addr = htonl(INADDR_ANY);
|
---|
| 113 | address.sin_port = htons(27777);
|
---|
| 114 | for(j = 0; j < max_connections; j++) {
|
---|
| 115 | if (PlayerList[j] != 0 && PlayerList[j]->ip && (PlayerList[j]->ip != inet_addr("127.0.0.1"))) {
|
---|
| 116 | int sent_bytes;
|
---|
| 117 | address.sin_addr.s_addr = htonl(PlayerList[j]->ip);//*((struct in_addr*)(int*)&(Players[j].ip));
|
---|
| 118 | sent_bytes = NetUDPServer_Send((sockaddr *) &address, (char*)packet, size);
|
---|
| 119 | if(sent_bytes == SOCKET_ERROR) NetCatchError();
|
---|
| 120 | else players++;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | return players;
|
---|
[573] | 124 | }
|
---|
| 125 |
|
---|
[579] | 126 | //FLsPublic_Event
|
---|
[578] | 127 | //Sends an event (door opening, player disconnecting, etc) to all players
|
---|
[580] | 128 | //Always make sure you send a pointer to this, even if it is just one arg. ;).
|
---|
[579] | 129 | //If it is void the double door in State crashes. Probably stack corruption,
|
---|
| 130 | //I'm not sure exactly why.
|
---|
[580] | 131 | //So we return 0 to stop that.
|
---|
[579] | 132 | int FLsPublic_Event( const unsigned int eventIndex, const int * args )
|
---|
[573] | 133 | {
|
---|
| 134 | int numArgs = FLrEvent_GetNumArgs( eventIndex );
|
---|
[579] | 135 | int ret;
|
---|
[573] | 136 | flatline_packet eventPacket = {0};
|
---|
| 137 | eventPacket.id = FLATLINE_EVENT;
|
---|
| 138 | eventPacket.flatline_event.event_index = eventIndex;
|
---|
[579] | 139 | ret = memcpy( eventPacket.flatline_event.intArray, args, sizeof(int) * numArgs );
|
---|
| 140 | ret = UDPServer_SendToAll( &eventPacket, sizeof(int) * (numArgs + 1) + FLATLINE_HEADER );
|
---|
| 141 | return 0;
|
---|
[573] | 142 | }
|
---|
| 143 |
|
---|
[580] | 144 | void FLsPingAll()
|
---|
| 145 | {
|
---|
| 146 | flatline_packet ping;
|
---|
| 147 | ping.id = PK_PING;
|
---|
| 148 | lastPingTime = ping.ping = ONgGameState->GameTime;
|
---|
| 149 | UDPServer_SendToAll(&ping, FLATLINE_HEADER + 4);
|
---|
| 150 | }
|
---|