1 ////////////////////////////////////////////////////////////////////////////////
2 // Copyright (C) 2004-2007 by The Allacrost Project
3 // All Rights Reserved
4 //
5 // This code is licensed under the GNU GPL version 2. It is free software
6 // and you may modify it and/or redistribute it under the terms of this license.
7 // See http://www.gnu.org/copyleft/gpl.html for details.
8 ////////////////////////////////////////////////////////////////////////////////
9
10 /** ****************************************************************************
11 *** \file global_utils.cpp
12 *** \author Tyler Olsen, rootslinux@allacrost.org
13 *** \brief Source file for global game utility code
14 *** ***************************************************************************/
15
16 #include "defs.h"
17 #include "utils.h"
18
19 #include "system.h"
20
21 #include "global.h"
22 #include "global_objects.h"
23 #include "global_utils.h"
24
25 using namespace std;
26
27 using namespace hoa_utils;
28
29 using namespace hoa_system;
30
31 using namespace hoa_global::private_global;
32
33 namespace hoa_global {
34
GetTargetText(GLOBAL_TARGET target)35 string GetTargetText(GLOBAL_TARGET target) {
36 switch (target) {
37 case GLOBAL_TARGET_SELF_POINT:
38 return Translate("Self — Point");
39 case GLOBAL_TARGET_ALLY_POINT:
40 return Translate("Ally — Point");
41 case GLOBAL_TARGET_FOE_POINT:
42 return Translate("Foe — Point");
43 case GLOBAL_TARGET_SELF:
44 return Translate("Self");
45 case GLOBAL_TARGET_ALLY:
46 return Translate("Ally");
47 case GLOBAL_TARGET_FOE:
48 return Translate("Foe");
49 case GLOBAL_TARGET_ALL_ALLIES:
50 return Translate("All Allies");
51 case GLOBAL_TARGET_ALL_FOES:
52 return Translate("All Foes");
53 default:
54 return Translate("Invalid Target");
55 }
56 }
57
58
59
IsTargetPoint(GLOBAL_TARGET target)60 bool IsTargetPoint(GLOBAL_TARGET target) {
61 if ((target == GLOBAL_TARGET_SELF_POINT) || (target == GLOBAL_TARGET_ALLY_POINT) || (target == GLOBAL_TARGET_FOE_POINT))
62 return true;
63 else
64 return false;
65 }
66
67
68
IsTargetActor(GLOBAL_TARGET target)69 bool IsTargetActor(GLOBAL_TARGET target) {
70 if ((target == GLOBAL_TARGET_SELF) || (target == GLOBAL_TARGET_ALLY) || (target == GLOBAL_TARGET_FOE))
71 return true;
72 else
73 return false;
74 }
75
76
77
IsTargetParty(GLOBAL_TARGET target)78 bool IsTargetParty(GLOBAL_TARGET target) {
79 if ((target == GLOBAL_TARGET_ALL_ALLIES) || (target == GLOBAL_TARGET_ALL_FOES))
80 return true;
81 else
82 return false;
83 }
84
85
86
IsTargetSelf(GLOBAL_TARGET target)87 bool IsTargetSelf(GLOBAL_TARGET target) {
88 if ((target == GLOBAL_TARGET_SELF_POINT) || (target == GLOBAL_TARGET_SELF))
89 return true;
90 else
91 return false;
92 }
93
94
95
IsTargetAlly(GLOBAL_TARGET target)96 bool IsTargetAlly(GLOBAL_TARGET target) {
97 if ((target == GLOBAL_TARGET_ALLY_POINT) || (target == GLOBAL_TARGET_ALLY) || (target == GLOBAL_TARGET_ALL_ALLIES))
98 return true;
99 else
100 return false;
101 }
102
103
104
IsTargetFoe(GLOBAL_TARGET target)105 bool IsTargetFoe(GLOBAL_TARGET target) {
106 if ((target == GLOBAL_TARGET_FOE_POINT) || (target == GLOBAL_TARGET_FOE) || (target == GLOBAL_TARGET_ALL_FOES))
107 return true;
108 else
109 return false;
110 }
111
112
113
GlobalCreateNewObject(uint32 id,uint32 count)114 GlobalObject* GlobalCreateNewObject(uint32 id, uint32 count) {
115 GlobalObject* new_object = NULL;
116
117 if ((id > 0) && (id <= MAX_ITEM_ID))
118 new_object = new GlobalItem(id, count);
119 else if ((id > MAX_ITEM_ID) && (id <= MAX_WEAPON_ID))
120 new_object = new GlobalWeapon(id, count);
121 else if ((id > MAX_WEAPON_ID) && (id <= MAX_LEG_ARMOR_ID))
122 new_object = new GlobalArmor(id, count);
123 else if ((id > MAX_LEG_ARMOR_ID) && (id <= MAX_SHARD_ID))
124 new_object = new GlobalShard(id, count);
125 else if ((id > MAX_SHARD_ID) && (id <= MAX_KEY_ITEM_ID))
126 new_object = new GlobalKeyItem(id, count);
127 else
128 IF_PRINT_WARNING(GLOBAL_DEBUG) << "function received an invalid id argument: " << id << endl;
129
130 return new_object;
131 }
132
133
134
IncrementIntensity(GLOBAL_INTENSITY & intensity,uint8 amount)135 bool IncrementIntensity(GLOBAL_INTENSITY& intensity, uint8 amount) {
136 if (amount == 0)
137 return false;
138 if ((intensity <= GLOBAL_INTENSITY_INVALID) || (intensity >= GLOBAL_INTENSITY_POS_EXTREME))
139 return false;
140
141 // This check protects against overflow conditions
142 if (amount > (GLOBAL_INTENSITY_TOTAL * 2)) {
143 IF_PRINT_WARNING(GLOBAL_DEBUG) << "attempted to increment intensity by an excessive amount: " << amount << endl;
144 if (intensity == GLOBAL_INTENSITY_POS_EXTREME) {
145 return false;
146 }
147 else {
148 intensity = GLOBAL_INTENSITY_POS_EXTREME;
149 return true;
150 }
151 }
152
153 intensity = GLOBAL_INTENSITY(intensity + amount);
154 if (intensity >= GLOBAL_INTENSITY_TOTAL)
155 intensity = GLOBAL_INTENSITY_POS_EXTREME;
156 return true;
157 }
158
159
160
DecrementIntensity(GLOBAL_INTENSITY & intensity,uint8 amount)161 bool DecrementIntensity(GLOBAL_INTENSITY& intensity, uint8 amount) {
162 if (amount == 0)
163 return false;
164 if ((intensity <= GLOBAL_INTENSITY_NEG_EXTREME) || (intensity >= GLOBAL_INTENSITY_TOTAL))
165 return false;
166
167 // This check protects against overflow conditions
168 if (amount > (GLOBAL_INTENSITY_TOTAL * 2)) {
169 IF_PRINT_WARNING(GLOBAL_DEBUG) << "attempted to decrement intensity by an excessive amount: " << amount << endl;
170 if (intensity == GLOBAL_INTENSITY_NEG_EXTREME) {
171 return false;
172 }
173 else {
174 intensity = GLOBAL_INTENSITY_NEG_EXTREME;
175 return true;
176 }
177 }
178
179 intensity = GLOBAL_INTENSITY(intensity - amount);
180 if (intensity <= GLOBAL_INTENSITY_INVALID)
181 intensity = GLOBAL_INTENSITY_NEG_EXTREME;
182 return true;
183 }
184
185 } // namespace hoa_global
186