Atrinik Client  4.0
target.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 
37 typedef struct target_widget {
41 
43 static void widget_draw(widgetdata *widget)
44 {
45  if (!widget->redraw) {
46  return;
47  }
48 
49  const char *str;
50 
51  if (cpl.target_code == CMD_TARGET_SELF) {
52  if (cpl.combat) {
53  str = "target self (hold attack)";
54  } else {
55  str = "target self";
56  }
57  } else if (cpl.target_code == CMD_TARGET_ENEMY) {
58  if (cpl.combat) {
59  str = "target and attack enemy";
60  } else {
61  str = "target enemy";
62  }
63  } else if (cpl.target_code == CMD_TARGET_NEUTRAL ||
64  cpl.target_code == CMD_TARGET_FRIEND) {
65  if (cpl.combat) {
66  if (cpl.combat_force && cpl.target_code != CMD_TARGET_FRIEND) {
67  str = "target and attack friend";
68  } else {
69  str = "target friend (hold attack)";
70  }
71  } else {
72  str = "target friend";
73  }
74  } else {
75  LOG(ERROR, "Invalid target code: %d", cpl.target_code);
76  str = "invalid target";
77  }
78 
80 
81  target_widget->button_combat.texture =
82  target_widget->button_combat.texture_over =
83  target_widget->button_combat.texture_pressed =
84  texture_get(TEXTURE_TYPE_CLIENT,
85  cpl.combat ? "target_attack" : "target_normal");
86 
87  target_widget->button_combat.surface = widget->surface;
88  button_set_parent(&target_widget->button_combat, widget->x, widget->y);
89  target_widget->button_combat.x = 5;
90  target_widget->button_combat.y = widget->h / 2 -
91  texture_surface(target_widget->button_combat.texture)->h / 2;
92  button_show(&target_widget->button_combat, "");
93 
94  int x = target_widget->button_combat.x;
95  int y = target_widget->button_combat.y +
96  texture_surface(target_widget->button_combat.texture)->h - 3;
97  surface_show(widget->surface, x, y, NULL, TEXTURE_CLIENT("target_hp_b"));
98 
99  if (cpl.target_code != CMD_TARGET_SELF) {
100  target_widget->button_talk.surface = widget->surface;
101  target_widget->button_talk.x = widget->w -
102  texture_surface(target_widget->button_talk.texture)->w - 5;
103  target_widget->button_talk.y = widget->h / 2 -
104  texture_surface(target_widget->button_talk.texture)->h / 2;
105  button_set_parent(&target_widget->button_talk, widget->x, widget->y);
106  button_show(&target_widget->button_talk, "");
107  }
108 
110  cpl.target_code != 0) {
111  int hp = cpl.target_hp;
112 
113  if (cpl.target_code == CMD_TARGET_SELF) {
114  hp = (int) (((double) cpl.stats.hp / (double) cpl.stats.maxhp) *
115  100.0);
116  } else {
117  hp = MIN(100, MAX(0, hp));
118  }
119 
120  SDL_Surface *target_hp = TEXTURE_CLIENT("target_hp");
121 
122  SDL_Rect box;
123  box.x = 0;
124  box.y = 0;
125  box.h = target_hp->h;
126  box.w = (Uint16) (target_hp->w * ((double) hp * 0.01));
127  surface_show(widget->surface, x + 1, y + 1, &box, target_hp);
128 
129  box.x = texture_surface(target_widget->button_combat.texture)->w +
130  5 * 2;
131  box.y = 1;
132  box.h = widget->h - 1 * 2;
133  box.w = widget->w - box.x - 5 * 2 -
134  texture_surface(target_widget->button_talk.texture)->w;
135 
136  const char *hp_color;
137 
138  if (hp > 90) {
139  hp_color = COLOR_GREEN;
140  } else if (hp > 75) {
141  hp_color = COLOR_DGOLD;
142  } else if (hp > 50) {
143  hp_color = COLOR_HGOLD;
144  } else if (hp > 25) {
145  hp_color = COLOR_ORANGE;
146  } else if (hp > 10) {
147  hp_color = COLOR_YELLOW;
148  } else {
149  hp_color = COLOR_RED;
150  }
151 
152  text_show_format(widget->surface, FONT_ARIAL11, box.x, box.y,
154  &box, "%s\n[c=#%s]HP: %d%%[/c] %s", cpl.target_name,
155  hp_color, hp, str);
156  }
157 }
158 
160 static int widget_event(widgetdata *widget, SDL_Event *event)
161 {
163 
164  if (button_event(&target_widget->button_combat, event)) {
165  keybind_process_command("?COMBAT");
166  return 1;
167  }
168 
169  if (cpl.target_code != CMD_TARGET_SELF) {
170  if (button_event(&target_widget->button_talk, event)) {
171  keybind_process_command("?HELLO");
172  return 1;
173  }
174  }
175 
176  return 0;
177 }
178 
180 static void widget_deinit(widgetdata *widget)
181 {
183  button_destroy(&target_widget->button_talk);
184  button_destroy(&target_widget->button_combat);
185 }
186 
193 {
195 
196  widget->draw_func = widget_draw;
197  widget->event_func = widget_event;
198  widget->deinit_func = widget_deinit;
199  target_widget = widget->subwidget = ecalloc(1, sizeof(*target_widget));
200  button_create(&target_widget->button_talk);
201  button_create(&target_widget->button_combat);
202  target_widget->button_talk.texture =
203  target_widget->button_talk.texture_over =
204  target_widget->button_talk.texture_pressed =
205  texture_get(TEXTURE_TYPE_CLIENT, "target_talk");
206 }
struct target_widget target_widget_t
int32_t hp
Definition: player.h:66
int32_t maxhp
Definition: player.h:69
void * subwidget
Definition: widget.h:107
SDL_Surface * texture_surface(texture_struct *texture)
Definition: texture.c:303
SDL_Surface * surface
Definition: widget.h:110
int button_event(button_struct *button, SDL_Event *event)
Definition: button.c:222
void button_destroy(button_struct *button)
Definition: button.c:94
char target_name[MAX_BUF]
Definition: player.h:138
char target_hp
Definition: player.h:170
Stats stats
Definition: player.h:167
#define COLOR_HGOLD
Definition: text.h:319
texture_struct * texture_get(texture_type_t type, const char *name)
Definition: texture.c:279
button_struct button_talk
The 'hello' button.
Definition: target.c:38
#define COLOR_ORANGE
Definition: text.h:291
#define COLOR_YELLOW
Definition: text.h:309
#define TEXT_MARKUP
Definition: text.h:224
button_struct button_combat
The combat toggle button.
Definition: target.c:39
#define COLOR_RED
Definition: text.h:295
static void widget_deinit(widgetdata *widget)
Definition: target.c:180
#define COLOR_GREEN
Definition: text.h:297
Client_Player cpl
Definition: client.c:50
texture_struct * texture_over
Definition: button.h:67
int64_t setting_get_int(int cat, int setting)
Definition: settings.c:414
uint8_t redraw
Definition: widget.h:72
void button_create(button_struct *button)
Definition: button.c:65
int target_code
Definition: player.h:132
int x
Definition: widget.h:48
texture_struct * texture
Definition: button.h:61
SDL_Surface * surface
Definition: button.h:46
int w
Definition: widget.h:54
int h
Definition: widget.h:57
texture_struct * texture_pressed
Definition: button.h:73
void surface_show(SDL_Surface *surface, int x, int y, SDL_Rect *srcrect, SDL_Surface *src)
Definition: sprite.c:761
uint8_t combat_force
Definition: player.h:264
static void widget_draw(widgetdata *widget)
Definition: target.c:43
void text_show_format(SDL_Surface *surface, font_struct *font, int x, int y, const char *color_notation, uint64_t flags, SDL_Rect *box, const char *format,...)
Definition: text.c:2289
uint8_t combat
Definition: player.h:258
void button_show(button_struct *button, const char *text)
Definition: button.c:161
char target_color[COLOR_BUF]
Definition: player.h:135
#define TEXT_VALIGN_CENTER
Definition: text.h:246
int keybind_process_command(const char *cmd)
Definition: keybind.c:553
static int widget_event(widgetdata *widget, SDL_Event *event)
Definition: target.c:160
#define COLOR_DGOLD
Definition: text.h:321
void widget_target_init(widgetdata *widget)
Definition: target.c:192
int y
Definition: widget.h:51