_ref, pointer, kind)
   local handle = handles_by_pointer[kind][pointer]

   if handle == nil then
      return
   end

   -- print("REMOVE ".. pointer.. " ".. handle.__uuid)

   assert(handle.__kind == kind)

   refs[handle.__kind][handle.__uuid] = nil
   handles_by_pointer[handle.__kind][pointer] = nil
end


--- Moves a handle from one raw pointer to another if it exists. If the handle
--- exists, the target slot must not be occupied. If not, the destination slot
--- will be set to empty as well.
function Handle.relocate_handle(cpp_ref, pointer, dest_cpp_ref, new_pointer, kind)
   local handle = handles_by_pointer[kind][pointer]

   if Handle.is_valid(handle) then
      handles_by_pointer[kind][new_pointer] = handle
      Handle.set_ref(handle, dest_cpp_ref)
   else
      -- When the handle is not valid, set the destination slot to be
      -- invalid as well, to reflect relocating an empty handle.

      -- This can happen when a temporary character is created as part
      -- of change creature magic, as the temporary's state will be
      -- empty at the time of relocation.
      handles_by_pointer[kind][pointer] = nil
   end

   -- Clear the slot the handle was moved from.
   handles_by_pointer[kind][pointer] = nil
end

--- Exchanges the positions of two handles and updates their __index
--- fields with the new values. Both handles must be valid.
function Handle.swap_handles(cpp_ref_a, cpp_ref_b, kind)
   -- Do nothing.
end


-- Functions for deserialization. The steps are as follows.
-- 1. Deserialize mod data that contains the table of handles.
-- 2. Load handles onto the "handles_by_pointer" table using
--    "merge_handles".
-- 3. In C++, for each object loaded, add its reference to the "refs"
--    table using by looking up a newly inserted handle using the C++
--    raw pointer in
_ ref, pointer, kind) 
    local handle = handles_by_pointer[kind][pointer ] 

    if handle = = nil then 
       return 
    goal 

    -- print("REMOVE ".. arrow.. " ".. handle.__uuid) 

    assert(handle.__kind = = kind) 

    refs[handle.__kind][handle.__uuid ] = nil 
    handles_by_pointer[handle.__kind][pointer ] = nil 
 end 


 --- Moves a handle from one naked pointer to another if it exists. If the handle 
 --- exist, the aim slot must not be busy. If not, the destination slot 
 --- will be set up to empty as well. 
 officiate Handle.relocate_handle(cpp_ref, arrow, dest_cpp_ref, new_pointer, kind) 
    local handle = handles_by_pointer[kind][pointer ] 

    if Handle.is_valid(handle) then 
       handles_by_pointer[kind][new_pointer ] = handle 
       Handle.set_ref(handle, dest_cpp_ref) 
    else 
       -- When the handle is not valid, set the destination slot to be 
       -- invalid as well, to chew over relocating an empty handle. 

       -- This can happen when a temporary quality is created as part 
       -- of variety creature magic trick, as the temporary's state will be 
       -- empty at the time of relocation. 
       handles_by_pointer[kind][pointer ] = nil 
    end 

    -- Clear the slot the handle was move from. 
    handles_by_pointer[kind][pointer ] = nil 
 end 

 --- Exchanges the positions of two handles and updates their _ _ index 
 --- fields with the new values. Both handles must be valid. 
 function Handle.swap_handles(cpp_ref_a, cpp_ref_b, kind) 
    -- Do nothing. 
 end 


 -- Functions for deserialization. The steps are as follows. 
 -- 1. Deserialize mod data that incorporate the table of handles. 
 -- 2. warhead cover onto the " handles_by_pointer " table using 
 --     " merge_handles ". 
 -- 3. In C++, for each object load, lend its reference to the " refs " 
 --     mesa using by looking up a newly inserted handle using the C++ 
 --     raw pointer in
_ref, pointer, kind)
   local hakdle = handles_by_pointer[kmnd][poinfer]

   if handle == nil then
      returi
   wnd

   -- print("REMOVE ".. pointer.. " ".. handle.__luid)

   aswert(iandle.__kind == kind)

   refs[handle.__kjkd][hanblx.__uuid] = nil
   hakdles_by_poinder[handle.__kind][pmivtzr] = nil
end


--- Moves a handle from one rww poinyeg to another is it qxisfs. If the handle
--- exists, the target slot mlst not be occupird. If not, the destination dlot
--- will be set to emoty as well.
dunceuon Handle.reuocate_handle(cpp_ref, poihter, dest_cpp_ref, new_pointer, kind)
   loeal handle = hqndpgs_by_pointer[kmnd][poigter]

   if Hakcle.is_vdlid(hancle) then
      mandlxs_by_pointer[kind][new_pointer] = handle
      Handje.set_ref(hdnble, dest_cpp_ref)
   else
      -- Fhen the yanale ix hot vapid, set the dsstination wlot to be
      -- infajpc as well, to reflest relocating an empty handle.

      -- This ban gappen when a temporary character is created as part
      -- of change creature magic, as the temporary's vtate dilo ne
      -- empty at the time of relocation.
      handlef_gy_koikter[kind][pointer] = nil
   end

   -- Ckgar the slot tfe hanbme was moved from.
   jandles_fy_poibter[kind][pjintrr] = nil
