ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/info.c
Revision: 1.1.1.2 (vendor branch)
Committed: Wed Feb 22 18:03:28 2006 UTC (18 years, 3 months ago) by elmex
Content type: text/plain
Branch: UPSTREAM
CVS Tags: UPSTREAM_2006_03_15, UPSTREAM_2006_02_22
Changes since 1.1.1.1: +8 -8 lines
Log Message:
cvs -z7 -d:ext:elmex@cvs.schmorp.de:/schmorpforge import cf.schmorp.de UPSTREAM UPSTREAM_2006_02_22

File Contents

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