-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCBAS_Actor.cpp
315 lines (249 loc) · 10.6 KB
/
CBAS_Actor.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
#include "CBAS_Actor.h"
//constants
#define DEFAULT_ATTACK_SPEED 1.0f //in case of emergency, default aspd
#define TOUCH_INTERVAL 200 //CBAS code is run every frame during weapon action but to improve performance we only check each animdata pointer once every 0.2s
#define UPDATE_INTERVAL 2000 //attack speed updated every 2000 or if wep changed
#define CLEANSE_CHECK_INTERVAL 60000 //check if any actors need to be removed from the map every 1 min
#define CLEANSE_INTERVAL 30000 //actors are removed from the map if they haven't been updated in last 30 seconds
//functions
#define TIME_PASSED(t,i) ((t + i) < GetTickCount())
#define GETINI( x ) (*CBAS_IniHandler)(CBAS_Config::IniEntries::x)
#define DIFF(d) SInt32(d - compareWith->d)
#define A_DIFF(x) (abs(DIFF(x))
#define AA_DIFF(y,z) (A_DIFF(y) > z))
/*************************
Actor Values
*************************/
CBAS_ActorVals::CBAS_ActorVals(Actor* a) :
SPD(a->GetActorValue(kActorVal_Speed)),
AGI(a->GetActorValue(kActorVal_Agility)),
_FTG(a->GetBaseActorValue(kActorVal_Fatigue))
{
t = GetTickCount();
//Weapon info
actorRef = a->refID;
wep = GetEquippedWeapon(a);
}
bool CBAS_ActorVals::OutOfDate(CBAS_ActorVals* compareWith) {
return (AA_DIFF(wep->ref,0) || ((t + UPDATE_INTERVAL) < compareWith->t) || AA_DIFF(_FTG,10));
}
void CBAS_ActorVals::UpdateAttackSpeed(Actor* a) {
//attack speed being updated, so update time
t = GetTickCount();
aSpd = DEFAULT_ATTACK_SPEED;
const SInt32 STR = a->GetActorValue(kActorVal_Strength);
//Attack [F]actors
//
float ftgF = CBAS_Lib::FatigueFactor(a,_FTG,GETINI(CBAS_FatigueThreshold)); //fatigue
float encF = CBAS_Lib::EncumbranceFactor(a,STR,GETINI(CBAS_LocalisedEncumbrance)); //encumbrance
float skillF = CBAS_Lib::SkillFactor(a,wep->type); //skill in equipped weapon (e.g. blade, blunt)
float attrF = CBAS_Lib::AttrFactor(a,STR,wep->type,wep->weight); //main attribute factor, depends on weapon type, e.g. melee weapons use str, bows use agi
float alacrF = SPD > AGI ? SPD*.01f : AGI*.01f; //
if( (*CBAS_IniHandler)() ){ //this gets whether or not we should use all components
aSpd = ftgF*encF*skillF*attrF*alacrF;
} else {
aSpd =
PRSNK_UNDERWEIGH(ftgF,GETINI(CBAS_FatigueComponent)) *
PRSNK_UNDERWEIGH(encF,GETINI(CBAS_FatigueComponent)) *
PRSNK_UNDERWEIGH(skillF,GETINI(CBAS_SkillComponent)) *
PRSNK_UNDERWEIGH(attrF,GETINI(CBAS_AttributeComponent)) *
PRSNK_UNDERWEIGH(alacrF,GETINI(CBAS_AlacrityComponent));
}
#if _DEBUG
_MESSAGE(" Calculating attack speed...");
_MESSAGE(" Fatigue factor: %f",ftgF);
_MESSAGE(" Encumbrance factor: %f",encF);
_MESSAGE(" Skill factor: %f",skillF);
_MESSAGE(" Attribute factor: %f",attrF);
_MESSAGE(" Alacrity factor: %f",alacrF);
_MESSAGE(" MaxSpeed [weight: %f]: %f",wep->weight,PRSNK_MAX_WEAP_SPEED(wep->weight));
_MESSAGE(" Full modification factor (PRE PRSNK %f)",aSpd);
#endif
aSpd = PRSNK_FACTOR_CONFIG(aSpd,CBAS_IniHandler->LowComplement,CBAS_IniHandler->LowMult);
_DOUT(" Final calculation speed: [%f x %f]",PRSNK_MAX_WEAP_SPEED(wep->weight),aSpd);
aSpd *= PRSNK_MAX_WEAP_SPEED(wep->weight);
_DOUT(" Raw (POST PRSNK): %f",aSpd);
aSpd *= GETINI(CBAS_FinalMult);
_DOUT(" Final (Config Multiplier): %f",aSpd);
}
/*************************
Actor Tracker
*************************/
CBAS_Actors::CBAS_Actors() {
t_c = GetTickCount(); //construct with current time as last cleansed time(s)
}
//cleanse actor map every so often so long play sessions don't fill up map with actor data
void CBAS_Actors::CleanseActorMap() {
t_c = GetTickCount();
_DOUT(" Cleansing old actors!");
CBAS_AVMap::iterator avIter;
for( avIter = ActorValuesMap.begin(); avIter != ActorValuesMap.end(); avIter++){
_DOUT(" Checking actor %s (%x)...",GetFullName(LookupFormByID(avIter->first)),avIter->first);
if( TIME_PASSED(avIter->second->t,CLEANSE_INTERVAL) ) {
_DOUT(" Cleansing actor!");
ActorValuesMap.erase(avIter->first); //erasing an actor from the map, invalidating the iterator
CleanseActorMap(); //start a new loop in case there are any more actors to erase
break;
}
}
}
void CBAS_Actors::CleanseAnimDataMap() {
t_c = GetTickCount();
_DOUT(" Cleansing old animdata!");
CBAS_AADMap::iterator aadIter;
for( aadIter = AnimDataMap.begin(); aadIter != AnimDataMap.end(); aadIter++){
_DOUT(" Checking animdata %i...",GetFullName(LookupFormByID(aadIter->first)),aadIter->first);
if( TIME_PASSED(aadIter->second,CLEANSE_INTERVAL) ) {
_DOUT(" Cleansing animdata!");
AnimDataMap.erase(aadIter->first); //erasing an actor from the map, invalidating the iterator
CleanseActorMap(); //start a new loop in case there are any more actors to erase
break;
}
}
}
void CBAS_Actors::LoadIniValues() {
#ifdef _DEBUG
delete CBAS_IniHandler;
#endif
CBAS_IniHandler = new CBAS_Config::IniHandler();
}
float CBAS_Actors::AttackSpeedFromActor(Actor* a){
bool actorExists = ActorValuesMap.count(a->refID) > 0; //first find out if this actor already exists in the map
bool update = false; //do we need to calc a new attack speed for the actor?
CBAS_ActorVals* AV;
CBAS_ActorVals *n_AV = new CBAS_ActorVals(a); //new actor vals to compare against
//No need to proceed if actor is wielding a weapon type that is disabled
if( ((n_AV->wep->type == CBAS_Lib::kType_Bow) && !GETINI(CBAS_Bows))
|| ((n_AV->wep->type == CBAS_Lib::kType_Staff) && !GETINI(CBAS_Staves))
|| ((n_AV->wep->type == CBAS_Lib::kType_HandToHand) && !GETINI(CBAS_Hands)) ) {
return 1.0;
}
if( actorExists ){
AV = ActorValuesMap[a->refID];
AV = ActorValuesMap.at(a->refID); //get actor from map
update = AV->OutOfDate(n_AV); //is this actor out of date?
}
if( !actorExists || update ){
//use the new actor as the current one in the map
AV = n_AV;
#if _DEBUG
DWORD ticks = (AV->t - t_c) / 1000;
const DWORD seconds = ticks % 60;
ticks /= 60;
const DWORD minutes = ticks % 60;
ticks /= 60;
const DWORD hours = ticks; // may exceed 24 hours.
const char* addOr = actorExists ? "Updating" : "Adding";
_MESSAGE("[Time: %d:%02d:%02d (%d)]: %s actor %s (%X)!",hours, minutes, seconds, AV->t,addOr,GetFullName(a),a->refID);
#endif
//calculate attack speed for new actor
AV->UpdateAttackSpeed(a);
//put new actor in place
ActorValuesMap[AV->actorRef] = AV;
}
//clean old actors every so often
if( update && TIME_PASSED(t_c,CLEANSE_CHECK_INTERVAL) ) { CleanseActorMap(); CleanseAnimDataMap(); }
#if _DEBUG
//TESObjectWEAP* eqObj = (TESObjectWEAP*)LookupFormByID(AV->wepRef);
//_MESSAGE("[Time: %d]: Returning attack speed for actor %s using weapon %s with weight %f to %f!",AV->t,GetFullName(a),GetFullName(eqObj),AV->wWgt,AV->aSpd);
//_MESSAGE("(Actor existed in map? %d. Actor needed updating? %d.)",actorExists, update);
#endif
return AV->aSpd;
}
void CBAS_Actors::SetAttackSpeed(void* p){
//_DOUT("SetAttackSpeed: %p at %i...",p,GetTickCount());
bool aadExist = AnimDataMap.count((uintptr_t)p) > 0; //find out if this animdata pointer has already an entry in the map
bool touched = false; //has this pointer actually been processed fully or is it still not time
#if _DEBUG
if(aadExist){
//_MESSAGE("%p timings: (%i - %i = %i)",p,GetTickCount(),AnimDataMap.at((uintptr_t)p),(GetTickCount() - AnimDataMap.at((uintptr_t)p)));
}
#endif
if( aadExist && !TIME_PASSED(AnimDataMap.at((uintptr_t)p), TOUCH_INTERVAL) ){
//_DOUT("Not checking %p because too soon (%i - %i = %i)",p,GetTickCount(),AnimDataMap.at((uintptr_t)p),(GetTickCount() - AnimDataMap.at((uintptr_t)p)));
return; //skip any further processing on this pointer
}
touched = true;
//_DOUT("Received pointer %p, attempting to get animdata...",p);
if ( Actor* a = ResolveActorFromAnimData(p) ){
//if it's a creature, and we're ignoring creatures... ignore
if( !GETINI(CBAS_Creatures) ) {
_DOUT("Not using creatures!");
if(TESCreature* creat = GetCreature(a)) {
_DOUT("%s (%X) is a creature! Exiting...",GetFullName(a),a->refID);
return;
}
}
//_DOUT(" GOT ACTOR FROM %p",p);
//Change the time period of the animation to the desired attack speed
_DOUT("CURRENT PERIOD IS: %f",(reinterpret_cast<CBAS_ActorAnimData*>(p))->t_period);
(reinterpret_cast<CBAS_ActorAnimData*>(p))->t_period = AttackSpeedFromActor(a);
} else {
//if we can't get actor
_DOUT(" Didn't get actor from %p",p);
}
if( !aadExist || touched ) {
AnimDataMap[(uintptr_t)p] = GetTickCount(); //update map
_DOUT("Updating map at %x to be %i because either %p didn't exist: %i, or touched: %i",(uintptr_t)p,GetTickCount(),p,!aadExist,touched);
}
}
Actor* CBAS_Actors::ResolveActorFromAnimData(void* p){
if( CBAS_ActorAnimData* c_aad = reinterpret_cast<CBAS_ActorAnimData*>(p) ){
if( NiNode* niNode = c_aad->niNode04 ){
if( niNode->m_extraDataList && niNode->m_extraDataListLen > 2 ){
NiExtraData* niExD = niNode->m_extraDataList[2];
if( std::string(niExD->GetType()->name) == "TESObjectExtraData" ){
if( Actor* ActorFromAnimData = reinterpret_cast<Actor*>(((TESObjectExtraData*)niExD)->refr) ){
return ActorFromAnimData;
PRSNK_ERR(" Failed to cast actor at last hurdle! %s",niExD->GetType()->name);
}
PRSNK_ERR(" NiExtraData does not match TESObjectExtraData! %s",niExD->GetType()->name);
}
PRSNK_ERR(" NiNode's extradatalist not found or too short from node at %p",niNode);
}
PRSNK_ERR(" NiNode not found from animdata at %p",c_aad);
}
PRSNK_ERR(" Animdata not found at %p",p);
}
return NULL;
}
#if 0
Actor* CBAS_Actors::ResolveActorFromAnimData(void* p){
if( CBAS_ActorAnimData* c_aad = reinterpret_cast<CBAS_ActorAnimData*>(p) ){
if( NiNode* niNode = c_aad->niNode04 ){
if( niNode->m_extraDataList && niNode->m_extraDataListLen > 2 ){
NiExtraData* niExD = niNode->m_extraDataList[2];
if( std::string(niExD->GetType()->name) == "TESObjectExtraData" ){
if( Actor* ActorFromAnimData = reinterpret_cast<Actor*>(((TESObjectExtraData*)niExD)->refr) ){
#ifdef _DEBUG
_MESSAGE(" Got actor %s at last hurdle from: %s",GetFullName(ActorFromAnimData),ActorFromAnimData->refID,niNode->m_extraDataList[2]->GetType()->name);
#endif
return ActorFromAnimData;
#ifdef _DEBUG
} else {
_MESSAGE(" Failed to cast actor at last hurdle! %s",niExD->GetType()->name);
#endif
}
#ifdef _DEBUG
} else {
_MESSAGE(" NiExtraData does not match TESObjectExtraData! %s",niExD->GetType()->name);
#endif
}
#ifdef _DEBUG
} else {
_MESSAGE(" NiNode's extradatalist not found or too short from node at %p",niNode);
#endif
}
#ifdef _DEBUG
} else {
_MESSAGE(" NiNode not found from animdata at %p",c_aad);
#endif
}
#ifdef _DEBUG
} else {
_MESSAGE(" Animdata not found at %p",p);
#endif
}
return NULL;
}
#endif