You might also not need to target this way. You could potentially find a pointer to an array of monsters near you, then access information out of this (say, coordinates, name, level, etc.) of each specific monster. Then, you take the pointer to that monster and write it into your target address. I realize that was a mouth-full, and probably confused you.
Lets, for example, say that there is an array in the game holding a few monsters like so:
Code: Select all
[0x12345678 + 0x0] = 0x2A34D200
[0x12345678 + 0x4] = 0x3CF20D00
[0x12345678 + 0x8] = 0x2AFA9F00
[0x12345678 + 0xC] = 0x2A45A100
[0x12345678 + 0x10] = 0x0 // NULL terminated
0x12345678 is an array of 4 byte pointers to monsters.
Then, the monster struct looks like this:
Code: Select all
0x2A34D200:
0x0 - int, HP
0x4 - int, max HP
0x8 - int, MP
0xC - int, max MP
0x10 - float, X position
0x14 - float, Y position
So, you read the array (0x12345678) and copy the contents it into a Lua table
Code: Select all
local monsterPointerTable = {};
local readval = 0;
while(true) do
readval = memoryReadInt(myProc, 0x12345678);
if( readval == 0 ) then
-- We reached the end of our array
break;
end
end
Now, we can construct an actual table of duplicate monster structs in our Lua script. First, let's create a class to handle data storage and updating.
Code: Select all
CMonster = class(
function (self, ptr) -- constructor
self.Address = ptr;
self.Level = 0;
self.HP = 0;
self.MaxHP = 1; -- good to prevent division by zero
self.MP = 0;
self.MaxMP = 1; -- see above
self.Xpos = 0.0;
self.Ypos = 0.0;
if( self.Address ) then self:update(); end; -- We were given an address, so read info
end
);
function CMonster:update()
-- All information on this struct is read ass offsets from the address to this monster.
self.HP = memoryReadInt(myProc, self.Address + 0x0);
self.MaxHP = memoryReadInt(myProc, self.Address + 0x4);
self.MP = memoryReadInt(myProc, self.Address + 0x8);
self.MaxMP = memoryReadInt(myProc, self.Address + 0xC);
self.Xpos = memoryReadFloat(myProc, self.Address + 0x10);
self.Ypos = memoryReadFloat(myProc, self.Address + 0x14);
end
Now, lets copy it into a Lua table of monsters:
Code: Select all
-- Assume monsterTable is a globally accessible table. First, lets destroy any old data in it.
monsterTable = {}; -- Cleared.
-- Copy all the available information out of the game on each monster, store it into monsterTable
for i,v in pairs(monsterPointerList) do
local tmp = CMonster(v);
table.insert(monsterTable, tmp);
end
Now you can perform the distance formula on each monster in monsterTable to find the closest monster to you, for example.
Code: Select all
local closestMonster = nil;
local lastDist = 9999;
function distance(x1, y1, x2, y2)
return math.sqrt( (y2-y1)*(y2-y1) + (x2-x1)*(x2-x1) );
end
for i,v in pairs(monsterList) do
local thisDist = distance(player.Xpos, player.Ypos, v.Xpos, v.Ypos);
if( thisDist < lastDist ) then
closestMonster = v;
lastDist = thisDist;
end;
end
if( closestMonster == nil ) then
-- no monsters found.
else
-- closestMonster is now a CMonster of the closest monster to the player.
end
This code was untested and wrote only for example purposes. It may contain a few errors