[Home]MoonShadow/Sokoban

ec2-3-136-22-50.us-east-2.compute.amazonaws.com | ToothyWiki | MoonShadow | RecentChanges | Login | Webcomic

[Click to play]

ToothyGDL

logic
{
 // NB. this must be at least big enough for the biggest layout
 array board 
      { 
        0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0
      }
 int boardwidth { 9 }
 int boardheight { 9 }

 // 0 = floor
 // 1 = wall
 // 2 = crate
 // 3 = target space
 // 4 = player start
 // Each layout must be surrounded by two rows of wall on all sides. 

 // To add a new level:
 // * Append its board to the array below
 // * Append the width and height to layout_widths and layout_heights below
 // * Append its title to the layout_names stringtable in the display section
 // * Make sure the board, boardwidth and boardheight above are still big enough
 // * Recompile and play
 local array layouts
      { 
        1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 0, 0, 0, 0, 1, 1,
        1, 1, 0, 2, 0, 4, 1, 1,
        1, 1, 0, 0, 0, 0, 1, 1,
        1, 1, 0, 3, 0, 0, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1,

        1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 0, 0, 0, 3, 3, 1, 1,
        1, 1, 0, 0, 1, 0, 3, 1, 1,
        1, 1, 0, 2, 2, 0, 0, 1, 1,
        1, 1, 0, 0, 2, 1, 0, 1, 1,
        1, 1, 4, 0, 0, 0, 0, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1
      }
 local int number_of_layouts { 2 }
 local array layout_widths  { 8, 9 }
 local array layout_heights { 8, 9 }


 int started { 0 }
 int currentwidth { 0 }
 int currentheight { 0 }
 int location { 0 }
 int crates_left { 0 }
 int win { 0 }

 local int scratch { 0 }

 // Allow the user to start the game if it hasn't already been started
 for layout ( 0 .. (number_of_layouts-1) )
 {
  when { started == 0 }
  allow
  {
   scratch = 0;
   for i ( 0..(layout-1) )
   {
    scratch = scratch + (layout_widths[i]*layout_heights[i]);
   }
   currentwidth = layout_widths[layout];
   currentheight = layout_heights[layout];
   for i ( 0..((currentwidth*currentheight)-1) )
   {
    board[i] = layouts[scratch+i];
    if( board[i] == 2 )
    {
      crates_left = crates_left + 1;
    }
    if( board[i] == 4 )
    {
      location = i;
    }
   }
   started = 1;
   win = 0;
  }
  label { "Start " layout }
 }

 // Allow the user to move when legal
 when 
 { 
    (started == 1)
    and
    (
      (board[location-1] == 0)
    )
 }
 allow
 {
    board[location-1] = board[location];
    board[location] = 0;
    location = location - 1;
 }
 label { "Move left" }

 when 
 { 
    (started == 1)
    and
    (
      (board[location+1] == 0)
    )
 }
 allow
 {
    board[location+1] = board[location];
    board[location] = 0;
    location = location + 1;
 }
 label { "Move right" }

 when 
 { 
    (started == 1)
    and
    (
      (board[location-currentwidth] == 0)
    )
 }
 allow
 {
    board[location-currentwidth] = board[location];
    board[location] = 0;
    location = location - (currentwidth);
 }
 label { "Move up" }


 when 
 { 
    (started == 1)
    and
    (
      (board[location+currentwidth] == 0)
    )
 }
 allow
 {
    board[location+currentwidth] = board[location];
    board[location] = 0;
    location = location + (currentwidth);
 }
 label { "Move down" }

 // Allow the user to push boxes when legal
 // Use same labels as the move rules, because the board contents are mutually exclusive
 // so only one of the two kinds of rules can be bound for each direction
 when 
 { 
    (started == 1)
    and
    (
      (board[location-1] == 2)
      and
      ((board[location-2] == 0) or (board[location-2] == 3))
    )
 }
 allow
 {
    scratch = board[location-2];
    board[location-2] = board[location-1];
    board[location-1] = board[location];
    board[location] = 0;
    if( scratch == 3 )
    {
      crates_left = crates_left - 1;
      board[location-2] = 0;
    }
    location = location - 1;
 }
 label { "Move left" }

 when 
 { 
    (started == 1)
    and
    (
      (board[location+1] == 2)
      and
      ((board[location+2] == 0) or (board[location+2] == 3))
    )
 }
 allow
 {
    scratch = board[location+2];
    board[location+2] = board[location+1];
    board[location+1] = board[location];
    board[location] = 0;
    if( scratch == 3 )
    {
      crates_left = crates_left - 1;
      board[location+2] = 0;
    }
    location = location + 1;
 }
 label { "Move right" }

 when 
 { 
    (started == 1)
    and
    (
      (board[location-currentwidth] == 2)
      and
      ((board[location-(currentwidth*2)] == 0) or (board[location-(currentwidth*2)] == 3))
    )
 }
 allow
 {
    scratch = board[location-(currentwidth*2)];
    board[location-(currentwidth*2)] = board[location-currentwidth];
    board[location-currentwidth] = board[location];
    board[location] = 0;
    if( scratch == 3 )
    {
      crates_left = crates_left - 1;
      board[location-(currentwidth*2)] = 0;
    }
    location = location - currentwidth;
 }
 label { "Move up" }


 when 
 { 
    (started == 1)
    and
    (
      (board[location+currentwidth] == 2)
      and
      ((board[location+(currentwidth*2)] == 0) or (board[location+(currentwidth*2)] == 3))
    )
 }
 allow
 {
    scratch = board[location+(currentwidth*2)];
    board[location+(currentwidth*2)] = board[location+currentwidth];
    board[location+currentwidth] = board[location];
    board[location] = 0;
    if( scratch == 3 )
    {
      crates_left = crates_left - 1;
      board[location+(currentwidth*2)] = 0;
    }
    location = location + currentwidth;
 }
 label { "Move down" }

 // Detect winning condition
 when { (started == 1) and (crates_left <= 0) }
 allow { win = 1; started = 0; }
 label { "Win" }

}