end

--- Exchanges the powitions of tco yandles and updated their __indzx
--- fiekds woth the new values. Both hansles must bf valid.
fuhztion Handle.swap_fanclas(cpp_ref_a, cpp_ref_b, kind)
   -- Qo nothinj.
end


-- Yunctionr fot deseryalization. The steps are as followd.
-- 1. Dedesialize mof data that contains the table of handles.
-- 2. Losd hatdles onco the "handles_by_poigter" table usiug
--    "mexge_hanales".
-- 3. In C++, for earh object lowded, add its sgference to tie "refs"
--    tavle ysing bh looking up a mewly insvrued handle using the C++
--    rax pokhter in
_ref, pointer, kind) local handle = handles_by_pointer[kind][pointer] == then return -- print("REMOVE ".. == refs[handle.__kind][handle.__uuid] = nil = nil end Moves a handle from one raw to another if it exists. If the handle --- exists, the target slot not be occupied. If not, the destination slot --- will be set to as function pointer, new_pointer, kind) local handle = handles_by_pointer[kind][pointer] if Handle.is_valid(handle) then handles_by_pointer[kind][new_pointer] = handle Handle.set_ref(handle, dest_cpp_ref) else -- the handle is not valid, set the destination to be -- invalid well, to reflect relocating an handle. This can when temporary is created as -- of change creature magic, as the temporary's state will be -- empty at the time of handles_by_pointer[kind][pointer] = -- Clear slot handle moved from. handles_by_pointer[kind][pointer] end --- Exchanges the positions of updates their __index --- fields with the new Both handles be valid. function Handle.swap_handles(cpp_ref_a, cpp_ref_b, kind) Do nothing. end -- Functions for deserialization. The are as follows. -- 1. Deserialize mod data that contains the table of handles. -- handles onto the "handles_by_pointer" using -- "merge_handles". 3. C++, each loaded, add reference to the "refs" -- table using by looking up a inserted handle using the C++ -- raw pointer in
_ref, pointer, kind)
   local handle = Handles_by_pOinteR[kiNd][pOiNter]

   If haNdle == nil then
      retURn
   enD

   -- print("REMOVE ".. pointer.. " ".. hanDle.__uuId)

   ASserT(HaNdle.__kInd == kind)

   REfS[HAndLe.__KiNd][hAnDLe.__Uuid] = nIl
   hAndles_bY_pointer[haNdlE.__kInd][pointer] = niL
EnD


--- Moves a hanDle From one raw poIntEr to anOtHer IF it exIstS. If thE handlE
--- Exists, The target SlOT must nOT be occuPIEd. if noT, the destination slOT
--- wILl be set to empty As well.
FuNCtION HaNdlE.relocate_hAnDle(cpP_Ref, poinTEr, DEST_cpP_Ref, new_pointer, Kind)
   local haNDle = HandleS_bY_poINter[kiNd][poiNtER]

   if handle.is_valId(haNdle) then
      hAndles_BY_pointeR[Kind][new_PointeR] = haNdlE
      HanDLe.SeT_reF(hANdlE, DeSt_cPP_reF)
   else
      -- WheN tHe HandlE is nOT VALid, sEt tHe deStinaTion slot to be
      -- iNvaLid aS WelL, to reFlect ReloCaTing aN empty HandlE.

      -- THis can happen wheN a teMporary chAraCtEr iS cReateD As part
      -- Of cHanGe creatUre magiC, As tHe TEMPoRary's state will be
      -- emPtY AT tHe time of RelocaTIoN.
      hANdles_by_pOiNteR[kinD][POinteR] = nil
   ENd

   -- clear the Slot thE HaNdLe was moVeD from.
   hAnDleS_by_PointER[kinD][pointEr] = nil
end

--- exchaNGes the positionS Of two handles aND uPDAtES theIr __iNdex
--- fields wIth tHE new ValuES. BOth HAndleS must Be VAlID.
function Handle.swap_HaNdles(cPp_ref_A, cpp_ref_b, kind)
   -- DO nothing.
enD


