-
Notifications
You must be signed in to change notification settings - Fork 27
/
creatureui.cpp
501 lines (433 loc) · 15.1 KB
/
creatureui.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//////////////////////////////////////////////////////////////////////
// Yet Another Tibia Client
//////////////////////////////////////////////////////////////////////
// Creature instance class
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
// TODO (ivucica#2#) review code added in 2739 which was written by mrauter, it's probably not 100% the way we want to do things, plus we need colorization
#include "creatureui.h"
#include "engine.h"
#include "fassert.h"
#include "gamecontent/creature.h"
#include "gamecontent/globalvars.h"
#include "options.h"
#include <cmath>
static std::map<uint32_t, Sprite*> s_spritecache;
extern uint32_t g_frameTime;
extern uint32_t g_frameDiff;
CreatureUI::CreatureUI() : ThingUI()
{
resetSelf();
}
CreatureUI::~CreatureUI()
{
this->unloadGfx();
m_obj = NULL;
}
void CreatureUI::resetSelf()
{
unloadGfx();
m_preWalk = false;
m_walkState = 1.f;
}
void CreatureUI::unloadGfx()
{
for (std::vector<Sprite*>::iterator it = m_gfx.begin(); it != m_gfx.end(); it++){
delete *it;
}
m_gfx.clear();
m_obj = NULL;
}
void CreatureUI::Blit(int x, int y, float scale, int map_x, int map_y) const
{
if (!m_obj)
return;
if(map_x != 0 && map_y != 0) {
x = x - std::floor(m_obj->xOffset * scale);
y = y - std::floor(m_obj->yOffset * scale);
}
else {
// shrink larger creatures to be 32x32
scale *= (32.f/(float)m_obj->rendersize);
}
if (!isLoaded()) {
printf("Not loaded!\n");
return;
}
uint32_t activeframe = 0;
uint32_t spriteSize, partSize, aniSize;
spriteSize = m_obj->width * m_obj->height * m_obj->blendframes;
partSize = spriteSize * m_obj->xdiv;
aniSize = partSize * m_obj->ydiv;
Creature* n = (Creature*)this;
if(n->getOutfit().m_looktype != 0){
if(map_x != 0 && map_y != 0)
activeframe = ((m_walkState == 1. && !isPreWalking()) ? n->getTurnDir() : n->getLookDir()) * spriteSize; // creature must have different turning direction and looking (moving) direction... if moving, moving direction takes precendence, if standing, turning direction takes precendence
else
activeframe = DIRECTION_SOUTH * spriteSize;
}
float walkoffx = 0.f, walkoffy = 0.f;
if(map_x != 0 && map_y != 0) {
getWalkOffset(walkoffx, walkoffy, scale);
}
//for(uint32_t k = 0; k < m_obj->blendframes; ++k){ // note: if it's anything except item, there won't be blendframes...
{
uint32_t aframes;
if(m_obj->animcount == 3 && (map_x != 0 && map_y != 0)){
aframes = aniSize * (m_walkState == 1. ? 0 : (((int)(m_walkState*100) / 25) % 2 + 1));
}
else if(m_obj->idleAnim){
// TODO (nfries88): all appearances that animate while idle.
//aframes = 0;
uint32_t animationTime = (g_frameTime - m_startTime)/100;
aframes = 0;/*(map_x % m_obj->xdiv + (map_y % m_obj->ydiv)*m_obj->xdiv +
(animationTime % m_obj->animcount)*m_obj->xdiv*m_obj->ydiv)*
spriteSize;*/
}
else if(n->getOutfit().m_looktype == 0 && n->getOutfit().m_lookitem == 0){
uint32_t animationTime = (g_frameTime - m_startTime)/100;
aframes = (map_x % m_obj->xdiv + (map_y % m_obj->ydiv)*m_obj->xdiv +
(animationTime % m_obj->animcount)*m_obj->xdiv*m_obj->ydiv)*
spriteSize;
activeframe = 0;
}
else {
aframes = 0;
}
//Square around the creature
if(g_frameTime - n->getSquareStart() < 1000){
g_engine->drawRectangleLines(x + walkoffx, y + walkoffy, 32*scale, 32*scale, oRGBA(0,0,0,0));
}
//Red square around the attacked creature
if(n->getID() == GlobalVariables::getAttackID()) {
// "Faded Red" - right color. (Verified)
g_engine->drawRectangleLines((x+1) + walkoffx, (y+1) + walkoffy, 32*scale, 32*scale, oRGBA(224, 64, 64, 255), 2*scale);
}
//Green square around the followed creature
else if(n->getID() == GlobalVariables::getFollowID()) {
// "Light Green" - right color. (Verified)
g_engine->drawRectangleLines((x+2) + walkoffx, (y+2) + walkoffy, 32*scale, 32*scale, oRGBA(64, 224, 64, 255), 2*scale);
}
// "Creature Squares" sent by the server
if((map_x != 0 && map_y != 0) && (n->getSquareStart() + 1000) >= g_frameTime)
{
g_engine->drawRectangleLines(x + walkoffx, y + walkoffy, 34*scale, 34*scale, n->getSquareColor(), 2*scale);
}
for(uint32_t i = 0; i < m_obj->height; ++i){
for(uint32_t j = 0; j < m_obj->width; ++j){
ASSERT(activeframe + aframes < m_obj->numsprites);
int draw_x = (int)(x - j*32*scale + walkoffx);
int draw_y = (int)(y - i*32*scale + walkoffy);
if (m_gfx[activeframe + aframes])
m_gfx[activeframe + aframes]->Blit(draw_x, draw_y, 0, 0, 32, 32, 32*scale, 32*scale);
else
printf("Warning: rendering creature failed since sprite %d is NULL\n", activeframe+aframes);
if(m_obj->ydiv != 1){
if(n->getOutfit().m_addons & 1){
m_gfx[activeframe + partSize + aframes]->Blit(draw_x, draw_y, 0, 0, 32, 32, 32*scale, 32*scale);
}
if(n->getOutfit().m_addons & 2){
m_gfx[activeframe + partSize*2 + aframes]->Blit(draw_x, draw_y, 0, 0, 32, 32, 32*scale, 32*scale);
}
}
activeframe++;
}
}
}
}
void CreatureUI::drawInfo(int x, int y, float scale, bool visible) const
{
Creature* n = (Creature*)this;
Outfit_t outfit = n->getOutfit();
if(!m_obj && !n->isPlayer() && outfit.m_lookitem == 0)
return; //It is a creature which cannot be seen by a player.
// NOTE (kilouco): Here we will take some position based conditions for names and healthbars to be rendered.
Position playerPos = GlobalVariables::getPlayerPosition();
Position creaturePos = n->getCurrentPos();
int relative_x = creaturePos.x - playerPos.x;
int relative_y = creaturePos.y - playerPos.y;
if (abs(relative_x) > 7 || abs(relative_y) > 5)
return; //Shouldn't render names and health bars in these cases.
// NOTE (Kilouco): Generic position formula
float walkoffx = 0.f, walkoffy = 0.f;
getWalkOffset(walkoffx, walkoffy, scale);
int c_xOffSet = 0, c_yOffSet = 0, result_x = 0, result_y = 0;
if(m_obj) {
c_xOffSet = m_obj->xOffset;
c_yOffSet = m_obj->yOffset;
if(outfit.m_lookitem == 0 && outfit.m_looktype == 0) {
c_xOffSet = 8;
c_yOffSet = 8;
}
if (c_xOffSet != 8 || c_yOffSet != 8) {
c_xOffSet = 0;
c_xOffSet = 0;
}
}
result_x = (x + walkoffx + (16 * scale) - (c_xOffSet * scale)) - 13;
result_y = (y + walkoffy - (c_xOffSet * scale)) - 3;
if (result_y <= 0)
result_y = 12;
if (options.shownames)
drawName(result_x, result_y, scale, visible);
result_x += 23;
result_y += 4;
drawSkullsShields(result_x, result_y, scale);
}
void CreatureUI::drawName(int x, int y, float scale, bool visible) const
{
//Health Bar
Creature* n = (Creature*)this;
int hp = n->getHealth();
if (hp == 0 && (n->getID() != GlobalVariables::getPlayerID())) return;
oRGBA col = getHealthColor(hp);
// FIXME (nfries88): This is the wrong color! But it proves the code works.
if(!visible) col = oRGBA(255./2., 255./2., 255./2., 255.);
g_engine->drawRectangle(x-1, y-1, 28, 4, oRGBA(0,0,0,255));
g_engine->drawRectangle(x, y, 26*(hp/100.), 2, col);
//Name
std::string name = n->getName().c_str();
name[0] = toupper(name[0]);
// NOTE (Kilouco): Not calculated here anymore.
//volatile float centralizationoffset = (g_engine->sizeText(name.c_str(), "gamefont" ) / 2);
x = (x + 14); // - centralizationoffset);
y -= 12;
g_engine->drawTextGW(name.c_str(), "gamefont", x, y, scale, col);
}
void CreatureUI::drawSkullsShields(int x, int y, float scale) const
{
Creature* n = (Creature*)this;
Outfit_t outfit = n->getOutfit();
uint32_t shield = n->getShield();
switch(shield) {
case SHIELD_YELLOW:
g_engine->getUISprite()->Blit(x, y, 54, 236, 11, 11);
break;
case SHIELD_BLUE:
g_engine->getUISprite()->Blit(x, y, 65, 236, 11, 11);
break;
case SHIELD_WHITEYELLOW:
g_engine->getUISprite()->Blit(x, y, 76, 236, 11, 11);
break;
case SHIELD_WHITEBLUE:
g_engine->getUISprite()->Blit(x, y, 87, 236, 11, 11);
break;
case SHIELD_YELLOW_SHAREDEXP:
g_engine->getUISprite()->Blit(x, y, 76, 214, 11, 11);
break;
case SHIELD_BLUE_SHAREDEXP:
g_engine->getUISprite()->Blit(x, y, 87, 214, 11, 11);
break;
case SHIELD_YELLOW_NOSHAREDEXP_BLINK:
// TODO (nfries88): actually make this blink...
g_engine->getUISprite()->Blit(x, y, 168, 261, 11, 11);
break;
case SHIELD_YELLOW_NOSHAREDEXP:
g_engine->getUISprite()->Blit(x, y, 168, 261, 11, 11);
break;
case SHIELD_BLUE_NOSHAREDEXP_BLINK:
// TODO (nfries88): actually make this blink...
g_engine->getUISprite()->Blit(x, y, 179, 261, 11, 11);
break;
case SHIELD_BLUE_NOSHAREDEXP:
g_engine->getUISprite()->Blit(x, y, 179, 261, 11, 11);
break;
default:
break;
}
// NOTE (nfries88): 11 for width of shield, 2 for padding
if(shield != SHIELD_NONE) x += 13;
uint32_t skull = n->getSkull();
switch (skull) {
case SKULL_GREEN:
g_engine->getUISprite()->Blit(x, y, 54, 225, 11, 11);
break;
case SKULL_YELLOW:
g_engine->getUISprite()->Blit(x, y, 65, 225, 11, 11);
break;
case SKULL_WHITE:
g_engine->getUISprite()->Blit(x, y, 76, 225, 11, 11);
break;
case SKULL_RED:
g_engine->getUISprite()->Blit(x, y, 87, 225, 11, 11);
break;
case SKULL_BLACK:
g_engine->getUISprite()->Blit(x, y, 98, 297, 11, 11);
break;
default: break;
}
// NOTE (nfries88): 11 for width of skull, 2 for padding
if(skull != SKULL_NONE) x += 13;
uint32_t emblem = n->getEmblem();
switch(emblem) {
case EMBLEM_GREEN:
g_engine->getUISprite()->Blit(x, y, 287, 218, 11, 11);
break;
case EMBLEM_RED:
g_engine->getUISprite()->Blit(x, y, 298, 218, 11, 11);
break;
case EMBLEM_BLUE:
g_engine->getUISprite()->Blit(x, y, 309, 218, 11, 11);
break;
default:
break;
}
}
oRGBA CreatureUI::getHealthColor(int hp)
{
oRGBA col;
if (hp > 92.0) {
col = oRGBA(0., 188., 0., 255.);
}
else if (hp > 60.0) {
col = oRGBA(80., 161., 80., 255.);
}
else if (hp > 30.0) {
col = oRGBA(161., 161., 0., 255.);
}
else if (hp > 8.0) {
col = oRGBA(160., 39., 39., 255.);
}
else if (hp > 3.0) {
col = oRGBA(160., 0., 0., 255.);
}
else {
col = oRGBA(79., 0., 0., 255.);
}
return col;
}
void CreatureUI::getWalkOffset(float &walkoffx, float &walkoffy, float scale) const
{
Creature* n = (Creature*)this;
float tilesize = 32*scale;
switch(n->getLookDir()) {
case DIRECTION_WEST:
walkoffx = -m_walkState * tilesize + (m_preWalk ? 0 : tilesize);
break;
case DIRECTION_EAST:
walkoffx = m_walkState * tilesize - (m_preWalk ? 0 : tilesize);
break;
case DIRECTION_NORTH:
walkoffy = -m_walkState * tilesize + (m_preWalk ? 0 : tilesize);
break;
case DIRECTION_SOUTH:
walkoffy = m_walkState * tilesize - (m_preWalk ? 0 : tilesize);
break;
case DIRECTION_NE:
walkoffx = m_walkState * tilesize - (m_preWalk ? 0 : tilesize);
walkoffy = -m_walkState * tilesize + (m_preWalk ? 0 : tilesize);
break;
case DIRECTION_SE:
walkoffx = m_walkState * tilesize - (m_preWalk ? 0 : tilesize);
walkoffy = m_walkState * tilesize - (m_preWalk ? 0 : tilesize);
break;
case DIRECTION_NW:
walkoffx = -m_walkState * tilesize + (m_preWalk ? 0 : tilesize);
walkoffy = -m_walkState * tilesize + (m_preWalk ? 0 : tilesize);
break;
case DIRECTION_SW:
walkoffx = -m_walkState * tilesize + (m_preWalk ? 0 : tilesize);
walkoffy = m_walkState * tilesize - (m_preWalk ? 0 : tilesize);
break;
}
}
void CreatureUI::advanceWalk(int groundspeed)
{
Creature *n = (Creature*)this;
if (m_walkState >= 1) {
m_walkState = 1;
return;
}
m_walkState += (n->getSpeed() / (float)groundspeed) * (g_frameDiff/1000.);
if (m_walkState >= 1)
m_walkState = 1;
}
void CreatureUI::loadOutfit()
{
Creature* n = (Creature*)this;
unloadGfx();
setupObject();
if (!m_obj)
return;
Outfit_t outfit = n->getOutfit();
for(uint32_t i = 0; i < m_obj->numsprites ; i++){
Sprite* spr;
if(m_obj->blendframes > 1){
// a templated creature!
// behold the magic!
if((i / (m_obj->height * m_obj->width)) % 2 ) {
// if it's a template, then let's just put a NULL in there
m_gfx.insert(m_gfx.end(), NULL);
continue;
}
else{
// so it's not a template, but regular graphics.
// great! let's colorize it using a template.
ASSERT(i + m_obj->height * m_obj->width < m_obj->numsprites);
ASSERT(m_obj->imageData)
spr = g_engine->createSprite("Tibia.spr", m_obj->imageData[i]);
Sprite* templatespr = g_engine->createSprite("Tibia.spr", m_obj->imageData[i + m_obj->height * m_obj->width]);
spr->templatedColorize(templatespr, outfit.m_lookhead, outfit.m_lookbody, outfit.m_looklegs, outfit.m_lookfeet);
delete templatespr;
}
}
else{
// it's not templated.
// so sad. let's just load regular graphics
ASSERT(m_obj->imageData)
spr = g_engine->createSprite("Tibia.spr", m_obj->imageData[i]);
}
m_gfx.insert(m_gfx.end(), spr);
}
}
bool CreatureUI::isLoaded() const {
if (!m_gfx.size()) {
if (!m_obj) {
printf("No object, need to reload gfx\n");
return false;
}
if (m_obj->numsprites > 0) {
printf("Need to reload gfx\n");
return false;
}
}
return true;
}
void CreatureUI::setupObject() {
Creature* n = (Creature*)this;
Outfit_t outfit = n->getOutfit();
m_startTime = g_frameTime;
if (!m_obj) {
if(!outfit.m_looktype && !outfit.m_lookitem){
// NOTE (nfries88): Kilouco reports 0x0D is proper effect for when invisible.
m_obj = Objects::getInstance()->getEffectType(0x0D);
}
else if(outfit.m_looktype == 0 && outfit.m_lookitem != 0){
m_obj = Objects::getInstance()->getItemType(outfit.m_lookitem);
}
else{
m_obj = Objects::getInstance()->getOutfitType(outfit.m_looktype);
}
if (!m_obj) {
printf("Could not load outfit of type %d\n", outfit.m_looktype);
return;
}
if (!m_obj->isGfxLoaded()) {
m_obj->loadGfx();
}
}
if(!m_obj)printf("FAILED to create outfit.\n");
}