home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / cellsim / v2_5 / src / cell.h < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-08  |  8.8 KB  |  329 lines

  1. /*****
  2.  *
  3.  *
  4.  * File: cell.h
  5.  *
  6.  * Cellular automata simulator
  7.  * Includes, definitions, and variable declarations/definitions.
  8.  *
  9.  *
  10.  *****/
  11.  
  12. /*
  13.  *
  14.  * Cellsim copyright 1989, 1990 by Chris Langton and Dave Hiebeler
  15.  * (cgl@lanl.gov, hiebeler@heretic.lanl.gov)
  16.  *
  17.  * This package may be freely distributed, as long as you don't:
  18.  * - remove this notice
  19.  * - try to make money by doing so
  20.  * - prevent others from copying it freely
  21.  * - distribute modified versions without clearly documenting your changes
  22.  *   and notifying us
  23.  *
  24.  * Please contact either of the authors listed above if you have questions
  25.  * or feel an exception to any of the above restrictions is in order.
  26.  *
  27.  * If you make changes to the code, or have suggestions for changes,
  28.  * let us know!  If we use your suggestion, you will receive full credit
  29.  * of course.
  30.  */
  31.  
  32. /*****
  33.  * Cellsim history:
  34.  *
  35.  * Cellsim was originally written on Apollo workstations by Chris Langton.
  36.  *
  37.  * Sun versions:
  38.  *
  39.  * - version 1.0
  40.  *   by C. Ferenbaugh and C. Langton
  41.  *   released 09/02/88
  42.  *
  43.  * - version 1.5
  44.  *   by Dave Hiebeler and C. Langton  May - June 1989
  45.  *   released 07/03/89
  46.  *
  47.  * - version 2.0
  48.  *   by Dave Hiebeler and C. Langton  July - August 1989
  49.  *   never officially released (unofficially released 09/08/89)
  50.  *
  51.  * - version 2.5
  52.  *   by Dave Hiebeler and C. Langton  September '89 - February 1990
  53.  *   released 02/26/90
  54.  *****/
  55.  
  56.  
  57.  
  58. /*****
  59.  *
  60.  * Includes
  61.  *
  62.  *****/
  63.   
  64. #include <math.h>
  65. #include <sys/types.h>
  66. #include <sys/stat.h>
  67. #include <sys/file.h>
  68. #include <sys/param.h>
  69. #include <sys/time.h>
  70.  
  71. #include "cell.def"
  72.  
  73.  
  74. /*****
  75.  *
  76.  * Defines
  77.  *
  78.  *****/
  79.  
  80. /* Used for the "Quick random" image generation */
  81. #define RSIZE (1 << 24)
  82. #define RMASK 0xFFFFFF            /* RSIZE - 1 */
  83.  
  84. /* Length of longest possible user-defined "sequence" */
  85. #define MAX_SEQUENCE_LENGTH 128        /* longest sequence possible */
  86. #define NUM_SEQUENCES    5        /* number of sequences */
  87.  
  88. #define CELLSIM_PORT 12700
  89.  
  90.  
  91. /*****
  92.  *
  93.  * Typedefs
  94.  *
  95.  *****/
  96.  
  97. typedef unsigned char ((*func_ptr)());
  98. typedef void ((*void_func_ptr)());
  99. typedef int ((*int_func_ptr)());
  100. typedef unsigned char byte;
  101.  
  102.  
  103. /*****
  104.  *
  105.  * Function declarations
  106.  *
  107.  *****/
  108.  
  109. void
  110.     which(),    /* look up full pathnames */
  111.     quit(), quit2();    /* quit with error msg */
  112.  
  113. double
  114.     compute_lambda();    /* compute current lambda value from LT */
  115.  
  116. void_func_ptr
  117.     default_segv_func, emergency_LT_save_ptr;
  118. void
  119.     emergency_LT_save();
  120.  
  121.  
  122. /*****
  123.  *
  124.  * External function declarations
  125.  *
  126.  *****/
  127.  
  128. extern char *getenv();        /* system stuff to get environment vars */
  129. extern and_color(), and_planemask();
  130. extern void def_CM_get_settings();
  131.  
  132.  
  133.  
  134. /*****
  135.  *
  136.  * External variables
  137.  *
  138.  *****/
  139.  
  140. /*** System stuff ***/
  141. extern char *sys_errlist[];    /* System error msgs */
  142. extern int errno;        /* Index into system error msgs */
  143.  
  144.  
  145. /*** General variable ***/
  146. extern State
  147.     *ca, *ia;            /* current and image arrays */
  148. extern int
  149.     parm1, parm2,    /* extra parameters for computed-fcns */
  150.     auto_image_change, auto_nhood_change,  /* whether or not to automatically
  151.                      * switch nhood and image-size when
  152.                      * reading in files */
  153.     use_2D_always;    /* load/save 2-D images, even when in 1-D mode */
  154.  
  155. /*** CM-related variables ***/
  156. extern int
  157.     use_Sun_disp,    /* whether or not to use Sun display when using CM */
  158.     use_FB,
  159.     CM_disp_interval,    /* for skipping frames when running w/ display */
  160.     CM_delay,    /* used to slow down CM */
  161.     CM_zoom, CM_panx, CM_pany;
  162.  
  163.  
  164. /*****
  165.  *
  166.  * Global variables
  167.  *
  168.  *****/
  169.  
  170. /*** Basic Parameters ***/
  171. short
  172.     S = 8,                /* # states per cell */
  173.     L,                    /* log S */
  174.     B = 64,                /* array size */
  175.     N,                    /* number of neighbors */
  176.     NHOOD = NH_VONN,            /* type of nhood in use */
  177.     DIM,                /* dimension (1 or 2) */
  178.     R = 1;                /* radius of neighborhood */
  179. int
  180.     table_symmetry = TRUE;        /* if true, store rotations of rules */
  181.  
  182.  
  183. /*** Misc. related variables ***/
  184. short
  185.     BM1,                /* B - 1      */
  186.     BPL1,                /* B + 1      */
  187.     TBM1,                /* 2 * B - 1  */
  188.     ABASE,                /* array base */
  189.     RCOUNT;                /* B          */
  190. int
  191.     BSQR,                /* B * B      */
  192.     BBUF,                /* BSQR + B   */
  193.     ASIZE,                /* with buffers */
  194.     ICBASE,                /* start of initial config. */
  195.     ICSIZE;                /* size of initial config.  */
  196.  
  197. int
  198.     TSIZE,                /* t-table size: S^N */
  199.     TMASK,                /* t index mask */
  200.     TMASK2;                /* second index mask */
  201. short
  202.     AMASK;                /* log S 1-bits */
  203.  
  204. /*** Socket vars ***/
  205. int
  206.     SOCKET_INUSE = FALSE,        /* boolean */
  207.     portnum;                /* port # of receiving socket */
  208. char
  209.     hostname[BUFLEN] = "";        /* host of receiving machine */
  210.  
  211. /*** Pointers to nhood-specific routines ***/
  212. int     (*get_address) (), (*to_nhood) (), (*to_index) (), (*rotate) (),
  213.         (*auto_step) (), (*auto_screen) (), (*learn) ();
  214.  
  215. /*** State-variables, etc ***/
  216. short
  217.     setting_sequence,    /* 0 if not setting sequence, otherwise index into
  218.              * sequence table */
  219.     sequence_length[NUM_SEQUENCES];    /* length of sequence */
  220. int
  221.     drawing = FALSE;    /* whether or not we're currently drawing something */
  222.  
  223.  
  224. /*** Misc configuration vars ***/
  225. byte
  226.     save_images_compressed = 1,    /* save images in compressed form? */
  227.     save_LT_compressed = 1;
  228. short
  229.     stop_on_mode,    /* when to automatically stop running, if ever */
  230.     closeup,        /* in closeup mode? */
  231.     shift_dist=8;    /* when user shifts image */
  232. int
  233.     blackwhite = FALSE,    /* boolean indicating display type */
  234.     mag,        /* current magnification level */
  235.     side,        /* how many rows/cols are currently displayed */
  236.     xstart, ystart,    /* start coords in image, for displaying */
  237.     MAXSIZE=512,    /* maximum allowed image size */
  238.     save_in_image_dir=0,  /* save images in image-dir (instead of cwd) */
  239.     save_in_LT_dir=0,    /* similarly for lookup tables */
  240.     save_in_cmap_dir=0,   /* and cmaps */
  241.     function,        /* using computed-function? */
  242.     use_CM;        /* whether or not we're using (attached to) the CM */
  243. char
  244.     Image_dir[256],    /* directory to load/save images in */
  245.     Table_dir[256],    /* similarly for LT's */
  246.     Cmap_dir[256],    /* and cmaps */
  247.     Fcn_dir[256],    /* and ".o" files (loading only, since can't save */
  248.     current_working_dir[256];    /* current working directory */
  249.  
  250. /*** Misc global vars ***/
  251. State
  252.     *ta;        /* lookup table */
  253. char
  254.     buf1[BUFLEN], buf2[BUFLEN];
  255. int
  256.     stime = 0,        /* system time */
  257.     *statecount;    /* state counter */
  258. FILE
  259.     *fp;        /* used to read/write files */
  260.  
  261. /*** Saved defaults for various things ***/
  262. int
  263.     saved_mag_factor,    /* to remember magnification during close-up */
  264.     saved_skip = INTUNDEF,    /* interval when doing a "skip" */
  265.     saved_display_interval = INTUNDEF,    /* display interval with "skip run" */
  266.     saved_num_steps = INTUNDEF,    /* bound with "bound" or "skip/bound" runs */
  267.     saved_side_length = 0, /* size of resized images when loading/saving */
  268.     saved_rule_number = 90,    /* wolfram rule-numbers */
  269.     saved_buffer = 1,    /* default buffer to use */
  270.     saved_plane_mask = 0,    /* default plane mask */
  271.     saved_density = 100,    /* density for "general random" proc */
  272.     saved_ox=0, saved_oy=0,    /* origin for general random proc */
  273.     saved_sx, saved_sy,        /* rectangle-size for general random proc */
  274.     saved_min_val, saved_max_val,    /* ...min-max vals */
  275.     saved_lambda_percent = INTUNDEF,    /* default lambda value */
  276.     saved_lambda_increment = 0,        /* ...and lambda increment */
  277.     saved_lambda_inc_set = 0,    /* whether or not lambda_inc was ever set */
  278.     saved_rho_percent[16],    /* array of rho values */
  279.     saved_xinc = INTUNDEF,     /* parms for drawing shaded circles */
  280.     saved_yinc = INTUNDEF,
  281.     saved_synchro_interval = 1000;    /* synchronization interval for CM */
  282. char
  283.     saved_image_file[BUFLEN],    /* filename for images */
  284.     saved_ienter_str[BUFLEN],    /* for entering initial configs in array */
  285.     saved_rule_file[BUFLEN];    /* rule (LT or obj-code) filename */
  286.  
  287. /*** CM-related variables ***/
  288. char
  289.     CM_hostname[80];    /* hostname of CM front-end to attach to */
  290. unsigned short
  291.     CM_port;        /* port on CM FE to attach to */
  292. long int
  293.     CM_host;        /* CM FE address */
  294.  
  295.  
  296. /*** Computed-function function-pointers (nhood routines need them) ***/
  297. func_ptr
  298.     update_function = NULL;
  299. void_func_ptr
  300.     before_function = NULL, after_function = NULL;
  301.  
  302. /*****
  303.  *
  304.  * Local variables
  305.  *
  306.  *****/
  307.  
  308. static char
  309.     saved_x_offset[BUFLEN], saved_y_offset[BUFLEN],    /* offsets for
  310.                              * resizing images */
  311.     saved_nhood[BUFLEN],    /* nhood for altering LT */
  312.     argv0[256];        /* pathname of Cellsim, used for dynamic linking */
  313.  
  314. static int
  315.     local_run,    /* used to run things locally when attached to CM */
  316.     non_local = 0,    /* occasionally used to flag a routine which would
  317.              * normally do something locally, to run on CM */
  318.     set_just_pressed;    /* whether or not "set" button was just pressed */
  319.  
  320. static void_func_ptr
  321.     initialization_function = NULL;
  322.  
  323. static int_func_ptr
  324.     sequence_array[NUM_SEQUENCES][MAX_SEQUENCE_LENGTH];
  325.  
  326. static struct stat
  327.     statbuf;    /* used by various routines to stat files */
  328.  
  329.