📄 action.c
字号:
#ifndef lintstatic char *sccsid = "@(#)action.c 4.2 (ULTRIX) 9/1/88";#endif lint/************************************************************************ * * * Copyright (c) 1985 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************/* * action.c * * Name: starship * Purpose: General data structure manipulation routines are in * this file. They initialize the shared data structures & * update them. * Environment: Ultrix-32, optionally with shared memory * Compile: see Makefile * Date: April 18, 1985 * Author: Alan Delorey * Remarks: These are the voyages of the independent star ships.Whose lifetime mission: To explore strange new galaxies, To seek out and destroy other star ships, To boldly go where no other star ship dares!*//* * Modification history * * 4-18-85 * Derived from former file "two.c" which had ship action & screen * update routines together. Ship action & screen update are now * split into action.c & screen.c */#include "star.h"#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <curses.h> /* also includes <sgtty.h> and <stdio.h> */#include <sys/time.h>/* external variables from main.c */extern struct univ *uptr; /* ptr to shared memory segment */#ifdef SHMEMextern int shmid; /* used to remove shared seg */#endif SHMEM/* Define new access paths to variables that are now in one struct, * to facilitate shared memory */#define universe uptr->quadrants#define num_users uptr->Num_users#define wait uptr->Wait#define ships uptr->Ships#define drones uptr->Dronesextern char esc;extern char bell; /* bell char */extern boolean userabort; /* true if abort-type exit */extern int errno;/* local variables */struct timeval times; /* time structures from sys/time.h */struct timezone timez;int seed; /* random number seed *//*****************************************************************************//* *//* START INTERNAL PROCEDURES *//* *//*****************************************************************************/get_rand() /* Get a random number according to the host system's random number * facility. Normalize it to be between 0 and 9 inclusive. */ { return (random() % 10); };star_random() /* Randomly set up the stars in the galaxy. There will be NUMSTARS stars in the galaxy. */ { register int i,j; /* loop indicies */ long int holdran [4]; /* to hold random integers */ for (i = 0; i < NUMSTARS; i ++) { for (j = 0; j < 4; j ++) /* 4 random #'s per star */ holdran[j] = get_rand(); /* assign stars to global universe data structure */ universe [holdran[0]][holdran[1]].sectors [holdran[2]][holdran[3]].image = STAR; universe [holdran[0]][holdran[1]].nstars++; }; }; /* proc star random */base_random() /* Randomly set up the star bases in the galaxy. There will be NUMBASES of them. */ { register int i,j; /* loop indices */ long int holdran[4]; /* to hold 4 random integers per base */ for (i = 0; i < NUMBASES; i ++) { for (j = 0; j < 4; j ++) /* 4 ran #s per base */ { holdran[j] = get_rand(); }; /* Assign bases to global universe */ universe [holdran[0]][holdran[1]].sectors [holdran[2]][holdran[3]].image = BASE; universe [holdran[0]][holdran[1]].nbases++; }; }; /* proc base random */ion_random() /* Randomly disperse a few ion clouds in the galaxy. * There will be NUMCLOUDS of them. */ { register i,j; /* loop indicies */ register rt, ct; /* row temp & col temp */ long int hr[4]; /* to hold 4 random ints per ion */ /* do NUMCLOUDS; 4 random #'s to start each ion cloud */ for (i = 0; i < NUMCLOUDS; i++) { for (j = 0; j < 4; j++) hr[j] = get_rand(); universe [hr[0]][hr[1]].sectors[hr[2]][hr[3]].image = ION; /* 2. same row, col + 1 */ ct = hr[3] + 1; if (ct > NUMSECTORS - 1) ct = hr[3] - 1; universe [hr[0]][hr[1]].sectors[hr[2]][ct].image = ION; /* 3. row + 1, col - 1 */ rt = hr[2] + 1; if (rt > NUMSECTORS - 1) rt = hr[2] - 1; ct = hr[3] - 1; if (ct < 0) ct = NUMSECTORS - 1; universe [hr[0]][hr[1]].sectors[rt][ct].image = ION; /* 4. row + 1, same col */ universe [hr[0]][hr[1]].sectors[rt][hr[3]].image = ION; /* 5. row + 1, col + 1 */ ct = hr[3] + 1; if (ct > NUMSECTORS - 1) ct = hr[3] - 1; universe [hr[0]][hr[1]].sectors[rt][ct].image = ION; /* 6. row + 2, same col */ rt = hr[2] + 2; if (rt > NUMSECTORS - 1) rt = hr[2] - 1; universe [hr[0]][hr[1]].sectors[rt][hr[3]].image = ION; } };init_d_s() /* This procedure initializes the global, shared data structures. It calls star_random & base_random. */ { register int i,j,k,l; /* loop indicies */ char ch; /* loop index */ esc = (char)ASCII_ESC; /* initialize random number generator */ gettimeofday (×,&timez); seed = times.tv_sec; srandom(seed); /* initialize "universe" - by looping thru all quadrants & sectors */ for (i = 0; i < NUMQUADS; i ++) /* loop thru all quadrants */ for (j = 0; j < NUMQUADS; j ++) { universe [i][j].nstars = 0; universe [i][j].nships = 0; universe [i][j].nbases = 0; for (k = 0; k < NUMSECTORS; k ++) /* loop thru all sectors */ for (l = 0; l < NUMSECTORS; l ++) universe [i][j].sectors [k][l].image = ' '; } /* blank out all ship letters */ for (ch = 'A'; ch <= 'Z'; ch ++) ships[ch-A].ship_ch = ' '; /* call star_random (to set up stars), * base random (to set up star bases), * ion_random (to set up ion clouds). */ star_random(); base_random(); ion_random();# ifdef DRONE for (i = 0; i < NUMDRONES; i++) { drones[i].d_char = ' '; drones[i].d_after = ' '; };# endif DRONE /* THIS LINE MUST BE LAST STMT IN THIS PROCEDURE !! */ wait = false; }; /* proc init_d_s */init_a_ship (sch,s_name) char sch; char *s_name; /* Initializes a new ship that enters the game. It gives a random quadrant and sector position to the ship. */ { register struct ship *pships; /* ptr to ships */ register struct torpedo_record *ptorps; /* ptr to ships */ register int i,j; /* loop index */ boolean open; /* set false if sector occupied */ long int holdran[4]; /* holds ran #s */ char ch; /* loop index */ /* initialize random number generator */ gettimeofday (×,&timez); seed = times.tv_sec; srandom(seed); do { open = true; for (j = 0; j < 4; j ++) holdran[j] = (random() % 10); if (universe [holdran[0]][holdran[1]].sectors [holdran[2]][holdran[3]].image != ' ') open = false; } while (!open); /* set up the ship info */ pships = ships + (sch - A); pships->ship_ch = sch; strcpy(pships->name, s_name); pships->energy = ENERGY_MAX; pships->shields = 0; pships->shield_time = 0; pships->torpedos = TORP_MAX; pships->torp_time = 0; pships->phasers = 0; pships->warp_drive = 0; pships->warp_set = 0.0; pships->warp_speed = 0.0; pships->q_row = holdran[0]; pships->q_col = holdran[1]; pships->s_row = holdran[2]; pships->s_col = holdran[3]; pships->bearing = 6; pships->impulse = 0; pships->sub_light = 0; pships->life_support = GREEN; pships->life_supp_time = 0; pships->sr_scan = 0; pships->lr_scan = 0; pships->radio = 0; pships->sensors = 0; pships->cloaking = 0; pships->dock = false; pships->docked = false; pships->ship_invis = false; pships->cloak = false; pships->msg_buf[0] = '\0'; pships->msg_timer = 0; pships->map_timer = 0; pships->kills = 0; pships->glow = 0; /* init torp info */ for (i = 0; i < TORP_MAX; i ++) { ptorps = pships->torp_info + i; ptorps->torp_dir = -1; ptorps->torp_dist = 0; }; pships->torp_active = false; /* set bases to "none seen" */ for (i = 0; i < NUMBASES; i++) { pships->bases[i][0] = -1; pships->bases[i][1] = -1; } pships->allbases = false; pships->locater = -1; pships->locatec = -1; pships->radiorow = -1; pships->radiocol = -1; pships->starcounter = 0; /* set approp fields to true to trigger screen fields to be updated */ pships->dis_energy = false; pships->dis_shields = false; pships->dis_torpedos = false; pships->dis_warp_set = false; pships->dis_warp_speed = false; pships->dis_quad = true; pships->dis_sector = true; pships->dis_bearing = false; pships->dis_msg = false; pships->dis_map = false; pships->dis_dir = false; /* Initialize pointers to strings for faster access (See K & R p 110.) * These strings are used to save long range scan info. * And initialize the "old" long range scan info. */ for (i=0; i < LRSCANSECTS; i++) for (j=0; j < LRSCANSECTS; j++) { pships->old_lr[i][j] = pships->old_lr1 [i][j]; strcpy (pships->old_lr[i][j], "000"); }#ifdef OLDSR /* Initialize "old" short range scan info */ for (i=0; i < SRSCANROWS; i++) for (j=0; j < SRSCANCOLS; j++) pships->old_sr [i][j] = ' ';#endif pships->ioff = SRSCANR; pships->joff1 = SRSCANR; pships->joff2 = SRSCANR; /* make all ships re-display fleet display */ for (ch = 'A'; ch <= 'Z'; ch ++) ships[ch-A].dis_fleet = true; universe[holdran[0]][holdran[1]].sectors [holdran[2]][holdran[3]].image = sch; universe[holdran[0]][holdran[1]].nships++; }; /* proc init_a_ship */exit_a_ship (sch, drone) char sch; /* ship to exit */ boolean drone; /* true if its a drone exiting */ /* Exits a ship from the game. Clear it from ships & universe. * If its a drone, don't clear screen. */ { register int i; /* loop index */ register struct ship *pships; /* ship pointer */ register struct torpedo_record *ptorps; /* ship pointer */ register struct drone *pdrones; /* ship pointer */ char ch; /* loop index */ pships = ships + (sch - A); if (!drone) { if (userabort) { clear_screen (); /* everyone gets a score for a QUITER! */ for (ch = 'A'; ch <= 'Z'; ch++) ships[ch - A].kills++; } else if (pships->life_support == RED) { move_to(COMMANDR+1,COMMANDC); string_out("Life support exhausted - crew died"); move_to(COMMANDR+2,COMMANDC); clear_to_eol(); } else if (pships->energy <= 0) { move_to(COMMANDR+1,COMMANDC); string_out("Out of energy - you lost "); move_to(COMMANDR+2,COMMANDC); clear_to_eol(); } } refresh_scr(); universe [pships->q_row][pships->q_col]. sectors [pships->s_row][pships->s_col].image = '-'; universe [pships->q_row][pships->q_col].nships--; /* clean up torpedos */ /* as a side effect of a ship dying-all of its torps magically disappear! */ for (i = 0; i < TORP_MAX; i ++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -