Gen1.cxx

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "../central_intelligence_agency/cia.hxx"

#define DELIMITER line("\n-----------------------------------------------------------\n");
#define SPACE line("");

#define MAX_LINE_LENGTH 76

#define TRUE  1
#define FALSE 0

#define LEARN	    0x0001
#define COMBAT      0x0002
#define FINISH_THEM 0x0004

void line (char *str);
void line (char *str,int no);
void comment (char *str,int EndNewLine = TRUE,int StartNewLine = TRUE);

// void leading_comment ();

void include_function (char* filename,int IsAFunction = TRUE);
int nocharacters (char* str,int max);

int mode = COMBAT;
FILE* output;

void main (int argc,char* argv[])
{
  char buf[128];
  int i;
  output = NULL;

  CCia theCia;

  theCia.LoadFile ("central_intelligence_agency/main.file");

  if (argc > 1)
    {	
      strcpy (buf,argv[1]);
      for (i = 0; buf[i] != '\0'; i++)
	buf[i] = toupper (buf[i]);
      if (strcmp (buf,"C") == 0 || strcmp (buf,"COMBAT") == 0)
	mode = COMBAT;
      if (strcmp (buf,"L") == 0 || strcmp (buf,"LEARN") == 0)
	mode = LEARN;
      if (strcmp (buf,"F") == 0 || strcmp (buf,"FINISH") == 0 || strcmp (buf,"FINISH_THEM") == 0)
	mode = FINISH_THEM;      
    }
  if (mode == LEARN)
    {
      if ((output = fopen ("spelare47x.adb","w")) == NULL)
	{
	  printf ("Couldn't open 'spelare47.adb' for output!\n");
	  exit (1);
	} 
    }
  else
    {
      if ((output = fopen ("spelare47.adb","w")) == NULL)
	{
	  printf ("Couldn't open 'spelare47.adb' for output!\n");
	  exit (1);
	} 
    }
  
  // generate beginning, and some comments
  include_function ("leading_comments.adb",FALSE);
  SPACE;
  if (mode == LEARN)
    line ("package body Spelare47x is");
  else
    line ("package body Spelare47 is");

  // generate variables containg (concealed) information about other players

  include_function ("global_variables1.adb");
  DELIMITER;
  line ("   type OppSpecsArray is array(1 .. %d) of OppSpecsRec; -- auto-generated",theCia.NumKnownOpponents());
  line ("   type MoveSpecsArray is array(1 .. %d) of MoveSpecsRec; -- auto-generated",theCia.NumKnownMoveSeries());

  include_function ("global_variables2.adb");

  // generate functions

  include_function ("default.adb");

  include_function ("titfortat.adb");
  include_function ("handletitfortat.adb");
  include_function ("alwayscooperate.adb");
  include_function ("alwaysdefect.adb");
  include_function ("inversetitfortat.adb");
  include_function ("insomeorder.adb");
  include_function ("crazyplayer.adb");
  include_function ("crazyplayer2.adb");
  include_function ("defwhen2.adb");
  include_function ("defwhen5.adb");
  include_function ("defwhen8.adb");
  include_function ("logic1.adb");
  include_function ("logic2.adb");
  include_function ("logic3.adb");
  include_function ("pattern1.adb");
  include_function ("pattern2.adb");
  include_function ("titfor2tat1.adb");
  include_function ("titfor2tat2.adb");
  include_function ("predefpattern.adb");

  // generate the exported player-function

  include_function ("player47.adb");

  // generate body and ending, and some comments

  DELIMITER;
  line ("begin");
  
  i = 0;
  int cnt = 1;
  char name[64];

  int tactics[2];
  struct ExpectStruct es;  

  while (theCia.GetTacticsForNextOpponent(&i,name,tactics,&es))
    {
      fprintf (output,
	       "   OpponentSpecs(%2d) := (\"%s\",(%d,%d),((%d,%d),(%d,%d)),0);\n",
	       cnt,name,
	       tactics[0],tactics[1],
	       es.exme[0],es.exopp[0],es.exme[1],es.exopp[1]);

      /*      fprintf (output,
	       "   OpponentSpecs(%d).Name := \"%s\";\n",
	       cnt,name);
	       fprintf (output,
	       "   OpponentSpecs(%d).Tactics := (%d,%d);\n",
	       cnt,tactics_1,tactics_2);
	       fprintf (output,
	       "   OpponentSpecs(%d).Expect := ((%d,%d),(%d,%d));\n",
	       cnt,exp_1_me,exp_1_opp,exp_2_me,exp_2_opp); */
      

      cnt++;
    }
  cnt = 1;
  for (i = 0; i < MAXNUMPLAYERS; i++)
    if (theCia.MoveH[i].tested == 1)
      {
	theCia.GroupToName (i+1,name);
	fprintf (output,"   MoveSpecs(%2d) := (\"%s\",\n                     ((",cnt,name);
	for (int j = 0; j < 2; j++)
	  {
	    int written = 22;
	    for (int k = 0; k < 32; k++)
	      {
		written += fprintf (output,k < 31 ? "%u," : "%u",theCia.MoveH[i].list[j][k]);
		if (written > 65)
		  {
		    written = 22;
		    fprintf (output,"\n                       ");
		  }		
	      }
	    fprintf (output,j == 0 ? "),\n                      (" : ")");
	  }
	fprintf (output,"));\n");
	cnt++;
      }
  
  if (mode == LEARN)
    line ("end Spelare47x;");
  else
    line ("end Spelare47;");

  fclose (output);
}

