1 #pragma once
2 
3 //********************************************************************************************
4 //*
5 //*    This file is part of Egoboo.
6 //*
7 //*    Egoboo is free software: you can redistribute it and/or modify it
8 //*    under the terms of the GNU General Public License as published by
9 //*    the Free Software Foundation, either version 3 of the License, or
10 //*    (at your option) any later version.
11 //*
12 //*    Egoboo is distributed in the hope that it will be useful, but
13 //*    WITHOUT ANY WARRANTY; without even the implied warranty of
14 //*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 //*    General Public License for more details.
16 //*
17 //*    You should have received a copy of the GNU General Public License
18 //*    along with Egoboo.  If not, see <http://www.gnu.org/licenses/>.
19 //*
20 //********************************************************************************************
21 
22 /// @file network.h
23 /// @brief Skeleton for Egoboo networking
24 
25 #include "egoboo_typedef.h"
26 #include "IDSZ_map.h"
27 
28 //--------------------------------------------------------------------------------------------
29 // Network stuff
30 //--------------------------------------------------------------------------------------------
31 
32 #define NETREFRESH          1000                    ///< Every second
33 #define NONETWORK           numservice
34 #define MAXSERVICE          16
35 #define NETNAMESIZE         16
36 #define MAXSESSION          16
37 #define MAXNETPLAYER         8
38 
39 #define TO_ANY_TEXT         25935                               ///< Message headers
40 #define TO_HOST_MODULEOK    14951
41 #define TO_HOST_MODULEBAD   14952
42 #define TO_HOST_LATCH       33911
43 #define TO_HOST_RTS         30376
44 #define TO_HOST_IM_LOADED   40192
45 #define TO_HOST_FILE        20482
46 #define TO_HOST_DIR         49230
47 #define TO_HOST_FILESENT    13131
48 #define TO_REMOTE_MODULE    56025
49 #define TO_REMOTE_LATCH     12715
50 #define TO_REMOTE_FILE      62198
51 #define TO_REMOTE_DIR       11034
52 #define TO_REMOTE_RTS        5143
53 #define TO_REMOTE_START     51390
54 #define TO_REMOTE_FILESENT  19903
55 
56 #define SHORTLATCH 1024.0f
57 #define CHARVEL 5.0f
58 #define MAXSENDSIZE 8192
59 #define COPYSIZE    4096
60 #define TOTALSIZE   2097152
61 #define MAX_PLAYER   8                               ///< 2 to a power...  2^3
62 #define MAXLAG      64
63 #define LAGAND      63
64 #define STARTTALK   10
65 
66 //--------------------------------------------------------------------------------------------
67 //--------------------------------------------------------------------------------------------
68 
69 /// A latch with a time attached
70 /// @details This is recieved over the network, or inserted into the list by the local system to simulate
71 ///  network traffic
72 struct s_time_latch
73 {
74     float   x;
75     float   y;
76     Uint32  button;
77     Uint32  time;
78 };
79 typedef struct s_time_latch time_latch_t;
80 
81 //--------------------------------------------------------------------------------------------
82 struct s_input_device
83 {
84     bool_t                  on;              ///< Is it alive?
85     BIT_FIELD               bits;
86 
87     float                   sustain;         ///< Falloff rate for old movement
88     float                   cover;           ///< For falloff
89 
90     latch_t                 latch;
91     latch_t                 latch_old;       ///< For sustain
92 };
93 typedef struct s_input_device input_device_t;
94 
95 void input_device_init( input_device_t * pdevice );
96 void input_device_add_latch( input_device_t * pdevice, float newx, float newy );
97 
98 //--------------------------------------------------------------------------------------------
99 
100 /// The state of a player
101 struct s_player
102 {
103     bool_t                  valid;                    ///< Player used?
104     CHR_REF                 index;                    ///< Which character?
105 
106     /// the buffered input from the local input devices
107     input_device_t          device;
108 
109     /// Local latch, set by set_one_player_latch(), read by sv_talkToRemotes()
110     latch_t                 local_latch;
111 
112     // quest log for this player
113     IDSZ_node_t             quest_log[MAX_IDSZ_MAP_SIZE];          ///< lists all the character's quests
114 
115     // Timed latches
116     Uint32                  tlatch_count;
117     time_latch_t            tlatch[MAXLAG];
118 
119     /// Network latch, set by unbuffer_player_latches(), used to set the local character's latch
120     latch_t                 net_latch;
121 };
122 
123 typedef struct s_player player_t;
124 
125 extern int                     local_numlpla;                                   ///< Number of local players
126 
127 #define INVALID_PLAYER MAX_PLAYER
128 
129 DECLARE_STACK_EXTERN( player_t, PlaStack, MAX_PLAYER );                         ///< Stack for keeping track of players
130 
131 #define VALID_PLA_RANGE(IPLA) ( ((IPLA) >= 0) && ((IPLA) < MAX_PLAYER) )
132 #define VALID_PLA(IPLA)       ( VALID_PLA_RANGE(IPLA) && ((IPLA) < PlaStack.count) && PlaStack.lst[IPLA].valid )
133 #define INVALID_PLA(IPLA)     ( !VALID_PLA_RANGE(IPLA) || ((IPLA) >= PlaStack.count)|| !PlaStack.lst[IPLA].valid )
134 
135 void           player_init( player_t * ppla );
136 void           pla_reinit( player_t * ppla );
137 CHR_REF        pla_get_ichr( const PLA_REF iplayer );
138 struct s_chr * pla_get_pchr( const PLA_REF iplayer );
139 
140 //--------------------------------------------------------------------------------------------
141 
142 /// The state of the network code used in old-egoboo
143 struct s_net_instance
144 {
145     bool_t  on;                      ///< Try to connect?
146     bool_t  serviceon;               ///< Do I need to free the interface?
147     bool_t  hostactive;              ///< Hosting?
148     bool_t  readytostart;            ///< Ready to hit the Start Game button?
149     bool_t  waitingforplayers;       ///< Has everyone talked to the host?
150 };
151 typedef struct s_net_instance net_instance_t;
152 
153 extern net_instance_t * PNet;
154 
155 //--------------------------------------------------------------------------------------------
156 //--------------------------------------------------------------------------------------------
157 
158 // Network orders
159 //extern unsigned char           ordervalid[MAXORDER];
160 //extern unsigned char           orderwho[MAXORDER][MAXSELECT];
161 //extern unsigned int            orderwhat[MAXORDER];
162 //extern unsigned int            orderwhen[MAXORDER];
163 
164 extern Uint32                  nexttimestamp;                ///< Expected timestamp
165 extern FILE                   *globalnetworkerr;             ///< For debuggin' network
166 
167 extern Uint32                  randsave;                  ///< Used in network timer
168 extern int                     networkservice;
169 extern int                     numservice;                                 ///< How many we found
170 extern char                    netservicename[MAXSERVICE][NETNAMESIZE];    ///< Names of services
171 extern int                     numsession;                                 ///< How many we found
172 extern char                    netsessionname[MAXSESSION][NETNAMESIZE];    ///< Names of sessions
173 extern int                     numplayer;                                  ///< How many we found
174 extern char                    netplayername[MAXNETPLAYER][NETNAMESIZE];   ///< Names of machines
175 
176 extern int                     local_machine;        ///< 0 is host, 1 is 1st remote, 2 is 2nd...
177 
178 extern int                     playersready;               ///< Number of players ready to start
179 extern int                     playersloaded;
180 
181 extern Uint32                  numplatimes;
182 
183 //--------------------------------------------------------------------------------------------
184 // Networking functions
185 //--------------------------------------------------------------------------------------------
186 
187 void listen_for_packets();
188 void unbuffer_player_latches();
189 void close_session();
190 
191 void net_initialize();
192 void net_shutDown();
193 void net_logf( const char *format, ... );
194 
195 void net_startNewPacket();
196 
197 void packet_addUnsignedByte( Uint8 uc );
198 void packet_addSignedByte( Sint8 sc );
199 void packet_addUnsignedShort( Uint16 us );
200 void packet_addSignedShort( Sint16 ss );
201 void packet_addUnsignedInt( Uint32 ui );
202 void packet_addSignedInt( Sint32 si );
203 void packet_addString( const char *string );
204 
205 void net_sendPacketToHost();
206 void net_sendPacketToAllPlayers();
207 void net_sendPacketToHostGuaranteed();
208 void net_sendPacketToAllPlayersGuaranteed();
209 void net_sendPacketToOnePlayerGuaranteed( int player );
210 void net_send_message();
211 
212 void net_updateFileTransfers();
213 int  net_pendingFileTransfers();
214 
215 void net_copyFileToAllPlayers( const char *source, const char *dest );
216 void net_copyFileToHost( const char *source, const char *dest );
217 void net_copyDirectoryToHost( const char *dirname, const char *todirname );
218 void net_copyDirectoryToAllPlayers( const char *dirname, const char *todirname );
219 void net_sayHello();
220 void cl_talkToHost();
221 void sv_talkToRemotes();
222 
223 int sv_hostGame();
224 int cl_joinGame( const char *hostname );
225 
226 void find_open_sessions();
227 void sv_letPlayersJoin();
228 void stop_players_from_joining();
229 // int create_player(int host);
230 // void turn_on_service(int service);
231 
232 void net_reset_players();
233 
234 void tlatch_ary_init( time_latch_t ary[], size_t len );
235 
236 player_t*      chr_get_ppla( const CHR_REF ichr );
237