View Single Post
Old 03-22-2012, 07:31 PM   #42
kaminkatze
Member
kaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with others
 
Posts: 17
Karma: 2600
Join Date: Mar 2012
Device: Kindle 3
Quote:
Originally Posted by geekmaster View Post
"Normal" shell arrays did not work for me, but substrings work.
echo ${v:1:1} returns -sh: syntax error: Bad substitution on my K3, and expr substr $v 1 1 is even slower than hexdump.

Here is a ev[ia]l array style version.
eval "v=\$A${x}x$y".

I used 4 values. 0/1 for dead 100/101 for living. 1 and 101 are the dirty states wich are rendered with filled circles.

Spoiler:

Code:
#!/bin/sh

#=================================
# initvar - initialize global vars
#---------------------------------
initvar() {
  W=15 H=20; MX=$((W-1)) MY=$((H-1)); G=19 R=5
  LL=300; DLL=$((2*LL))
  for y in $(seq 0 $MY); do for x in $(seq 0 $MX); do
    eval "N${x}x$y=0"
  done; done
  DZ=/dev/zero DN=/dev/null DF=/dev/fb0
  B=$(echo -ne '\xff'); B=$B$B$B$B$B$B$B$B$B$B
  B=$B$B$B$B$B$B$B$B$B$B; B=$B$B$B$B$B$B$B$B$B$B
  eips -c
}

#============================
# load - load common patterns
# usage: load name
#----------------------------
load() {
  case $1 in
    glider) N1x0=101 N2x1=101 N0x2=101 N1x2=101 N2x2=101 ;;
    fill) for y in $(seq 0 $MY); do for x in $(seq 0 $MX); do
            eval "N${x}x$y=101"
          done; done;;
    spaceship) N1x0=101 N2x0=101 N3x0=101 N4x0=101 N4x1=101
               N4x2=101 N3x3=101 N0x1=101 N0x3=101 ;;
  esac
}

#==========================================
# circle - filled midpoint circle algorithm
# usage: echo $B | circle cx cy r
#        circle cx cy r <$DZ
#------------------------------------------
circle() {
  local cx=$1 cy=$2 r=$3; local e=$((-r)) x=$r y=0
  local x0 x1 x2 y0 y1 y2
  while [[ $x -ge $y ]]; do
    x0=$(((cy-x)*DLL+cx)) x1=$(((cy+x)*DLL+cx)) x2=$((2*x))
    y0=$(((cy-y)*DLL+cx)) y1=$(((cy+y)*DLL+cx)) y2=$((2*y))
    dd of=$DF bs=1 count=$x2 seek=$((y1-x))
    dd of=$DF bs=1 count=$x2 seek=$((y1+LL-x))
    dd of=$DF bs=1 count=$x2 seek=$((y0-x))
    dd of=$DF bs=1 count=$x2 seek=$((y0+LL-x))
    dd of=$DF bs=1 count=$y2 seek=$((x1-y))
    dd of=$DF bs=1 count=$y2 seek=$((x1+LL-y))
    dd of=$DF bs=1 count=$y2 seek=$((x0-y))
    dd of=$DF bs=1 count=$y2 seek=$((x0+LL-y))
    e=$((e+2*y+1)); y=$((y+1))
    if [[ $e -ge 0 ]]; then e=$((e-2*x-1)); x=$((x-1)); fi
  done 2>$DN
}

#======================
# update - update field
#----------------------
update() {
  for y in $(seq 0 $MY); do for x in $(seq 0 $MX); do
    eval "c=\$((\$L$(((x+MX)%W))x$(((y+MY)%H))\
               +\$L${x}x$(((y+MY)%H))\
               +\$L$(((x+1)%W))x$(((y+MY)%H))\
               +\$L$(((x+MX)%W))x$y\
               +\$L$(((x+1)%W))x$y\
               +\$L$(((x+MX)%W))x$(((y+1)%H))\
               +\$L${x}x$(((y+1)%H))\
               +\$L$(((x+1)%W))x$(((y+1)%H))))
          v=\$L${x}x$y
          if [[ \$v -lt 2 ]]; then
            [[ \$c -gt 299 -a \$c -lt 400 ]] && v=101 || v=0
          else
            [[ \$c -lt 200 -o \$c -gt 399 ]] && v=1 || v=100
          fi
          N${x}x$y=\$v"
  done; done
}

#=====================================
# render - copy current state and draw
#-------------------------------------
render() {
  for y in $(seq 0 $MY); do for x in $(seq 0 $MX); do
    eval "v=\$N${x}x$y; L${x}x$y=\$v"
    case $v in
      101) echo $B | circle $((x*G+G)) $((y*G+G)) $R ;;
      1) circle $((x*G+G)) $((y*G+G)) $R <$DZ ;;
    esac
  done; done; eips ''
}

#==========================
# loop - render update loop
#--------------------------
loop() { while true; do render; update; done; }

initvar
load glider
loop
kaminkatze is offline   Reply With Quote