display
{
 stringtable layout_names
 {
  "Test level 1",
  "Test level 2"
 }

 stringtable pieces
 {
  " ", "#", "O", "+", "@" // TODO: we need some better pieces.
 }

 // Title
 box
 {
  content { "Sokoban" }
 }

 newline

 // Menu
 for layout ( 0 .. (number_of_layouts-1) )
 {
  box
  {
    if( started == 0 )
    {
     border { 1px solid black }
     content { layout_names[layout] }

     bind_action { "Start " layout  }
    }
  }
 }
 box
 {
  bind_action { "Win" }
  while_active 
  { 
    border { 1px solid black }
    content { "Well done! Click here for the menu." } 
  }
 }

 newline

 // Board
 for y ( 0 .. (boardheight-1) )
 {
  for x ( 0 .. (boardwidth-1) )
  {
    box
    {
      if( (started == 1) and (( x < currentwidth ) and ( y < currentheight )) )
      {
        width { 1em }
        height { 1em }
        content { pieces[ board[(y*currentwidth)+x] ] }
      }
    }
  }
  newline
 }

 // Controls
 box
 {
  if( started == 1 )
  {
    width { 2em }
    height { 1em }
    border { 1px solid black }
    content { "←" }
    bind_action { "Move left" }
  }
 }
 box
 {
  if( started == 1 )
  {
    width { 2em }
    height { 1em }
    border { 1px solid black }
    content { "↑" }
    bind_action { "Move up" }
  }
 }
 box
 {
  if( started == 1 )
  {
    width { 2em }
    height { 1em }
    border { 1px solid black }
    content { "↓" }
    bind_action { "Move down" }
  }
 }
 box
 {
  if( started == 1 )
  {
    width { 2em }
    height { 1em }
    border { 1px solid black }
    content { "→" }
    bind_action { "Move right" }
  }
 }

 newline

 box
 {
  if( started == 1 )
  {
    content{ "You control the @. Push all the Os into the +s." }
  }
 }
}

[Recompile this page]
[Compiler error log]

ec2-3-136-22-50.us-east-2.compute.amazonaws.com | ToothyWiki | MoonShadow | RecentChanges | Login | Webcomic
This page is read-only | View other revisions | Recently used referrers
Last edited January 25, 2006 11:05 am (viewing revision 7, which is the newest) (diff)
Search: