Skip to content

Commit

Permalink
A few uses of "guard"
Browse files Browse the repository at this point in the history
  • Loading branch information
aardappel committed Jul 19, 2023
1 parent f868c8e commit cac44e2
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 89 deletions.
34 changes: 17 additions & 17 deletions modules/astar.lobster
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def astar_generic(startnode, endcondition, generatenewstates, heuristic):
openlist.remove_obj(n)
n.closed = true
generatenewstates(n) delta, cost, nn:
if not nn.closed:
let G = n.G + cost
if (not nn.open and openlist.push(nn)) or G < nn.G:
nn.open = true
nn.delta = delta
nn.previous = n
nn.H = heuristic(nn.state)
nn.G = G
nn.F = G + nn.H
guard not nn.closed
let G = n.G + cost
if (not nn.open and openlist.push(nn)) or G < nn.G:
nn.open = true
nn.delta = delta
nn.previous = n
nn.H = heuristic(nn.state)
nn.G = G
nn.F = G + nn.H
n = nil
for(openlist) c:
if not n or c.F < n.F or (c.F == n.F and c.H < n.H):
Expand Down Expand Up @@ -100,12 +100,12 @@ def astar_goap<T>(goapactions:[goapaction], initstate:T, heuristic, endcondition
endcondition(_.state)
fn n, f:
for(goapactions) act:
if act.precondition(n.state):
let nstate = n.state.copy
act.effect(nstate)
var i = existingnodes.find(): equal(_.state, nstate)
if i < 0:
i = existingnodes.length
existingnodes.push(goap_node<T> { nstate, nil })
f(act, 1, existingnodes[i])
guard act.precondition(n.state)
let nstate = n.state.copy
act.effect(nstate)
var i = existingnodes.find(): equal(_.state, nstate)
if i < 0:
i = existingnodes.length
existingnodes.push(goap_node<T> { nstate, nil })
f(act, 1, existingnodes[i])
fn: heuristic(_)
38 changes: 19 additions & 19 deletions modules/navmap.lobster
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ def create_nav_map(start:int2, worlddim:int2, allow_diag:int, blocked_cost:int,
let c = pathq.pop()
for(cardinal_directions) d:
let dpos = c.pos - d
if in_range(dpos, worlddim):
var cost = 1
if not is_blocked(dpos) or (cost = blocked_cost):
let n = grid[dpos]
if n.psteps >= 0:
if allow_diag and cost == 1 and n.cost == 1 and c.cost == 1 and
n.psteps == c.psteps + 1 and
n.pdir.x * n.pdir.y == 0 and n.pdir.x * d.x == 0 and n.pdir.y * d.y == 0:
let diag = n.pdir + d
if not is_blocked(n.pos + diag):
n.pdir = diag
else:
n.psteps = c.psteps + cost
n.pdir = d // All these vecs are shared, so low final memory cost.
n.pos = dpos
n.cost = cost
var i = pathq.length
while i > 0 and pathq[i - 1].psteps < n.psteps: i--
pathq.insert(i, n)
guard in_range(dpos, worlddim)
var cost = 1
guard not is_blocked(dpos) or (cost = blocked_cost)
let n = grid[dpos]
if n.psteps >= 0:
if allow_diag and cost == 1 and n.cost == 1 and c.cost == 1 and
n.psteps == c.psteps + 1 and
n.pdir.x * n.pdir.y == 0 and n.pdir.x * d.x == 0 and n.pdir.y * d.y == 0:
let diag = n.pdir + d
if not is_blocked(n.pos + diag):
n.pdir = diag
else:
n.psteps = c.psteps + cost
n.pdir = d // All these vecs are shared, so low final memory cost.
n.pos = dpos
n.cost = cost
var i = pathq.length
while i > 0 and pathq[i - 1].psteps < n.psteps: i--
pathq.insert(i, n)
return nav_map { mapxy(worlddim) v: grid[v].pdir, start }

61 changes: 30 additions & 31 deletions modules/std.lobster
Original file line number Diff line number Diff line change
Expand Up @@ -162,42 +162,41 @@ def rnd_pick2(xs):
return rnd_pick(rnd_pick(xs))

def qsort<T>(xs:[T], lt) -> [T]:
if xs.length <= 1:
guard xs.length > 1:
return xs
else:
let pivot = xs[0]
let tail = xs.slice(1, -1)
let f1, f2 = tail.partition(): lt(_, pivot)
return append(append(qsort(f1, lt), [ pivot ]),
qsort(f2, lt))
let pivot = xs[0]
let tail = xs.slice(1, -1)
let f1, f2 = tail.partition(): lt(_, pivot)
return append(append(qsort(f1, lt), [ pivot ]),
qsort(f2, lt))

def qsort_in_place(xs, lt):
def rec(s, e) -> void:
let l = e - s
if l > 1:
let pivot = xs[s]
var sp = s + 1
var ep = e
while sp < ep:
let c = xs[sp]
if lt(c, pivot):
xs[sp - 1] = xs[sp]
sp++
else:
xs[sp] = xs[--ep]
xs[ep] = c
xs[--sp] = pivot
rec(s, sp)
rec(ep, e)
guard l > 1
let pivot = xs[s]
var sp = s + 1
var ep = e
while sp < ep:
let c = xs[sp]
if lt(c, pivot):
xs[sp - 1] = xs[sp]
sp++
else:
xs[sp] = xs[--ep]
xs[ep] = c
xs[--sp] = pivot
rec(s, sp)
rec(ep, e)
rec(0, xs.length)

def insertion_sort(xs, lt):
for(xs) key, i:
if i:
var j = i
while j > 0 and lt(key, xs[j - 1]):
xs[j--] = xs[j - 1]
xs[j] = key
guard i
var j = i
while j > 0 and lt(key, xs[j - 1]):
xs[j--] = xs[j - 1]
xs[j] = key

def insert_ordered(xs, x, lt):
for(xs) key, i:
Expand All @@ -208,10 +207,10 @@ def insert_ordered(xs, x, lt):

def shuffle(xs):
for(xs) x, i:
if i:
let j = rnd(i + 1)
xs[i] = xs[j]
xs[j] = x
guard i
let j = rnd(i + 1)
xs[i] = xs[j]
xs[j] = x
return xs

def nest_if(c, nest, with):
Expand Down
15 changes: 7 additions & 8 deletions modules/texture.lobster
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ def loadtexturecached(name, tf:texture_format = texture_format_none):
let n, i = binary_search(texture_names, name)
if n:
return textures[i]
let tex = gl_load_texture(name, tf)
if tex:
texture_names.insert(i, name)
textures.insert(i, tex)
return tex
else:
let tex = gl_load_texture(name, tf)
if tex:
texture_names.insert(i, name)
textures.insert(i, tex)
return tex
else:
print "unable to load texture: {name}"
return from program
print "unable to load texture: {name}"
return from program

// Pass nil for tex to have it created. Recreates tex if the wrong size. Renders f. Returns tex.
// Depth texture automatically created if depth is true and depthtex is nil.
Expand Down
16 changes: 8 additions & 8 deletions samples/lobstercraft.lobster
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def inside(v): return all(v > 0) and all(v < lssize - 1) // keep the outer bl

let cells = mapxyz(lssize) v:
// Generate blocks using noise.
let h = simplex(float(v) / float3 { 64.0, 64.0, 32.0 } + 11.0, 6, 1.0, 0.55)
let h = simplex(float(v) / float3 { 64.0, 64.0, 32.0 } + 11.0, 6, 1.0, 0.55)
// more likely to be solid the lower it is, and only when not on the outside:
let solid = h / 1.5 > div(v.z, lssize.z) - 0.5 and inside(v)
// pick material with noise too:
Expand All @@ -41,13 +41,13 @@ def generate_mesh(ci):
forxyz(int3_1 * csize) cv: // For all cells in a chunk
let v = ci * csize + cv
let e = cells[v]
if e: // if this cell is solid
for(nbdirs) nv, i:
if(not cells[nv + v]): // and neighbor is empty
for(tris) ti:
vpositions.push(float(v) + nbpolys[i][ti])
vcolors.push(colors[e - 1])
vnormals.push(float(nv))
guard e // if this cell is solid
for(nbdirs) nv, i:
guard not cells[nv + v] // and neighbor is empty
for(tris) ti:
vpositions.push(float(v) + nbpolys[i][ti])
vcolors.push(colors[e - 1])
vnormals.push(float(nv))
meshes[ci] = gl_new_mesh("PCN", vpositions, vcolors, vnormals, [], [], [])
forxyz(lssize / csize) v: generate_mesh(v)

Expand Down
12 changes: 6 additions & 6 deletions samples/pythtree.lobster
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ def branch(n, max, scale, len) -> void:
gl_color(color { 0.3, 0.1, 0.0, 0.7 })
for(2) i:
gl_line(poly[i], poly[3 - i], 0.2)
if n < max:
gl_translate(float2 { 0.0, len })
gl_scale(scale)
for(2) i:
gl_rotate_z(sincos((i * 2.0 - 1.0) * 30.0 + sin(gl_time() * 50.0) * n + rnd(40) - 20.0)):
branch(n + 1, max, scale, len + 0.5)
guard n < max
gl_translate(float2 { 0.0, len })
gl_scale(scale)
for(2) i:
gl_rotate_z(sincos((i * 2.0 - 1.0) * 30.0 + sin(gl_time() * 50.0) * n + rnd(40) - 20.0)):
branch(n + 1, max, scale, len + 0.5)

fatal(gl_window("Pythagoras tree in Lobster", 1024, 768))

Expand Down

0 comments on commit cac44e2

Please sign in to comment.