<?php

require_once (dirname(__FILE__) . "/fetch.php");

// Move file pointer to the corresponding closing bracket of the current
// element.
function move_to_closing_bracket($fp, &$char)
{
   if ($char == CLOSE_ELEMENT_CHAR) return;

   $ii = 1;
   while (($char = fgetc($fp)) != NULL && !feof($fp))
   {
      if ($char == OPEN_ELEMENT_CHAR) $ii++;
      else
      {
         if ($char == CLOSE_ELEMENT_CHAR) $ii--;
         if ($ii == 0) break;
      }
   }
}

// Store the file contents as a string, replace the specified string with a new
// string, and re-write the file.
function replace_string_in_file($file_name, $old, $new="")
{
   $file_contents = file_get_contents($file_name);
   $file_contents = str_replace($old, $new, $file_contents);

   $fp = fopen($file_name, 'w');
   
   fwrite($fp, $file_contents);
}

// Erase file contents between and including element's brackets.
function read_raw_element_string($fp, &$char)
{
   $element_open_position = ftell($fp)-1;
   move_to_closing_bracket($fp, $char);
   $element_close_position = ftell($fp);

   if (char_is_white_space($char = fgetc($fp)))
   {
      skip_white_space($fp, $char);
      $element_close_position = ftell($fp)-1;
   }

   $byte_range = $element_close_position - $element_open_position;
   fseek($fp, $element_open_position);
   $element = fread($fp, $byte_range);

   return $element;
}

// If the submitted element points to an object, convert the element to its
// raw string representation.  Otherwise, leave the string unchanged.
function convert_element_to_raw($element)
{
   if (is_object($element))
   {
      $element = $element->convert_to_raw_string();
   }

   return $element;
}

// Replace in a file the element identified by specified ID w/ a new element.
// The new element can be an object, element string, or empty.  If empty, the
// old element will be removed and replaced with nothing.
function replace_element_by_id($file_name, $id, $element="")
{
   if (file_exists($file_name))
   {
      $fp = fopen($file_name, 'r+');
      move_to_element_by_id($fp, $char, $id);
      if (feof($fp)) return;
      $old_raw_element = read_raw_element_string($fp, $char, $id);
      $new_raw_element = convert_element_to_raw($element);
      fclose($fp);

      if ($old_raw_element != NULL)
      {
         replace_string_in_file(
            $file_name, $old_raw_element, $new_raw_element);
      }
      return $old_raw_element;
   }
}

// Replace an element with a new element.  If the new element is unspecified,
// the old element will be deleted.
function run_replace($file_name, $id, $element)
{
   if (!file_exists($file_name)) return;

   replace_element_by_id($file_name, $id, $element);
   $message = "!! Removing element $id... !!\n\n";
   if ($element != NULL)
   {
      $message .= "Replacing with\n--------------\n";
      $message .= convert_element_to_raw($element);
      $message .= "\n\n";
   }
   $message .= "New file\n--------\n";
   $message .= run_parse($file_name);

   return $message;
}
<?php

require_once (dirname(__FILE__) . "/fetch.php");
require_once (dirname(__FILE__) . "/replace.php");

// Insert a string into the file, leaving the current data intact.
function insert_string_at_cursor($fp, $string)
{
   $position = ftell($fp);
   $file_meta_data = fstat($fp);
   $length = $file_meta_data["size"] - $position;

   $tail = fread($fp, $length);
   fseek($fp, $position);
   $new_content = $string . $tail;

   ftruncate($fp, $position);
   fwrite($fp, $new_content);
}

// Insert the element data at the end of the file.
function insert_element_into_file($fp, $element)
{
   fwrite($fp, $element);
}

// Insert an element into the element referenced by id.  This will create a new
// child for the referenced element.
function insert_element_into_element($fp, $element, $id)
{
   move_to_element_by_id($fp, $char, $id);
   move_to_closing_bracket($fp, $char);
   fseek($fp, ftell($fp)-1);
   insert_string_at_cursor($fp, $element);
}

// Move to the position either before or after the element currently under the
// cursor.
function move_cursor_based_on_insertion_method($fp, $char, $before)
{
   if ($before == False)
   {
      move_to_closing_bracket($fp, $char);
   }
   else
   {
      fseek($fp, ftell($fp)-1);
   }
}

// Insert an element before or after the element referenced by id.
function insert_element_at_id($fp, $element, $id, $before=False)
{
   move_to_element_by_id($fp, $char, $id);
   move_cursor_based_on_insertion_method($fp, $char, $before);
   insert_string_at_cursor($fp, $element);
}

// Open file for writing or appending based on where the new element will be
// inserted.
function open_file_based_on_method($file_name, $id)
{
   if ($id != NULL)
   {
      $fp = fopen($file_name, 'r+');
   }
   else
   {
      $fp = fopen($file_name, 'a');
   }

   return $fp;
}

// Choose one of three methods for insertion based on the existence and values
// of the id and child parameters.
function apply_insertion_method($fp, $raw_element, $id, $child, $before)
{
   if ($child == True)
   {
      insert_element_into_element($fp, $raw_element, $id);
   }
   elseif ($id != NULL)
   {
      insert_element_at_id($fp, $raw_element, $id, $before);
   }
   else
   {
      insert_element_into_file($fp, $raw_element);
   }
}   

// Insert an element into the specified file.  The before parameter indicates
// whether or not the incoming element should be placed before the referenced
// element.
function insert_element(
   $file_name, $element, $id=NULL, $child=False, $before=False)
{
   $fp = open_file_based_on_method($file_name, $id);
   $raw_element = convert_element_to_raw($element);
   apply_insertion_method($fp, $raw_element, $id, $child, $before);
}

// Insert an element into the specified file.  Return relevant messages for
// verbose output.
function run_insert($file_name, $element, $id=NULL, $child=False, $before=False)
{
   $message = "Inserting\n---------\n";
   $message .= convert_element_to_raw($element) . "\n\n";

   $child = ($child && $child != '0') ? True : False;
   $before = ($before && $before != '0') ? True : False;
   insert_element($file_name, $element, $id, $child, $before);

   $message .= "New File\n--------\n";
   $message .= run_parse($file_name);

   return $message;
}
216.73.216.215
216.73.216.215
216.73.216.215
 
August 12, 2013

I've been researching tartan/plaid recently for decoration in my updated version of Ball & Cup, now called Send. I want to create the atmosphere of a sports event, so I plan on drawing tartan patterns at the vertical edges of the screen as backgrounds for areas where spectator ants generate based on player performance. I figured I would make my own patterns, but after browsing tartans available in the official register, I decided to use existing ones instead.

I made a list of the tartans that had what I thought were interesting titles and chose 30 to base the game's levels on. I sequenced them, using their titles to form a loose narrative related to the concept of sending. Here are three tartans in the sequence (levels 6, 7 and 8) generated by an algorithm I inferred by looking at examples that reads a tartan specification and draws its pattern using a simple dithering technique to blend the color stripes.


Acadia


Eve


Spice Apple

It would be wasting an opportunity if I didn't animate the tartans, so I'm thinking about animations for them. One effect I want to try is making them look like water washing over the area where the ants are spectating. I've also recorded some music for the game. Here are the loops for the game over and high scores screens.

Game Over

High Scores