1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
* Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 |
* Copyright (©) 1992 Frank Tore Johansen |
7 |
* |
8 |
* Deliantra is free software: you can redistribute it and/or modify it under |
9 |
* the terms of the Affero GNU General Public License as published by the |
10 |
* Free Software Foundation, either version 3 of the License, or (at your |
11 |
* option) any later version. |
12 |
* |
13 |
* This program is distributed in the hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
* GNU General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the Affero GNU General Public License |
19 |
* and the GNU General Public License along with this program. If not, see |
20 |
* <http://www.gnu.org/licenses/>. |
21 |
* |
22 |
* The authors can be reached via e-mail to <support@deliantra.net> |
23 |
*/ |
24 |
|
25 |
/* This file defines various flags that both the new client and |
26 |
* newserver uses. These should never be changed, only expanded. |
27 |
* Changing them will likely cause all old clients to not work properly. |
28 |
* While called newclient, it is really used by both the client and |
29 |
* server to keep some values the same. |
30 |
* |
31 |
* Name format is CS_(command)_(flag) |
32 |
* CS = Client/Server. |
33 |
* (command) is protocol command, ie ITEM |
34 |
* (flag) is the flag name |
35 |
*/ |
36 |
|
37 |
/* It is trivial to keep a link of copy of this file in the client |
38 |
* or server area. But keeping one common file should make things |
39 |
* more reliable, as both the client and server will definately be |
40 |
* talking about the same values. |
41 |
*/ |
42 |
|
43 |
#ifndef NETWORK_H |
44 |
#define NETWORK_H |
45 |
|
46 |
//+GPL |
47 |
|
48 |
#include <cstdarg> |
49 |
|
50 |
#include "compiler.h" |
51 |
|
52 |
/* Maximum size of any packet we expect. |
53 |
* |
54 |
* The size must be the same in the server and the client (stupid). |
55 |
* TODO: make the actual network configurable per client and use 2**16. |
56 |
*/ |
57 |
#define MAXSOCKBUF 10239 |
58 |
|
59 |
#define CS_QUERY_YESNO 0x1 /* Yes/no question */ |
60 |
#define CS_QUERY_SINGLECHAR 0x2 /* Single character response expected */ |
61 |
#define CS_QUERY_HIDEINPUT 0x4 /* Hide input being entered */ |
62 |
|
63 |
/* These are multiplication values that should be used when changing |
64 |
* floats to ints, and vice version. MULTI is integer representatin |
65 |
* (float to int), MULTF is float, for going from int to float. |
66 |
*/ |
67 |
#define FLOAT_MULTI 100000 |
68 |
#define FLOAT_MULTF 100000.0 |
69 |
|
70 |
/* ID's for the various stats that get sent across. */ |
71 |
|
72 |
#define CS_STAT_HP 1 |
73 |
#define CS_STAT_MAXHP 2 |
74 |
#define CS_STAT_SP 3 |
75 |
#define CS_STAT_MAXSP 4 |
76 |
#define CS_STAT_STR 5 |
77 |
#define CS_STAT_INT 6 |
78 |
#define CS_STAT_WIS 7 |
79 |
#define CS_STAT_DEX 8 |
80 |
#define CS_STAT_CON 9 |
81 |
#define CS_STAT_CHA 10 |
82 |
#define CS_STAT_EXP 11 |
83 |
#define CS_STAT_LEVEL 12 |
84 |
#define CS_STAT_WC 13 |
85 |
#define CS_STAT_AC 14 |
86 |
#define CS_STAT_DAM 15 |
87 |
#define CS_STAT_ARMOUR 16 |
88 |
#define CS_STAT_SPEED 17 |
89 |
#define CS_STAT_FOOD 18 |
90 |
#define CS_STAT_WEAP_SP 19 |
91 |
#define CS_STAT_RANGE 20 |
92 |
#define CS_STAT_TITLE 21 |
93 |
#define CS_STAT_POW 22 |
94 |
#define CS_STAT_GRACE 23 |
95 |
#define CS_STAT_MAXGRACE 24 |
96 |
#define CS_STAT_FLAGS 25 |
97 |
#define CS_STAT_WEIGHT_LIM 26 |
98 |
#define CS_STAT_EXP64 28 |
99 |
#define CS_STAT_SPELL_ATTUNE 29 |
100 |
#define CS_STAT_SPELL_REPEL 30 |
101 |
#define CS_STAT_SPELL_DENY 31 |
102 |
|
103 |
/* Start & end of resistances, inclusive. */ |
104 |
#define CS_STAT_RESIST_START 100 |
105 |
#define CS_STAT_RESIST_END 117 |
106 |
#define CS_STAT_RES_PHYS 100 |
107 |
#define CS_STAT_RES_MAG 101 |
108 |
#define CS_STAT_RES_FIRE 102 |
109 |
#define CS_STAT_RES_ELEC 103 |
110 |
#define CS_STAT_RES_COLD 104 |
111 |
#define CS_STAT_RES_CONF 105 |
112 |
#define CS_STAT_RES_ACID 106 |
113 |
#define CS_STAT_RES_DRAIN 107 |
114 |
#define CS_STAT_RES_GHOSTHIT 108 |
115 |
#define CS_STAT_RES_POISON 109 |
116 |
#define CS_STAT_RES_SLOW 110 |
117 |
#define CS_STAT_RES_PARA 111 |
118 |
#define CS_STAT_TURN_UNDEAD 112 |
119 |
#define CS_STAT_RES_FEAR 113 |
120 |
#define CS_STAT_RES_DEPLETE 114 |
121 |
#define CS_STAT_RES_DEATH 115 |
122 |
#define CS_STAT_RES_HOLYWORD 116 |
123 |
#define CS_STAT_RES_BLIND 117 |
124 |
|
125 |
/* CS_STAT_SKILLINFO is used as the starting index point. Skill number->name |
126 |
* map is generated dynamically for the client, so a bunch of entries will |
127 |
* be used here. CS_NUM_SKILLS does not match how many skills there really |
128 |
* are - instead, it is used as a range of values so that the client |
129 |
* can have some idea how many skill categories there may be. |
130 |
*/ |
131 |
#define CS_STAT_SKILLINFO 140 |
132 |
#define CS_NUM_SKILLS 50 |
133 |
|
134 |
/* These are used with CS_STAT_FLAGS above to communicate S->C what the |
135 |
* server thinks the fireon & runon states are. |
136 |
*/ |
137 |
#define SF_FIREON 0x01 |
138 |
#define SF_RUNON 0x02 |
139 |
|
140 |
/* Flags for the item command */ |
141 |
enum {a_none, a_readied, a_wielded, a_worn, a_active, a_applied}; |
142 |
|
143 |
#define F_APPLIED 0x000F |
144 |
#define F_LOCATION 0x00F0 |
145 |
#define F_UNPAID 0x0200 |
146 |
#define F_MAGIC 0x0400 |
147 |
#define F_CURSED 0x0800 |
148 |
#define F_DAMNED 0x1000 |
149 |
#define F_OPEN 0x2000 |
150 |
#define F_NOPICK 0x4000 |
151 |
#define F_LOCKED 0x8000 |
152 |
|
153 |
#define CF_FACE_NONE 0 |
154 |
#define CF_FACE_BITMAP 1 |
155 |
#define CF_FACE_XPM 2 |
156 |
#define CF_FACE_PNG 3 |
157 |
#define CF_FACE_CACHE 0x10 |
158 |
|
159 |
/* Used in the new_face structure on the magicmap field. Low bits |
160 |
* are color informatin. For now, only high bit information we need |
161 |
* is floor information. |
162 |
*/ |
163 |
#define FACE_FLOOR 0x80 |
164 |
#define FACE_WALL 0x40 /* Or'd into the color value by the server |
165 |
* right before sending. |
166 |
*/ |
167 |
#define FACE_COLOR_MASK 0xf |
168 |
|
169 |
#define UPD_LOCATION 0x01 |
170 |
#define UPD_FLAGS 0x02 |
171 |
#define UPD_WEIGHT 0x04 |
172 |
#define UPD_FACE 0x08 |
173 |
#define UPD_NAME 0x10 |
174 |
#define UPD_ANIM 0x20 |
175 |
#define UPD_ANIMSPEED 0x40 |
176 |
#define UPD_NROF 0x80 |
177 |
|
178 |
/* The flag values sent by the updspell command */ |
179 |
#define UPD_SP_MANA 0x01 |
180 |
#define UPD_SP_GRACE 0x02 |
181 |
#define UPD_SP_LEVEL 0x04 // was UPD_SP_DAMAGE in <= server 2.93, client 2.10 |
182 |
|
183 |
#define SOUND_NORMAL 0 |
184 |
#define SOUND_SPELL 1 |
185 |
|
186 |
//-GPL |
187 |
|
188 |
struct data |
189 |
{ |
190 |
unsigned int len; |
191 |
const void *ptr; |
192 |
|
193 |
data (const void *ptr, int len) : len (len), ptr (ptr) { } |
194 |
data (const char *str) : len (strlen (str ? str : 0)), ptr ((void *)str) { } |
195 |
data (shstr_tmp sh) : len (sh.length ()), ptr ((void *)&sh) { } |
196 |
data (dynbuf &buf) : len (buf.size ()), ptr (buf.linearise ()) { } |
197 |
}; |
198 |
|
199 |
struct data8 |
200 |
{ |
201 |
unsigned int len; |
202 |
const void *ptr; |
203 |
|
204 |
data8 (const void *ptr, int len) : len (len), ptr (ptr) { } |
205 |
data8 (const char *str) : len (strlen (str ? str : 0)), ptr ((void *)str) { } |
206 |
data8 (shstr_tmp sh) : len (sh.length ()), ptr ((void *)&sh) { } |
207 |
data8 (dynbuf &buf) : len (buf.size ()), ptr (buf.linearise ()) { } |
208 |
}; |
209 |
|
210 |
struct data16 |
211 |
{ |
212 |
unsigned int len; |
213 |
const void *ptr; |
214 |
|
215 |
data16 (const void *ptr, int len) : len (len), ptr (ptr) { } |
216 |
data16 (const char *str) : len (strlen (str ? str : 0)), ptr ((void *)str) { } |
217 |
data16 (shstr_tmp sh) : len (sh.length ()), ptr ((void *)&sh) { } |
218 |
data16 (dynbuf &buf) : len (buf.size ()), ptr (buf.linearise ()) { } |
219 |
}; |
220 |
|
221 |
struct ber32 |
222 |
{ |
223 |
enum { size = 5 }; // maximum length of an encoded ber32 |
224 |
uint32 val; |
225 |
|
226 |
ber32 (uint32 val) : val (val) { } |
227 |
|
228 |
static int encoded_size (uint32 val) |
229 |
{ |
230 |
if (val >= (1 << 7*4)) return 5; |
231 |
if (val >= (1 << 7*3)) return 4; |
232 |
if (val >= (1 << 7*2)) return 3; |
233 |
if (val >= (1 << 7*1)) return 2; |
234 |
return 1; |
235 |
} |
236 |
}; |
237 |
|
238 |
/* Contains the base information we use to make up a packet we want to send. */ |
239 |
struct packet |
240 |
{ |
241 |
enum { bufsize = MAXSOCKBUF }; |
242 |
enum { hdrlen = 2 }; // keep this many bytes free at the front |
243 |
|
244 |
// strategy: only check on variable-length adds |
245 |
// and allow 256 bytes for any headers. |
246 |
uint8 *cur; |
247 |
uint8 buf_ [hdrlen + bufsize + 256]; |
248 |
|
249 |
packet () |
250 |
{ |
251 |
reset (); |
252 |
} |
253 |
|
254 |
packet (const char *name); |
255 |
|
256 |
uint8 operator [](unsigned int i) const { return buf_ [i + hdrlen]; } |
257 |
uint8 &operator [](unsigned int i) { return buf_ [i + hdrlen]; } |
258 |
|
259 |
uint8 *buf () { return buf_ + hdrlen; } |
260 |
const uint8 *buf () const { return buf_ + hdrlen; } |
261 |
|
262 |
void reset (int offset = 0) { cur = buf () + offset; } |
263 |
int length () const { return cur - buf (); } |
264 |
int room () { return bufsize - (cur - buf ()); } |
265 |
|
266 |
// unchecked |
267 |
packet &operator <<(uint8 v) { *cur++ = v; return *this; } |
268 |
|
269 |
packet &operator <<(uint16 v) { return *this << uint8 (v >> 8) << uint8 (v); } |
270 |
packet &operator <<(uint32 v) { return *this << uint16 (v >> 16) << uint16 (v); } |
271 |
packet &operator <<(uint64 v) { return *this << uint32 (v >> 32) << uint32 (v); } |
272 |
|
273 |
packet &operator <<(sint8 v) { return *this << (uint8 )v; } |
274 |
packet &operator <<(sint16 v) { return *this << (uint16)v; } |
275 |
packet &operator <<(sint32 v) { return *this << (uint32)v; } |
276 |
packet &operator <<(sint64 v) { return *this << (uint64)v; } |
277 |
|
278 |
// checked |
279 |
packet &operator <<(const ber32 v); |
280 |
|
281 |
packet &operator <<(const data &v); |
282 |
packet &operator <<(const data8 &v); |
283 |
packet &operator <<(const data16 &v); |
284 |
|
285 |
packet &operator <<(const char *v); |
286 |
packet &operator <<(shstr_tmp sh) { return *this << data (sh); } |
287 |
packet &operator <<(const std::string &s) { return *this << data (s.data (), s.size ()); } |
288 |
|
289 |
void vprintf (const char *format, va_list ap); |
290 |
|
291 |
void printf (const char *format, ...) attribute ((format (printf, 2, 3))) |
292 |
{ |
293 |
va_list ap; |
294 |
va_start (ap, format); |
295 |
vprintf (format, ap); |
296 |
va_end (ap); |
297 |
} |
298 |
}; |
299 |
|
300 |
inline uint16 net_uint16 (uint8 *data) { return (data [0] << 8) | data [1]; } |
301 |
inline uint32 net_uint32 (uint8 *data) { return (net_uint16 (data) << 16) | net_uint16 (data + 2); } |
302 |
inline sint16 net_sint16 (uint8 *data) { return sint16 (net_uint16 (data)); } |
303 |
inline sint16 net_sint32 (uint8 *data) { return sint32 (net_uint32 (data)); } |
304 |
|
305 |
#endif |
306 |
|