DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T g

⟦57da5bb64⟧ TextFile

    Length: 6373 (0x18e5)
    Types: TextFile
    Names: »game.c«

Derivation

└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987
    └─⟦this⟧ »EUUGD18/X/Xrobots/game.c« 

TextFile

/*
 * game.c  --  xrobots v1.0
 *
 * Most of the game logic is here.  
 */

#include "game.h"
#include "score.h"

/* some of these are global */
int human_x, human_y, last_human_x, last_human_y;

int robot_array[MAXX][MAXY],
    robot_array_bak[MAXX][MAXY];	
	/* These arrays are filled with the robots and the heaps 
	 * They do not include the player(human).
	 */
int score;
int num_robots;
int level;
int game_active;
int sonic_used;

/*----------------------------------------------------------------------*/
void
new_game()
{
  score = 0;
  level = 0;
  sonic_used = 1;
  game_active = 1;
  new_level();
  update_score(score);
}

/*----------------------------------------------------------------------*/
void
add_score(n)
  int n;
{
  score += 10 * n; /* ten points for each wasted robot. */
  num_robots -= n;
  if(n) update_score(score);
}

/*----------------------------------------------------------------------*/
void
new_level()
{
  int x,y,tmp;

  reset_sonic_button();
  if(!sonic_used) {
    score += 50; 	/* bonus for not using sonic screwdriver on last level*/
    update_score(score);
  }
  sonic_used = 0;
  level++;
  num_robots = tmp = level*5;

  for_each                            /* clean out both the robot arrays */
  {
    robot_array[x][y] = EMPTY;
    robot_array_bak[x][y] = EMPTY;
  }
  
  human_x = (int)random()%MAXX;                  /* assign human to new space */
  human_y = (int)random()%MAXY;
  robot_array[human_x][human_y] = ROBOT;

  while(tmp--)
  {
    x = (int)random()%MAXX;
    y = (int)random()%MAXY;
    if(robot_array[x][y] == ROBOT) tmp++;        /* space already occupied */
    robot_array[x][y] = ROBOT;
  }
  robot_array[human_x][human_y] = EMPTY;
  display_level();
  display_possible_moves();
}

/*----------------------------------------------------------------------*/
int
chase()
{
  /* chase() returns the number of robots that were wasted in each call. */
  /* after each call, be sure to check if all the robots are dead */
  int x,y;
  int newx,newy;
  int num_wasted = 0;

  for_each
  {
    robot_array_bak[x][y] = robot_array[x][y];
    if(robot_array[x][y] != HEAP)
      robot_array[x][y] = 0;
  }

  for_each
  {
    if(robot_array_bak[x][y] != ROBOT)
      continue;

    if(x>human_x)                           /* move toward the human */
      newx = x-1;
    else  
      if(x<human_x)
        newx = x+1;
      else
        newx = x;

    if(y>human_y) 
      newy = y-1;
    else  
      if(y<human_y)
        newy = y+1;
      else
        newy = y;

#   ifdef DEBUG
    printf("moving (%d,%d) to (%d,%d)\n",x,y,newx,newy);
#   endif

    /* check to see if a robot or heap was already in that spot */
    if(robot_array[newx][newy] == ROBOT)
    {
      robot_array[newx][newy] = HEAP;
      num_wasted += 2;
      continue;
    }
    if(robot_array[newx][newy] == HEAP)
    {
      num_wasted++;
      continue;
    }
    /* move robot to new location */
    robot_array[newx][newy] = ROBOT;
  }
  return(num_wasted);
}

/*----------------------------------------------------------------------*/
void
undo_chase()
{
  int x,y;
  for_each
    robot_array[x][y] = robot_array_bak[x][y];
}

/*----------------------------------------------------------------------*/
void
teleport()
{
  int num_wasted;

  if(!game_active) return;
  do
  {
    human_x = (int)random()%MAXX;        /* pick a spot not already occupied */
    human_y = (int)random()%MAXY;
  }
  while(robot_array[human_x][human_y] != EMPTY);

  show_teleport();

  num_wasted = chase();

  if(robot_array[human_x][human_y] != EMPTY)
  {
    /* death... */
    undo_chase();    
    /* it is a matter of preference to clear the screen when you die... */
    display_level();
    do_death();
    check_score(score);
    game_active = 0;
    return;
  }

  display_level();

  score += num_robots;       /* bonus for teleporting */
  score += num_wasted * 10;  /* score for any that collided */
  num_robots -= num_wasted;
  update_score(score);

  if(!num_robots) 
    new_level();
  else 
    display_possible_moves();

}

/*----------------------------------------------------------------------*/
void
sonic_screwdriver()
{
  /* remove the neighboring robots by calling chase(), then clear out */
  /* the human's position of robots */
  int num_wasted;

  if(!game_active) return;
  if(sonic_used) return;
  sonic_used = 1;
  show_sonic();
  num_wasted = chase();
  if(robot_array[human_x][human_y] == ROBOT)
    add_score(1);
  robot_array[human_x][human_y] = EMPTY;

  last_human_x = human_x;
  last_human_y = human_y;
  show_movement();

  add_score(num_wasted);
  if(!num_robots) 
    new_level();
  else 
    display_possible_moves();
}

/*----------------------------------------------------------------------*/
void
wait_for_em()
{
/* call chase until any robot is breathing right down the human's neck  */
  int num_wasted;

  if(!game_active) return;

  for(;;)
  {
    num_wasted = chase();
    if(!num_robots)
    {
      add_score(num_wasted);
      new_level();
      break;
    }
    if(robot_array[human_x][human_y] != EMPTY)
    {
      /* backout of latest chase() and break loop */
      undo_chase();
      break;
    }
    add_score(num_wasted);
    show_movement();
  }
  display_possible_moves();
}

/*----------------------------------------------------------------------*/


int
can_go(x,y)
  int x,y;
{
/* check if (x,y) is a legit move. */
  if( INYRANGE(y-1) ) {
    if( INXRANGE(x-1) )
      if(robot_array[x-1][y-1] == ROBOT)  return 0;
    if( INXRANGE(x) )
      if(robot_array[x][y-1] == ROBOT)  return 0;
    if( INXRANGE(x+1) )
      if(robot_array[x+1][y-1] == ROBOT)  return 0;
  }

  if( INYRANGE(y) ) {
    if( INXRANGE(x-1) )
      if(robot_array[x-1][y] == ROBOT)  return 0;
    if( INXRANGE(x) ) {
      if(robot_array[x][y] == ROBOT)  return 0;
      if(robot_array[x][y] == HEAP)   return 0;
      }
    if( INXRANGE(x+1) )
      if(robot_array[x+1][y] == ROBOT)  return 0;
  }

  if( INYRANGE(y+1) ) {
    if( INXRANGE(x-1) )
      if(robot_array[x-1][y+1] == ROBOT)  return 0;
    if( INXRANGE(x) )
      if(robot_array[x][y+1] == ROBOT)  return 0;
    if( INXRANGE(x+1) )
      if(robot_array[x+1][y+1] == ROBOT)  return 0;
  }

  if( !INXRANGE(x) )  return 0;
  if( !INYRANGE(y) )  return 0;

  return 1;
}

/*----------------------------------------------------------------------*/