source: Daodan/MSVC/Flatline_Server.c@ 573

Last change on this file since 573 was 573, checked in by gumby, 14 years ago
File size: 4.3 KB
RevLine 
[567]1#include "Flatline.h"
2#include "Flatline_Server.h"
3
4int total_players = 0;
5uint16_t max_connections = MAX_CONNECTIONS;
6
[573]7player_info* FLrServer_AddPlayer(int ip, char* name, bool is_server, bool is_bot) {
[567]8 flatline_packet new_char = {0};
9 CharacterObject* Char;
10 uint32_t player_slot = 0;
11 int playerlist_slot = 0;
12 if(is_server) goto server;
13 if(total_players < max_connections) {
14 char* zero = strchr(name, 0);
15 int i = 0;
16 int k = 0;
17 playerlist_slot = FLr_FindEmptyListSlot();
18
19
20 total_players++;
21
22 //send new player packet to all players
23 //char tempname[32] = {0};
24
25 //checks to see if a name exists or not
26 //then appends [#] on the end of it if it does
27
28 if(zero - name > 28) zero = name + 28;
29 for(i = 0; i < max_connections; i++) {
30 if(PlayerList[i] != 0 && !strcmp(name, PlayerList[i]->name)) {
31 k++;
32 sprintf(zero, "[%i]", k);
33 i = 0;
34 }
35 }
36
37server: ;
38 //memset( new_char, 0, sizeof(new_char);
39 //new_char = {0};
[573]40 new_char.new_player.Playernumber = playerlist_slot;
[567]41 Char = &new_char.new_player.Character;
42 memset(Char, 0, sizeof(CharacterObject));
43 Char->Header.Type = 'CHAR';
44 //Char->OSD.Options = char_dontaim;
45 sprintf(Char->OSD.Name,"%s",name);
46 sprintf(Char->OSD.Class, "%s", "konoko_generic");
[573]47 if(is_bot) {
48 Char->OSD.MeleeID = 22;
49 Char->OSD.JobID = 1;
50 Char->OSD.MinimalAlertLevel = 4;
51 Char->OSD.InvestigatingAlertLevel = 4;
52 Char->OSD.InitialAlertLevel = 4;
53 Char->OSD.StartJobAlertLevel = 4;
54 }
55 else if( !is_server )
56 {
57 Char->Header.Position.X = PlayerList[0]->Chr->Position.X;
58 Char->Header.Position.Y = PlayerList[0]->Chr->Position.Y;
59 Char->Header.Position.Z = PlayerList[0]->Chr->Position.Z;
60 }
[567]61
62 //TMrInstance_GetDataPtr('ONCC', "striker_easy_1", PlayerList[playerlist_slot]->Chr->ONCC);
63
64
65 new_char.id = NEW_PLAYER;
66 if(!is_server) {
67 ONrGameState_NewCharacter(Char, NULL, NULL, &(player_slot));
[568]68 //move this to a set up characters function...
[573]69 if(!is_bot) ONgGameState->CharacterStorage[player_slot].charType = 0;
[568]70
[567]71 PlayerList[playerlist_slot] = Players+player_slot;
[568]72 PlayerList[playerlist_slot]->spawnnumber = player_slot;
[567]73 PlayerList[playerlist_slot]->Chr = &((Character *)(((GameState * )(ONgGameState))->CharacterStorage))[player_slot];
[568]74// PlayerList[playerlist_slot]->Chr->Flags = chr_dontaim | chr_unkillable; //&= 0xFFBFFFFF; //WTF
[573]75 if(!is_bot) PlayerList[playerlist_slot]->Chr->Flags &= 0xFFBFFFFF; //WTF
[567]76 sprintf(PlayerList[playerlist_slot]->Chr->Name, "%.31s", name);
77 UDPServer_SendToAll( (char*)&new_char, sizeof(new_player) + FLATLINE_HEADER );
78
79 }
80 else {
81 PlayerList[0] = Players;
82 PlayerList[0]->Chr = (Character *)(((GameState * )(ONgGameState))->CharacterStorage);
83 }
84
85 //add player to list
86
87 PlayerList[playerlist_slot]->ip = ip;
88 PlayerList[playerlist_slot]->list_slot = playerlist_slot;
89 sprintf(PlayerList[playerlist_slot]->name,"%.31s",name);
90
91 return &Players[player_slot];
92 }
93 return (player_info*)(-1);
94}
95
96void FLrServer_Initialize(){
[573]97 FLrServer_AddPlayer(inet_addr("127.0.0.1"), "host", 1, 0);
[568]98}
[567]99
[568]100//UDPServer_SendToAll
101//Sends a packet to all the clients currently connected.
102//Returns the number of players sent to.
103int UDPServer_SendToAll(void* packet, int size) {
104 int j;
105 int players = 0;
106 sockaddr_in address;
107 memset(&address, 0, sizeof(sockaddr_in));
108 address.sin_family = AF_INET;
109 address.sin_addr.s_addr = htonl(INADDR_ANY);
110 address.sin_port = htons(27777);
111 for(j = 0; j < max_connections; j++) {
112 if (PlayerList[j] != 0 && PlayerList[j]->ip && (PlayerList[j]->ip != inet_addr("127.0.0.1"))) {
113 int sent_bytes;
114 address.sin_addr.s_addr = htonl(PlayerList[j]->ip);//*((struct in_addr*)(int*)&(Players[j].ip));
115 sent_bytes = NetUDPServer_Send((sockaddr *) &address, (char*)packet, size);
116 if(sent_bytes == SOCKET_ERROR) NetCatchError();
117 else players++;
118 }
119 }
120 return players;
[573]121}
122
123void FLsPublic_Event( unsigned int eventIndex, int * args )
124{
125 int numArgs = FLrEvent_GetNumArgs( eventIndex );
126 flatline_packet eventPacket = {0};
127 eventPacket.id = FLATLINE_EVENT;
128 eventPacket.flatline_event.event_index = eventIndex;
129 memcpy( eventPacket.flatline_event.intArray, args, sizeof(int) * numArgs );
130 UDPServer_SendToAll( &eventPacket, sizeof(int) * (numArgs + 1) + FLATLINE_HEADER );
131}
132
Note: See TracBrowser for help on using the repository browser.