More Awesome Than You!

The Bowels of Trogdor => The Small Intestines of Trogdor => Topic started by: dizzy on 2007 September 19, 05:26:51



Title: Memory Deleter
Post by: dizzy on 2007 September 19, 05:26:51
I have this memory delete code, but I can't use it because the damn pie menu code (in the engine itself) is too slow to handle it in extreme situations. Rather than toss it, I thought I'd share it in case it's useful to someone else.

Here's the menu builder:

Code:
-- stack object = personId
-- local 2 = menu index
-- return true if still building menu, false otherwise

local cp = CachedPerson.new(GetStackObjectId())
local nid = cp:getPersonData(PersonData["neighbor id"])
local cn = CachedNeighbor.new(nid)

SetScriptReturnValue(false)

local t = {}
local t2 = {}
local i = 1

local x = GetLocal(2) + 1

if x == 1 then
  TempTable = {}
  local ii = 1

  local nlist = GlobalObjManager:getNeighborIds()
  local nguid = {}
  for k,v in nlist do
    local cnx = CachedNeighbor.new(v)
    local g0 = cnx:getGUID() -- neighbor GUID
    local g1 = GetObjectDefinitionField(g0, ObjDef["guid_1 - Read Only"])
    local g2 = GetObjectDefinitionField(g0, ObjDef["guid_2 - Read Only"])
    nguid[CreateObjectGUID(g2, g1)] = v -- catalog GUID -> nid
  end

  local inv = Inventory.new(Inventory.kTypeNeighbor, nid)

  local mmult = {}
  local gmult = {}
  local xx = 1
  local vv = inv:getTokenAtIndex(xx)
  while vv ~= nil do
    if vv:testFlag(1) and vv:testFlag(2) then -- visible memory
      local g = vv:getGUID()
      if gmult[g] == 1 then -- already in the guid table?
        mmult[g] = 1 -- multiples of the same guid
      else
        gmult[g] = 1
      end
    end
    xx = xx + 1
    vv = inv:getTokenAtIndex(xx)
  end

  xx = 1
  vv = inv:getTokenAtIndex(xx)
  while vv ~= nil do
    if vv:testFlag(1) and vv:testFlag(2) then -- visible memory
      local mname = GetCatalogName(vv:getGUID())
      local sname = ""

      local s1, s2 = string.find(mname, "%$Subject")
      if s1 ~= nil then
        local g = CreateObjectGUID(vv:getProperty(7), vv:getProperty(6))
        if nguid[g] ~= nil then
          local cn2 = CachedNeighbor.new(nguid[g])
          sname = cn2:getFullName()
        else
          sname = GetCatalogName(g)
        end
        mname = string.sub(mname, 1, s1 - 1) .. "..." .. string.sub(mname, s2 + 1)
        if mmult[vv:getGUID()] ~= 1 then
          mname = string.sub(mname, 1, s1 - 1) .. sname .. string.sub(mname, s2 + 1)
          sname = ""
        end
      end

      local s1, s2 = string.find(mname, "%$Constant:4097:7")
      if s1 ~= nil then
        local mx = GetConstantWithGuid(vv:getGUID(), 4097, 7)
        mname = string.format("%s%d%s", string.sub(mname, 1, s1 - 1), mx, string.sub(mname, s2 + 1))
      end

      if sname == "" then
        TempTable[ii] = string.format(".../%s", mname)
        TempTable[ii+1] = xx
        ii = ii + 2
      else
        TempTable[ii] = string.format(".../%s/%s", mname, sname)
        TempTable[ii+1] = xx
        ii = ii + 2
      end
    end
    xx = xx + 1
    vv = inv:getTokenAtIndex(xx)
  end
end

local i2 = 1
while i2 <= table.getn(TempTable) do
  t[i] = "Delete Memory.../" .. cn:getName() .. TempTable[i2]
  t2[i] = TempTable[i2+1]
  i = i + 1
  i2 = i2 + 2
end

if x <= table.getn(t) then
  SetLocal(2, x)
  nGameState.SetProperty("luaDialogString", t[x])
  SetTemp(0, t2[x])
  SetScriptReturnValue(true)
end

Here's the action code:

Code:
-- tree param 0 = personId, tree param 1 = action

local x = GetTreeParameter(1)

SetScriptReturnValue(true)

if x < 0 then
  SetScriptReturnValue(false)
else
  local cp = CachedPerson.new(GetTreeParameter(0))
  local nid = cp:getPersonData(PersonData["neighbor id"])

  local inv = Inventory.new(Inventory.kTypeNeighbor, nid)
  local vv = inv:getTokenAtIndex(x)
  vv:setFlag(InventoryToken.kFlagMarkedForDeletion)
  inv:removeTokensMarkedForDeletion()
end