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