Atrinik Client  4.0
playerdoll.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 
32 #include <global.h>
33 #include <toolkit/string.h>
34 
40 static int player_doll_positions[PLAYER_EQUIP_MAX][2] = {
41  {22, 44},
42  {22, 6},
43  {22, 82},
44  {102, 82},
45  {102, 120},
46  {22, 158},
47  {62, 6},
48  {62, 44},
49  {62, 82},
50  {62, 120},
51  {62, 158},
52  {102, 6},
53  {102, 44},
54  {22, 82},
55  {22, 120},
56  {102, 158},
57  {-1, -1}
58 };
59 
63 static const char *player_doll_text =
64  "[center][font=sans 12][b]Statistics[/b][/font][/center]\n"
65  "Strength[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
66  "Dexterity[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
67  "Constitution[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
68  "Intelligence[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
69  "Power[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
70  "Speed[c=#ffffff][right][font=mono]%3.2f[/font][/right][/c]\n"
71  "Armour class[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
72  "[y=4][center][font=sans 12][b]Melee[/b][/font][/center]\n"
73  "Damage[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
74  "Weapon class[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
75  "Weapon speed[c=#ffffff][right][font=mono]%3.2fs[/font][/right][/c]\n"
76  "[y=4][center][font=sans 12][b]Ranged[/b][/font][/center]\n"
77  "Damage[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
78  "Weapon class[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
79  "Weapon speed[c=#ffffff][right][font=mono]%3.2fs[/font][/right][/c]\n";
80 
84 static const char *player_doll_text_abbr =
85  "[center][font=sans 12][b]Stats[/b][/font][/center]\n"
86  "Str[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
87  "Dex[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
88  "Con[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
89  "Int[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
90  "Pow[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
91  "Speed[c=#ffffff][right][font=mono]%3.2f[/font][/right][/c]\n"
92  "AC[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
93  "[y=4][center][font=sans 12][b]Melee[/b][/font][/center]\n"
94  "DMG[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
95  "WC[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
96  "WS[c=#ffffff][right][font=mono]%3.2fs[/font][/right][/c]\n"
97  "[y=4][center][font=sans 12][b]Ranged[/b][/font][/center]\n"
98  "DMG[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
99  "WC[c=#ffffff][right][font=mono]%02d[/font][/right][/c]\n"
100  "WS[c=#ffffff][right][font=mono]%3.2fs[/font][/right][/c]\n";
101 
102 #define PLAYER_DOLL_TEXT_RENDER(flags, box) \
103  text_show_format(widget->surface, FONT_ARIAL10, 10, 10, COLOR_HGOLD, \
104  TEXT_MARKUP | flags, box, text, \
105  cpl.stats.Str, cpl.stats.Dex, cpl.stats.Con, \
106  cpl.stats.Int, cpl.stats.Pow, \
107  cpl.stats.speed, cpl.stats.ac, \
108  cpl.stats.dam, cpl.stats.wc, cpl.stats.weapon_speed, \
109  cpl.stats.ranged_dam, cpl.stats.ranged_wc, cpl.stats.ranged_ws);
110 
111 object *playerdoll_get_equipment(int i, int *xpos, int *ypos)
112 {
113  object *obj;
114 
115  if (cpl.equipment[i] == 0) {
116  return NULL;
117  }
118 
119  if (player_doll_positions[i][0] == -1 &&
120  player_doll_positions[i][1] == -1) {
121  return NULL;
122  }
123 
124  obj = object_find(cpl.equipment[i]);
125 
126  if (obj == NULL) {
127  return NULL;
128  }
129 
130  if (i == PLAYER_EQUIP_SHIELD) {
131  object *obj2 = NULL;
132 
133  if (cpl.equipment[PLAYER_EQUIP_WEAPON_RANGED] != 0) {
134  obj2 = object_find(cpl.equipment[PLAYER_EQUIP_WEAPON_RANGED]);
135  } else if (cpl.equipment[PLAYER_EQUIP_WEAPON] != 0) {
136  obj2 = object_find(cpl.equipment[PLAYER_EQUIP_WEAPON]);
137  }
138 
139  if (obj2 != NULL && obj2->flags & CS_FLAG_WEAPON_2H) {
140  obj = obj2;
141  }
142  } else if (i == PLAYER_EQUIP_WEAPON_RANGED) {
143  if (obj->flags & CS_FLAG_WEAPON_2H) {
144  return NULL;
145  }
146  }
147 
148  *xpos = player_doll_positions[i][0] + 2;
149  *ypos = player_doll_positions[i][1] + 2;
150 
151  return obj;
152 }
153 
155 static void widget_draw(widgetdata *widget)
156 {
157  int i, xpos, ypos, xoff, yoff;
158  SDL_Surface *texture_slot_border, *texture;
159  object *obj;
160  SDL_Rect box, box2;
161  const char *text;
162 
163  if (!widget->redraw) {
164  return;
165  }
166 
167  if (cpl.gender == GENDER_FEMALE) {
168  texture = TEXTURE_CLIENT("player_doll_f");
169  } else {
170  texture = TEXTURE_CLIENT("player_doll");
171  }
172 
173  xoff = widget->w - texture->w + 10;
174  yoff = widget->h / 2 - texture->h / 2;
175 
176  box.w = xoff - 10;
177  box.h = widget->h - 10 * 2;
178  box2.w = 0;
179  box2.h = 0;
180 
181  text = player_doll_text;
182 
183  PLAYER_DOLL_TEXT_RENDER(TEXT_MAX_WIDTH, &box2);
184 
185  if (box2.w > box.w) {
186  text = player_doll_text_abbr;
187  }
188 
189  PLAYER_DOLL_TEXT_RENDER(0, &box);
190 
191  texture_slot_border = TEXTURE_CLIENT("player_doll_slot_border");
192 
193  for (i = 0; i < PLAYER_EQUIP_MAX; i++) {
194  if (player_doll_positions[i][0] == -1 &&
195  player_doll_positions[i][1] == -1) {
196  continue;
197  }
198 
199  rectangle_create(widget->surface, player_doll_positions[i][0] + xoff,
200  player_doll_positions[i][1] + yoff, texture_slot_border->w,
201  texture_slot_border->h, PLAYER_DOLL_SLOT_COLOR);
202  }
203 
204  surface_show(widget->surface, xoff, yoff, NULL, texture);
205 
206  for (i = 0; i < PLAYER_EQUIP_MAX; i++) {
207  if (player_doll_positions[i][0] == -1 &&
208  player_doll_positions[i][1] == -1) {
209  continue;
210  }
211 
212  surface_show(widget->surface, player_doll_positions[i][0] + xoff,
213  player_doll_positions[i][1] + yoff, NULL, texture_slot_border);
214 
215  obj = playerdoll_get_equipment(i, &xpos, &ypos);
216 
217  if (!obj) {
218  continue;
219  }
220 
221  object_show_centered(widget->surface, obj, xpos + xoff, ypos + yoff,
223  }
224 }
225 
227 static int widget_event(widgetdata *widget, SDL_Event *event)
228 {
229  if (event->type == SDL_MOUSEMOTION) {
230  char buf[HUGE_BUF];
231  object *obj;
232  int i, xpos, ypos, xoff, yoff;
233 
234  buf[0] = '\0';
235  xoff = widget->w - TEXTURE_CLIENT("player_doll")->w + 10;
236  yoff = widget->h / 2 - TEXTURE_CLIENT("player_doll")->h / 2;
237 
238  for (i = 0; i < PLAYER_EQUIP_MAX; i++) {
239  obj = playerdoll_get_equipment(i, &xpos, &ypos);
240 
241  if (obj == NULL) {
242  continue;
243  }
244 
245  xpos += xoff;
246  ypos += yoff;
247 
248  if (event->motion.x - widget->x > xpos &&
249  event->motion.x - widget->x <= xpos + INVENTORY_ICON_SIZE &&
250  event->motion.y - widget->y > ypos &&
251  event->motion.y - widget->y <= ypos + INVENTORY_ICON_SIZE) {
252  if (buf[0] != '\0') {
253  strncat(buf, "\n", sizeof(buf) - strlen(buf) - 1);
254  }
255 
256  if (obj->nrof > 1) {
257  snprintfcat(buf, sizeof(buf), "%" PRIu32 " %s", obj->nrof,
258  obj->s_name);
259  } else {
260  strncat(buf, obj->s_name, sizeof(buf) - strlen(buf) - 1);
261  }
262  }
263  }
264 
265  if (buf[0] != '\0') {
266  tooltip_create(event->motion.x, event->motion.y, FONT_ARIAL11, buf);
268  tooltip_multiline(200);
269  return 1;
270  }
271  }
272 
273  return 0;
274 }
275 
276 void widget_playerdoll_init(widgetdata *widget)
277 {
278  widget->draw_func = widget_draw;
279  widget->event_func = widget_event;
280 }
static void widget_draw(widgetdata *widget)
Definition: playerdoll.c:155
Definition: item.h:43
void rectangle_create(SDL_Surface *surface, int x, int y, int w, int h, const char *color_notation)
Definition: sprite.c:1443
char s_name[NAME_LEN]
Definition: item.h:60
static const char * player_doll_text
Definition: playerdoll.c:63
uint32_t nrof
Definition: item.h:66
SDL_Surface * surface
Definition: widget.h:110
void tooltip_create(int mx, int my, font_struct *font, const char *text)
Definition: tooltip.c:60
uint8_t gender
Definition: player.h:212
static int player_doll_positions[PLAYER_EQUIP_MAX][2]
Definition: playerdoll.c:40
Client_Player cpl
Definition: client.c:50
uint8_t redraw
Definition: widget.h:72
int x
Definition: widget.h:48
void tooltip_enable_delay(uint32_t delay)
Definition: tooltip.c:82
#define INVENTORY_ICON_SIZE
Definition: inventory.h:60
#define GENDER_FEMALE
Definition: player.h:42
static int widget_event(widgetdata *widget, SDL_Event *event)
Definition: playerdoll.c:227
int w
Definition: widget.h:54
int h
Definition: widget.h:57
void tooltip_multiline(int max_width)
Definition: tooltip.c:96
object * object_find(tag_t tag)
Definition: item.c:139
void surface_show(SDL_Surface *surface, int x, int y, SDL_Rect *srcrect, SDL_Surface *src)
Definition: sprite.c:761
uint32_t flags
Definition: item.h:87
void object_show_centered(SDL_Surface *surface, object *tmp, int x, int y, int w, int h, bool fit)
Definition: item.c:545
#define TEXT_MAX_WIDTH
Definition: text.h:277
static const char * player_doll_text_abbr
Definition: playerdoll.c:84
int y
Definition: widget.h:51