Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
temp
  • Loading branch information
anatoliy-savchak committed Feb 25, 2024
1 parent c2dc994 commit d72d6b3
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 21 deletions.
4 changes: 2 additions & 2 deletions TemplePlus/action_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ uint32_t ActionSequenceSystem::MoveSequenceParse(D20Actn* d20aIn, ActnSeq* actSe
| PathQueryFlags::PQF_TARGET_OBJ | PathQueryFlags::PQF_ADJUST_RADIUS | PathQueryFlags::PQF_ADJ_RADIUS_REQUIRE_LOS);


if (reach < 0.1){ reach = 3.0; }
if (reach < 0.1){ reach = 1.0; }
actSeq->targetObj = d20a->d20ATarget;
pathQ.distanceToTargetMin = distToTgtMin * INCH_PER_FEET;
pathQ.tolRadius = reach * INCH_PER_FEET - fourPointSevenPlusEight;
Expand Down Expand Up @@ -1143,7 +1143,7 @@ uint32_t ActionSequenceSystem::MoveSequenceParse(D20Actn* d20aIn, ActnSeq* actSe

// find path
*pathfindingSys.rollbackSequenceFlag = 0;
if ( (d20aCopy.d20Caf & D20CAF_CHARGE ) || d20a->d20ActType == D20A_RUN || d20a->d20ActType == D20A_DOUBLE_MOVE)
if ( (d20aCopy.d20Caf & D20CAF_CHARGE ) || d20a->d20ActType == D20A_RUN /* || d20a->d20ActType == D20A_DOUBLE_MOVE*/)
{
*reinterpret_cast<int*>(&pathQ.flags) |= PQF_DONT_USE_PATHNODES; // so it runs in a straight line
}
Expand Down
110 changes: 109 additions & 1 deletion TemplePlus/ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,16 @@ objHndl AiSystem::FindSuitableTarget(objHndl handle){
if (aiSearchingTgt)
return objHndl::null;

auto kosCandidate = objHndl::null;

py::tuple args = py::make_tuple(py::cast<objHndl>(handle), py::cast<int>(aiSearchingTgt));
auto pyResult = pythonObjIntegration.ExecuteScript("d20_ai.targeting", "find_suitable_target", args.ptr());
if (pyResult && PyObjHndl_Check(pyResult) && ConvertObjHndl(pyResult, &kosCandidate)) {
if (!objSystem->IsValidHandle(kosCandidate))
kosCandidate = objHndl::null;
}
return kosCandidate;

// begin search section
aiSearchingTgt = 1;

Expand Down Expand Up @@ -863,7 +873,6 @@ objHndl AiSystem::FindSuitableTarget(objHndl handle){
}
}

auto kosCandidate = objHndl::null;
for (auto i =0; i < numCritters; i++){
auto target = critterList[i];

Expand Down Expand Up @@ -1071,6 +1080,16 @@ void AiSystem::GetAiFightStatus(objHndl handle, AiFightStatus* status, objHndl*

void AiSystem::AlertAllies(objHndl handle, objHndl alertFrom, int rangeIdx){

auto kosCandidate = objHndl::null;

py::tuple args = py::make_tuple(py::cast<objHndl>(handle), py::cast<objHndl>(alertFrom));
auto pyResult = pythonObjIntegration.ExecuteScript("d20_ai.targeting", "alert_allies", args.ptr());
if (PyInt_Check(pyResult)) {
auto result = _PyInt_AsInt(pyResult);
if (!result)
return;
}

auto rangeTiles = temple::GetRef<int[]>(0x102BD4D0)[rangeIdx];
auto tileDelta = locSys.GetTileDeltaMax(alertFrom, handle);

Expand Down Expand Up @@ -1145,6 +1164,14 @@ void AiSystem::AlertAlly(objHndl handle, objHndl alertFrom, objHndl alertDispatc

void AiSystem::AlertAllies2(objHndl handle, objHndl alertFrom)
{
py::tuple args = py::make_tuple(py::cast<objHndl>(handle), py::cast<objHndl>(alertFrom));
auto pyResult = pythonObjIntegration.ExecuteScript("d20_ai.targeting", "alert_allies2", args.ptr());
if (PyInt_Check(pyResult)) {
auto result = _PyInt_AsInt(pyResult);
if (!result)
return;
}

ObjList objList;
const int ALLY_ALERTING_DISTANCE = 24;
// test cases:
Expand Down Expand Up @@ -3448,6 +3475,83 @@ int AiSystem::AiTimeEventExpires(TimeEvent* evt)
return 1;
}

BOOL AiSystem::NpcWander(objHndl handle, int move)
{
static auto GetStandPointLoc = [](locXY& loc, objHndl critter) {
if (critterSys.GetLeader(critter).handle)
return 0;

auto is_day = gameSystems->GetTimeEvent().IsDaytime();
auto npc_flags = objSystem->GetObject(critter)->GetNPCFlags();
StandPoint sp;
if (is_day || ((npc_flags & NpcFlag::ONF_WAYPOINTS_DAY) && !scriptSys.GetGlobalFlag(144))) {
sp = critterSys.GetStandPoint(critter, StandPointType::Day);
}
else
sp = critterSys.GetStandPoint(critter, StandPointType::Night);
loc = sp.location.location;
if (loc.locx || loc.locy)
return 1;
return 0;
};

auto obj = objSystem->GetObject(handle);
auto isSleeping = critterSys.IsSleeping(handle);
locXY standPointLoc;
if (!isSleeping && obj->GetFlags() & (OF_DONTDRAW | OF_OFF) || !GetStandPointLoc(standPointLoc, handle))
return 0;

auto critterMapId = critterSys.GetCritterMap(handle);
auto currentMapId = maps.GetCurrentMapId();
if (critterMapId == currentMapId) {
if (!move) {
auto hour = temple::GetRef<uint32_t(__cdecl)()>(0x1005FF70)(); // GetElapsedTimeHour
if (hour == 6)
{
if (rngSys.GetInt(1, 1000) != 1) {
return 0;
}
}
else if (hour == 18 && (rngSys.GetInt(1, 1000) != 1)) {
return 0;
}
}
}
else if (!move)
return 0;

auto npcFlags = obj->GetNPCFlags();
int64_t threshhold = (npcFlags & (NpcFlag::ONF_WANDERS_IN_DARK | NpcFlag::ONF_WANDERS)) != 0 ? 4 : 1;
int64_t tileDelta = locSys.GetTileDeltaMaxBtwnLocs(standPointLoc, obj->GetLocation());
if (tileDelta > threshhold) {
if (!isSleeping) {
if (move) {
if (critterMapId == currentMapId) {
gameSystems->GetAnim().Interrupt(handle, AnimGoalPriority::AGP_4, false);
objects.Move(handle, LocAndOffsets::create(standPointLoc, 0, 0));
return 1;
}
}
else {
temple::GetRef<int(__cdecl)(objHndl handle, LocAndOffsets loc, float dist)>(0x10058590)(handle, LocAndOffsets::create(standPointLoc, 0, 0), 1.0f); // NpcWander_10058590
}
}
}
else if (!isSleeping)
{
if (npcFlags & NpcFlag::ONF_AI_SPREAD_OUT) {
//temple::GetRef<int(__cdecl)(objHndl handle, LocAndOffsets loc, float dist)>(0x10058590)(handle, LocAndOffsets::create(standPointLoc, 0, 0), 1.0f); // NpcWander_10058590
return 1;
}
if (npcFlags & NpcFlag::ONF_JILTED) {
temple::GetRef<int(__cdecl)(objHndl handle, uint64_t loc, uint32_t radius)>(0x1001A720)(handle, (uint64_t)standPointLoc, 4); // PickLockInRadius
return 1;
}
return 0;
}
return 1;
}

#pragma endregion

#pragma region AI replacement functions
Expand Down Expand Up @@ -4019,6 +4123,10 @@ class AiReplacements : public TempleFix
return result;
});

replaceFunction<BOOL(__cdecl)(objHndl, int)>(0x1005BC00, [](objHndl obj, int move) {
return aiSys.NpcWander(obj, move);
});

}
} aiReplacements;

Expand Down
2 changes: 1 addition & 1 deletion TemplePlus/ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ struct AiSystem : temple::AddressTable
bool AiProcessPc(objHndl handle);

int AiTimeEventExpires(TimeEvent* evt);

BOOL NpcWander(objHndl handle, int move);
private:
void (__cdecl *_ShitlistAdd)(objHndl npc, objHndl target);
void (__cdecl *_AiRemoveFromList)(objHndl npc, objHndl target, int listType);
Expand Down
6 changes: 4 additions & 2 deletions TemplePlus/animgoals/animgoals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2568,8 +2568,10 @@ AnimationGoals::AnimationGoals()

const AnimGoal & AnimationGoals::GetByType(AnimGoalType type) const
{
using AnimGoalArray = const AnimGoal*[82];
static auto mGoals = temple::GetRef<AnimGoalArray>(0x102BD1B0);
//using AnimGoalArray = const AnimGoal*[82];
//static auto mGoals = temple::GetRef<AnimGoalArray>(0x102BD1B0);
using AnimGoalArray = const AnimGoal* [82];
AnimGoalArray& goals = temple::GetRef<AnimGoalArray>(0x102BD1B0);

//return *mGoals[type];

Expand Down
6 changes: 5 additions & 1 deletion TemplePlus/animgoals/animgoals_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,11 @@ int GoalUnconcealCleanup(AnimSlot &slot) {
// Originally @ 0x10018050
int GoalResetToIdleAnim(AnimSlot &slot) {
static auto org = temple::GetRef<std::remove_pointer<GoalCallback>::type>(0x10018050);
return org(slot);
auto result = org(slot);
if (slot.animObj && slot.uniqueActionId) {
d20Sys.D20SignalPython(slot.animObj, "GoalResetToIdleAnim", slot.uniqueActionId, slot.id.uniqueId);
}
return result;
}

// Originally @ 0x10018160
Expand Down
2 changes: 1 addition & 1 deletion TemplePlus/condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct CondHashSystem : ToEEHashtableSystem < CondStruct >

uint32_t ConditionHashtableInit(ToEEHashtable<CondStruct> * hashtable)
{
return HashtableInit(hashtable, 1000);
return HashtableInit(hashtable, 2000);
}

uint32_t CondStructAddToHashtable(CondStruct * condStruct, bool overriding = false)
Expand Down
13 changes: 13 additions & 0 deletions TemplePlus/d20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,19 @@ void LegacyD20System::NewD20ActionsInit()

//d20Defs[D20A_DISARM] = d20Defs[D20A_STANDARD_ATTACK];
//d20Defs[d20Type].actionCost = _ActionCostNull; // just for testing - REMOVE!!!

d20Type = D20A_THROW_GRENADE;
//d20Defs[d20Type].addToSeqFunc = AddToSeqGrenade @0x10094840
//d20Defs[d20Type].turnBasedStatusCheck = actSeqSys.StdAttackTurnBasedStatusCheck;
//d20Defs[d20Type].actionCheckFunc = nullptr;
//d20Defs[d20Type].tgtCheckFunc = nullptr;
//d20Defs[d20Type].locCheckFunc = ActionCheckThrowGrenade @ 0x1008f690;
//d20Defs[d20Type].performFunc = PerformFuncThrowGrenade @ 0x1008fb50
//d20Defs[d20Type].actionFrameFunc = ActionFrameThrowGrenade @ 0x10090000
//d20Defs[d20Type].projectileHitFunc = ThrowGrenadeProjectilePerformFunc @ 0x1008fbe0
//d20Defs[d20Type].actionCost = ActionCostStandardAttack
//d20Defs[d20Type].seqRenderFunc = PickerFuncAttackChancesWithCoverProvidersHighlighted @ 0x1008f460

}

void LegacyD20System::InfinityEngineBullshit(){
Expand Down
2 changes: 1 addition & 1 deletion TemplePlus/fonts/fonts_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int FontRenderFix::FontDraw(const char* text, TigRect* extents, TigTextStyle* st
auto& layouter = tig->GetTextLayouter();

if (extents->x < 0 || extents->width < 0){
logger->warn("Negative Text extents! Aborting draw.");
//logger->warn("Negative Text extents! Aborting draw.");
return 0;
}

Expand Down
6 changes: 5 additions & 1 deletion TemplePlus/obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,11 @@ class ObjectReplacements : public TempleFix {
replaceFunction<BOOL(__cdecl)(objHndl)>(0x1002B390, [](objHndl handle) ->BOOL {
return objects.IsPlayerControlled(handle) ? TRUE:FALSE;
});
}

static BOOL(__cdecl * orgaiForceSpreadout)(objHndl, LocAndOffsets) = replaceFunction<BOOL(__cdecl)(objHndl, LocAndOffsets)>(0x1005A640, [](objHndl handle, LocAndOffsets location)->BOOL {
return orgaiForceSpreadout(handle, location);
});
}
} objReplacements;

int(*ObjectReplacements::orgMove)(objHndl, LocAndOffsets);
Expand Down
18 changes: 9 additions & 9 deletions TemplePlus/pathfinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ Pathfinding::Pathfinding() {

loc = &locSys;

aStarMaxTimeMs = 4000;
aStarMaxWindowMs = 5000;
aStarMaxTimeMs = 10000;
aStarMaxWindowMs = 10000;
aStarTimeIdx = -1;

memset(pathCache, 0, sizeof(pathCache));
Expand Down Expand Up @@ -1554,7 +1554,7 @@ int Pathfinding::FindPath(PathQuery* pq, PathQueryResult* pqr)
if (pq->critter)
{
pdbgMover = pq->critter;
logger->info("Starting path attempt for {}", description.getDisplayName(pdbgMover));
//logger->info("Starting path attempt for {}", description.getDisplayName(pdbgMover));
}
pdbgFrom = pq->from;
if ((pq->flags & PQF_TARGET_OBJ) && pq->targetObj )
Expand Down Expand Up @@ -1591,10 +1591,10 @@ int Pathfinding::FindPath(PathQuery* pq, PathQueryResult* pqr)
}

//if (!config.pathfindingDebugModeFlushCache )
if (PathCacheGet(pq, pqr)){
if (PathCacheGet(pq, pqr) && pqr->flags & PF_COMPLETE){
// has this query been done before? if so copies it and returns the result
if (config.pathfindingDebugMode || !combatSys.isCombatActive())
logger->info("Query found in cache, fetching result.");
//logger->info("Query found in cache, fetching result.");
return pqr->nodeCount;
}

Expand Down Expand Up @@ -1624,7 +1624,7 @@ int Pathfinding::FindPath(PathQuery* pq, PathQueryResult* pqr)
{
if (config.pathfindingDebugMode || !combatSys.isCombatActive())
{
logger->info("Attempting sans nodes...");
;// logger->info("Attempting sans nodes...");
}
gotPath = FindPathSansNodes(pq, pqr);
}
Expand All @@ -1651,7 +1651,7 @@ int Pathfinding::FindPath(PathQuery* pq, PathQueryResult* pqr)
if (config.pathfindingDebugMode || !combatSys.isCombatActive())
{
if (pq->critter)
logger->info("{} pathed successfully to {}", description.getDisplayName(pq->critter), pqr->to);
;// logger->info("{} pathed successfully to {}", description.getDisplayName(pq->critter), pqr->to);
}

pqr->flags |= PF_COMPLETE;
Expand Down Expand Up @@ -2099,10 +2099,10 @@ int Pathfinding::FindPathShortDistanceSansTarget(PathQuery* pq, Path* pqr)
int attemptCount;
if (objects.GetType(pq->critter) == obj_t_npc)
{
if (npcPathFindRefTime && (timeGetTime() - npcPathFindRefTime) < 1000 ) // && !pathNodeSys.hasClearanceData limits the number of attempt to 10 per second and cumulative time to 250 sec
if (npcPathFindRefTime && (timeGetTime() - npcPathFindRefTime) < 10000 ) // && !pathNodeSys.hasClearanceData limits the number of attempt to 10 per second and cumulative time to 250 sec
{
attemptCount = npcPathFindAttemptCount;
if ( (npcPathFindAttemptCount > 10 + (40 * (pathNodeSys.hasClearanceData == true)) ) || npcPathTimeCumulative > 250)
if ( (npcPathFindAttemptCount > 100 + (40 * (pathNodeSys.hasClearanceData == true)) ) || npcPathTimeCumulative > 2500)
{
if (config.pathfindingDebugMode)
{
Expand Down
20 changes: 20 additions & 0 deletions TemplePlus/python/python_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <pathfinding.h>
#include <gamesystems/map/sector.h>
#include <location.h>
#include <animgoals\anim.h>

static std::string GetNiceName(void* ptr) {

Expand Down Expand Up @@ -227,6 +228,24 @@ PyObject *PyDebug_DumpAiTactics() {
return result;
}

PyObject* PyDebug_DumpAnimGoals() {
using AnimGoalArray = const AnimGoal* [82];
AnimGoalArray& goals = temple::GetRef<AnimGoalArray>(0x102BD1B0);
auto cap = 82;
auto result = PyList_New(cap);

for (int i = 0; i < cap; ++i) {
auto goal = goals[i];
if (!goal) continue;
auto c = Py_BuildValue("s",
GetAnimGoalTypeName((AnimGoalType)i)
);

PyList_SET_ITEM(result, i, c);
}

return result;
}

PyObject *PyDebug_RecalculatePathNodeNeighbours()
{
Expand Down Expand Up @@ -349,6 +368,7 @@ static PyMethodDef PyDebug_Methods[] = {
{ "dump_feats", (PyCFunction)PyDebug_DumpFeats, METH_NOARGS, NULL },
{ "dump_d20actions", (PyCFunction)PyDebug_DumpD20Actions, METH_NOARGS, NULL },
{ "dump_ai_tactics", (PyCFunction)PyDebug_DumpAiTactics, METH_NOARGS, NULL },
{ "dump_anim_goals", (PyCFunction)PyDebug_DumpAnimGoals, METH_NOARGS, NULL },
{ "recalc_neighbours", (PyCFunction)PyDebug_RecalculatePathNodeNeighbours, METH_NOARGS, NULL },
{ "flush_nodes", (PyCFunction)PyDebug_FlushNodes, METH_NOARGS, NULL },
{ "recip", (PyCFunction)PyDebug_ReciprocityDebug, METH_NOARGS, NULL },
Expand Down
1 change: 1 addition & 0 deletions TemplePlus/python/python_integration_obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ PyObject* PythonObjIntegration::ExecuteScript(const char* moduleName, const char
Py_DECREF(locals);
if (!module) { */
logger->error("Unable to find Python module {}", moduleName);
PyErr_Print();
Py_RETURN_NONE;
}

Expand Down
Loading

0 comments on commit d72d6b3

Please sign in to comment.