I've been doing some more thinking about all the targeting functions and how they are similar.
1. targeting npcs -
get objectlist
search object list for name
target address
attack
2. target object -
get objectlist
search object list for name
target address
attack
optionally wait for collect time.
3. harvesting -
get objectlist
Search for nearest id that matches node db.
target address
attack
wait for self.harvesting = false
search for next node ignoring current.
Conclusions:
Because the body of 3. is so different it would have to be a totally different function. 1. and 2. are so similar they could be the same function or based on the same function. I can't think of a good name that would suite both so I thought we could keep target_NPC() and use maybe target_Object() to target objects. Because it would be handy to be able to search for an object without actually harvesting it, I thought the common part of 1. and 2. can be a search function that returns the closest object eg.
Code: Select all
function CPlayer:findClosestNameOrId(_objnameorid)
local closestObject = nil;
local obj = nil;
local objectList = CObjectList();
objectList:update();
for i = 0,objectList:size() do
obj = objectList:getObject(i);
if( obj ~= nil ) then
if( obj.Type == PT_NODE and (obj.Id == _objnameorid or obj.Name == _objnameorid )) then
local dist = distance(self.X, self.Z, obj.X, obj.Z);
if( closestObject == nil ) then
if( distance(self.X, self.Z, obj.X, obj.Z ) < settings.profile.options.HARVEST_DISTANCE ) then
closestObject = obj;
end
else
if( distance(self.X, self.Z, obj.X, obj.Z) <
distance(self.X, self.Z, closestObject.X, closestObject.Z) ) then
-- this node is closer
closestObject = obj;
end
end
end
end
end
return closestObject;
end
Then you could do this;
Code: Select all
function CPlayer:target_NPC(_npcname)
local npc = self:findClosestNameOrId(_npcname)
if npc then
memoryWriteInt(getProc(), self.Address + addresses.pawnTargetPtr_offset, npc.Address);
RoMScript("UseSkill(1,1)");
yrest(1500);
return true
else
return false
end
end
function CPlayer:target_Object(_objname,_waittime)
local obj = self:findClosestNameOrId(_objname)
if obj then
memoryWriteInt(getProc(), self.Address + addresses.pawnTargetPtr_offset, obj.Address);
RoMScript("UseSkill(1,1)");
yrest(waittime);
return true
else
return false
end
end
You would of course still need to add the messages and error checks, etc.