• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[JASS] On Vexorian's tracks...

Status
Not open for further replies.
Level 5
Joined
May 22, 2006
Messages
150
Basing on Vexorians location stack (which I did not get to work...) I tried to write a list...
As it is little surprise, this one does not work, too...
If someone enjoys to seek for mistakes, whose kind he does not even know, this should be a piece of special cake for him.

A list is defined by it's head element.
So to "create" a list, you need to create a location and store data (for example a dummy real) in it's X-coordinate. Then the function "List_Add" can be used with the variable pointing on this location to create a list.
... Yet it is not possible to remove the head without destroying the whole list.
So dummy data in the first element is a good idea.

The common functions for type casting:
JASS:
function R2E takes real r returns effect
  return r
  return null
endfunction

function R2Loc takes real r returns lightning
  return r
  return null
endfunction

function R2U takes real r returns unit
  return r
  return null
endfunction

function H2R takes handle h returns real
  return h
  return 0.
endfunction

The functions for movement inside the list...
Way is defined as from head to foot (LIFO):
JASS:
function List_NextElement takes location l returns location
  return R2Loc(GetLocationY(l))
endfunction

function List_LastElement takes location l returns location
  local location element = l
  if l != null then
    loop
      exitwhen List_NextElement(element) == null
      set element = List_NextElement(element)
    endloop
  endif
  return element
endfunction

The functions to insert/extract/remove data:
JASS:
function List_Add takes location l,real i returns nothing
  local location element
  if l == null then
    set l = Location(i,0)
  else
    set element = l
    loop
      exitwhen List_NextElement(element) == null
      set element = List_NextElement(element)
    endloop
    call MoveLocation(element,GetLocationX(element),H2R(Location(i,0)))
  endif
  set element = null
endfunction

function List_Remove takes location l,location toRemove returns nothing
  local location element
  if l != null then
    set element = l
    if element != toRemove then
      loop
        exitwhen List_NextElement(element) == toRemove or List_NextElement(element) == null
        set element = List_NextElement(element)
      endloop
      if List_NextElement(element) != null then
        call MoveLocation(element,GetLocationX(element),H2R(List_NextElement(List_NextElement(element))))
      endif
    endif
  endif
  set element = null
endfunction

function List_Destroy takes location l returns nothing
  local location element
  if l != null then
    loop
      exitwhen List_NextElement(l) == null
      set element = l
      loop
        exitwhen List_NextElement(List_NextElement(element)) == null
        set element = List_NextElement(element)
      endloop
      call RemoveLocation(List_NextElement(element))
      call MoveLocation(element,GetLocationX(element),0)
    endloop
    call RemoveLocation(l)
  endif
  set element = null
  set l = null
endfunction
 
Level 5
Joined
May 22, 2006
Messages
150
Then, Vexorian's "object" would not have worked, too.
Of course it did not when I tried to use it, but it must have worked, when he used it to compute it's time delay...
In fact, I discovered just a minor fault: R2Loc returned lightning ínstead of location.
... Better than nothing, I guess - but it still does not work.

However, I changed some more stuff:
- only native function calls inside the functions
- "setHeadNext"-function to delete a head without losing reference to the other elements
- "find"-function to retrieve the first location with a certain value
- fix for the "remove"-function to deal with the list's head
- fix for the "remove"-function, which leaked the removed element

JASS:
function List_SetHeadNext takes location l returns location
  local location element
  if l != null then
    set element = R2Loc(GetLocationY(l))
    call RemoveLocation(l)
    return element
  endif
  set element = null
  return null
endfunction

function List_LastElement takes location l returns location
  local location element = l
  if l != null then
    loop
      exitwhen GetLocationY(element) == 0
      set element = R2Loc(GetLocationY(element))
    endloop
  endif
  return element
endfunction

function List_Add takes location l,real i returns nothing
  local location element
  if l != null then
    set element = l
    loop
      exitwhen GetLocationY(element) == 0
      set element = R2Loc(GetLocationY(element))
    endloop
    call MoveLocation(element,GetLocationX(element),H2R(Location(i,0)))
  endif
  set element = null
endfunction

function List_Find takes location l,real toFind returns location
  local location element
  if l != null and toFind != 0 then
    set element = l
    loop
      exitwhen element == null or GetLocationX(element) == toFind
      set element = R2Loc(GetLocationY(element))
    endloop
    return element
  endif
  set element = null
  return null
endfunction

function List_Remove takes location l,real toRemove returns nothing
  local location deleted
  local location element
  if l != null and toRemove != 0 then
    set element = l
    if GetLocationX(l) != toRemove then
      loop
        exitwhen GetLocationX(R2Loc(GetLocationY(element))) == toRemove or GetLocationY(element) == 0
        set element = R2Loc(GetLocationY(element))
      endloop
      if GetLocationY(element) != 0 then
        set deleted = R2Loc(GetLocationY(element))
        call MoveLocation(element,GetLocationX(element),GetLocationY(deleted))
        call RemoveLocation(deleted)
      endif
    else
      call MoveLocation(l,0,GetLocationY(l))
    endif
  endif
  set deleted = null
  set element = null
endfunction

function List_Destroy takes location l returns nothing
  local location element
  if l != null then
    loop
      exitwhen GetLocationY(l) == 0
      set element = l
      loop
        exitwhen GetLocationY(R2Loc(GetLocationY(element))) == 0
        set element = R2Loc(GetLocationY(element))
      endloop
      call RemoveLocation(R2Loc(GetLocationY(element)))
      call MoveLocation(element,GetLocationX(element),0)
    endloop
    call RemoveLocation(l)
  endif
  set element = null
  set l = null
endfunction
 
Status
Not open for further replies.
Top