Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Count the items and get the lowest value

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Razi
Expert Cheater
Reputation: 0

Joined: 17 Jan 2018
Posts: 126

PostPosted: Thu Jul 22, 2021 1:21 am    Post subject: Count the items and get the lowest value Reply with quote

In the game, there are several items in the inventory with different values (0xFFFFFF2B, 0xFFFFFF2A) that need to be counted. And get the lowest quantity. The quantity of items is different.

Can this code be optimized?
Code:
local listb = {UDF1.CEListView1}

local Mime = 0
local Manipulate = 0
local Deathblow = 0
local Morph = 0
local Throw = 0
local Sense = 0
local Steal = 0

for x=0,199 do                            --inventory has 200 slots
local currentmat = readInteger(0x9E8D4C+x*4)
if currentmat == 0xFFFFFF2B then
Mime = Mime+1
elseif currentmat == 0xFFFFFF2A then
Manipulate = Manipulate+1
elseif currentmat == 0xFFFFFF29 then
Deathblow = Deathblow+1
elseif currentmat == 0xFFFFFF28 then
Morph = Morph+1
elseif currentmat == 0xFFFFFF27 then
Throw = Throw+1
elseif currentmat == 0xFFFFFF25 then
Sense = Sense+1
elseif currentmat == 0xFFFFFF24 then
Steal = Steal+1
end
end

listb[1].Items[0].SubItems.text = Mime
listb[1].Items[1].SubItems.text = Manipulate
listb[1].Items[2].SubItems.text = Deathblow
listb[1].Items[3].SubItems.text = Morph
listb[1].Items[4].SubItems.text = Throw
listb[1].Items[5].SubItems.text = Sense
listb[1].Items[6].SubItems.text = Steal

UDF1.CELabel1.Caption = (math.min(listb[1].Items[0].SubItems.text,
listb[1].Items[1].SubItems.text, listb[1].Items[2].SubItems.text,
listb[1].Items[3].SubItems.text, listb[1].Items[4].SubItems.text,
listb[1].Items[5].SubItems.text, listb[1].Items[6].SubItems.text))
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 95

Joined: 06 Jul 2014
Posts: 2998

PostPosted: Thu Jul 22, 2021 2:15 am    Post subject: Reply with quote

Instead of doing 200 calls to readProcessMemory (expensive), you could do 1 call for 800 bytes and turn them into integers.

