#include <stdio.h>
#include <stdlib.h>
#include "encode.h"
#include "read.h"
#include "pattern.h"

int encode_level(FILE* input_file, FILE* output_file)
{
   char character[1] = {' '};
   char* mode_string = encode_mode_string(input_file, output_file, character);
   if (!mode_string) return -1;
   if (!encode_sub_mode_string(input_file, output_file, character)) return -1;
   if (!encode_color_values(input_file, output_file, character)) return -1;
   if (!strcmp(mode_string, RAINBOW_MODE_STRING))
   {
      int ii = 0;
      char* beam_patterns[BEAM_PATTERN_COUNT];
      while (ii < BEAM_PATTERN_COUNT)
      {
         beam_patterns[ii++] = parse_pattern(input_file, character);
      }
      encode_course(beam_patterns, output_file);
   }
}

void encode_course(char** beam_patterns, FILE* output_file)
{
   int ii = 0, jj = 0, kk = 0, ll = 0;
   u8 color_index, position, length, gap;
   char* color_pattern = beam_patterns[0];
   char* position_pattern = beam_patterns[1];
   char* length_pattern = beam_patterns[2];
   char* gap_pattern = beam_patterns[3];
   while (color_pattern[ii])
   {
      if (!position_pattern[jj]) jj = 0;
      if (!length_pattern[kk]) kk = 0;
      if (!gap_pattern[ll]) ll = 0;
      printf(
         "%c%c%c%c ", color_pattern[ii], position_pattern[jj],
         length_pattern[kk], gap_pattern[ll]);
      color_index =  color_pattern[ii++] - 'A';
      position = position_pattern[jj++] - '0';
      length = length_pattern[kk++] - '0';
      gap = gap_pattern[ll++] - '0';
      fwrite(&color_index, sizeof(u8), 1, output_file);
      fwrite(&position, sizeof(u8), 1, output_file);
      fwrite(&length, sizeof(u8), 1, output_file);
      fwrite(&gap, sizeof(u8), 1, output_file);
   }
   printf("\n");
}

int encode_color_values(FILE* input_file, FILE* output_file, char* character)
{
   int ii;
   u32 byte_sequence;
   char* color_string;
   for (ii = 0; ii < COLOR_COUNT; ii++)
   {
      color_string = read_next_word(input_file, character);
      byte_sequence = strtol(color_string, NULL, 16);
      fwrite(&byte_sequence, sizeof(u8), 3, output_file);
      if (*character == '\n') break;
   }
   while (++ii < COLOR_COUNT)
   {
      byte_sequence = determine_color_from_index(output_file, ii);
      fwrite(&byte_sequence, sizeof(u8), 3, output_file);
   }
}

u32 determine_color_from_index(FILE* file, int ii)
{
   switch (ii)
   {
      case 0: return COLOR_RED;
      case 1: return COLOR_ORANGE;
      case 2: return COLOR_YELLOW;
      case 3: return COLOR_GREEN;
      case 4: return COLOR_BLUE;
      case 5: return COLOR_INDIGO;
      case 6: return COLOR_VIOLET;
   }
}
         

int encode_sub_mode_string(FILE* input_file, FILE* output_file, char* character)
{
   u8 byte_sequence;
   char* survival_sub_mode_string = read_next_word(input_file, character);
   if (!strcmp(survival_sub_mode_string, RACE_SUB_MODE_STRING))
   {
      byte_sequence = RACE_SUB_MODE_BYTE_SEQUENCE;
   }
   else if (!strcmp(survival_sub_mode_string, SURVIVAL_SUB_MODE_STRING))
   {
      byte_sequence = SURVIVAL_SUB_MODE_BYTE_SEQUENCE;
   }
   else
   {
      printf("Unrecognized sub-mode value in level definition\n");
      return -1;
   }
   fwrite(&byte_sequence, sizeof(u8), 1, output_file);
}

char* encode_mode_string(FILE* input_file, FILE* output_file, char* character)
{
   u8 byte_sequence;
   char* mode_string = read_next_word(input_file, character);
   if (!strcmp(mode_string, RAINBOW_MODE_STRING))
   {
      byte_sequence = RAINBOW_MODE_BYTE_SEQUENCE;
   }
   else
   {
      printf("Unrecognized mode value in level definition\n");
      return NULL;
   }
   fwrite(&byte_sequence, sizeof(u8), 1, output_file);
   return mode_string;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "initialization.h"

FILE* initialize_translator(
   char* argv[], int* decode_flag, FILE** output_file, FILE** javascript_file)
{
   char output_method[3], input_method[3];
   determine_methods(output_method, input_method, *decode_flag);

   FILE* input_file = NULL;
   char* input_file_path = parse_arguments(argv, decode_flag);
   char* output_file_path;
   char* javascript_file_path;
   if (input_file_path)
   {
      input_file = open_file(input_file_path, input_method);
      if (input_file)
      {
         output_file_path =
            build_output_file_name(input_file_path, *decode_flag);
         *output_file = open_file(output_file_path, output_method);
         javascript_file_path =
            replace_extension(
               input_file_path, LEVEL_EXTENSION, JAVASCRIPT_EXTENSION);
         *javascript_file = open_file(javascript_file_path, "w");
      }
   }

   return input_file;
}

void determine_methods(
   char* output_method, char* input_method, int decode_flag)
{
   if (decode_flag)
   {
      strcpy(input_method, "rb");
      strcpy(output_method, "w");
   }
   else
   {
      strcpy(input_method, "r");
      strcpy(output_method, "wb+");
   }
}

// I think you may be wrong there *wink*
FILE* open_file(char* file_name, char* mode)
{
   FILE* file = fopen(file_name, mode);
   if (file == NULL)
   {
      printf("File could not be opened: %s\n", file_name);
   }
   return file;
}

char* build_output_file_name(char* input_file_path, int decode_flag)
{
   if (input_file_path == NULL) return;
   char* output_file_path = decode_flag ?
      replace_extension(input_file_path, ENCODED_EXTENSION, LEVEL_EXTENSION) :
      replace_extension(input_file_path, LEVEL_EXTENSION, ENCODED_EXTENSION);

   return output_file_path;
}

char* replace_extension(char* path, char* extension, char* replacement)
{
   int new_length = strlen(path) + strlen(replacement) - strlen(extension);
   char* new_path = malloc(sizeof(char) * (new_length+1));
   strcpy(new_path, path);
   char* truncate_position = strstr(new_path, extension);
   *truncate_position = '\0';
   strcat(new_path, replacement);
   return new_path;
}

char* parse_arguments(char* argv[], int* decode_flag)
{
   int ii;
   for (ii = 1; argv[ii]; ii++)
   {
      if (argv[ii][0] == '-')
      {
         if (argv[ii][1] == DECODE_OPTION_INDICATOR) *decode_flag = TRUE;
      }
      else return argv[ii];
   }
   print_usage();
   return NULL;
}
216.73.216.144
216.73.216.144
216.73.216.144
 
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