Arc Forumnew | comments | leaders | submitlogin
1 point by almkglor 6027 days ago | link | parent

First tip: Avoid using '( ... ) . Use (list ...) instead. When you learn about mutating objects, then use '( ... ) , and only for optimization or if you really, really want to.

  (def directawayfrombounds (creature)
    (with ((oldxdir oldydir) creature!dir
           (oldxpos oldypos) creature!pos
           delimit
           (fn (minpos oldpos maxpos olddir)
             (if
               (is oldpos minpos)
                 (if (is olddir -1)
                     1
                     olddir)
               (is oldpos maxpos)
                 (if (is olddir 1)
                     -1
                     olddir)
               ; else
                 olddir)))
       `(,(delimit 0 oldxpos (- (len (world 0)) 1)
                   oldxdir)
         ,(delimit 0 oldypos (- (len (world 0)) 1)
                   oldydir))))


1 point by bOR_ 6026 days ago | link

So.. (let newdir '(nil nil) should become (let newdir (list nil nil) and creature!pos is equivalent to (creature 'pos) and you make a temporary anonymous function within a function but still give it a name (delimit, through the with command) that does the comparing. Three new things learned :).

I'll see if I can improve it some more though, might be able to do something with testing whether position + car / cadr direction would put a creature out of bounds in the world array, and then multiply the direction by -1. Not at home now, so will have to wait.

-----