… | |
… | |
45 | /******************************************************************************* |
45 | /******************************************************************************* |
46 | * |
46 | * |
47 | * Functions related to sending object data to the client. |
47 | * Functions related to sending object data to the client. |
48 | * |
48 | * |
49 | ******************************************************************************/ |
49 | ******************************************************************************/ |
50 | |
|
|
51 | /** |
|
|
52 | * Adds string to socklist. |
|
|
53 | * |
|
|
54 | * This is a simple function that we use a lot here. It basically |
|
|
55 | * adds the specified buffer into the socklist, but prepends a |
|
|
56 | * single byte in length. If the data is longer than that byte, it is |
|
|
57 | * truncated approprately. |
|
|
58 | */ |
|
|
59 | inline void |
|
|
60 | add_stringlen_to_sockbuf (const char *buf, SockList * sl) |
|
|
61 | { |
|
|
62 | int len; |
|
|
63 | |
|
|
64 | len = strlen (buf); |
|
|
65 | if (len > 255) |
|
|
66 | len = 255; |
|
|
67 | SockList_AddChar (sl, (char) len); |
|
|
68 | strncpy ((char *) sl->buf + sl->len, buf, len); |
|
|
69 | sl->len += len; |
|
|
70 | } |
|
|
71 | |
50 | |
72 | /** |
51 | /** |
73 | * This is a similar to query_name, but returns flags |
52 | * This is a similar to query_name, but returns flags |
74 | * to be sended to client. |
53 | * to be sended to client. |
75 | */ |
54 | */ |
… | |
… | |
234 | || !pl->map |
213 | || !pl->map |
235 | || pl->map->in_memory != MAP_IN_MEMORY |
214 | || pl->map->in_memory != MAP_IN_MEMORY |
236 | || out_of_map (pl->map, pl->x, pl->y)) |
215 | || out_of_map (pl->map, pl->x, pl->y)) |
237 | return; |
216 | return; |
238 | |
217 | |
239 | for (tmp = get_map_ob (pl->map, pl->x, pl->y); tmp && tmp->above; tmp = tmp->above); |
218 | for (tmp = get_map_ob (pl->map, pl->x, pl->y); tmp && tmp->above; tmp = tmp->above) |
|
|
219 | ; |
240 | |
220 | |
241 | SockList sl (MAXSOCKBUF); |
221 | SockList sl (MAXSOCKBUF); |
242 | |
222 | |
243 | Write_String_To_Socket (&pl->contr->socket, "delinv 0", sizeof ("delinv 0") - 1); |
223 | Write_String_To_Socket (&pl->contr->socket, "delinv 0", sizeof ("delinv 0") - 1); |
244 | |
224 | |
… | |
… | |
287 | if (end_look > NUM_LOOK_OBJECTS) |
267 | if (end_look > NUM_LOOK_OBJECTS) |
288 | { |
268 | { |
289 | /* What we basically do is make a 'fake' object - when the user applies it, |
269 | /* What we basically do is make a 'fake' object - when the user applies it, |
290 | * we notice the special tag the object has, and act accordingly. |
270 | * we notice the special tag the object has, and act accordingly. |
291 | */ |
271 | */ |
292 | SockList_AddInt (&sl, 0x80000000 | (pl->contr->socket.look_position + NUM_LOOK_OBJECTS)); |
272 | sl << uint32 (0x80000000 | (pl->contr->socket.look_position + NUM_LOOK_OBJECTS)) |
293 | SockList_AddInt (&sl, 0); |
273 | << uint32 (0) |
294 | SockList_AddInt (&sl, (uint32) - 1); |
274 | << uint32 ((uint32) - 1) |
295 | SockList_AddInt (&sl, empty_face->number); |
275 | << uint32 (empty_face->number); |
|
|
276 | |
296 | sprintf (buf, "Click here to see next group of items"); |
277 | sl.printf ("Click here to see next group of items"); |
297 | add_stringlen_to_sockbuf (buf, &sl); |
278 | |
298 | SockList_AddShort (&sl, 0); |
279 | sl << uint16 (0) |
299 | SockList_AddChar (&sl, 0); |
280 | << uint8 (0) |
300 | SockList_AddInt (&sl, 0); |
281 | << uint32 (0); |
|
|
282 | |
301 | if (pl->contr->socket.itemcmd == 2) |
283 | if (pl->contr->socket.itemcmd == 2) |
302 | SockList_AddShort (&sl, 0); |
284 | sl << uint16 (0); |
|
|
285 | |
303 | break; |
286 | break; |
304 | } |
287 | } |
|
|
288 | |
305 | if (tmp->head) |
289 | if (tmp->head) |
306 | head = tmp->head; |
290 | head = tmp->head; |
307 | else |
291 | else |
308 | head = tmp; |
292 | head = tmp; |
309 | |
293 | |
… | |
… | |
311 | got_one++; |
295 | got_one++; |
312 | |
296 | |
313 | if (sl.len >= (MAXSOCKBUF - MAXITEMLEN)) |
297 | if (sl.len >= (MAXSOCKBUF - MAXITEMLEN)) |
314 | { |
298 | { |
315 | Send_With_Handling (&pl->contr->socket, &sl); |
299 | Send_With_Handling (&pl->contr->socket, &sl); |
|
|
300 | |
|
|
301 | sl.reset (); |
316 | sprintf ((char *) sl.buf, "item%d ", pl->contr->socket.itemcmd); |
302 | sl.printf ("item%d ", pl->contr->socket.itemcmd); |
317 | sl.len = strlen ((char *) sl.buf); |
303 | sl << uint32 (0); |
318 | SockList_AddInt (&sl, 0); |
|
|
319 | got_one = 0; |
304 | got_one = 0; |
320 | } |
305 | } |
321 | } /* If LOOK_OBJ() */ |
306 | } /* If LOOK_OBJ() */ |
322 | } |
307 | } |
323 | |
308 | |
… | |
… | |
333 | void |
318 | void |
334 | esrv_send_inventory (object *pl, object *op) |
319 | esrv_send_inventory (object *pl, object *op) |
335 | { |
320 | { |
336 | object *tmp; |
321 | object *tmp; |
337 | int got_one = 0; |
322 | int got_one = 0; |
338 | SockList sl; |
|
|
339 | |
323 | |
340 | sl.buf = (unsigned char *) malloc (MAXSOCKBUF); |
324 | SockList sl (MAXSOCKBUF); |
341 | |
325 | |
342 | sprintf ((char *) sl.buf, "delinv %d", op->count); |
326 | sl.printf ("delinv %d", op->count); |
343 | sl.len = strlen ((char *) sl.buf); |
|
|
344 | Send_With_Handling (&pl->contr->socket, &sl); |
327 | Send_With_Handling (&pl->contr->socket, &sl); |
345 | |
328 | |
|
|
329 | sl.reset (); |
346 | sprintf ((char *) sl.buf, "item%d ", pl->contr->socket.itemcmd); |
330 | sl.printf ("item%d ", pl->contr->socket.itemcmd); |
347 | sl.len = strlen ((char *) sl.buf); |
|
|
348 | |
331 | |
349 | SockList_AddInt (&sl, op->count); |
332 | sl << uint32 (op->count); |
350 | |
333 | |
351 | for (tmp = op->inv; tmp; tmp = tmp->below) |
334 | for (tmp = op->inv; tmp; tmp = tmp->below) |
352 | { |
335 | { |
353 | object *head; |
336 | object *head; |
354 | |
337 | |
… | |
… | |
370 | if (sl.len >= (MAXSOCKBUF - MAXITEMLEN)) |
353 | if (sl.len >= (MAXSOCKBUF - MAXITEMLEN)) |
371 | { |
354 | { |
372 | Send_With_Handling (&pl->contr->socket, &sl); |
355 | Send_With_Handling (&pl->contr->socket, &sl); |
373 | sprintf ((char *) sl.buf, "item%d ", pl->contr->socket.itemcmd); |
356 | sprintf ((char *) sl.buf, "item%d ", pl->contr->socket.itemcmd); |
374 | sl.len = strlen ((char *) sl.buf); |
357 | sl.len = strlen ((char *) sl.buf); |
375 | SockList_AddInt (&sl, op->count); |
358 | sl << uint32 (op->count); |
376 | got_one = 0; |
359 | got_one = 0; |
377 | } |
360 | } |
378 | } /* If LOOK_OBJ() */ |
361 | } /* If LOOK_OBJ() */ |
379 | } |
362 | } |
|
|
363 | |
380 | if (got_one) |
364 | if (got_one) |
381 | Send_With_Handling (&pl->contr->socket, &sl); |
365 | Send_With_Handling (&pl->contr->socket, &sl); |
|
|
366 | |
382 | sl.free (); |
367 | sl.free (); |
383 | } |
368 | } |
384 | |
369 | |
385 | /** |
370 | /** |
386 | * Updates object *op for player *pl. |
371 | * Updates object *op for player *pl. |
… | |
… | |
391 | */ |
376 | */ |
392 | |
377 | |
393 | void |
378 | void |
394 | esrv_update_item (int flags, object *pl, object *op) |
379 | esrv_update_item (int flags, object *pl, object *op) |
395 | { |
380 | { |
396 | SockList sl; |
|
|
397 | |
|
|
398 | /* If we have a request to send the player item, skip a few checks. */ |
381 | /* If we have a request to send the player item, skip a few checks. */ |
399 | if (op != pl) |
382 | if (op != pl) |
400 | { |
383 | { |
401 | if (!LOOK_OBJ (op)) |
384 | if (!LOOK_OBJ (op)) |
402 | return; |
385 | return; |
… | |
… | |
413 | * and track back the call. |
396 | * and track back the call. |
414 | */ |
397 | */ |
415 | LOG (llevDebug, "We have not sent item %s (%d)\n", &op->name, op->count); |
398 | LOG (llevDebug, "We have not sent item %s (%d)\n", &op->name, op->count); |
416 | } |
399 | } |
417 | |
400 | |
418 | sl.buf = (unsigned char *) malloc (MAXSOCKBUF); |
401 | SockList sl (MAXSOCKBUF); |
419 | |
402 | |
420 | strcpy ((char *) sl.buf, "upditem "); |
403 | sl << "upditem " |
421 | sl.len = strlen ((char *) sl.buf); |
404 | << uint8 (flags); |
422 | |
|
|
423 | SockList_AddChar (&sl, (char) flags); |
|
|
424 | |
405 | |
425 | if (op->head) |
406 | if (op->head) |
426 | op = op->head; |
407 | op = op->head; |
427 | |
408 | |
428 | SockList_AddInt (&sl, op->count); |
409 | sl << uint32 (op->count); |
429 | |
410 | |
430 | if (flags & UPD_LOCATION) |
411 | if (flags & UPD_LOCATION) |
431 | SockList_AddInt (&sl, op->env ? op->env->count : 0); |
412 | sl << uint32 (op->env ? op->env->count : 0); |
432 | |
413 | |
433 | if (flags & UPD_FLAGS) |
414 | if (flags & UPD_FLAGS) |
434 | SockList_AddInt (&sl, query_flags (op)); |
415 | sl << uint32 (query_flags (op)); |
435 | |
416 | |
436 | if (flags & UPD_WEIGHT) |
417 | if (flags & UPD_WEIGHT) |
437 | { |
418 | { |
438 | sint32 weight = WEIGHT (op); |
419 | sint32 weight = WEIGHT (op); |
439 | |
420 | |
440 | SockList_AddInt (&sl, QUERY_FLAG (op, FLAG_NO_PICK) ? -1 : weight); |
421 | sl << uint32 (QUERY_FLAG (op, FLAG_NO_PICK) ? -1 : weight); |
|
|
422 | |
441 | if (pl == op) |
423 | if (pl == op) |
442 | { |
|
|
443 | op->contr->last_weight = weight; |
424 | op->contr->last_weight = weight; |
444 | } |
|
|
445 | } |
425 | } |
446 | |
426 | |
447 | if (flags & UPD_FACE) |
427 | if (flags & UPD_FACE) |
448 | { |
428 | { |
449 | if (!(pl->contr->socket.faces_sent[op->face->number] & NS_FACESENT_FACE)) |
429 | if (!(pl->contr->socket.faces_sent[op->face->number] & NS_FACESENT_FACE)) |
450 | esrv_send_face (&pl->contr->socket, op->face->number, 0); |
430 | esrv_send_face (&pl->contr->socket, op->face->number, 0); |
451 | SockList_AddInt (&sl, op->face->number); |
431 | |
|
|
432 | sl << uint32 (op->face->number); |
452 | } |
433 | } |
453 | |
434 | |
454 | if (flags & UPD_NAME) |
435 | if (flags & UPD_NAME) |
455 | { |
436 | { |
456 | int len; |
437 | int len; |
… | |
… | |
473 | } |
454 | } |
474 | |
455 | |
475 | strncpy (item_n + len + 1, item_p, 127); |
456 | strncpy (item_n + len + 1, item_p, 127); |
476 | item_n[254] = 0; |
457 | item_n[254] = 0; |
477 | len += strlen (item_n + 1 + len) + 1; |
458 | len += strlen (item_n + 1 + len) + 1; |
478 | SockList_AddChar (&sl, (char) len); |
459 | |
479 | memcpy (sl.buf + sl.len, item_n, len); |
460 | sl << data8 (item_n, len); |
480 | sl.len += len; |
|
|
481 | } |
461 | } |
482 | |
462 | |
483 | if (flags & UPD_ANIM) |
463 | if (flags & UPD_ANIM) |
484 | SockList_AddShort (&sl, op->animation_id); |
464 | sl << uint16 (op->animation_id); |
485 | |
465 | |
486 | if (flags & UPD_ANIMSPEED) |
466 | if (flags & UPD_ANIMSPEED) |
487 | { |
467 | { |
488 | int anim_speed = 0; |
468 | int anim_speed = 0; |
489 | |
469 | |
… | |
… | |
498 | else if (FABS (op->speed) >= 1.0) |
478 | else if (FABS (op->speed) >= 1.0) |
499 | anim_speed = 1; |
479 | anim_speed = 1; |
500 | else |
480 | else |
501 | anim_speed = (int) (1.0 / FABS (op->speed)); |
481 | anim_speed = (int) (1.0 / FABS (op->speed)); |
502 | } |
482 | } |
|
|
483 | |
503 | if (anim_speed > 255) |
484 | if (anim_speed > 255) |
504 | anim_speed = 255; |
485 | anim_speed = 255; |
505 | } |
486 | } |
506 | SockList_AddChar (&sl, (char) anim_speed); |
487 | |
|
|
488 | sl << uint8 (anim_speed); |
507 | } |
489 | } |
|
|
490 | |
508 | if (flags & UPD_NROF) |
491 | if (flags & UPD_NROF) |
509 | SockList_AddInt (&sl, op->nrof); |
492 | sl << uint32 (op->nrof); |
510 | |
493 | |
511 | Send_With_Handling (&pl->contr->socket, &sl); |
494 | Send_With_Handling (&pl->contr->socket, &sl); |
512 | sl.free (); |
495 | sl.free (); |
513 | } |
496 | } |
514 | |
497 | |
… | |
… | |
516 | * Sends item's info to player. |
499 | * Sends item's info to player. |
517 | */ |
500 | */ |
518 | void |
501 | void |
519 | esrv_send_item (object *pl, object *op) |
502 | esrv_send_item (object *pl, object *op) |
520 | { |
503 | { |
521 | SockList sl; |
|
|
522 | |
|
|
523 | /* If this is not the player object, do some more checks */ |
504 | /* If this is not the player object, do some more checks */ |
524 | if (op != pl) |
505 | if (op != pl) |
525 | { |
506 | { |
526 | /* We only send 'visibile' objects to the client */ |
507 | /* We only send 'visibile' objects to the client */ |
527 | if (!LOOK_OBJ (op)) |
508 | if (!LOOK_OBJ (op)) |
… | |
… | |
534 | pl->contr->socket.update_look = 1; |
515 | pl->contr->socket.update_look = 1; |
535 | return; |
516 | return; |
536 | } |
517 | } |
537 | } |
518 | } |
538 | |
519 | |
539 | sl.buf = (unsigned char *) malloc (MAXSOCKBUF); |
520 | SockList sl (MAXSOCKBUF); |
540 | |
521 | |
541 | sprintf ((char *) sl.buf, "item%d ", pl->contr->socket.itemcmd); |
522 | sl.printf ("item%d ", pl->contr->socket.itemcmd); |
542 | sl.len = strlen ((char *) sl.buf); |
|
|
543 | |
523 | |
544 | if (op->head) |
524 | if (op->head) |
545 | op = op->head; |
525 | op = op->head; |
546 | |
526 | |
547 | SockList_AddInt (&sl, op->env ? op->env->count : 0); |
527 | sl << uint32 (op->env ? op->env->count : 0); |
548 | |
528 | |
549 | add_object_to_socklist (pl->contr->socket, sl, op); |
529 | add_object_to_socklist (pl->contr->socket, sl, op); |
550 | |
530 | |
551 | Send_With_Handling (&pl->contr->socket, &sl); |
531 | Send_With_Handling (&pl->contr->socket, &sl); |
552 | SET_FLAG (op, FLAG_CLIENT_SENT); |
532 | SET_FLAG (op, FLAG_CLIENT_SENT); |
… | |
… | |
560 | */ |
540 | */ |
561 | |
541 | |
562 | void |
542 | void |
563 | esrv_del_item (player *pl, int tag) |
543 | esrv_del_item (player *pl, int tag) |
564 | { |
544 | { |
565 | SockList sl; |
545 | SockList sl (MAXSOCKBUF); |
566 | |
546 | |
567 | sl.buf = (unsigned char *) malloc (MAXSOCKBUF); |
547 | sl << "delitem " |
568 | |
548 | << uint32 (tag); |
569 | strcpy ((char *) sl.buf, "delitem "); |
|
|
570 | sl.len = strlen ((char *) sl.buf); |
|
|
571 | SockList_AddInt (&sl, tag); |
|
|
572 | |
549 | |
573 | Send_With_Handling (&pl->socket, &sl); |
550 | Send_With_Handling (&pl->socket, &sl); |
574 | sl.free (); |
551 | sl.free (); |
575 | } |
552 | } |
576 | |
553 | |
… | |
… | |
810 | return; |
787 | return; |
811 | } |
788 | } |
812 | |
789 | |
813 | if (!to) |
790 | if (!to) |
814 | { /* drop it to the ground */ |
791 | { /* drop it to the ground */ |
815 | |
|
|
816 | /* LOG(llevDebug, "Drop it on the ground.\n");*/ |
|
|
817 | |
|
|
818 | if (op->map && !op->env) |
792 | if (op->map && !op->env) |
819 | { |
|
|
820 | |
|
|
821 | /* LOG(llevDebug,"Dropping object to ground that is already on ground\n");*/ |
|
|
822 | return; |
793 | return; |
823 | } |
794 | |
824 | /* If it is an active container, then we should drop all objects |
795 | /* If it is an active container, then we should drop all objects |
825 | * in the container and not the container itself. |
796 | * in the container and not the container itself. |
826 | */ |
797 | */ |
827 | if (op->inv && QUERY_FLAG (op, FLAG_APPLIED)) |
798 | if (op->inv && QUERY_FLAG (op, FLAG_APPLIED)) |
828 | { |
799 | { |
… | |
… | |
831 | for (current = op->inv; current != NULL; current = next) |
802 | for (current = op->inv; current != NULL; current = next) |
832 | { |
803 | { |
833 | next = current->below; |
804 | next = current->below; |
834 | drop_object (pl, current, 0); |
805 | drop_object (pl, current, 0); |
835 | } |
806 | } |
|
|
807 | |
836 | esrv_update_item (UPD_WEIGHT, pl, op); |
808 | esrv_update_item (UPD_WEIGHT, pl, op); |
837 | } |
809 | } |
838 | else |
810 | else |
839 | { |
811 | { |
840 | drop_object (pl, op, nrof); |
812 | drop_object (pl, op, nrof); |
… | |
… | |
849 | |
821 | |
850 | pl->contr->count = nrof; |
822 | pl->contr->count = nrof; |
851 | pick_up (pl, op); |
823 | pick_up (pl, op); |
852 | return; |
824 | return; |
853 | } |
825 | } |
|
|
826 | |
854 | env = esrv_get_ob_from_count (pl, to); |
827 | env = esrv_get_ob_from_count (pl, to); |
855 | if (!env) |
828 | if (!env) |
856 | { |
829 | { |
857 | LOG (llevDebug, "Player '%s' tried to move object to the unknown location (%d)\n", &pl->name, to); |
830 | LOG (llevDebug, "Player '%s' tried to move object to the unknown location (%d)\n", &pl->name, to); |
858 | return; |
831 | return; |
859 | } |
832 | } |
|
|
833 | |
860 | /* put_object_in_sack presumes that necessary sanity checking |
834 | /* put_object_in_sack presumes that necessary sanity checking |
861 | * has already been done (eg, it can be picked up and fits in |
835 | * has already been done (eg, it can be picked up and fits in |
862 | * in a sack, so check for those things. We should also check |
836 | * in a sack, so check for those things. We should also check |
863 | * an make sure env is in fact a container for that matter. |
837 | * an make sure env is in fact a container for that matter. |
864 | */ |
838 | */ |
865 | if (env->type == CONTAINER && can_pick (pl, op) && sack_can_hold (pl, env, op, nrof)) |
839 | if (env->type == CONTAINER && can_pick (pl, op) && sack_can_hold (pl, env, op, nrof)) |
866 | { |
840 | { |
867 | put_object_in_sack (pl, env, op, nrof); |
841 | put_object_in_sack (pl, env, op, nrof); |
868 | } |
842 | } |
869 | } |
843 | } |
|
|
844 | |