void line (char *str)
{
  fprintf (output,"%s\n",str);
}

void line (char *str,int no)
{
  fprintf(output,str,no,no,no,no,no); // C does accept this, no trouble...
  fprintf(output,"\n");
}

void comment (char *str,int EndNewLine,int StartNewLine)
{
  char buf[256];
  char *ptr = str;
  int cnt = 0;

  if (StartNewLine)
    fprintf (output,"\n");
  buf[0] = '\0';
  
  while (*ptr != '\0')
    {
      cnt = strchr (ptr,' ') != NULL ? strchr (ptr,' ')-ptr : strlen (ptr);
      if (strlen (buf)+cnt < MAX_LINE_LENGTH-3)
	{
	  strncat (buf,ptr,cnt);
	  if (strchr (ptr,' ') != NULL)
	    strcat (buf," ");
	  ptr += cnt + 1;
	}
      else
	{
	  fprintf (output,"-- %s\n",buf);
	  buf[0] = '\0';
	  if (cnt >= MAX_LINE_LENGTH-3)
	    {
	      strncpy (buf,ptr,cnt);
	      if (strchr (ptr,' ') != NULL)
		strcat (buf," ");
	      ptr += cnt + 1;
	      fprintf (output,"-- %s\n",buf);
	    }
	}
    }
  if (buf[0] != '\0')
    fprintf (output,"-- %s\n",buf);

  if (EndNewLine)
    fprintf (output,"\n");
}

void include_function (char* filename,int IsAFunction)
{
  FILE* input;
  char buf[1024];
  int newlines = 0;
  int modeonly = 0;

  if ((input = fopen (filename,"r")) == NULL)
    {
      printf ("Couldn't open '%s' for input!\n",filename);
      exit (1);
    }
  if (IsAFunction)
    DELIMITER;
  while (!feof(input))
    {
      buf[0] = '\0';
      fgets (buf,1024,input);
      if (nocharacters(buf,1024))
	newlines = 1; /*newlines++;*/
      else
      	{
	  if (strstr (buf,"--}") != NULL)
	    modeonly = 0;
	  if (strstr (buf,"--{L") != NULL)
	    modeonly |= LEARN;
	  if (strstr (buf,"--{C") != NULL)
	    modeonly |= COMBAT;
	  if (strstr (buf,"--{F") != NULL)
	    modeonly |= FINISH_THEM;
	  if (strstr (buf,"--}") == NULL && strstr (buf,"--{") == NULL &&
		   (modeonly == 0 || (modeonly & mode)))
	    {
	      for (; newlines; newlines--)
		fputc ('\n',output);
	      if (IsAFunction)
		fprintf (output,"   %s",buf);
	      else
		fprintf (output,buf);
	    }
	}
    }
  fclose (input);
}
/*
void leading_comment ()
{
  comment ("This briefing is from FILE spelare47.adb",FALSE,FALSE);
  comment ("CLASSIFIED",FALSE,FALSE);
  comment ("TOP SECRET",FALSE,FALSE);
  comment ("SUBJECT is Thief of Budapest.",FALSE,FALSE);
  
  comment ("A ms 1- attack player with the most advanced AI-implementation in the system today.",FALSE);
  
  comment ("It\'s been created somewhere in the western wildernes by development group f1pt-47 - f1pt-47 has promised to reveal the code only after the project is closed.",FALSE);
  
  comment ("We suspect that Spy, an unofficial GameEngine of the agency that built Thief of Budapest, is secretly helping the Thief in return for it\'s services.",FALSE);
  
  switch (mode)
    {
    case LEARN:
      comment ("Generated in LEARN-mode: This version is highly specialized and only work together with Spy. It's sole purpose is to take exact orders about what to do in order to test itself, hence it learns about and become able to match the other players' tactics long before they know they've been studied.",FALSE);
      break;
    case COMBAT:
      comment ("Generated in COMBAT-mode: This is the version whose object-code is public.",FALSE);
      break;
    case FINISH_THEM:
      comment ("Generated in FINISH_THEM-mode: This version contains one or two additional tricks, and is therefore not to be released until a short time before times up.",FALSE);
      break;
    }

  comment ("With Thief and Spy operating as a team - with effectivity rivalling the brightest politicians, backed by unmatched firepower, Thief of Budapest is too dangerous to be left in unenlighted hands. - Keeping it secret is your first priority. EOF");
}
*/
int nocharacters (char* str,int max)
{
  int i = 0;
  
  while (*(str+i) != '\0' && i < max)
    {
      if (*(str+i) != ' ' && *(str+i) != '\n' && *(str+i) != '\t')
	return FALSE;
      i++;
    }
  return TRUE;
}

Tillbaka till huvudsidan