ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/info.c
Revision: 1.2
Committed: Sat Mar 18 15:40:21 2006 UTC (18 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.1: +14 -10 lines
Log Message:
"1 times It gets darker."
Save miniscule amounts of bandwidth - and ridicule.

File Contents

# Content
1 /*
2 * static char *rcsid_sock_info_c =
3 * "$Id$";
4 */
5
6 /*
7 CrossFire, A Multiplayer game for X-windows
8
9 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10 Copyright (C) 1992 Frank Tore Johansen
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 The authors can be reached via e-mail at crossfire-devel@real-time.com
27 */
28
29 /**
30 * \file
31 * Basic client output functions.
32 *
33 * \date 2003-12-02
34 *
35 * This file implements some of the simpler output functions to the
36 * client. Basically, things like sending text strings along
37 */
38
39 #include <global.h>
40 #include <sproto.h>
41 #include <stdarg.h>
42 #include <spells.h>
43 #include <skills.h>
44
45 /**
46 * Draws a normal message on the client. It is pretty
47 * much the same thing as the draw_info above, but takes a color
48 * parameter. the esrv_drawinfo functions should probably be
49 * replaced with this, just using black as the color.
50 */
51 static void esrv_print_msg(NewSocket *ns,int color, const char *str)
52 {
53 char buf[HUGE_BUF];
54
55 if (ns->status == Ns_Old) {
56 snprintf(buf,HUGE_BUF,"%s\n", str);
57 } else {
58 snprintf(buf,HUGE_BUF, "drawinfo %d %s", color, str);
59 }
60 /* LOG(llevDebug,"sending %s to socket, len=%d\n", buf, strlen(buf));*/
61 Write_String_To_Socket(ns, buf, strlen(buf));
62 }
63
64 /**
65 * Draws an extended message on the client.
66 * ns the socket to send message to
67 * color color informations (used mainly if client does not support message type)
68 * type,
69 * subtype type and subtype of text message
70 * intro Intro message to send with main message if client does not support the message type
71 * message The main message
72 */
73 static void esrv_print_ext_msg(NewSocket *ns,int color,uint8 type, uint8 subtype, const char *message)
74 {
75 char buf[HUGE_BUF];
76 snprintf(buf,HUGE_BUF, "drawextinfo %d %hhu %hhu %s", color, type, subtype, message);
77 Write_String_To_Socket(ns, buf, strlen(buf));
78 /* LOG(llevDebug,"sending %s to socket, len=%d", buf, strlen(buf));*/
79
80 }
81
82 /**
83 * Frontend for esrv_print_msg
84 * \param colr message color
85 * \param pl player to send to. Can be NULL
86 * \param tmp message to send. Can be NULL
87 *
88 * If pl is NULL or without contr set, writes message to log.
89 *
90 * Else sends message to player via esrv_print_msg
91 */
92
93 static void print_message(int colr, const object *pl, const char *tmp) {
94
95 if(tmp == (char *) NULL) {
96 tmp="[NULL]";
97 }
98
99 if(!pl || (pl->type==PLAYER && pl->contr==NULL)) {
100 fprintf(logfile,"%s\n",tmp);
101 return;
102 }
103 if (pl->type == PLAYER) {
104 esrv_print_msg(&pl->contr->socket,colr,(char*) tmp);
105 return;
106 }
107 }
108
109
110 /**
111 * Prints out the contents of specified buffer structures,
112 * and clears the string.
113 */
114
115 void flush_output_element(const object *pl, Output_Buf *outputs)
116 {
117 char tbuf[MAX_BUF];
118
119 if (outputs->buf==NULL) return;
120 if (outputs->count > 1) {
121 snprintf(tbuf,MAX_BUF, "%d times %s", outputs->count, outputs->buf);
122 print_message(NDI_BLACK, pl, tbuf);
123 } else
124 print_message(NDI_BLACK, pl, outputs->buf);
125
126 free_string(outputs->buf);
127 outputs->buf=NULL;
128 outputs->first_update=0; /* This way, it will be reused */
129 }
130
131 /**
132 * Sends message to player through output buffers.
133 * \param pl player to send message
134 * \param buf message to send
135 *
136 * If player's output_count is 1, sends message directly.
137 *
138 * Else checks output buffers for specified message.
139 *
140 * If found, checks if message should be sent now.
141 *
142 * If message not already in buffers, flushes olders buffer,
143 * and adds message to queue.
144 */
145
146 static void check_output_buffers(const object *pl, const char *buf)
147 {
148 int i, oldest=0;
149
150 if (pl->contr->outputs_count<2) {
151 print_message(NDI_BLACK, pl, buf);
152 return;
153 }
154 else {
155 for (i=0; i<NUM_OUTPUT_BUFS; i++) {
156 if (pl->contr->outputs[i].buf &&
157 !strcmp(buf, pl->contr->outputs[i].buf)) break;
158 else if (pl->contr->outputs[i].first_update <
159 pl->contr->outputs[oldest].first_update)
160 oldest=i;
161 }
162 /* We found a match */
163 if (i<NUM_OUTPUT_BUFS) {
164 pl->contr->outputs[i].count++;
165 if (pl->contr->outputs[i].count>=pl->contr->outputs_count) {
166 flush_output_element(pl, &pl->contr->outputs[i]);
167 }
168 }
169 /* No match - flush the oldest, and put the new one in */
170 else {
171 flush_output_element(pl, &pl->contr->outputs[oldest]);
172
173 pl->contr->outputs[oldest].first_update = pticks;
174 pl->contr->outputs[oldest].count = 1;
175 if (pl->contr->outputs[oldest].buf!=NULL)
176 free_string(pl->contr->outputs[oldest].buf);
177 pl->contr->outputs[oldest].buf = add_string(buf);
178 }
179 }
180 }
181
182
183
184 /**
185 * Sends message to player(s).
186 *
187 * flags is various flags - mostly color, plus a few specials.
188 *
189 * pri is priority. It is a little odd - the lower the value, the more
190 * important it is. Thus, 0 gets sent no matter what. Otherwise, the
191 * value must be less than the listening level that the player has set.
192 * Unfortunately, there is no clear guideline on what each level does what.
193 *
194 * pl can be passed as NULL - in fact, this will be done if NDI_ALL is set
195 * in the flags.
196 *
197 * If message is black, and not NDI_UNIQUE, gets sent through output buffers.
198 *
199 */
200
201 void new_draw_info(int flags, int pri, const object *pl, const char *buf)
202 {
203
204 if (flags & NDI_ALL) {
205 player *tmppl;
206 int i;
207
208 for (tmppl=first_player; tmppl!=NULL; tmppl=tmppl->next)
209 new_draw_info((flags & ~NDI_ALL), pri, tmppl->ob, buf);
210
211 for (i=1; i<socket_info.allocated_sockets; i++) {
212 if (init_sockets[i].status == Ns_Old && init_sockets[i].old_mode != Old_Listen && pri< 10) {
213 cs_write_string(&init_sockets[i], buf, strlen(buf));
214 /* Most messages don't have a newline, so add one */
215 cs_write_string(&init_sockets[i], "\n", 1);
216 }
217 }
218
219 return;
220 }
221 if(!pl || (pl->type==PLAYER && pl->contr==NULL)) {
222 /* Write to the socket? */
223 print_message(0, NULL, buf);
224 return;
225 }
226 if (pl->type!=PLAYER) return;
227 if (pri>=pl->contr->listening) return;
228
229 if ((flags&NDI_COLOR_MASK)==NDI_BLACK && !(flags &NDI_UNIQUE)) {
230 /* following prints stuff out, as appropriate */
231 check_output_buffers(pl, buf);
232 }
233 else {
234 print_message(flags&NDI_COLOR_MASK, pl, buf);
235 }
236 }
237
238 /**
239 * Wrapper for new_draw_info printf-like.
240 *
241 * This is a pretty trivial function, but it allows us to use printf style
242 * formatting, so instead of the calling function having to do it, we do
243 * it here. It may also have advantages in the future for reduction of
244 * client/server bandwidth (client could keep track of various strings
245 */
246
247 void new_draw_info_format(int flags, int pri, const object *pl, const char *format, ...)
248 {
249 char buf[HUGE_BUF];
250
251 va_list ap;
252 va_start(ap, format);
253
254 vsnprintf(buf, HUGE_BUF, format, ap);
255
256 va_end(ap);
257
258 new_draw_info(flags, pri, pl, buf);
259 }
260
261
262 void draw_ext_info(
263 int flags, int pri, const object *pl, uint8 type,
264 uint8 subtype, const char* message, const char* oldmessage){
265
266 if(!pl || (pl->type!=PLAYER) || (pl->contr==NULL))
267 return;
268
269 if (pri>=pl->contr->listening) return;
270 if (!CLIENT_SUPPORT_READABLES(&pl->contr->socket,type)){
271 char *buf = (char*)malloc(strlen(oldmessage==NULL?message:oldmessage)+1);
272 if (buf==NULL)
273 LOG(llevError,"info::draw_ext_info -> Out of memory!");
274 else{
275 strcpy(buf,oldmessage==NULL?message:oldmessage);
276 strip_media_tag(buf);
277 new_draw_info(flags, pri, pl, buf);
278 free(buf);
279 }
280 }else{
281 esrv_print_ext_msg(&pl->contr->socket,flags&NDI_COLOR_MASK,type,subtype,message);
282 }
283 }
284
285 void draw_ext_info_format(
286 int flags, int pri, const object *pl, uint8 type,
287 uint8 subtype, const char* old_format,
288 char* new_format, ...){
289
290 char buf[HUGE_BUF];
291 if(!pl || (pl->type!=PLAYER) || (pl->contr==NULL))
292 return;
293
294 if (pri>=pl->contr->listening) return;
295 if (!CLIENT_SUPPORT_READABLES(&pl->contr->socket,type)){
296 va_list ap;
297 LOG(llevDebug,"Non supported extension text type for client.\n");
298 va_start(ap, new_format);
299 vsnprintf(buf, HUGE_BUF, old_format, ap);
300 va_end(ap);
301 new_draw_info(flags, pri, pl, buf);
302 return;
303 }else{
304 va_list ap;
305 va_start(ap, new_format);
306 vsnprintf(buf, HUGE_BUF, new_format, ap);
307 va_end(ap);
308 strip_media_tag(buf);
309 esrv_print_ext_msg(&pl->contr->socket,flags&NDI_COLOR_MASK,type,subtype,buf);
310 }
311 }
312 /**
313 * Writes to everyone on the map *except* op. This is useful for emotions.
314 */
315
316 void new_info_map_except(int color, mapstruct *map, object *op, const char *str) {
317 player *pl;
318
319 for(pl = first_player; pl != NULL; pl = pl->next)
320 if(pl->ob != NULL && pl->ob->map == map && pl->ob != op) {
321 new_draw_info(color, 0, pl->ob, str);
322 }
323 }
324
325 /**
326 * Writes to everyone on the map except op1 and op2
327 */
328
329 void new_info_map_except2(int color, mapstruct *map, object *op1, object *op2,
330 const char *str) {
331 player *pl;
332
333 for(pl = first_player; pl != NULL; pl = pl->next)
334 if(pl->ob != NULL && pl->ob->map == map
335 && pl->ob != op1 && pl->ob != op2) {
336 new_draw_info(color, 0, pl->ob, str);
337 }
338 }
339
340 /**
341 * Writes to everyone on the specified map
342 */
343
344 void new_info_map(int color, mapstruct *map, const char *str) {
345 player *pl;
346
347 for(pl = first_player; pl != NULL; pl = pl->next)
348 if(pl->ob != NULL && pl->ob->map == map) {
349 new_draw_info(color, 0, pl->ob, str);
350 }
351 }
352
353
354 /**
355 * This does nothing now. However, in theory, we should probably send
356 * something to the client and let the client figure out how it might want
357 * to handle this
358 */
359 void clear_win_info(object *op)
360 {
361 }
362
363 /**
364 * Get player's current range attack in obuf.
365 */
366 void rangetostring(object *pl,char *obuf)
367 {
368 switch(pl->contr->shoottype) {
369 case range_none:
370 strcpy(obuf,"Range: nothing");
371 break;
372
373 case range_bow:
374 {
375 object *op;
376
377 for (op = pl->inv; op; op=op->below)
378 if (op->type == BOW && QUERY_FLAG (op, FLAG_APPLIED))
379 break;
380 if(op==NULL) break;
381
382 sprintf (obuf, "Range: %s (%s)", query_base_name(op, 0),
383 op->race ? op->race : "nothing");
384 }
385 break;
386
387 case range_magic:
388 if (settings.casting_time == TRUE) {
389 if (pl->casting_time > -1) {
390 if (pl->casting_time == 0)
391 sprintf(obuf,"Range: Holding spell (%s)",
392 pl->spell->name);
393 else
394 sprintf(obuf,"Range: Casting spell (%s)",
395 pl->spell->name);
396 } else
397 sprintf(obuf,"Range: spell (%s)",
398 pl->contr->ranges[range_magic]->name);
399 } else
400 sprintf(obuf,"Range: spell (%s)",
401 pl->contr->ranges[range_magic]->name);
402 break;
403
404 case range_misc:
405 sprintf(obuf,"Range: %s",
406 pl->contr->ranges[range_misc]?
407 query_base_name(pl->contr->ranges[range_misc],0): "none");
408 break;
409
410 /* range_scroll is only used for controlling golems. If the
411 * the player does not have a golem, reset some things.
412 */
413 case range_golem:
414 if (pl->contr->ranges[range_golem]!=NULL)
415 sprintf(obuf,"Range: golem (%s)",pl->contr->ranges[range_golem]->name);
416 else {
417 pl->contr->shoottype = range_none;
418 strcpy(obuf,"Range: nothing");
419 }
420 break;
421
422 case range_skill:
423 sprintf(obuf,"Skill: %s", pl->chosen_skill!=NULL ?
424 pl->chosen_skill->name : "none");
425 break;
426
427 case range_builder:
428 sprintf( obuf, "Builder: %s", query_base_name( pl->contr->ranges[ range_builder ], 0 ) );
429 break;
430
431 default:
432 strcpy(obuf,"Range: illegal");
433 }
434 }
435
436 /**
437 * Sets player title.
438 */
439 void set_title(object *pl, char *buf)
440 {
441 /* Eneq(@csd.uu.se): Let players define their own titles. */
442 if (pl->contr->own_title[0]=='\0')
443 sprintf(buf,"Player: %s the %s",pl->name,pl->contr->title);
444 else
445 sprintf(buf,"Player: %s %s",pl->name,pl->contr->own_title);
446 }
447
448
449 /**
450 * Helper for magic map creation.
451 *
452 * Takes a player, the map_mark array and an x and y starting position.
453 * pl is the player.
454 * px, py are offsets from the player.
455 *
456 * This function examines all the adjacant spaces next to px, py.
457 * It updates the map_mark arrow with the color and high bits set
458 * for various code values.
459 */
460 static void magic_mapping_mark_recursive(object *pl, char *map_mark, int px, int py)
461 {
462 int x, y, dx, dy,mflags;
463 sint16 nx, ny;
464 mapstruct *mp;
465 New_Face *f;
466
467 for (dx = -1; dx <= 1; dx++) {
468 for (dy = -1; dy <= 1; dy++) {
469 x = px + dx;
470 y = py + dy;
471
472 if (FABS(x) >= MAGIC_MAP_HALF || FABS(y) >= MAGIC_MAP_HALF) continue;
473
474 mp = pl->map;
475 nx = pl->x + x;
476 ny = pl->y + y;
477
478 mflags = get_map_flags(pl->map, &mp, nx, ny, &nx, &ny);
479 if (mflags & P_OUT_OF_MAP) continue;
480
481 if (map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE* (MAGIC_MAP_HALF + y)] == 0) {
482 f= GET_MAP_FACE(mp, nx, ny, 0);
483 if (f == blank_face)
484 f= GET_MAP_FACE(mp, nx, ny, 1);
485 if (f == blank_face)
486 f= GET_MAP_FACE(mp, nx, ny, 2);
487
488 /* Should probably have P_NO_MAGIC here also, but then shops don't
489 * work.
490 */
491 if (mflags & P_BLOCKSVIEW)
492 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE* (MAGIC_MAP_HALF + y)] = FACE_WALL | (f?f->magicmap:0);
493 else {
494 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE* (MAGIC_MAP_HALF + y)] = FACE_FLOOR | (f?f->magicmap:0);
495 magic_mapping_mark_recursive(pl, map_mark, x, y);
496 }
497 }
498 }
499 }
500 }
501
502
503 /**
504 * Creates magic map for player.
505 *
506 * Note: For improved magic mapping display, the space that blocks
507 * the view is now marked with value 2. Any dependencies of map_mark
508 * being nonzero have been changed to check for 1. Also, since
509 * map_mark is a char value, putting 2 in should cause no problems.
510 *
511 * This function examines the map the player is on, and determines what
512 * is visible. 2 is set for walls or objects that blocks view. 1
513 * is for open spaces. map_mark should already have been initialized
514 * to zero before this is called.
515 * strength is an initial strength*2 rectangular area that we automatically
516 * see in/penetrate through.
517 */
518
519 void magic_mapping_mark(object *pl, char *map_mark, int strength)
520 {
521 int x, y, mflags;
522 sint16 nx, ny;
523 mapstruct *mp;
524 New_Face *f;
525
526 for (x = -strength; x <strength; x++) {
527 for (y = -strength; y <strength; y++) {
528 mp = pl->map;
529 nx = pl->x + x;
530 ny = pl->y + y;
531 mflags = get_map_flags(pl->map, &mp, nx, ny, &nx, &ny);
532 if (mflags & P_OUT_OF_MAP)
533 continue;
534 else {
535 f= GET_MAP_FACE(mp, nx, ny, 0);
536 if (f == blank_face)
537 f= GET_MAP_FACE(mp, nx, ny, 1);
538 if (f == blank_face)
539 f= GET_MAP_FACE(mp, nx, ny, 2);
540 }
541
542 if (mflags & P_BLOCKSVIEW)
543 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE* (MAGIC_MAP_HALF + y)] = FACE_WALL | (f?f->magicmap:0);
544 else {
545 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE* (MAGIC_MAP_HALF + y)] = FACE_FLOOR | (f?f->magicmap:0);
546 magic_mapping_mark_recursive(pl, map_mark, x, y);
547 }
548 }
549 }
550 }
551
552
553 /**
554 * Creates and sends magic map to player.
555 *
556 * The following function is a lot messier than it really should be,
557 * but there is no real easy solution.
558 *
559 * Mark Wedel
560 */
561
562 void draw_magic_map(object *pl)
563 {
564 int x,y;
565 char *map_mark = (char *) calloc(MAGIC_MAP_SIZE*MAGIC_MAP_SIZE, 1);
566 int xmin, xmax, ymin, ymax;
567 SockList sl;
568
569 if (pl->type!=PLAYER) {
570 LOG(llevError,"Non player object called draw_map.\n");
571 return;
572 }
573
574 /* First, we figure out what spaces are 'reachable' by the player */
575 magic_mapping_mark(pl, map_mark, 3);
576
577 /* We now go through and figure out what spaces have been
578 * marked, and thus figure out rectangular region we send
579 * to the client (eg, if only a 10x10 area is visible, we only
580 * want to send those 100 spaces.)
581 */
582 xmin = MAGIC_MAP_SIZE;
583 ymin = MAGIC_MAP_SIZE;
584 xmax = 0;
585 ymax = 0;
586 for(x = 0; x < MAGIC_MAP_SIZE ; x++) {
587 for(y = 0; y < MAGIC_MAP_SIZE; y++) {
588 if (map_mark[x + MAP_WIDTH(pl->map) * y] | FACE_FLOOR) {
589 xmin = x < xmin ? x : xmin;
590 xmax = x > xmax ? x : xmax;
591 ymin = y < ymin ? y : ymin;
592 ymax = y > ymax ? y : ymax;
593 }
594 }
595 }
596
597 sl.buf=malloc(MAXSOCKBUF);
598 snprintf((char*)sl.buf, MAXSOCKBUF, "magicmap %d %d %d %d ", (xmax-xmin+1), (ymax-ymin+1),
599 MAGIC_MAP_HALF - xmin, MAGIC_MAP_HALF - ymin);
600 sl.len=strlen((char*)sl.buf);
601
602 for (y = ymin; y <= ymax; y++) {
603 for (x = xmin; x <= xmax; x++) {
604 sl.buf[sl.len++]= map_mark[x+MAGIC_MAP_SIZE*y] & ~FACE_FLOOR;
605 } /* x loop */
606 } /* y loop */
607
608 Send_With_Handling(&pl->contr->socket, &sl);
609 free(sl.buf);
610 free(map_mark);
611 }
612
613
614 /**
615 * Send a kill log record to sockets
616 */
617
618 void Log_Kill(const char *Who,
619 const char *What, int WhatType,
620 const char *With, int WithType)
621 {
622 int i;
623 size_t len;
624 char buf[MAX_BUF];
625
626 if (With!=NULL) {
627 snprintf(buf, MAX_BUF, "%s\t%s\t%d\t%s\t%d\n",Who,What,WhatType,With,WithType);
628 }
629 else {
630 snprintf(buf,MAX_BUF, "%s\t%s\t%d\n",Who,What,WhatType);
631 }
632 len=strlen(buf);
633 for(i=1; i<socket_info.allocated_sockets; i++) {
634 if (init_sockets[i].old_mode == Old_Listen) {
635 cs_write_string(&init_sockets[i], buf, len);
636 }
637 }
638 }