/* * game.c -- xrobots * * Copyright 1989 Brian Warkentine * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation, and that the author's name not be used in advertising or * publicity pertaining to distribution of the software without specific, * written prior permission. The author makes no representations about the * suitability of this software for any purpose. It is provided "as is" * without express or implied warranty. * * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE * AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * The author's current employer has no connection with this software, as it * was written before being employed at Sun. The following address is for * contacting the author only and does not imply any responsibility on the * behalf of Sun Microsystems. * * Contact the author at: * Brian Warkentine (brianw@Sun.COM) * Sun Microsystems * 2550 Garcia Avenue * Mountain View, Ca 94043-1100 * * HP-UX :- following two defines */ #define srandom srand #define random rand #include #include "xrobots.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, num_robots, level, game_active, sonic_used; static void autochase(); /*----------------------------------------------------------------------*/ 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(xhuman_y) newy = y-1; else if(y