Advertisement
Guest User

pokemon.lua

a guest
Apr 28th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.24 KB | None | 0 0
  1. -- http://stackoverflow.com/questions/20325332/how-to-check-if-two-tablesobjects-have-the-same-value-in-lua
  2. local function is_table_equal(t1,t2,ignore_mt)
  3.    local ty1 = type(t1)
  4.    local ty2 = type(t2)
  5.    if ty1 ~= ty2 then return false end
  6.    -- non-table types can be directly compared
  7.    if ty1 ~= 'table' and ty2 ~= 'table' then return t1 == t2 end
  8.    -- as well as tables which have the metamethod __eq
  9.    local mt = getmetatable(t1)
  10.    if not ignore_mt and mt and mt.__eq then return t1 == t2 end
  11.    for k1,v1 in pairs(t1) do
  12.       local v2 = t2[k1]
  13.       if v2 == nil or not is_table_equal(v1,v2) then return false end
  14.    end
  15.    for k2,v2 in pairs(t2) do
  16.       local v1 = t1[k2]
  17.       if v1 == nil or not is_table_equal(v1,v2) then return false end
  18.    end
  19.    return true
  20. end
  21.  
  22. function SavePartyToFile(party)
  23.     local party_string = "{\"party\":["
  24.     for k,v in ipairs(party) do
  25.         party_string = party_string .. string.format("{\"id\":\"%s\",\"level\":\"%s\"}",party[k]['id'],party[k]['level']) .. ','
  26.     end
  27.     party_string = party_string:sub(1,-2)
  28.     party_string = party_string .. "]}"
  29.     local file = io.open("pokemon.json", "w")
  30.     file:write(party_string)
  31.     file:close()
  32.     print("Party Saved.")
  33. end
  34.  
  35. local party = {}
  36. -- http://datacrystal.romhacking.net/wiki/Pok%C3%A9mon_Red/Blue:RAM_map#Player
  37. local memory_addresses = {
  38.     [1] = {["id"] = 0xD164, ["level"] = 0xD18C},
  39.     [2] = {["id"] = 0xD165, ["level"] = 0xD1B8},
  40.     [3] = {["id"] = 0xD166, ["level"] = 0xD1E4},
  41.     [4] = {["id"] = 0xD167, ["level"] = 0xD210},
  42.     [5] = {["id"] = 0xD168, ["level"] = 0xD23C},
  43.     [6] = {["id"] = 0xD169, ["level"] = 0xD268}
  44. }
  45.  
  46. -- http://bulbapedia.bulbagarden.net/wiki/List_of_Pok%C3%A9mon_by_index_number_(Generation_I)
  47. while true do
  48.     local temp_party = {}
  49.     for i = 1,6 do
  50.         pokemon_id = memory.readbyte(memory_addresses[i]["id"])
  51.         pokemon_level = memory.readbyte(memory_addresses[i]["level"])
  52.         table.insert(temp_party, {["id"] = pokemon_id, ["level"] = pokemon_level})
  53.     end
  54.     if is_table_equal(party, temp_party) == false then
  55.         party = temp_party
  56.         print("Party changed: ")
  57.         print(party)
  58.         SavePartyToFile(party)
  59.     end
  60.     vba.frameadvance()
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement