1 /***************************************************************************
2 * Mechanized Assault and Exploration Reloaded Projectfile *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
20 #include "input/mouse/cursor/mousecursorattack.h"
21
22 #include "main.h"
23 #include "video.h"
24 #include "game/data/units/unit.h"
25 #include "game/data/map/map.h"
26 #include "game/logic/attackjob.h"
27 #include "utility/position.h"
28
29 //------------------------------------------------------------------------------
cMouseCursorAttack()30 cMouseCursorAttack::cMouseCursorAttack() :
31 currentHealthPercent (-1),
32 newHealthPercent (-1),
33 inRange (true)
34 {}
35
36 //------------------------------------------------------------------------------
cMouseCursorAttack(const cUnit & sourceUnit,const cPosition & targetPosition,const cMap & map)37 cMouseCursorAttack::cMouseCursorAttack (const cUnit& sourceUnit, const cPosition& targetPosition, const cMap& map) :
38 currentHealthPercent (-1),
39 newHealthPercent (-1),
40 inRange (sourceUnit.isInRange (targetPosition))
41 {
42 const sUnitData& data = sourceUnit.data;
43 const cUnit* target = cAttackJob::selectTarget (targetPosition, data.canAttack, map, sourceUnit.getOwner());
44
45 if (target && (target != &sourceUnit))
46 {
47 currentHealthPercent = 100 * target->data.getHitpoints() / target->data.getHitpointsMax();
48 newHealthPercent = 100 * target->calcHealth (data.getDamage()) / target->data.getHitpointsMax();
49 }
50 assert (currentHealthPercent >= newHealthPercent);
51 }
52
53 //------------------------------------------------------------------------------
cMouseCursorAttack(int currentHealthPercent_,int newHealthPercent_,bool inRange_)54 cMouseCursorAttack::cMouseCursorAttack (int currentHealthPercent_, int newHealthPercent_, bool inRange_) :
55 currentHealthPercent (currentHealthPercent_),
56 newHealthPercent (newHealthPercent_),
57 inRange (inRange_)
58 {
59 assert (currentHealthPercent >= newHealthPercent);
60 }
61
62 //------------------------------------------------------------------------------
getSurface() const63 SDL_Surface* cMouseCursorAttack::getSurface() const
64 {
65 if (surface == nullptr) generateSurface();
66 return surface.get();
67 }
68
69 //------------------------------------------------------------------------------
getHotPoint() const70 cPosition cMouseCursorAttack::getHotPoint() const
71 {
72 return cPosition (19, 19);
73 }
74
75 //------------------------------------------------------------------------------
equal(const cMouseCursor & other) const76 bool cMouseCursorAttack::equal (const cMouseCursor& other) const
77 {
78 auto other2 = dynamic_cast<const cMouseCursorAttack*> (&other);
79 return other2 && other2->currentHealthPercent == currentHealthPercent && other2->newHealthPercent == newHealthPercent && other2->inRange == inRange;
80 }
81
82 //------------------------------------------------------------------------------
generateSurface() const83 void cMouseCursorAttack::generateSurface() const
84 {
85 SDL_Surface* sourceSurface = inRange ? GraphicsData.gfx_Cattack.get() : GraphicsData.gfx_Cattackoor.get();
86 surface = AutoSurface (SDL_CreateRGBSurface (0, sourceSurface->w, sourceSurface->h, Video.getColDepth(), 0, 0, 0, 0));
87
88 SDL_FillRect (surface.get(), nullptr, 0xFF00FF);
89 SDL_SetColorKey (surface.get(), SDL_TRUE, 0xFF00FF);
90
91 SDL_BlitSurface (sourceSurface, nullptr, surface.get(), nullptr);
92
93 const int barWidth = 35;
94
95 if (currentHealthPercent < 0 || currentHealthPercent > 100 || newHealthPercent < 0 || newHealthPercent > 100)
96 {
97 SDL_Rect rect = {1, 29, barWidth, 3};
98 SDL_FillRect (surface.get(), &rect, 0);
99 }
100 else
101 {
102 const auto currentHealthWidth = static_cast<int> (currentHealthPercent / 100. * barWidth);
103 const auto newHealthWidth = static_cast<int> (newHealthPercent / 100. * barWidth);
104
105 SDL_Rect rect = {1, 29, newHealthWidth, 3};
106 SDL_FillRect (surface.get(), &rect, 0x00FF00);
107
108 rect.x += rect.w;
109 rect.w = currentHealthWidth - newHealthWidth;
110
111 SDL_FillRect (surface.get(), &rect, 0xFF0000);
112
113 rect.x += rect.w;
114 rect.w = barWidth - currentHealthWidth;
115
116 SDL_FillRect (surface.get(), &rect, 0);
117 }
118 }
119