home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / games / volume13 / xsokoban2 / part02 / readscreen.c < prev    next >
C/C++ Source or Header  |  1992-01-12  |  1KB  |  63 lines

  1. #include <stdio.h>
  2. #include "sokoban.h"
  3.  
  4. extern char *malloc();
  5. extern FILE *fopen();
  6.  
  7. extern short level, packets, savepack, rows, cols;
  8. extern char  map[MAXROW+1][MAXCOL+1];
  9. extern POS   ppos;
  10.  
  11. short readscreen() {
  12.  
  13.    FILE *screen;
  14.    char *fnam;
  15.    int y, x;
  16.    short j, c, ret = 0;
  17.  
  18.    for(y = 0; y < MAXROW; y++)
  19.      for(x = 0; x < MAXCOL; x++)
  20.        map[y][x] = ground;
  21.  
  22.    fnam = malloc( strlen( SCREENPATH) + 12);
  23.    sprintf( fnam, "%s/screen.%d", SCREENPATH, level);
  24.    if( (screen = fopen( fnam, "r")) == NULL) 
  25.       ret = E_FOPENSCREEN;
  26.    else {
  27.       packets = savepack = rows = j = cols  = 0;
  28.       ppos.x = -1; ppos.y = -1;
  29.       while( (ret == 0) && ((c = getc( screen)) != EOF)) {
  30.          if( c == '\n') {
  31.         map[rows++][j] = '\0';
  32.         if( rows > MAXROW) 
  33.            ret = E_TOMUCHROWS;
  34.         else {
  35.            if( j > cols) cols = j;
  36.            j = 0;
  37.         }
  38.      }
  39.      else if( (c == player) || (c == playerstore)) {
  40.         if( ppos.x != -1) 
  41.            ret = E_PLAYPOS1;
  42.         else { 
  43.            ppos.x = rows; ppos.y = j;
  44.            map[rows][j++] = c;
  45.            if( j > MAXCOL) ret = E_TOMUCHCOLS;
  46.         }
  47.      }
  48.      else if( (c == save) || (c == packet) ||
  49.           (c == wall) || (c == store) ||
  50.           (c == ground)) {
  51.         if( c == save)   { savepack++; packets++; }
  52.         if( c == packet) packets++;
  53.         map[rows][j++] = c;
  54.         if( j > MAXCOL) ret = E_TOMUCHCOLS;
  55.      }
  56.      else ret = E_ILLCHAR;
  57.       }
  58.       fclose( screen);
  59.       if( (ret == 0) && (ppos.x == -1)) ret = E_PLAYPOS2;
  60.    }
  61.    return( ret);
  62. }
  63.