#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.33
216.73.216.33
216.73.216.33
 
July 18, 2022


A new era ‼

Our infrastructure has recently upgraded ‼

Nugget Communications Bureau 👍

You've never emailed like this before ‼

Roundcube

Webmail software for reading and sending email from @nugget.fun and @shampoo.ooo addresses.

Mailman3

Email discussion lists, modernized with likes and emojis. It can be used for announcements and newsletters in addition to discussions. See lists for Picture Processing or Scrapeboard. Nowadays, people use Discord, but you really don't have to!

FreshRSS

With this hidden in plain sight, old technology, even regular people like you and me can start our own newspaper or social media feed.

Nugget Streaming Media 👍

The content you crave ‼

HLS

A live streaming, video format based on M3U playlists that can be played with HTML5.

RTMP

A plugin for Nginx can receive streaming video from ffmpeg or OBS and forward it as an RTMP stream to sites like Youtube and Twitch or directly to VLC.


Professional ‼

Nugget Productivity Suite 👍

Unleash your potential ‼

Kanboard

Virtual index cards you can use to gamify your daily grind.

Gitea

Grab whatever game code you want, share your edits, and report bugs.

Nugget Security 👍

The real Turing test ‼

Fail2ban

Banning is even more fun when it's automated.

Spamassassin

The documentation explains, "an email which mentions rolex watches, Viagra, porn, and debt all in one" will probably be considered spam.

GoAccess

Display HTTP requests in real time, so you can watch bots try to break into WordPress.

Nugget Entertainment Software 👍

The best in gaming entertainment ‼

Emoticon vs. Rainbow

With everything upgraded to the bleeding edge, this HTML4 game is running better than ever.


Zoom ‼

The game engine I've been working on, SPACE BOX, is now able to export to web, so I'm planning on turning nugget.fun into a games portal by releasing my games on it and adding an accounts system. The upgraded server and software will make it easier to create and maintain. I'm also thinking of using advertising and subscriptions to support the portal, so some of these services, like webmail or the RSS reader, may be offered to accounts that upgrade to a paid subscription.