home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / games / gkit.zip / G0KIT.C < prev    next >
Text File  |  1988-05-05  |  16KB  |  566 lines

  1. /* Designed and written by David M. Larson, BBS: 916-753-8788 */
  2. /* Copyright (c) 1988,   Dynasoft, P.O. Box 915, Davis, CA 95617 */
  3. /* Refer to GRULES.DOC and GKIT.DOC for details */
  4. #define DEF_GLOBALS 1
  5. #include "GATEWAYS.H"
  6. #include "fcntl.h" /* should only be needed by overgame */
  7. #include "errno.h"
  8. #if IBM_TURBO || IBM_MICROSOFT
  9. #include "conio.h"
  10. #endif
  11. #if USE_longjmp
  12. #include "setjmp.h"
  13. #endif
  14. #if LARGE_DATA
  15. int reserved[30000]; /* reserved data space for other games */
  16. #else
  17. int reserved[8000]; /* reserved data space for other games */
  18. #endif
  19. static int malloc_cnt=0;
  20. static int problem_flag=0;
  21. static long og_file_bitmap=0;
  22. static int test_score;
  23. #if PROTO_ON
  24. void error_check(void);
  25. #else
  26. void error_check();
  27. #endif
  28. #if USE_longjmp
  29. jmp_buf save_env;
  30. #endif
  31. /*=====================================================================*/
  32. main() /* driver function for your game */
  33. {
  34. int i=0;
  35. #if REDUCE_STACK
  36. int reserved[2048]; /* stack space reserved for overgame */
  37. #else
  38. int reserved[512];  /* stack space reserved for overgame */
  39. #endif
  40.   problem_flag=2; /* temporarily suppress carrier loss test */
  41.   outstr("\nWelcome to the Gateways development environment!");
  42.  
  43. #if AMIGA_MANX
  44.    g0_setup_amiga();
  45. #endif
  46.  
  47. #if RANDOM_START
  48.   outstr("\nPress any key to start");
  49.   while(keystat()==-2)
  50.      {
  51.      irandom(1);
  52.      i =(++i & 8191);
  53.      }
  54.   keyin(); /* discard the keystroke */
  55. # if IBM_MICROSOFT || IBM_TURBO
  56.     srand(i); /* seed the pseudo random # generator */
  57. # endif
  58. #endif
  59. #if VALIDATE
  60.      outstr("\nFirst I would like to validate a few g0kit.c functions");
  61.      i=og_create("VALIDATE");
  62.      if(i<0) 
  63.          { 
  64.          extern int errno;
  65.          outstr("\nCould not create VALIDATE file"); 
  66.          sprintf(gss,"  errno=%d ",errno); outstr(gss);
  67.          exit(1); 
  68.          }
  69.    writeline(i,"Test");
  70.    writeline(i," This is exciting! ");
  71.    og_close(i);
  72.    i=og_open("VALIDATE",0);
  73.      if(i<0) { outstr("\nCould not open VALIDATE file"); exit(1); }
  74.    readline(i,gss,sizeof(gss),0);
  75.    readline(i,gss,sizeof(gss),0);
  76.    og_close(i);
  77.    trim(gss); upcfy(gss);
  78.    if(strcmp(gss,"THIS IS EXCITING!")!=0)
  79.      {
  80.      outstr("\nThere is a problem with the file functions");
  81. #if AMIGA_MANX
  82.        g0_close_amiga();
  83. #endif
  84.      exit(1); /* only I can use exit() */
  85.      }
  86.    gp0=og_malloc(8000);
  87.    gp1=og_malloc(8000);
  88.    if(gp1==0 || gp0==0 || gp0==gp1)
  89.      {
  90.      outstr("\nThere is a problem with og_malloc()");
  91.      if(gp1==0) outstr("\nIt thinks there isn't enough memory");
  92. #if AMIGA_MANX
  93.        g0_close_amiga();
  94. #endif
  95.      exit(1); /* only I can use exit() */
  96.      }
  97.     og_free(gp0); og_free(gp1);
  98.    outstr("\ndouble random #s between 0.0 to 1.0:\n");
  99.    for(i=0;i<5;i++) { sprintf(gss,"%g  ",drandom()); outstr(gss); }
  100.    outstr("\ninteger random #s between 0 and 199:\n");
  101.    for(i=0;i<5;i++) { sprintf(gss,"%d  ",irandom(200)); outstr(gss); }
  102.    outstr("\nDo these numbers look reasonable? ");
  103. /* IF YOU GET STUCK HERE, You need to fix keystat() and keyin() */
  104.    while(keystat()==1) keyin(); /* flush keyboard buffer */
  105.    while(keystat()==-2) ; /* wait for keystroke */
  106.    /* notice keystat() and keyin() is being validated */
  107.    i=keyin();
  108.    if(i!='Y' && i!='y')
  109.      {
  110.      outstr("\nSince you said the random #s are not ok, I'll quit");
  111. #if AMIGA_MANX
  112.        g0_close_amiga();
  113. #endif
  114.      exit(1); /* only I can use exit() */
  115.      }
  116.    error_check();
  117. #endif
  118.  
  119. again:
  120.  
  121. problem_flag=2;
  122. gfd0=gfd1=gfd2=-1;
  123. gp0=gp1=gp2=gp3=gp4=gp5=gp6=gp7=gp8=gp9=(void *) 0;
  124. outstr("\nBuckle your seat belts!\n");
  125. problem_flag=0;
  126. #if USE_longjmp
  127. if(setjmp(save_env)==0) /* notice the then part of this is GAME() */
  128. #endif
  129.  /* your game function entry point, random player# passed */
  130.    GAME(irandom(MAX_PLAYERS),MAX_PLAYERS,irandom(NUMGATES),NUMGATES);
  131. problem_flag|=2;
  132. if((problem_flag & 1)==0)
  133.      {
  134.      sprintf(gss,"\nYour score was %d",test_score);
  135.      outstr(gss);
  136.      }
  137. error_check();
  138. outstr("\nDo you want to try again? ");
  139. for(;;) /* endless loop waiting for the right characters*/
  140.      {
  141.         i=keyin();
  142.         i=upchar(i);
  143.         if(i=='Y')
  144.           {
  145.           outstr("Y\n");
  146.           goto again; /* I could have used a while() loop */
  147.           }
  148.         if(i=='N')
  149.           {
  150.           outstr("N\n");
  151.           break;
  152.           }
  153.      }
  154. outstr("\nExiting Gateways\n");
  155. #if AMIGA_MANX
  156.   g0_close_amiga();
  157. #endif
  158. for(i=0;i<5;i++) reserved[i]=0; /* trick the lint type error checkers */
  159. return 0;
  160. }
  161. /*=====================================================================*/
  162. void error_check()
  163.    {
  164.    int flag;
  165.    flag=0;
  166.    if(og_file_bitmap!=0)
  167.      {
  168.      flag=1;
  169.      sprintf(gss,"\n***ERROR*** files still open, use og_close()! %d",og_file_bitmap);
  170.      outstr(gss);
  171.      }
  172.    if(malloc_cnt!=0)
  173.       {
  174.       flag=1;
  175.       sprintf(gss,"\n***ERROR*** all og_malloc() 'ed memory is not og_free()'d %d",malloc_cnt);
  176.       outstr(gss);
  177.       }
  178.    if(flag) outstr("\n");
  179.    }
  180. /*=====================================================================*/
  181. #ifndef O_BINARY
  182. #define O_BINARY 0
  183. #endif
  184. int og_open(filename,mode)
  185. /* use this function to open ALL files  (unbuffered) */
  186. char *filename;
  187. int mode; /* mode can be 1 for O_RDWR or 0 for O_RDONLY */
  188. {
  189. /* it will return -3 if you must exit your game
  190.                  (due to filelock or error or carrier loss) */
  191. /* it will return -1 if file did not exist */
  192. extern int errno;
  193. int fd;
  194. if(problem_flag==0 && SIM_CARRIER && irandom(35)==12) /* do not remove! */
  195.    {
  196.    problem_flag=2;
  197.    outstr("\nRANDOM TEST OF og_open() carrier loss or error \n");
  198.    problem_flag=1;
  199.    og_carrier();
  200.    }
  201. if(mode==1) fd=open(filename,O_RDWR+O_BINARY);
  202. else if(mode==0) fd=open(filename,O_RDONLY+O_BINARY);
  203. else { outstr("\nInvalid mode passed to og_open()\n"); return -3; }
  204. if(fd==-1 && errno!=ENOENT) return -3;
  205. og_file_bitmap |= (1L<<fd);
  206. return fd;
  207. }
  208. /*=====================================================================*/
  209. int og_create(filename) char *filename;
  210. { /* use this function to create ALL files (unbuffered) */
  211. /* it will return -3 if you must exit your game
  212.                  (due to filelock or error or carrier loss) */
  213. int fd;
  214. if(problem_flag==0 && SIM_CARRIER && irandom(15)==3) /* do not remove! */
  215.    {
  216.    problem_flag=2;
  217.    outstr("\nRANDOM TEST OF og_creat() carrier loss or error \n");
  218.    problem_flag=1;
  219.    og_carrier();
  220.    }
  221. fd=open(filename,O_RDWR|O_CREAT|O_TRUNC|O_BINARY);
  222. if(fd==-1) return -3;
  223. else og_file_bitmap |= (1L<<fd);
  224. return fd;
  225. }
  226. /*=====================================================================*/
  227. int og_close(fd) int fd; /* MAKE SURE YOU CLOSE ALL FILES */
  228. {
  229. int i;
  230. /* it will return -3 if you must exit your game due to error */
  231. i=close(fd);
  232. if(i==-1) return -3;
  233. else og_file_bitmap &= ~(1L<<fd);
  234. return 0;
  235. }
  236. /*=====================================================================*/
  237. int readline(fd,buffer,size,stopat) int fd,size; char *buffer,stopat;
  238. {
  239. char *ptr,c;
  240. int i,j,k;
  241. size--; /* to account for the null byte */
  242. i=uread(fd,buffer,size);
  243. if(i<1 || buffer[0]==26) { buffer[0]=0; return -1; }
  244. for(ptr=buffer,j=0;
  245.      j<i && *ptr!=stopat && *ptr!=13 && *ptr!=10 && *ptr!=26;j++) ptr++;
  246. k=j;
  247. c=*ptr;
  248. *ptr++=0;
  249. if(c!=26) j++;
  250. if(j<i && *ptr==10 && c==13) { j++; ptr++; }
  251. if(j<i)
  252.      {
  253.      lseek(fd,((long)(j-i)),1); /* back up to the right spot */
  254.      }
  255. else if(i==size)
  256.      {
  257.          c=0;
  258.         while(uread(fd,&c,1)>0 && c!=stopat && c!=13 && c!=10) ;
  259.    if(c==13)
  260.               {
  261.               uread(fd,&c,1);
  262.               if(c!=10) lseek(fd,-1L,1);
  263.               }
  264.    }
  265. return k;
  266. }
  267. /*=====================================================================*/
  268. int writeline(fd,buffer) int fd; char *buffer;
  269. {
  270. int i;
  271. i=writestr(fd,buffer);
  272. writestr(fd,"\r\n");
  273. return i;
  274. }
  275. /*=====================================================================*/
  276. int writestr(fd,buffer) int fd; char *buffer;
  277. {
  278. char *ptr;
  279. int i;
  280. for(i=0,ptr=buffer;*ptr;ptr++) i++;
  281. if(i>0) write(fd,buffer,i);
  282. return i;
  283. }
  284. /*=====================================================================*/
  285. #if PROTO_ON
  286. void (*fn_onexit)(void)=0;
  287. #else
  288. void (*fn_onexit)()=0;
  289. #endif
  290. /*=====================================================================*/
  291. #if PROTO_ON
  292. void og_onexit(fn) void (*fn)(void);
  293. #else
  294. void og_onexit(fn) void (*fn)();
  295. #endif
  296. /*
  297. fn is function in your game to execute if carrier is lost such as to
  298. close files
  299. */
  300. {
  301. fn_onexit=fn;
  302. }
  303. /*=====================================================================*/
  304. void og_carrier()
  305. {
  306. if(!SIM_CARRIER || (problem_flag & 2)) return;
  307. if(problem_flag || irandom(C_LOSS_P+100)==52)
  308.    {
  309.    problem_flag |=2;
  310.    if((problem_flag & 1)==0)
  311.       outstr("\nRANDOM TEST OF carrier loss");
  312.    problem_flag=3;
  313.    if(fn_onexit!=0)
  314.       {
  315.       outstr("\nAttempting to execute your exit function");
  316.       (*fn_onexit)();
  317.       }
  318. #if USE_longjmp
  319.    longjmp(save_env,-1); /* jump back to main() */
  320. #else
  321.    error_check();
  322.    outstr("\nThats all folks!\n");
  323.    exit(0);
  324. #endif
  325.    }
  326. }
  327. /*=====================================================================*/
  328. int keyin()
  329. /* returns character pressed */
  330. { /* get character, no echo */
  331. while(keystat()==-2) ; /* wait for character or carrier loss */
  332. /*********** You need an operating system call to get a character without */
  333. /*********** echoing it here.. */
  334. /*********** it should be compatible with the one used in keystat() */
  335. #if IBM_AZTEC
  336. return bdos(7,0); /* aztec c, use dos function so printscreen doesnt crash */
  337. #endif
  338. #if CPM_AZTEC
  339. return bdos(6,255);
  340. #endif
  341. #if IBM_MICROSOFT || IBM_TURBO
  342. return getch();
  343. #endif
  344. #if AMIGA_MANX
  345. return getchar();
  346. #endif
  347. }
  348. /*=====================================================================*/
  349. int keystat()
  350. /* returns -2 if no char waiting, 1 if char waiting */
  351. {
  352. int retval;
  353. retval=0;
  354. og_carrier();
  355. /*************  you need operating sytem function to check for keyboard */
  356. /*************  activity here (without waiting for a key) */
  357. #if IBM_AZTEC || CPM_AZTEC
  358. /* yep, some IBM functions are clones of CPM */
  359. if(bdos(11,0)==0) retval=-2; else retval=1;
  360. #endif
  361. #if IBM_MICROSOFT || IBM_TURBO
  362. if(kbhit()) retval=1; else retval=-2;
  363. #endif
  364. /***** IF THE OPERATING SYSTEM FUNCTION WASN'T AVAILABLE, FAKE IT ****/
  365. /* return no character waiting most of the time to simulate reality */
  366. if(retval)
  367.   {
  368.   if(irandom(99)==55) retval=1; else retval=-2;
  369.   }
  370. return retval;
  371. }
  372. /*=====================================================================*/
  373. void keyout(c) char c;
  374. {
  375. og_carrier(); /* check for carrier loss */
  376. putchar(c);
  377. }
  378. /*=====================================================================*/
  379. void trim(ss) char *ss;
  380. {
  381. char *ptr1,*ptr2,*ptr3;
  382. ptr1=ptr2=ptr3=ss;
  383. if(*ptr1<=' ')
  384.     {
  385.     while(*ptr1 && *ptr1<=' ') ptr1++;
  386.     while(*ptr1)
  387.      {
  388.      if((*ptr2++ = *ptr1++)>' ') ptr3=ptr2;
  389.         }
  390.      }
  391. else while(*ptr1)
  392.      {
  393.         if(*ptr1++>' ') ptr3=ptr1;
  394.         }
  395. *ptr3=0;
  396. }
  397. /*=====================================================================*/
  398. char upchar(c) char c; /* convert a character to uppercase */
  399. {
  400. if(c>='a' && c<='z') c-=32;
  401. return c;
  402. }
  403. /*=====================================================================*/
  404. void upcfy(ss) char *ss; /* convert a string to upper case */
  405. {
  406. while(*ss)
  407.      {
  408.      if(*ss>='a' && *ss<='z') *ss -=32;
  409.      ss++;
  410.      }
  411. }
  412. /*=====================================================================*/
  413. int og_mode(i) int i; /* set modes of operation */
  414. { /* use not defined yet */
  415. int dummy;
  416. for(dummy=i;dummy!=i;) ;
  417. return 0;
  418. }
  419. /*=====================================================================*/
  420. int og_gateways(gate) int gate; /*  returns -3 must exit -2 continue */
  421. {
  422. int c,dummy;
  423. for(dummy=gate;dummy!=gate;) ;
  424. outstr("\nPress (1)return to game (2)exit to overgame  ?");
  425. for(;;)
  426.      {
  427.      c=keyin();
  428.      switch(c)
  429.           {
  430.           case '1': outstr("return to game\n"); return -2;
  431.           case -3: return -3; /* carrier loss */
  432.           case '2': outstr("entering gateway\n"); return -3;
  433.           }
  434.      }
  435. }
  436. /*=====================================================================*/
  437. char *og_plname(num) int num; /* return point to player #num's name */
  438. {
  439. int dummy;
  440. for(dummy=num;dummy!=num;) ;
  441. return "Test Player";
  442. }
  443. /*=====================================================================*/
  444. int *og_plscore(num) int num; /* return player #num's score */
  445. {
  446. int dummy;
  447. for(dummy=num;dummy!=num;) ;
  448. return &test_score;
  449. }
  450. /*=====================================================================*/
  451. double drandom() /* return a random double d>=0.0 && d<1.0 */
  452. {
  453. #if IBM_AZTEC || CPM_AZTEC || AMIGA_MANX
  454. double ran();
  455. return ran();
  456. #endif
  457. #if IBM_MICROSOFT || IBM_TURBO
  458. int rand();
  459. double d;
  460. d=rand(); /* automatic cast from int rand() to double d */
  461. d /=32768.0;
  462. return d;
  463. #endif
  464. }
  465. /*=====================================================================*/
  466. int irandom(max) int max; /* return a random integer i>=0 && i<=max-1 */
  467. {
  468. #if IBM_AZTEC || CPM_AZTEC || AMIGA_MANX
  469. double ran();
  470. int v;
  471. v=(int)(ran()*(double)max);
  472. return v;
  473. #endif
  474. #if IBM_MICROSOFT || IBM_TURBO
  475. int rand();
  476. long v;
  477. v=rand();
  478. v *=max;
  479. v /=32768L;
  480. return (int) v;
  481. #endif
  482. }
  483. /*=====================================================================*/
  484. void outstr(ss) char *ss; /* output a string */
  485. {
  486. while(*ss) keyout(*ss++);
  487. }
  488. /*=====================================================================*/
  489. int inpstr(ss,len) /* input a string ss, len doesnt include the null char */
  490. char *ss; int len;
  491. {
  492. char *ptr,*ptrmax;
  493. int c;
  494. ptr=ss; ptrmax=ss+len;
  495. for(;;)
  496.      {
  497.      c=keyin();
  498.    if(c==13) { *ptr=0; return 0; } /* user pressed return */
  499.    if(c==8 && ptr>ss) /* user pressed escape */
  500.      {
  501.      ptr--;
  502.      keyout(8);
  503.      keyout(' ');
  504.      keyout(8);
  505.      }
  506.    else if(c>=' ' && c<127 && ptr<ptrmax)
  507.      {
  508.      keyout(c);
  509.      *ptr++=c;
  510.      }
  511.      }
  512.  
  513. }
  514. /*=====================================================================*/
  515. int isin(s0,c) char *s0,c;
  516. {
  517. char *s;
  518. s=s0;
  519. while(*s)
  520.   if(*s==c) return s-s0+1;
  521.   else s++;
  522. return 0;
  523. }
  524. /*=====================================================================*/
  525. void *og_malloc(size) int size;
  526. {
  527. void *ptr;
  528. #if IBM_AZTEC || CPM_AZTEC || AMIGA_MANX
  529. void *malloc();
  530. #endif
  531. ptr=malloc((unsigned) size);
  532. if(ptr!=0) malloc_cnt++;
  533. return ptr;
  534. }
  535. /*=====================================================================*/
  536. void og_free(ptr) void *ptr;
  537. {
  538. if(ptr==0) return;
  539. free(ptr);
  540. malloc_cnt--;
  541. }
  542. /*=====================================================================*/
  543. #if AMIGA_MANX
  544. g0_setup_amiga()
  545. {
  546. struct sgttyb stty;
  547. /* To allow raw key input; temporary measure */
  548. /* Set up ioctl() */
  549. ioctl(fileno(stdin),TIOCGETP,&stty);     /* Read the stdin status */
  550. stty.sg_flags |= RAW;                    /* set mode to RAW       */
  551. ioctl(fileno(stdin),TIOCSETP,&stty);     /* write new mode        */
  552. }
  553.  
  554. g0_close_amiga()
  555. {
  556. struct sgttyb stty;
  557. /* Necessary or CLI screwed up. I don't know what will happen if this */
  558. /* is run from the Workbench */
  559. ioctl(fileno(stdin),TIOCGETP,&stty); /* Go through the whole rigamorole */
  560. stty.sg_flags &= ~RAW;               /* again, to restore things to     */
  561. ioctl(fileno(stdin),TIOCSETP,&stty); /* normal.                         */
  562. }
  563. #endif
  564. /*=====================================================================*/
  565. /* Designed and written by David M. Larson, BBS: 916-753-8788 */
  566. /* Dynasoft, P.O. Box 915, Davis, CA 95617 */