something like this (untested):
Code:
local mem = readBytes(address, 800, true)
local integers = {}
for i = 0, 199 do
  local t = {}
  table.move(mem, i * 4 + 1, i * 4 + 4, 1, t)
  integers[#integers+1] = byteTableToDword(t, false)
end

-- use array "integers" instead of many calls to RPM via readInteger


Other minor notes:

Making listb an array seems pointless in this example.

Using math.min on strings probably won't give you the results you want.
Code:
math.min('100', '11') == '100'
Just use the original variables (Mime, Manipulate...) directly- they're numbers.
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Razi
Expert Cheater
Reputation: 0

Joined: 17 Jan 2018
Posts: 126

PostPosted: Thu Jul 22, 2021 2:54 am    Post subject: Reply with quote

ParkourPenguin wrote:
Other minor notes:

I also want to get rid of those "if" and "elseifs". Do I need to create a function for this or something else?
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 95

Joined: 06 Jul 2014
Posts: 2998

PostPosted: Thu Jul 22, 2021 11:27 am    Post subject: Reply with quote

Lua doesn't have a switch or match statement, but you can use tables to get the same effect.
http://lua-users.org/wiki/SwitchStatement

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Razi
Expert Cheater
Reputation: 0

Joined: 17 Jan 2018
Posts: 126

PostPosted: Thu Jul 22, 2021 1:17 pm    Post subject: Reply with quote

ParkourPenguin wrote:
Instead of doing 200 calls to readProcessMemory (expensive), you could do 1 call for 800 bytes and turn them into integers.

something like this (untested):
Code:
local mem = readBytes(address, 800, true)
local integers = {}
for i = 0, 199 do
  local t = {}
  table.move(mem, i * 4 + 1, i * 4 + 4, 1, t)
  integers[#integers+1] = byteTableToDword(t, false)
end

-- use array "integers" instead of many calls to RPM via readInteger


Code:
local mem = readBytes(0x9E8D4C, 8, true)
local integers = {}
for i = 0, 1 do
  local t = {}
  table.move(mem, i * 4 + 1, i * 4 + 4, 1, t)
  integers[#integers+1] = byteTableToDword(t, false)
end

print(integers[1])

When I use this code, it gives no result. It should be a table with four byte values. Is there something wrong with this code? Or am I doing something wrong?
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 95

Joined: 06 Jul 2014
Posts: 2998

PostPosted: Thu Jul 22, 2021 1:55 pm    Post subject: Reply with quote

The address is probably wrong or CE isn't attached to the process.
This works fine for me:
Code:
assert(autoAssemble([[
globalalloc(foo,4096)
foo:
  db 64 00 00 00  // 100
  db ff ff ff ff  // 4294967295
]]))

local mem = readBytes('foo', 8, true)
local integers = {}
for i = 0, 1 do
  local t = {}
  table.move(mem, i * 4 + 1, i * 4 + 4, 1, t)
  integers[#integers+1] = byteTableToDword(t, false)
end

for k,v in ipairs(integers) do
  print(k,v)
end

--[[ Output:
1 100
2 4294967295
]]

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Razi
Expert Cheater
Reputation: 0

Joined: 17 Jan 2018
Posts: 126

PostPosted: Thu Jul 22, 2021 3:41 pm    Post subject: Reply with quote

ParkourPenguin wrote:
The address is probably wrong or CE isn't attached to the process.
This works fine for me:
Code:
assert(autoAssemble([[
globalalloc(foo,4096)
foo:
  db 64 00 00 00  // 100
  db ff ff ff ff  // 4294967295
]]))

local mem = readBytes('foo', 8, true)
local integers = {}
for i = 0, 1 do
  local t = {}
  table.move(mem, i * 4 + 1, i * 4 + 4, 1, t)
  integers[#integers+1] = byteTableToDword(t, false)
end

for k,v in ipairs(integers) do
  print(k,v)
end

--[[ Output:
1 100
2 4294967295
]]


Strange, it doesn't work for me.
Code:
for k,v in ipairs(integers) do --doesn't work
  print(k,v)
end

print(integers[1]) --doesn't work

print(mem[1]) -- works
--[[ Output:
100
]]

It's the same with the process. works only print(mem[1]) Output: 1 byte value. print(mem[2]) Output: second 1 byte value.
Perhaps it's the version of the Cheat Engine, 7.1.
Back to top
View user's profile Send private message
TheyCallMeTim13
Wiki Contributor
Reputation: 39

Joined: 24 Feb 2017
Posts: 824
Location: 'Merica

PostPosted: Thu Jul 22, 2021 4:02 pm    Post subject: Reply with quote

Can confirm, 7.1 has something different with "byteTableToDword". But removing the second parameter gets it working. Seems the second parameter didn't exist tell 7.2, at least it's not documented in the "celua.txt" file.

Code:
assert(autoAssemble([[
globalalloc(foo,4096)
foo:
  db 64 00 00 00  // 100
  db ff ff ff ff  // 4294967295
]]))

local mem = readBytes('foo', 8, true)
local integers = {}
for i = 0, 1 do
  local t = {}
  table.move(mem, i * 4 + 1, i * 4 + 4, 1, t)
  integers[#integers+1] = byteTableToDword(t)
end
for k,v in ipairs(integers) do
  print(k,v)
end

--[[ Output:
1 100
2 4294967295
]]

_________________
Back to top
View user's profile Send private message
Razi
Expert Cheater
Reputation: 0

Joined: 17 Jan 2018
Posts: 126

PostPosted: Thu Jul 22, 2021 4:31 pm    Post subject: Reply with quote

TheyCallMeTim13 wrote:
Can confirm, 7.1 has something different with "byteTableToDword". But removing the second parameter gets it working. Seems the second parameter didn't exist tell 7.2, at least it's not documented in the "celua.txt" file.

Thanks, it works now.

ParkourPenguin wrote:
Lua doesn't have a switch or match statement, but you can use tables to get the same effect.


Can you show example with this code? :
Code:
local mem = readBytes(0x9E8D4C, 800, true)
local integers = {}
for i = 0, 199 do
  local t = {}
  table.move(mem, i * 4 + 1, i * 4 + 4, 1, t)
  integers[#integers+1] = byteTableToDword(t)
end

local Mime = 0
local Manipulate = 0
local Deathblow = 0
local Morph = 0
local Throw = 0
local Sense = 0
local Steal = 0

for x=0,199 do                            --inventory has 200 slots
local currentmat = integers[x+1]
if currentmat == 0xFFFFFF2B then
Mime = Mime+1
elseif currentmat == 0xFFFFFF2A then
Manipulate = Manipulate+1
elseif currentmat == 0xFFFFFF29 then
Deathblow = Deathblow+1
elseif currentmat == 0xFFFFFF28 then
Morph = Morph+1
elseif currentmat == 0xFFFFFF27 then
Throw = Throw+1
elseif currentmat == 0xFFFFFF25 then
Sense = Sense+1
elseif currentmat == 0xFFFFFF24 then
Steal = Steal+1
end
end
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites