home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / alt / sources / 2608 / dstar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-23  |  3.9 KB  |  225 lines

  1. #ifndef lint
  2. static    char    sccsid[] = "@(#)dstar.c 1.2 92/05/28 SMI" ;
  3.     /* from dstar.c 1.5 90/05/02 SMI */
  4. #endif
  5.  
  6. /*
  7.  * Copyright (c) 1986 by Sun Microsystems, Inc.
  8.  */
  9.  
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <math.h>
  13. #include <sys/types.h>
  14. #include <sys/time.h>
  15. #include <signal.h>
  16. #include "dstar.h"
  17. #include "object_types.h"
  18. #ifdef XV
  19. #include <xview/notify.h>
  20. #else
  21. #include <sunwindow/notify.h>
  22. #endif XV
  23.  
  24.  
  25.  
  26. extern    int        optind ;
  27. extern    char        *optarg ;
  28.  
  29.  
  30. static    struct timeval    Last_Poll ;
  31. static    struct timeval    countdown ;
  32. static    Blast_State    blast_state ;
  33. static    int        sigint() ;
  34.  
  35.  
  36. #ifdef    COMMENT
  37. extern    Notify_value    dstar_prioritizer() ;
  38. #endif    COMMENT
  39.     void        timefunc(), sleepfunc() ;
  40.  
  41. main(argc,argv)
  42. int    argc;
  43. char    **argv;
  44. {
  45.     int    i,j,k,iret ;
  46.     int    c ;
  47.     Pt3d    p1, p2, p3, p4, p5 ;
  48.  
  49.     gettimeofday(&Now, NULL) ;
  50.     Last_Time.tv_sec = -1 ;
  51.     Last_Poll = Now ;
  52.  
  53.     debug_level = 0 ;
  54.  
  55.     srandom(getpid()) ;
  56.  
  57.     Main_Timer.it_interval.tv_usec = DISPLAY_INTERVAL ;
  58.     Main_Timer.it_interval.tv_sec = 0 ;
  59.     Main_Timer.it_value.tv_usec = 0 ;
  60.     Main_Timer.it_value.tv_sec = 1 ;
  61.  
  62.     Sleep_Timer.it_interval.tv_usec = 0 ;
  63.     Sleep_Timer.it_interval.tv_sec = SLEEP_INTERVAL ;
  64.     Sleep_Timer.it_value.tv_usec = 0 ;
  65.     Sleep_Timer.it_value.tv_sec = POLL_INTERVAL ;
  66.  
  67.     blast_state = B_NIL ;
  68.  
  69.     init_objects() ;        /* init object descriptions */
  70.     Me = &objects[0] ;        /* gets changed later */
  71.  
  72.     init_game_special(argc,argv) ;    /* init graphic stuff (if any) */
  73.  
  74.  
  75.     notify_set_signal_func(&Main_Timer, sigint, SIGHUP, NOTIFY_SYNC) ;
  76.     notify_set_signal_func(&Main_Timer, sigint, SIGINT, NOTIFY_SYNC) ;
  77.     notify_set_signal_func(&Main_Timer, sigint, SIGTERM, NOTIFY_SYNC) ;
  78.     notify_set_signal_func(&Main_Timer, sigint, SIGXCPU, NOTIFY_SYNC) ;
  79. #ifdef    COMMENT
  80.     notify_set_prioritizer_func(&Main_Timer, dstar_prioritizer) ;
  81. #endif    COMMENT
  82.  
  83.     if( Me->status == OBJ_SLEEPING )
  84.       (void) notify_set_itimer_func(&Main_Timer, sleepfunc,
  85.           ITIMER_REAL, &Sleep_Timer, 0) ;
  86.     else
  87.       (void) notify_set_itimer_func(&Main_Timer, timefunc,
  88.           ITIMER_REAL, &Main_Timer, 0) ;
  89.  
  90.     dstar_main_loop() ;
  91.     exit(0) ;
  92. }
  93.  
  94.  
  95.  
  96.  
  97. /* this routine called at start-up to zero out all objects */
  98.  
  99. init_objects()
  100. {
  101.     int    i ;
  102.     Pt3d    p1,p2,p3 ;
  103.  
  104.     for(i=0; i<MAX_OBJECTS; objects[i++].class = OBJ_EMPTY ) ;
  105.     for(i=0; i<MAX_DESCRIPTIONS; descriptions[i++] = NULL ) ;
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. /* interrupt handler.  Simply tells the notifier to get the hell out */
  115.  
  116.  
  117. static    int
  118. sigint(me, signal, when)
  119.     int    *me, signal ;
  120.     Notify_signal_mode when ;
  121. {
  122.     terminate_game() ;
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130. /* this routine called whenever the clock ticks.  Current mode affects
  131.    what happens */
  132.  
  133.  
  134. void
  135. timefunc(client, which)
  136.     int    *client ;
  137.     int    which ;
  138. {
  139.     long    interval ;
  140.  
  141.     gettimeofday(&Now, NULL) ;
  142.     if(Last_Time.tv_sec == -1)
  143.       Last_Time = Now ;
  144.  
  145.     Dtime = (Now.tv_sec - Last_Time.tv_sec) + 
  146.         (Now.tv_usec - Last_Time.tv_usec) * .000001 ;
  147.  
  148.     switch(Me->status)
  149.     {
  150.       case OBJ_ACTIVE:
  151.         normal_action() ;
  152.         break ;
  153.  
  154.       case OBJ_DEAD:
  155.         switch(blast_state)
  156.         {
  157.           case B_NIL:
  158.         blast_state = B_FLASH ;
  159.         set_blast_state(blast_state) ;
  160.         countdown = Now ;
  161.         break ;
  162.  
  163.           case B_FLASH:
  164.         interval = (Now.tv_sec - countdown.tv_sec)*1000000 + 
  165.                 Now.tv_usec - countdown.tv_usec ;
  166.         if(interval > FLASH_INTERVAL)
  167.         {
  168.           blast_state = B_WAIT ;
  169.           set_blast_state(blast_state) ;
  170.         }
  171.         blast_action() ;
  172.         break ;
  173.  
  174.           case B_WAIT:
  175.         interval = Now.tv_sec - countdown.tv_sec ;
  176.         if(interval > DEATH_INTERVAL)
  177.         {
  178.           blast_state = B_NIL ;
  179.           set_blast_state(blast_state) ;
  180.         }
  181.         else
  182.           blast_action() ;
  183.         break ;
  184.         }
  185.         break ;
  186.     }
  187.  
  188.     if(Now.tv_sec - Last_Poll.tv_sec > POLL_INTERVAL)
  189.     {
  190.       net_poll() ;
  191.       Last_Poll = Now ;
  192.     }
  193.     Last_Time = Now ;
  194.  
  195.     if( Me->id != -1 )
  196.       net_flush() ;
  197. }
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. /* this routine called whenever the clock ticks while asleep. */
  205.  
  206.  
  207. void
  208. sleepfunc(client, which)
  209.     int    *client ;
  210.     int    which ;
  211. {
  212.     long    interval ;
  213.  
  214.     gettimeofday(&Now, NULL) ;
  215.  
  216.     net_poll() ;
  217.     Last_Poll = Now ;
  218.     Last_Time = Now ;
  219.  
  220.     special_sleep_func() ;
  221.  
  222.     if( Me->id != -1 )
  223.       net_flush() ;
  224. }
  225.