ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/item.C
(Generate patch)

Comparing deliantra/server/socket/item.C (file contents):
Revision 1.28 by root, Wed Dec 20 09:14:22 2006 UTC vs.
Revision 1.82 by root, Fri Nov 6 13:07:28 2009 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
6 7 *
7 This program is free software; you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify it under
8 it under the terms of the GNU General Public License as published by 9 * the terms of the Affero GNU General Public License as published by the
9 the Free Software Foundation; either version 2 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
10 (at your option) any later version. 11 * option) any later version.
11 12 *
12 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 16 * GNU General Public License for more details.
16 17 *
17 You should have received a copy of the GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
18 along with this program; if not, write to the Free Software 19 * and the GNU General Public License along with this program. If not, see
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * <http://www.gnu.org/licenses/>.
20 21 *
21 The author can be reached via e-mail to <crossfire@schmorp.de> 22 * The authors can be reached via e-mail to <support@deliantra.net>
22*/ 23 */
23 24
24/** 25/**
25 * \file 26 * \file
26 * Client/server logic. 27 * Client/server logic.
27 * 28 *
28 * \date 2003-12-02 29 * \date 2003-12-02
29 * 30 *
30 * This containes item logic for client/server. It doesn't contain 31 * This contains item logic for client/server. It doesn't contain
31 * the actual commands that send the data, but does contain 32 * the actual commands that send the data, but does contain
32 * the logic for what items should be sent. 33 * the logic for what items should be sent.
33 */ 34 */
34 35
35#include <global.h> 36#include <global.h>
36#include <object.h> /* LOOK_OBJ */ 37#include <object.h>
37#include <sproto.h> 38#include <sproto.h>
38 39
39/** This is the maximum number of bytes we expect any one item to take up */ 40/** This is the maximum number of bytes we expect any one item to take up */
40#define MAXITEMLEN 300 41#define MAXITEMLEN 300
41
42#if 0
43tag_t
44client_container::tag () const
45{
46 switch (type)
47 {
48 case CC_INVENTORY:
49 return ns->pl->count;
50 case CC_MAPSPACE:
51 return 0;
52 case CC_CONTAINER:
53 return env->count;
54 }
55
56 abort ();
57}
58
59void
60client_container::clear ()
61{
62 switch (type)
63 {
64 case CC_INVENTORY:
65 abort ();
66
67 case CC_MAPSPACE:
68 case CC_CONTAINER:
69 ns->send_packet_printf ("delinv %d", tag ());
70 break;
71 }
72
73 for (iterator i = begin (); i != end (); ++i)
74 i->op->seen_by = 0;
75
76 vector< refitem, slice_allocator<refitem> >::clear ();
77}
78
79inline iterator
80client_container::merge_item (iterator i, object *op)
81{
82 if (i != end () && i->op == op)
83 return ++i;
84
85 if (op->seen_by)
86 return; // seen by another entity already
87
88 op->seen_by = this;
89
90 refitem ref;
91 ref.op = op;
92
93 return insert (i, ref);
94}
95
96void
97client_container::update (int offset)
98{
99 iterator i = begin ();
100
101 switch (type)
102 {
103 case CC_INVENTORY:
104 case CC_CONTAINER:
105 {
106 object *env = type == CC_INVENTORY
107 ? ns->pl->ob
108 : this->env;
109
110 // pass 1, erase all objects no longer in container
111 for (iterator j = begin (); j != end (); ++j)
112 if (j->op->env != env)
113 {
114 j->op->seen_by = 0;
115 erase (j);
116 }
117
118 // pass 2 merge items
119 for (object *op = env->inv; op; op = op->below)
120 {
121 if (--offset < 0)
122 i = merge_item (i, op);
123 else if (offset < -FLOORBOX_PAGESIZE)
124 break;
125 }
126 }
127 break;
128
129 case CC_MAPSPACE:
130 {
131 // pass 1, erase all objects no longer on space
132 for (iterator j = begin (); j != end (); ++j)
133 if (j->op->x != x || j->op->y != y || j->op->map != map)
134 {
135 j->op->seen_by = 0;
136 erase (j);
137 }
138
139 // pass 2 merge items
140 for (object *op = GET_MAP_OB (map, x, y); op; op = op->above)
141 {
142 if (--offset < 0)
143 i = merge_item (i, op);
144 else if (offset < -FLOORBOX_PAGESIZE)
145 break;
146 }
147 }
148 break;
149 }
150
151 // pass 3, erase all extra items
152 for (iterator j = i; j != end (); ++j)
153 j->op->seen_by = 0;
154
155 if (i != end ())
156 erase (i, end ());
157}
158
159void
160client_container::set_mapspace (maptile *map, int x, int y)
161{
162 if (type == CC_MAPSPACE
163 && this->map == map
164 && this->x == x
165 && this->y == y)
166 return;
167
168 clear ();
169
170 type = CC_MAPSPACE;
171 this->map = map;
172 this->x = x;
173 this->y = y;
174}
175
176void
177client_container::set_container (object *env)
178{
179}
180#endif
181 42
182/******************************************************************************* 43/*******************************************************************************
183 * 44 *
184 * Functions related to sending object data to the client. 45 * Functions related to sending object data to the client.
185 * 46 *
187 48
188/** 49/**
189 * This is a similar to query_name, but returns flags 50 * This is a similar to query_name, but returns flags
190 * to be sent to client. 51 * to be sent to client.
191 */ 52 */
192unsigned int 53static unsigned int
193query_flags (object *op) 54query_flags (object *op)
194{ 55{
195 unsigned int flags = 0; 56 unsigned int flags = 0;
196 57
197 if (QUERY_FLAG (op, FLAG_APPLIED)) 58 if (QUERY_FLAG (op, FLAG_APPLIED))
198 {
199 switch (op->type) 59 switch (op->type)
200 { 60 {
201 case BOW: 61 case BOW:
202 case WAND: 62 case WAND:
203 case ROD: 63 case ROD:
204 case HORN: 64 case HORN:
205 flags = a_readied; 65 flags = a_readied;
206 break; 66 break;
207 case WEAPON: 67 case WEAPON:
208 flags = a_wielded; 68 flags = a_wielded;
209 break; 69 break;
210 case SKILL: 70 case SKILL:
211 case ARMOUR: 71 case ARMOUR:
212 case HELMET: 72 case HELMET:
213 case SHIELD: 73 case SHIELD:
214 case RING: 74 case RING:
215 case BOOTS: 75 case BOOTS:
216 case GLOVES: 76 case GLOVES:
217 case AMULET: 77 case AMULET:
218 case GIRDLE: 78 case GIRDLE:
219 case BRACERS: 79 case BRACERS:
220 case CLOAK: 80 case CLOAK:
221 flags = a_worn; 81 flags = a_worn;
222 break; 82 break;
223 case CONTAINER: 83 case CONTAINER:
224 flags = a_active; 84 flags = a_active;
225 break; 85 break;
226 default: 86 default:
227 flags = a_applied; 87 flags = a_applied;
228 break; 88 break;
229 } 89 }
230 }
231 90
232 if (op->type == CONTAINER && ((op->env && op->env->container == op) || (!op->env && QUERY_FLAG (op, FLAG_APPLIED)))) 91 if (op->is_open_container ())
233 flags |= F_OPEN; 92 flags |= F_OPEN;
234 93
235 if (QUERY_FLAG (op, FLAG_KNOWN_CURSED)) 94 if (QUERY_FLAG (op, FLAG_KNOWN_CURSED))
236 { 95 {
237 if (QUERY_FLAG (op, FLAG_DAMNED)) 96 if (QUERY_FLAG (op, FLAG_DAMNED))
255 * animation of face to the client. 114 * animation of face to the client.
256 */ 115 */
257static void 116static void
258add_object_to_socklist (client &ns, packet &sl, object *head) 117add_object_to_socklist (client &ns, packet &sl, object *head)
259{ 118{
260 int flags, len, anim_speed;
261 char item_n[MAX_BUF];
262 const char *item_p;
263
264 flags = query_flags (head); 119 int flags = query_flags (head);
120
265 if (QUERY_FLAG (head, FLAG_NO_PICK)) 121 if (QUERY_FLAG (head, FLAG_NO_PICK))
266 flags |= F_NOPICK; 122 flags |= F_NOPICK;
267 123
268 if (!(ns.faces_sent[head->face->number] & NS_FACESENT_FACE)) 124 ns.send_face (head->face, -50);
269 esrv_send_face (&ns, head->face->number, 0); 125 ns.flush_fx ();
270 126
271 if (QUERY_FLAG (head, FLAG_ANIMATE) && !ns.anims_sent[head->animation_id]) 127 if (QUERY_FLAG (head, FLAG_ANIMATE) && !ns.anims_sent[head->animation_id])
272 esrv_send_animation (&ns, head->animation_id); 128 ns.send_animation (head->animation_id);
273 129
274 sl << uint32 (head->count) 130 sl << uint32 (head->count)
275 << uint32 (flags) 131 << uint32 (flags)
276 << uint32 (QUERY_FLAG (head, FLAG_NO_PICK) ? -1 : WEIGHT (head)) 132 << uint32 (QUERY_FLAG (head, FLAG_NO_PICK) ? -1 : head->client_weight ())
277 << uint32 (head->face->number); 133 << uint32 (head->face);
278 134
279 if (!head->custom_name) 135 if (!head->custom_name)
280 { 136 {
281 strncpy (item_n, query_base_name (head, 0), 127); 137 const char *name;
282 item_n[127] = 0; 138 uint8 *len = sl.cur++; // patch up length later
283 len = strlen (item_n); 139
284 item_p = query_base_name (head, 1); 140 name = query_base_name (head, 0); sl << data (name, min (127, strlen (name)));
141 sl << uint8 (0);
142 name = query_base_name (head, 1); sl << data (name, min (127, strlen (name)));
143
144 *len = sl.cur - len - 1;
285 } 145 }
286 else 146 else
287 { 147 {
288 strncpy (item_n, head->custom_name, 127); 148 int len = min (head->custom_name.length (), 127);
289 item_n[127] = 0;
290 len = strlen (item_n);
291 item_p = head->custom_name;
292 }
293 149
294 strncpy (item_n + len + 1, item_p, 127); 150 sl << uint8 (len * 2 + 1)
295 item_n[254] = 0; 151 << data (head->custom_name, len)
296 len += strlen (item_n + 1 + len) + 1; 152 << uint8 (0)
153 << data (head->custom_name, len);
154 }
297 155
298 sl << data8 (item_n, len)
299 << uint16 (head->animation_id); 156 sl << uint16 (head->animation_id);
300 157
301 anim_speed = 0; 158 int anim_speed = !head->flag [FLAG_ANIMATE] ? 0
302 if (QUERY_FLAG (head, FLAG_ANIMATE)) 159 : head->anim_speed ? clamp (head->anim_speed, 1, 255)
303 { 160 : 1. / clamp (fabs (head->speed), 1./255., 1./1.);
304 if (head->anim_speed)
305 anim_speed = head->anim_speed;
306 else
307 {
308 if (FABS (head->speed) < 0.001)
309 anim_speed = 255;
310 else if (FABS (head->speed) >= 1.0)
311 anim_speed = 1;
312 else
313 anim_speed = (int) (1.0 / FABS (head->speed));
314 }
315
316 if (anim_speed > 255)
317 anim_speed = 255;
318 }
319 161
320 sl << uint8 (anim_speed) 162 sl << uint8 (anim_speed)
321 << uint32 (head->nrof); 163 << uint32 (head->nrof);
322 164
323 if (ns.itemcmd == 2) 165 if (ns.itemcmd == 2)
324 sl << uint16 (head->client_type); 166 sl << uint16 (head->client_type);
325
326 SET_FLAG (head, FLAG_CLIENT_SENT);
327} 167}
328 168
169static faceidx
170need_face_now (player *pl, const char *name)
171{
172 faceidx face = face_find (name, empty_face);
173
174 pl->ns->send_face (face, -50);
175 pl->ns->flush_fx ();
176
177 return face;
178}
179
180#define FINGER_UP "finger_up.x11"
181#define FINGER_DOWN "finger_down.x11"
329 182
330/** 183/**
331 * Send the look window. Don't need to do animations here 184 * Send the look window. Don't need to do animations here
332 * This sends all the faces to the client, not just updates. This is 185 * This sends all the faces to the client, not just updates. This is
333 * because object ordering would otherwise be inconsistent. 186 * because object ordering would otherwise be inconsistent.
334 */ 187 */
335void 188void
336esrv_draw_look (object *pl) 189esrv_draw_look (player *pl)
337{ 190{
338 object *tmp, *last; 191 object *ob = pl->ob;
339 int got_one = 0, start_look = 0, end_look = 0;
340 char buf[MAX_BUF];
341 192
342 if (!pl->contr->socket->update_look) 193 if (!pl->ns->update_look)
343 { 194 {
344 LOG (llevDebug, "esrv_draw_look called when update_look was not set\n"); 195 LOG (llevDebug, "esrv_draw_look called when update_look was not set (player %s)\n", &ob->name);
345 return; 196 return;
346 } 197 }
347 else 198
348 pl->contr->socket->update_look = 0; 199 pl->ns->update_look = 0;
349 200
350 if (QUERY_FLAG (pl, FLAG_REMOVED) 201 if (QUERY_FLAG (ob, FLAG_REMOVED)
351 || !pl->map 202 || !ob->map
352 || pl->map->in_memory != MAP_IN_MEMORY 203 || ob->map->in_memory != MAP_ACTIVE
353 || out_of_map (pl->map, pl->x, pl->y)) 204 || out_of_map (ob->map, ob->x, ob->y))
354 return; 205 return;
355 206
356 for (tmp = GET_MAP_OB (pl->map, pl->x, pl->y); tmp && tmp->above; tmp = tmp->above)
357 ;
358
359 pl->contr->socket->send_packet ("delinv 0"); 207 pl->ns->send_packet ("delinv 0");
360 208
361 packet sl; 209 packet sl;
362 sl.printf ("item%d ", pl->contr->socket->itemcmd); 210 sl.printf ("item%d ", pl->ns->itemcmd);
363 211
364 sl << uint32 (0); 212 sl << uint32 (0);
365 213
366 if (!(pl->contr->socket->faces_sent[empty_face->number] & NS_FACESENT_FACE)) 214 int start_pos = pl->ns->look_position;
367 esrv_send_face (pl->contr->socket, empty_face->number, 0); 215 bool dirty = false;
368 216
369 if (pl->contr->socket->look_position) 217 mapspace &ms = ob->ms ();
218
219 // manage a ring buffer of the "last FLOORBOX_PAGESIZE" items and
220 // start from the top
221 object *items [FLOORBOX_PAGESIZE];
222 int item_idx = 0, item_cnt = 0;
223 int pos = 0;
224
225 // find our items by walking down
226 object *item = ms.top;
227
228 for (; item && item_cnt < FLOORBOX_PAGESIZE; item = item->below)
229 {
230 if (!item->client_visible ())
231 continue;
232
233 if (++pos < start_pos)
234 continue;
235
236 // record item
237 items [item_idx] = item; item_idx = item_idx < FLOORBOX_PAGESIZE ? item_idx + 1 : 0;
238 item_cnt++;
239
240 // stop at first floor
241 if (item->flag [FLAG_IS_FLOOR])
242 {
243 // we are finished, don't append "next group of items"
244 // by setting item to ms.bot it becomes zero which is checked after the while loop
245 item = ms.bot;
246 }
370 { 247 }
371 char buf[80];
372 snprintf (buf, 80, "Apply this to see %d previous items", FLOORBOX_PAGESIZE);
373 248
249 // see if there are more if we cared - we ignore invisible objects
250 if (item)
251 {
252 /* What we basically do is make a 'fake' object - when the user applies it,
253 * we notice the special tag the object has, and act accordingly.
254 */
374 sl << uint32 (0x80000000 | (pl->contr->socket->look_position - FLOORBOX_PAGESIZE)) 255 sl << uint32 (0x80000000 | (start_pos + FLOORBOX_PAGESIZE))
375 << uint32 (0) 256 << uint32 (0)
376 << sint32 (-1) 257 << uint32 ((uint32) - 1)
377 << uint32 (empty_face->number) 258 << uint32 (need_face_now (pl, FINGER_DOWN))
378 << data8 (buf) 259 << data8 ("Apply this to see the items below")
379 << uint16 (0) 260 << uint16 (0)
380 << uint8 (0) 261 << uint8 (0)
381 << uint32 (0); 262 << uint32 (0);
382 263
383 if (pl->contr->socket->itemcmd == 2) 264 if (pl->ns->itemcmd == 2)
384 sl << uint16 (0); 265 sl << uint16 (0);
385 }
386 266
387 for (last = NULL; tmp != last; tmp = tmp->below) 267 dirty = true;
388 { 268 }
389 object *head;
390 269
391 if (QUERY_FLAG (tmp, FLAG_IS_FLOOR) && !last) 270 // now send out all items in the ring buffer in reverse order
271 while (item_cnt)
272 {
273 --item_cnt;
274 item_idx = (item_idx ? item_idx : FLOORBOX_PAGESIZE) - 1;
275 object *item = items [item_idx];
276
277 add_object_to_socklist (*pl->ns, sl, item->head_ ());
278
279 dirty = true;
280
281 // if packet got too large, send it and begin a new one
282 if (sl.length () > MAXSOCKBUF - MAXITEMLEN)
392 { 283 {
393 last = tmp->below; /* assumes double floor mode */ 284 pl->ns->send_packet (sl);
394 if (last && QUERY_FLAG (last, FLAG_IS_FLOOR)) 285
395 last = last->below; 286 sl.reset ();
287 sl.printf ("item%d ", pl->ns->itemcmd);
288 sl << uint32 (0);
289
290 dirty = false;
396 } 291 }
397
398 if (LOOK_OBJ (tmp))
399 {
400 if (++start_look < pl->contr->socket->look_position)
401 continue;
402
403 end_look++;
404
405 if (end_look > FLOORBOX_PAGESIZE)
406 {
407 /* What we basically do is make a 'fake' object - when the user applies it,
408 * we notice the special tag the object has, and act accordingly.
409 */
410 sl << uint32 (0x80000000 | (pl->contr->socket->look_position + FLOORBOX_PAGESIZE))
411 << uint32 (0)
412 << uint32 ((uint32) - 1)
413 << uint32 (empty_face->number)
414 << data8 ("Apply this to see next group of items")
415 << uint16 (0)
416 << uint8 (0)
417 << uint32 (0);
418
419 if (pl->contr->socket->itemcmd == 2)
420 sl << uint16 (0);
421
422 break;
423 }
424
425 if (tmp->head)
426 head = tmp->head;
427 else
428 head = tmp;
429
430 add_object_to_socklist (*pl->contr->socket, sl, head);
431 got_one++;
432
433 if (sl.length () >= (MAXSOCKBUF - MAXITEMLEN))
434 {
435 pl->contr->socket->send_packet (sl);
436
437 sl.reset ();
438 sl.printf ("item%d ", pl->contr->socket->itemcmd);
439 sl << uint32 (0);
440 got_one = 0;
441 }
442 }
443 } 292 }
444 293
445 if (got_one) 294 if (start_pos)
295 {
296 sl << uint32 (0x80000000 | (start_pos - FLOORBOX_PAGESIZE))
297 << uint32 (0)
298 << sint32 (-1)
299 << uint32 (need_face_now (pl, FINGER_UP))
300 << data8 ("Apply this to see the items higher up")
301 << uint16 (0)
302 << uint8 (0)
303 << uint32 (0);
304
305 if (pl->ns->itemcmd == 2)
306 sl << uint16 (0);
307
308 dirty = true;
309 }
310
311 if (dirty)
446 pl->contr->socket->send_packet (sl); 312 pl->ns->send_packet (sl);
447
448} 313}
449 314
450/** 315/**
451 * Sends whole inventory. 316 * Sends whole inventory.
452 */ 317 */
453void 318void
454esrv_send_inventory (object *pl, object *op) 319esrv_send_inventory (object *pl, object *op)
455{ 320{
456 object *tmp; 321 if (!pl->contr->ns)//D
322 return;
323
457 int got_one = 0; 324 int got_one = 0;
458 325
459
460 pl->contr->socket->send_packet_printf ("delinv %d", op->count); 326 pl->contr->ns->send_packet_printf ("delinv %d", op->count);
461 327
462 packet sl; 328 packet sl;
463 sl.printf ("item%d ", pl->contr->socket->itemcmd); 329 sl.printf ("item%d ", pl->contr->ns->itemcmd);
464 330
465 sl << uint32 (op->count); 331 sl << uint32 (op->count);
466 332
467 for (tmp = op->inv; tmp; tmp = tmp->below) 333 for (object *tmp = op->inv; tmp; tmp = tmp->below)
468 { 334 {
469 object *head; 335 object *head;
470 336
471 if (tmp->head) 337 if (tmp->head)
472 head = tmp->head; 338 head = tmp->head;
473 else 339 else
474 head = tmp; 340 head = tmp;
475 341
476 if (LOOK_OBJ (head)) 342 if (head->client_visible ())
477 { 343 {
478 add_object_to_socklist (*pl->contr->socket, sl, head); 344 add_object_to_socklist (*pl->contr->ns, sl, head);
479 345
480 got_one++; 346 got_one++;
481 347
482 /* IT is possible for players to accumulate a huge amount of 348 /* IT is possible for players to accumulate a huge amount of
483 * items (especially with some of the bags out there) to 349 * items (especially with some of the bags out there) to
484 * overflow the buffer. IF so, send multiple item commands. 350 * overflow the buffer. IF so, send multiple item commands.
485 */ 351 */
486 if (sl.length () >= (MAXSOCKBUF - MAXITEMLEN)) 352 if (sl.length () > MAXSOCKBUF - MAXITEMLEN)
487 { 353 {
488 pl->contr->socket->send_packet (sl); 354 pl->contr->ns->send_packet (sl);
489 355
490 sl.reset (); 356 sl.reset ();
491 sl.printf ("item%d ", pl->contr->socket->itemcmd); 357 sl.printf ("item%d ", pl->contr->ns->itemcmd);
492 sl << uint32 (op->count); 358 sl << uint32 (op->count);
493 got_one = 0; 359 got_one = 0;
494 } 360 }
495 } /* If LOOK_OBJ() */ 361 }
496 } 362 }
497 363
498 if (got_one) 364 if (got_one)
499 pl->contr->socket->send_packet (sl); 365 pl->contr->ns->send_packet (sl);
500} 366}
501 367
502/** 368/**
503 * Updates object *op for player *pl. 369 * Updates object *op for player *pl.
504 * 370 *
505 * flags is a list of values to update 371 * flags is a list of values to update
506 * to the client (as defined in newclient.h - might as well use the 372 * to the client (as defined in newclient.h - might as well use the
507 * same value both places. 373 * same value both places.
508 */ 374 */
509
510void 375void
511esrv_update_item (int flags, object *pl, object *op) 376esrv_update_item (int flags, object *pl, object *op)
512{ 377{
513 /* If we have a request to send the player item, skip a few checks. */ 378 /* If we have a request to send the player item, skip a few checks. */
514 if (op != pl) 379 if (op != pl && !op->client_visible ())
515 {
516 if (!LOOK_OBJ (op))
517 return; 380 return;
518 /* we remove the check for op->env, because in theory, the object
519 * is hopefully in the same place, so the client should preserve
520 * order.
521 */
522 }
523 381
524 if (!QUERY_FLAG (op, FLAG_CLIENT_SENT)) 382 client *ns = pl->contr->ns;
525 { 383 if (!ns)
526 /* FLAG_CLIENT_SENT is debug only. We are using it to see where 384 return;
527 * this is happening - we can set a breakpoint here in the debugger
528 * and track back the call.
529 */
530 LOG (llevDebug, "We have not sent item %s (%d)\n", &op->name, op->count);
531 }
532 385
533 packet sl ("upditem"); 386 packet sl ("upditem");
534 387
535 sl << uint8 (flags); 388 sl << uint8 (flags);
536 389
537 if (op->head)
538 op = op->head; 390 op = op->head_ ();
539 391
540 sl << uint32 (op->count); 392 sl << uint32 (op->count);
541 393
542 if (flags & UPD_LOCATION) 394 if (flags & UPD_LOCATION)
543 sl << uint32 (op->env ? op->env->count : 0); 395 sl << uint32 (op->env ? op->env->count : 0);
545 if (flags & UPD_FLAGS) 397 if (flags & UPD_FLAGS)
546 sl << uint32 (query_flags (op)); 398 sl << uint32 (query_flags (op));
547 399
548 if (flags & UPD_WEIGHT) 400 if (flags & UPD_WEIGHT)
549 { 401 {
550 sint32 weight = WEIGHT (op); 402 sint32 weight = op->client_weight ();
551 403
552 sl << uint32 (QUERY_FLAG (op, FLAG_NO_PICK) ? -1 : weight); 404 if (op == pl)
553
554 if (pl == op)
555 op->contr->last_weight = weight; 405 ns->last_weight = weight;
406
407 sl << uint32 (weight);
556 } 408 }
557 409
558 if (flags & UPD_FACE) 410 if (flags & UPD_FACE)
559 { 411 {
560 if (!(pl->contr->socket->faces_sent[op->face->number] & NS_FACESENT_FACE)) 412 ns->send_face (op->face, -50);
561 esrv_send_face (pl->contr->socket, op->face->number, 0); 413 ns->flush_fx ();
562
563 sl << uint32 (op->face->number); 414 sl << uint32 (op->face);
564 } 415 }
565 416
566 if (flags & UPD_NAME) 417 if (flags & UPD_NAME)
567 { 418 {
568 int len; 419 int len;
569 const char *item_p; 420 const char *item_p;
570 char item_n[MAX_BUF]; 421 char item_n[127 * 2];
571 422
572 if (!op->custom_name) 423 if (!op->custom_name)
573 { 424 {
574 strncpy (item_n, query_base_name (op, 0), 127); 425 len = assign (item_n, query_base_name (op, 0), 127);
575 item_n[127] = 0;
576 len = strlen (item_n);
577 item_p = query_base_name (op, 1); 426 item_p = query_base_name (op, 1);
578 } 427 }
579 else 428 else
580 { 429 {
581 strncpy (item_n, op->custom_name, 127); 430 len = assign (item_n, &op->custom_name, 127);
582 item_n[127] = 0;
583 len = strlen (item_n);
584 item_p = op->custom_name; 431 item_p = &op->custom_name;
585 } 432 }
586 433
587 strncpy (item_n + len + 1, item_p, 127); 434 len += assign (item_n + len, item_p, 127);
588 item_n[254] = 0;
589 len += strlen (item_n + 1 + len) + 1;
590 435
591 sl << data8 (item_n, len); 436 sl << data8 (item_n, len);
592 } 437 }
593 438
594 if (flags & UPD_ANIM) 439 if (flags & UPD_ANIM)
602 { 447 {
603 if (op->anim_speed) 448 if (op->anim_speed)
604 anim_speed = op->anim_speed; 449 anim_speed = op->anim_speed;
605 else 450 else
606 { 451 {
607 if (FABS (op->speed) < 0.001) 452 if (fabs (op->speed) < 0.001)
608 anim_speed = 255; 453 anim_speed = 255;
609 else if (FABS (op->speed) >= 1.0) 454 else if (fabs (op->speed) >= 1.0)
610 anim_speed = 1; 455 anim_speed = 1;
611 else 456 else
612 anim_speed = (int) (1.0 / FABS (op->speed)); 457 anim_speed = (int) (1.0 / fabs (op->speed));
613 } 458 }
614 459
615 if (anim_speed > 255) 460 if (anim_speed > 255)
616 anim_speed = 255; 461 anim_speed = 255;
617 } 462 }
620 } 465 }
621 466
622 if (flags & UPD_NROF) 467 if (flags & UPD_NROF)
623 sl << uint32 (op->nrof); 468 sl << uint32 (op->nrof);
624 469
625 pl->contr->socket->send_packet (sl); 470 pl->contr->ns->send_packet (sl);
626} 471}
627 472
628/** 473/**
629 * Sends item's info to player. 474 * Sends item's info to player.
630 */ 475 */
631void 476void
632esrv_send_item (object *pl, object *op) 477esrv_send_item (object *pl, object *op)
633{ 478{
634 /* If this is not the player object, do some more checks */ 479 if (!pl->contr->ns)
635 if (op != pl)
636 {
637 /* We only send 'visibile' objects to the client */
638 if (!LOOK_OBJ (op))
639 return; 480 return;
640 /* if the item is on the ground, mark that the look needs to 481
641 * be updated. 482 /* We only send 'visible' objects to the client, and sometimes the player */
642 */ 483 if (!op->client_visible () && op->type != PLAYER)
643 if (!op->env)
644 {
645 pl->contr->socket->floorbox_update ();
646 return; 484 return;
647 }
648 }
649 485
650 packet sl; 486 packet sl;
651 487
652 sl.printf ("item%d ", pl->contr->socket->itemcmd); 488 sl.printf ("item%d ", pl->contr->ns->itemcmd);
653 489
654 if (op->head)
655 op = op->head; 490 op = op->head_ ();
656 491
657 sl << uint32 (op->env ? op->env->count : 0); 492 sl << uint32 (op->env ? op->env->count : 0);
658 493
659 add_object_to_socklist (*pl->contr->socket, sl, op); 494 add_object_to_socklist (*pl->contr->ns, sl, op);
660 495
661 pl->contr->socket->send_packet (sl); 496 pl->contr->ns->send_packet (sl);
662 SET_FLAG (op, FLAG_CLIENT_SENT);
663} 497}
664 498
665/** 499/**
666 * Tells the client to delete an item. Uses the item 500 * Tells the client to delete an item.
667 * command with a -1 location.
668 */ 501 */
669
670void 502void
671esrv_del_item (player *pl, int tag) 503esrv_del_item (player *pl, int tag)
672{ 504{
505 if (!pl->ns)
506 return;
507
673 packet sl ("delitem"); 508 packet sl ("delitem");
674 509
675 sl << uint32 (tag); 510 sl << uint32 (tag);
676 511
677 pl->socket->send_packet (sl); 512 pl->ns->send_packet (sl);
678} 513}
679 514
680 515
681/******************************************************************************* 516/*******************************************************************************
682 * 517 *
686 521
687/** 522/**
688 * Takes a player and object count (tag) and returns the actual object 523 * Takes a player and object count (tag) and returns the actual object
689 * pointer, or null if it can't be found. 524 * pointer, or null if it can't be found.
690 */ 525 */
691 526static object *
692object *
693esrv_get_ob_from_count (object *pl, tag_t count) 527esrv_get_ob_from_count (object *pl, tag_t count)
694{ 528{
695 object *op, *tmp;
696
697 if (pl->count == count) 529 if (pl->count == count)
698 return pl; 530 return pl;
699 531
700 for (op = pl->inv; op; op = op->below) 532 for (object *op = pl->inv; op; op = op->below)
701 if (op->count == count) 533 if (op->count == count)
702 return op; 534 return op;
703 else if (op->type == CONTAINER && pl->container == op) 535 else if (op->type == CONTAINER && pl->container_ () == op)
704 for (tmp = op->inv; tmp; tmp = tmp->below) 536 for (object *tmp = op->inv; tmp; tmp = tmp->below)
705 if (tmp->count == count) 537 if (tmp->count == count)
706 return tmp; 538 return tmp;
707 539
708 for (op = GET_MAP_OB (pl->map, pl->x, pl->y); op; op = op->above) 540 for (object *op = GET_MAP_OB (pl->map, pl->x, pl->y); op; op = op->above)
709 if (op->head && op->head->count == count) 541 if (op->head && op->head->count == count)
710 return op; 542 return op;
711 else if (op->count == count) 543 else if (op->count == count)
712 return op; 544 return op;
713 else if (op->type == CONTAINER && pl->container == op) 545 else if (op->type == CONTAINER && pl->container_ () == op)
714 for (tmp = op->inv; tmp; tmp = tmp->below) 546 for (object *tmp = op->inv; tmp; tmp = tmp->below)
715 if (tmp->count == count) 547 if (tmp->count == count)
716 return tmp; 548 return tmp;
717 549
550#if 0
551 /* If the high bit is set, player examined a pseudo object. */
552 if (count & 0x80000000)
553 return 0;
554#endif
555
718 return 0; 556 return 0;
719} 557}
720 558
721
722/** Client wants to examine some object. So lets do so. */ 559/** Client wants to examine some object. So lets do so. */
723void 560void
724ExamineCmd (char *buf, int len, player *pl) 561ExamineCmd (char *buf, int len, player *pl)
725{ 562{
726 tag_t tag = atoi (buf); 563 tag_t tag = atoi (buf);
727
728 /* If the high bit is set, player examined a pseudo object. */
729 if (tag & 0x80000000)
730 return;
731
732 object *op = esrv_get_ob_from_count (pl->ob, tag);
733
734 if (!op)
735 {
736 LOG (llevDebug, "Player '%s' tried to examine the unknown object (%ld)\n", &pl->ob->name, tag);
737 return;
738 }
739
740 examine (pl->ob, op);
741}
742
743/** Client wants to apply some object. Lets do so. */
744void
745ApplyCmd (char *buf, int len, player *pl)
746{
747 tag_t tag = atoi (buf);
748
749 /* sort of a hack, but if the player saves and the player then manually
750 * applies a savebed (or otherwise tries to do stuff), we run into trouble.
751 */
752 if (QUERY_FLAG (pl->ob, FLAG_REMOVED))
753 return;
754 564
755 /* If the high bit is set, player applied a pseudo object. */ 565 /* If the high bit is set, player applied a pseudo object. */
756 if (tag & 0x80000000) 566 if (tag & 0x80000000)
757 { 567 {
758 pl->socket->look_position = tag & 0x7fffffff; 568 pl->ns->look_position = tag & 0x7fffffff;
759 pl->socket->floorbox_update (); 569 pl->ns->floorbox_update ();
570 return;
571 }
572
573 object *op = esrv_get_ob_from_count (pl->ob, tag);
574
575 if (!op)
576 {
577 LOG (llevDebug, "Player '%s' tried to examine the unknown object (%ld)\n", &pl->ob->name, tag);
578 return;
579 }
580
581 examine (pl->ob, op);
582}
583
584/** Client wants to examine some object. So lets do so. */
585void
586ExCmd (char *buf, int len, player *pl)
587{
588 tag_t tag = atoi (buf);
589
590 if (object *op = esrv_get_ob_from_count (pl->ob, tag))
591 {
592 std::string s = op->describe (pl->ob);
593
594 packet sl ("ex");
595 sl << ber32 (tag)
596 << data (*pl->expand_cfpod (s.c_str ()));
597
598 pl->ns->send_packet (sl);
599 }
600}
601
602/** Client wants to apply some object. Lets do so. */
603void
604ApplyCmd (char *buf, int len, player *pl)
605{
606 tag_t tag = atoi (buf);
607
608 /* If the high bit is set, player applied a pseudo object. */
609 if (tag & 0x80000000)
610 {
611 pl->ns->look_position = tag & 0x7fffffff;
612 pl->ns->floorbox_update ();
760 return; 613 return;
761 } 614 }
762 615
763 object *op = esrv_get_ob_from_count (pl->ob, tag); 616 object *op = esrv_get_ob_from_count (pl->ob, tag);
764 617
779 tag_t tag = net_uint32 ((uint8 *)data + 1); 632 tag_t tag = net_uint32 ((uint8 *)data + 1);
780 object *op = esrv_get_ob_from_count (pl->ob, tag); 633 object *op = esrv_get_ob_from_count (pl->ob, tag);
781 634
782 if (!op) 635 if (!op)
783 { 636 {
784 new_draw_info (NDI_UNIQUE, 0, pl->ob, "Could not find object to lock/unlock"); 637 pl->failmsg ("Could not find object to lock/unlock");
785 return; 638 return;
786 } 639 }
787 640
788 if (!flag) 641 if (!flag)
789 CLEAR_FLAG (op, FLAG_INV_LOCKED); 642 CLEAR_FLAG (op, FLAG_INV_LOCKED);
800 tag_t tag = net_uint32 ((uint8 *)data); 653 tag_t tag = net_uint32 ((uint8 *)data);
801 object *op = esrv_get_ob_from_count (pl->ob, tag); 654 object *op = esrv_get_ob_from_count (pl->ob, tag);
802 655
803 if (!op) 656 if (!op)
804 { 657 {
805 new_draw_info (NDI_UNIQUE, 0, pl->ob, "Could not find object to mark"); 658 pl->failmsg ("Could not find object to mark");
806 return; 659 return;
807 } 660 }
808 661
809 pl->mark = op; 662 pl->mark = op;
810 new_draw_info_format (NDI_UNIQUE, 0, pl->ob, "Marked item %s", query_name (op)); 663 pl->ob->statusmsg (format ("Marked item %s", query_name (op)));
811} 664}
812 665
813/** 666/**
814 * look_at prints items on the specified square. 667 * look_at prints items on the specified square.
815 * 668 *
816 * [ removed EARTHWALL check and added check for containers inventory. 669 * [ removed EARTHWALL check and added check for containers inventory.
817 * Tero.Haatanen@lut.fi ] 670 * Tero.Haatanen@lut.fi ]
818 */ 671 */
819void 672static void
820look_at (object *op, int dx, int dy) 673look_at (player *pl, int dx, int dy)
821{ 674{
822 object *tmp; 675 dynbuf_text &buf = msg_dynbuf; buf.clear ();
823 int flag = 0; 676 object *ob = pl->ob;
824 sint16 x, y;
825 maptile *m;
826 677
827 x = op->x + dx; 678 if (!pl->observe->map)
828 y = op->y + dy;
829
830 if (out_of_map (op->map, x, y))
831 return; 679 return;
832 680
833 m = get_map_from_coord (op->map, &x, &y); 681 bool wiz = ob->flag [FLAG_WIZ] || ob->flag [FLAG_WIZLOOK];
834 if (!m) 682
683 mapxy pos (pl->observe);
684 pos.move (dx, dy);
685
686 if (wiz)
687 {
688 if (pos.normalise ())
689 {
690 mapspace &ms = *pos;
691 ms.update ();
692
693 buf.printf (" map: %s%+d%+d (%+d%+d)\n"
694 " smell %u/%u; flags %x; light %d; block %x; slow %x; on %x; off %x; volume %lld\n\n",
695 &pos.m->path, pos.x, pos.y, dx, dy,
696 (unsigned int)ms.smell, (unsigned int)mapspace::smellcount, ms.flags (), ms.light,
697 ms.move_block, ms.move_slow, ms.move_on, ms.move_off, (long long)ms.volume ()
698 );
699 }
700 else
701 buf << "off-map\n\n";
702 }
703
704 int darkness = pl->blocked_los (dx, dy);
705
706 if (darkness == LOS_BLOCKED)
707 buf << "You cannot see that place from your position. H<Something is in between or it is too far away.>";
708 else if (darkness == LOS_MAX)
709 buf << "That place is too dark to see anything. H<Move nearer or find better lighting.>";
710 else
711 {
712 if (pos.normalise ())
713 for (object *tmp = pos->top; tmp; tmp = tmp->below)
714 {
715 if (tmp->invisible && !QUERY_FLAG (ob, FLAG_WIZ))
716 continue;
717
718 if (wiz)
719 buf.printf (" - %s (%d).\n", query_name (tmp), tmp->count);
720 else
721 buf.printf (" - %s.\n", query_name (tmp));
722
723 object *head = tmp->head_ ();
724
725 if (head->inv)
726 if ((head->type != CONTAINER && head->type != FLESH)
727 || QUERY_FLAG (ob, FLAG_WIZ))
728 buf << head->query_inventory (ob, " ");
729
730 if (QUERY_FLAG (tmp, FLAG_IS_FLOOR) && !wiz) /* don't continue under the floor */
731 break;
732 }
733 }
734
735 if (buf.empty ())
736 buf << "You see nothing there.";
737
738 pl->infobox (MSG_CHANNEL ("lookat"), buf);
739}
740
741/** Client wants to look at some object. Lets do so. */
742void
743LookAt (char *buf, int len, player *pl)
744{
745 char *cp;
746
747 int dx = atoi (buf);
748 if (!(cp = strchr (buf, ' ')))
835 return; 749 return;
836 750
837 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL && tmp->above != NULL; tmp = tmp->above);
838
839 for (; tmp != NULL; tmp = tmp->below)
840 {
841 if (tmp->invisible && !QUERY_FLAG (op, FLAG_WIZ))
842 continue;
843
844 if (!flag)
845 {
846 if (dx || dy)
847 new_draw_info (NDI_UNIQUE, 0, op, "There you see:");
848 else
849 {
850 clear_win_info (op);
851 new_draw_info (NDI_UNIQUE, 0, op, "You see:");
852 }
853 flag = 1;
854 }
855
856 if (QUERY_FLAG (op, FLAG_WIZ))
857 new_draw_info_format (NDI_UNIQUE, 0, op, "- %s (%d).", query_name (tmp), tmp->count);
858 else
859 new_draw_info_format (NDI_UNIQUE, 0, op, "- %s.", query_name (tmp));
860
861 if (((tmp->inv != NULL || (tmp->head && tmp->head->inv)) &&
862 (tmp->type != CONTAINER && tmp->type != FLESH)) || QUERY_FLAG (op, FLAG_WIZ))
863 inventory (op, tmp->head == NULL ? tmp : tmp->head);
864
865 if (QUERY_FLAG (tmp, FLAG_IS_FLOOR) && !QUERY_FLAG (op, FLAG_WIZ)) /* don't continue under the floor */
866 break;
867 }
868
869 if (!flag)
870 {
871 if (dx || dy)
872 new_draw_info (NDI_UNIQUE, 0, op, "You see nothing there.");
873 else
874 new_draw_info (NDI_UNIQUE, 0, op, "You see nothing.");
875 }
876}
877
878
879
880/** Client wants to look at some object. Lets do so. */
881void
882LookAt (char *buf, int len, player *pl)
883{
884 int dx, dy;
885 char *cp;
886
887 dx = atoi (buf);
888 if (!(cp = strchr (buf, ' ')))
889 {
890 return;
891 }
892 dy = atoi (cp); 751 int dy = atoi (cp);
893 752
894 if (FABS (dx) > pl->socket->mapx / 2 || FABS (dy) > pl->socket->mapy / 2)
895 return;
896
897 if (pl->blocked_los[dx + pl->socket->mapx / 2][dy + pl->socket->mapy / 2])
898 return;
899
900 look_at (pl->ob, dx, dy); 753 look_at (pl, dx, dy);
901} 754}
902 755
903/** Move an object to a new location */ 756/** Move an object to a new location */
904void 757void
905esrv_move_object (object *pl, tag_t to, tag_t tag, long nrof) 758esrv_move_object (object *pl, tag_t to, tag_t tag, long nrof)
906{ 759{
907 object *op, *env;
908
909 op = esrv_get_ob_from_count (pl, tag); 760 object *op = esrv_get_ob_from_count (pl, tag);
910 if (!op) 761 if (!op)
911 { 762 {
912 LOG (llevDebug, "Player '%s' tried to move an unknown object (%ld)\n", &pl->name, tag); 763 LOG (llevDebug, "Player '%s' tried to move an unknown object (%ld)\n", &pl->name, tag);
913 return; 764 return;
914 } 765 }
921 /* If it is an active container, then we should drop all objects 772 /* If it is an active container, then we should drop all objects
922 * in the container and not the container itself. 773 * in the container and not the container itself.
923 */ 774 */
924 if (op->inv && QUERY_FLAG (op, FLAG_APPLIED)) 775 if (op->inv && QUERY_FLAG (op, FLAG_APPLIED))
925 { 776 {
926 object *current, *next; 777 int cnt = MAX_ITEM_PER_DROP;
927 778
928 for (current = op->inv; current != NULL; current = next) 779 for (object *current = op->inv; current && cnt--; )
929 { 780 {
930 next = current->below; 781 object *next = current->below;
931 drop_object (pl, current, 0); 782 drop_object (pl, current, 0);
783 current = next;
932 } 784 }
933 785
934 esrv_update_item (UPD_WEIGHT, pl, op); 786 if (cnt <= 0)
787 op->failmsg ("Only dropped some items, can't drop that many items at once.");
935 } 788 }
936 else 789 else
937 drop_object (pl, op, nrof); 790 drop_object (pl, op, nrof);
938 791
939 return; 792 return;
947 pl->contr->count = nrof; 800 pl->contr->count = nrof;
948 pick_up (pl, op); 801 pick_up (pl, op);
949 return; 802 return;
950 } 803 }
951 804
952 env = esrv_get_ob_from_count (pl, to); 805 object *env = esrv_get_ob_from_count (pl, to);
953 if (!env) 806 if (!env)
954 { 807 {
955 LOG (llevDebug, "Player '%s' tried to move object to the unknown location (%d)\n", &pl->name, to); 808 LOG (llevDebug, "Player '%s' tried to move object to the unknown location (%d)\n", &pl->name, to);
956 return; 809 return;
957 } 810 }
959 /* put_object_in_sack presumes that necessary sanity checking 812 /* put_object_in_sack presumes that necessary sanity checking
960 * has already been done (eg, it can be picked up and fits in 813 * has already been done (eg, it can be picked up and fits in
961 * in a sack, so check for those things. We should also check 814 * in a sack, so check for those things. We should also check
962 * an make sure env is in fact a container for that matter. 815 * an make sure env is in fact a container for that matter.
963 */ 816 */
964 if (env->type == CONTAINER && can_pick (pl, op) && sack_can_hold (pl, env, op, nrof)) 817 if (env->type == CONTAINER
818 && can_pick (pl, op)
819 && sack_can_hold (pl, env, op, nrof))
965 put_object_in_sack (pl, env, op, nrof); 820 put_object_in_sack (pl, env, op, nrof);
966} 821}
967 822

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines