Atrinik Server  4.0
info.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program 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 2 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, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
37 #include <global.h>
38 #include <stdarg.h>
39 #include <toolkit/packet.h>
40 #include <player.h>
41 #include <object.h>
42 
43 #define DRAW_INFO_FORMAT_CONSTRUCT() \
44  char buf[HUGE_BUF]; \
45  va_list ap; \
46  va_start(ap, format); \
47  vsnprintf(buf, sizeof(buf), format, ap); \
48  va_end(ap);
49 
50 void draw_info_send(uint8_t type, const char *name, const char *color,
51  socket_struct *ns, const char *buf)
52 {
53  packet_struct *packet;
54 
55  packet = packet_new(CLIENT_CMD_DRAWINFO, 256, 512);
56  packet_enable_ndelay(packet);
57  packet_debug_data(packet, 0, "Type");
58  packet_append_uint8(packet, type);
59  packet_debug_data(packet, 0, "Color");
60  packet_append_string_terminated(packet, color);
61 
62  packet_debug_data(packet, 0, "Message");
63 
64  if (name) {
65  packet_append_string(packet, "[a=#charname]");
66  packet_append_string(packet, name);
67  packet_append_string(packet, "[/a]: ");
68  }
69 
70  packet_append_string_terminated(packet, buf);
71  socket_send_packet(ns, packet);
72 }
73 
85 void draw_info_full(uint8_t type, const char *name, const char *color, StringBuffer *sb_capture, object *pl, const char *buf)
86 {
87  /* Handle global messages. */
88  if (!pl) {
89  player *tmppl;
90 
91  for (tmppl = first_player; tmppl; tmppl = tmppl->next) {
92  draw_info_full(type, name, color, NULL, tmppl->ob, buf);
93  }
94 
95  return;
96  }
97 
98  if (!pl || pl->type != PLAYER) {
99  return;
100  }
101 
102  if (CONTR(pl)->cs->state != ST_PLAYING) {
103  return;
104  }
105 
106  if (sb_capture) {
107  stringbuffer_append_string(sb_capture, buf);
108  stringbuffer_append_string(sb_capture, "\n");
109  } else {
110  draw_info_send(type, name, color, CONTR(pl)->cs, buf);
111  }
112 }
113 
125 void draw_info_full_format(uint8_t type, const char *name, const char *color, StringBuffer *sb_capture, object *pl, const char *format, ...)
126 {
127  DRAW_INFO_FORMAT_CONSTRUCT();
128  draw_info_full(type, name, color, sb_capture, pl, buf);
129 }
130 
131 void draw_info_type(uint8_t type, const char *name, const char *color, object *pl, const char *buf)
132 {
133  draw_info_full(type, name, color, NULL, pl, buf);
134 }
135 
136 void draw_info_type_format(uint8_t type, const char *name, const char *color, object *pl, const char *format, ...)
137 {
138  DRAW_INFO_FORMAT_CONSTRUCT();
139  draw_info_full(type, name, color, NULL, pl, buf);
140 }
141 
142 void draw_info(const char *color, object *pl, const char *buf)
143 {
144  draw_info_full(CHAT_TYPE_GAME, NULL, color, NULL, pl, buf);
145 }
146 
147 void draw_info_format(const char *color, object *pl, const char *format, ...)
148 {
149  DRAW_INFO_FORMAT_CONSTRUCT();
150  draw_info_full(CHAT_TYPE_GAME, NULL, color, NULL, pl, buf);
151 }
152 
153 static int draw_info_map_internal(mapstruct *tiled, mapstruct *map, uint8_t type,
154  const char *name, const char *color, object *op, object *op2,
155  const char *buf, int dist, int x, int y)
156 {
157  object *pl;
158  rv_vector rv;
159 
160  for (pl = tiled->player_first; pl != NULL; pl = CONTR(pl)->map_above) {
161  if (pl != op && pl != op2 && get_rangevector_from_mapcoords(map, x, y,
162  pl->map, pl->x, pl->y, &rv, RV_NO_DISTANCE) &&
163  POW2(rv.distance_x) + POW2(rv.distance_y) <= dist) {
164  draw_info_type(type, name, color, pl, buf);
165  }
166  }
167 
168  return 0;
169 }
170 
195 void draw_info_map(uint8_t type, const char *name, const char *color, mapstruct *map, int x, int y, int dist, object *op, object *op2, const char *buf)
196 {
197  int distance;
198 
199  if (!map || map->in_memory != MAP_IN_MEMORY) {
200  return;
201  }
202 
203  if (dist == MAP_INFO_ALL) {
204  object *pl;
205 
206  for (pl = map->player_first; pl; pl = CONTR(pl)->map_above) {
207  if (pl != op && pl != op2) {
208  draw_info_type(type, name, color, pl, buf);
209  }
210  }
211 
212  return;
213  }
214 
215  distance = POW2(dist);
216 
217  MAP_TILES_WALK_START(map, draw_info_map_internal, type, name, color, op,
218  op2, buf, distance, x, y)
219  {
220  }
222 }
void draw_info_full(uint8_t type, const char *name, const char *color, StringBuffer *sb_capture, object *pl, const char *buf)
Definition: info.c:85
#define MAP_IN_MEMORY
Definition: map.h:170
#define RV_NO_DISTANCE
Definition: map.h:813
#define MAP_INFO_ALL
Definition: global.h:85
uint8_t type
One of operation types.
Definition: sound_ambient.c:45
uint32_t in_memory
Definition: map.h:627
int distance_x
Definition: map.h:778
object * ob
Definition: player.h:185
#define MAP_TILES_WALK_START(_m, _fnc,...)
Definition: map.h:733
#define PLAYER
Definition: define.h:122
int distance_y
Definition: map.h:781
int16_t y
Definition: object.h:276
struct mapdef * map
Definition: object.h:139
void draw_info_full_format(uint8_t type, const char *name, const char *color, StringBuffer *sb_capture, object *pl, const char *format,...)
Definition: info.c:125
int16_t x
Definition: object.h:273
int get_rangevector_from_mapcoords(mapstruct *map1, int x, int y, mapstruct *map2, int x2, int y2, rv_vector *retval, int flags)
Definition: map.c:2297
void draw_info_map(uint8_t type, const char *name, const char *color, mapstruct *map, int x, int y, int dist, object *op, object *op2, const char *buf)
Definition: info.c:195
uint8_t type
Definition: object.h:360
shstr * name
More definite name, like "kobold".
Definition: arch.h:46
object * player_first
Definition: map.h:594
player * first_player
Definition: main.c:57
#define MAP_TILES_WALK_END
Definition: map.h:766
Definition: map.h:536
struct pl_player * next
Definition: player.h:145