--Utility Functions for Tables function create_list() t = {}; t.elements = {}; t.size = 0; return t; end function add_element_to(t, e) t.size = t.size + 1; t.elements[t.size] = e; return true; end function remove_element_from(t, index) for i = index,t.size-1,1 do t.elements[i] = t.elements[i+1]; end t.size = t.size - 1; return true; end function get_element(t, index) return t.elements[index]; end function find_in(t, e) not_found = true; i = 0; while (not_found and i < t.size) do i = i + 1; if (match(t.elements[i], e)) then not_found = false return i; end end return 0; end function get_neighbors(e, target, map) d = {}; d[1] = {coords = {x = e.coords.x-1, y = e.coords.y}}; d[2] = {coords = {x = e.coords.x, y = e.coords.y+1}}; d[3] = {coords = {x = e.coords.x+1, y = e.coords.y}}; d[4] = {coords = {x = e.coords.x, y = e.coords.y-1}}; n = create_list(); for i = 1,4,1 do if not wall_at(d[i].coords.y, d[i].coords.x) then d[i].score = distance(d[i].coords, target.coords); d[i].parent = e; d[i].path_length = e.path_length + 1; add_element_to(n, d[i]); end end return n; end function find_min(t) lowest = -1; lowest_index = -1; for i = 1,t.size,1 do if (lowest_index == -1 or t.elements[i].score < lowest) then lowest = t.elements[i].score; lowest_index = i; end end return lowest_index; end function match(e1, e2) if (e1.coords.x == e2.coords.x and e1.coords.y == e2.coords.y) then return true; else return false; end end function get_parent(e) return e.parent; end function get_path_from(e, start) r = create_list(); add_element_to(r, e); l = e; while not (l == nil) do add_element_to(r, l); l = get_parent(l); end return r; end function swap_elements(t, index1, index2) temp = t.elements[index1]; t.elements[index1] = t.elements[index2] t.elements[index2] = temp; return true; end function sort_table(t) made_swaps = true; while(made_swaps) do made_swaps = false; for i = 1,t.size-1,1 do if get_element(t, i).score > get_element(t, i+1).score then swap_elements(t, i, i+1); made_swaps = true; end end end return true; end