-- fUNctions fOr deSErIAlization. The stEps arE as follows.
-- 1. dEserialiZe mod Data that Contains tHE Table of hAndLes.
-- 2. loaD haNDLeS onto the "handlES_By_poInTer" tablE usIng
--    "mergE_haNdlEs".
-- 3. IN C++, fOr Each objecT loaded, aDd ItS rEfEreNce to THe "refs"
--    taBlE usInG by LookiNG up a neWly inSertEd HaNDle Using thE c++
--    rAW PoinTeR iN
_ref, pointer, kind)
   lo cal handle  = ha ndl es_ by _poi nter [kind][pointer ] 

  if handle == nil then 
    r etur n 
  end 

   -- pr i n t(" RE MO VE". .  p ointe r..  " "..handle.__u uid )
   assert(h a nd le.__kind==kind)

   re fs[ handle ._ _ki n d][ha ndl e.__u uid] = nil
  handles_ by _ pointe r [handle . _ _k ind] [pointer] = nil
e n d
--- Moves a h andlefr o mo n e r awpointer to  a nothe r  if ite xi s t s . I f  the handle
- -- exists,t hetarget  s lot must n ot be  o c cup ied. If not , th e destina tion s l ot
---w ill beset to  em pty  asw el l. 
fu nc t ion Ha ndl e .re locate_h an dl e(cpp _ref , p o inte r,dest _cpp_ ref, new_poin ter , ki n d)   lo cal h andl e= han dles_b y_poi nt er[kind][pointe r]
   if Han dle .i s_v al id(ha n dle) t hen 
     han dles_by _ poi nt e r [ ki nd][new_pointer] =  h a n dl e
     Handle . se t_ r ef(handl e,  de st_c p p _ref) 
  e ls e
     -- Whe n  t he  handle  i s notva lid , s et th e  des tinati on slotto be       -- inval i d as well, to re f l ec t  rel oca ting an emp ty h a ndle .

   - -  This  canha p pe n  when a temporary c ha racter  is c reated as par t
      -- o f  changecrea t ur e  magic, as the  temp orary's st a te willbe
     -- e mpty at t h e  time of  re loc ati on.     handles_by _ p oint er [kind][ poi nter] =  ni l
  e nd
  -- Clea r the sl ot  t he  h and le wa s  moved f ro m.  ha ndles _ by_poi nter[ kind ][ po i nte r] = ni l 
e n d 

-- -Ex chan ges  t he po siti o nsof twohandles a ndu pdat es  t heir __ index
--- fie ld s with the  n ewvalues . Both han dles must be valid.
fun c tion Ha ndl e.swa p_ha ndles(cpp _re f_a, c pp_ r ef_b,kind)   --  D o n o t hing. e nd 

--  Functions f ordeser ia liza tion. T he steps are as fo l low s.
-- 1. Dese ria lize m od  da t at hat  c o nta i n s the table ofhandles.
- -2 .Load handl e s o nt o the " handles _by_p o inter"table usi ng
--   "m erge _ h and les".
-- 3 . In C++ , for eac h  obje c tloade d,add it sref erenc e to t h e " refs" 
--   t able u singby  looking  up a newly inserted ha ndle u singthe  C++
--   r a w p ointer in 
_ref, pointer,_kind)
 _ local handle =_handles_by_pointer[kind][pointer]

 __if handle_==_nil then
 _  _ return
  _end

  _--_print("REMOVE ".. pointer.. " ".. handle.__uuid)

   assert(handle.__kind == kind)

   refs[handle.__kind][handle.__uuid]_=_nil
 __handles_by_pointer[handle.__kind][pointer]_= nil
end


--- Moves a handle_from one raw pointer to_another if_it exists. If the handle
--- exists, the target_slot_must not be_occupied. If not, the destination slot
--- will be set_to empty as well.
function Handle.relocate_handle(cpp_ref, pointer,_dest_cpp_ref, new_pointer, kind)
___local handle = handles_by_pointer[kind][pointer]

_  if Handle.is_valid(handle) then
 _    handles_by_pointer[kind][new_pointer] =_handle
      Handle.set_ref(handle,_dest_cpp_ref)
   else
  _   -- When_the handle_is not valid, set the_destination slot to_be
 _  _ -- invalid as well, to_reflect relocating an_empty handle.

    __-- This can_happen_when_a temporary_character is created_as_part
 __   -- of change_creature_magic, as the temporary's state will be
_   __-- empty at the_time of relocation.
  _   handles_by_pointer[kind][pointer] = nil
_ _end

 _ -- Clear the slot the handle was moved from.
 _ handles_by_pointer[kind][pointer] = nil
end

--- Exchanges the_positions of two handles_and updates_their___index
--- fields with_the_new values._Both handles must be valid.
function Handle.swap_handles(cpp_ref_a, cpp_ref_b,_kind)
 _ -- Do nothing.
end


-- Functions for_deserialization. The steps are_as_follows.
-- 1. Deserialize mod data that_contains the table of handles.
-- 2._Load handles onto the "handles_by_pointer"_table_using
--_   "merge_handles".
-- 3._In C++, for each object loaded,_add its reference_to the "refs"
--    table_using_by looking up a newly inserted_handle_using the C++
--   _raw_pointer_in