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