summaryrefslogtreecommitdiffstats
path: root/grv.c
blob: 1a1476e51fa1739d1359950108f176e1d37c64b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * grv.c
 *
 * Main program - converted from BASIC
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include "highscore.h"
#include "graphics.h"
#include "grv.h"

struct gameparams gp;
struct monster ghost[MAXGHOST];

void init_gameparams(void)
{
  gp.Sc = gp.SBs = 0;
  gp.Life = 3;
  gp.SBs = 0;
  gp.Lvf = 0;
  gp.GkCh = 0;
  gp.MLev = 0;
  gp.KulSpr = 0;
  gp.Level = 0;
  gp.Cheat = 0;
  gp.EOLWait = 1;

  /* As good a start as any... */
  gp.gameid = (uint64_t)genrand_int32() << 32;
  gp.have_id = !get_random_bytes(&gp.gameid, sizeof gp.gameid);
}

void initscreen(int w)
{
  width(w);
  color(0,gp.c);
  cls();
}

void newlevel(void)
{
  gp.Bon = 0;
  gp.TF = -1;
  gp.Hyp = 0;
  gp.Bar = 0;
  gp.ChBd = gp.GkCh;
  gp.FS = 1;			/* Ghosts are not frozen or reversed */
  /* 150 ms/step is the speed for level 0 */
  gp.Speed = (150*ELev)/(gp.Level+ELev);
  gp.Tid = 0.0;
  if ( gp.Level < ELev-1 )
    gp.ZLevel = ELev-1;

  if ( gp.Level >= 15 && gp.MLev < 15 ) {
    /* Announce Escape option */
    levelscreen();
    color(0,gp.c2);
    lprint(12,1," Now you have completed the first 15    ");
    lprint(13,1," levels of this game.  Now you can use  ");
    lprint(14,1," your Escape key to get away!           ");

    if ( gp.EOLWait )
      mymssleep(5000);
  }

  gp.EOLWait = 1;

  gp.MLev = max(gp.Level, gp.MLev);

  reset_actions();
  addaction(0, 0, 1800.0, act_toolate);	/* 30 minutes -> dead */

  if ( gp.Level == ELev-1 ) {
    /* Last level */
    gp.KulSpr = 0;
  } else {
    /* Not the last level */
    int e;
    for ( e = gp.KulSpr ; e ; e-- ) {
      if ( irnd(10) )
	gp.KulSpr--;		/* 10% chance to keep unused shots */
    }
    if ( gp.KulSpr > 80 )
      gp.KulSpr = 80;
  }

  if ( gp.Level > gp.MLev )
    gp.MLev = gp.Level;

  drawlevel();

  play();
}

void game(void)
{
  init_gameparams();
  while ( gp.Level < ELev && gp.Life >= 0 ) {
    newlevel();
  }
  highscore_endgame();
}

struct opts opt;

int main(int argc, char *argv[])
{
  int i;

  randomize();

  memset(&opt, 0, sizeof opt);

  for ( i = 1 ; i < argc ; i++ ) {
    if ( !strcmp(argv[i], "-nonet") ||
	 !strcmp(argv[i], "-no-net") )
      opt.nonet = 1;
    else if ( !strcmp(argv[i], "-window") )
      opt.window = 1;
    else if ( !strcmp(argv[i], "-debug") )
      opt.debug = 1;
  }

  goto_grv_dir();

  /* Init the highscore data structures */
  /* This is done here because it's a destructive operation and must
     only be done once! */
  highscore_init();

  load_prefs();

  if ( screen_init(opt.window) ) {
    fprintf(stderr, "%s: Cannot initalize graphics!\n", argv[0]);
    exit(1);
  }

  highscore_load();
  highscore_download();
  highscore_upload();		/* If we have something the server didn't... */

  while ( 1 ) {
    intro();
    switch ( get_key()->keysym.sym ) {
    case SDLK_k:
      set_keys();
      break;
    case SDLK_p:
      game();
      break;
    case SDLK_q:
      return 0;
    case SDLK_v:
      highscore_show(0,0);
      break;
    default:
      break;
    }
  }
}