aboutsummaryrefslogtreecommitdiffstats
path: root/game.c
blob: 0cb27f977388c22fc889aef8cd6d9d1fc926fe77 (plain) (blame)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*  $Id: game.c,v 1.1.1.1 2010/07/17 17:30:32 culot Exp $  */

/*
 * Copyright (c) 2010 Frederic Culot <frederic@culot.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer in the documentation and/or other
 *        materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "oldrunner.h"

#ifdef LEVELS_PATH
#  define  LEVELS  LEVELS_PATH "/original.lvl"
#else
#  define  LEVELS  "./levels/original.lvl"
#endif

#define  POINTS_FOR_MONEY  50
#define  LIVES_AT_START    5

static struct game_info ginfo;

static void
game_lost (void)
{
  struct usr_input input;
  
  gfx_game_over ();
  gfx_get_input (&input);
  game_end ();
}

void
game_won (void)
{
  struct usr_input input;
  
  gfx_game_won ();
  gfx_get_input (&input);
  game_end ();
}

void
game_init (int startlvl)
{
  lvl_init ();
  if (!game_load (LEVELS))
    EXIT ("Could not load game levels. Aborting...");
  ginfo.lives = LIVES_AT_START;
  ginfo.level = startlvl + 1;
  gfx_init ();  
  lvl_load (startlvl);
  hero_init ();
  game_update ();
}

/*
 * The window will be centered on player either if the user requests it or if
 * the level board is too large to fit it the screen.
 */
static void
set_view (void)
{
  struct size lvl;

  lvl.w = lvl_width ();
  lvl.h = lvl_height ();
  
  if (AUTO_CENTER_MODE || lvl.h > VIEWPORT_HEIGHT || lvl.w > VIEWPORT_WIDTH)
    {
      struct coord player_pos;

      hero_get_pos (&player_pos);      
      gfx_center_at (&player_pos);
    }
  else
    {
      struct coord fixed_pos;

      coord_set_yx (&fixed_pos, VIEWPORT_HEIGHT / 2, lvl.w / 2);
      gfx_center_at (&fixed_pos);
    }
}

void
game_update (void)
{
  set_view ();
  lvl_objects_update ();
  hero_draw ();
  if (!(ginfo.state & NO_CHANGE))
    {
      gfx_update_info (&ginfo);
      ginfo.state = NO_CHANGE;
    }
  gfx_update ();
}

void
game_end (void)
{
  gfx_end ();
  exit (0);
}

static unsigned
read_level_row (char *row, unsigned rownum)
{
  char *rowend;
  int len;
  
  if (*row != '[')
    return 0;
  row++;

  if (!(rowend = strchr (row, ']')))
    return 0;
  *rowend = '\0';
  len = rowend - row;
  
  return lvl_set_row (rownum, len, row);
}

static unsigned
store_level_attr (const char *attr, char *val)
{
  if (!strcmp (attr, "level"))
    return lvl_set_name (val);
  else if (!strcmp (attr, "author"))
    return lvl_set_author (val);
  else if (!strcmp (attr, "size"))
    return lvl_set_size (val);
  else
    return 0;
}

/* XXX use strsep instead. */
static unsigned
read_level_attr (char *line)
{
  char *p, *attr, *val;

  attr = p = line;

  while (!isblank ((int)(*p)))
    p++;
  *p++ = '\0';

  while (isblank ((int)(*p)))
    p++;
  val = p;

  return store_level_attr (attr, val);
}

/*
 * XXX improve comment.
 *
 * A game file contains the description of the levels.
 * The following rules apply:
 *   - empty lines are ignored, as well as those beginning with #
 *   - the following keywords are used to describe a level:
 *     -  level: name of the level (default: "")
 *     - author: author of the level (default: "")
 *     -   size: in the form widthxlength (default: 26x16)
 *   - if keywords are present, they must be placed before the level layout
 *   - the reading of a level row stops as soon as the line does
 *     not start with '[', meaning a layout description must not have empty
 *     lines
 */
unsigned
game_load (const char *path)
{
  FILE *f;
  char *buf;
  unsigned rownum, newlevel;
  
  if (!(f = io_fopen (LEVELS)))
    return 0;

  newlevel = 1;
  rownum = 0;
  while ((buf = io_getln (f)))
    {
      char *p;

#define CHK_NEW_LVL  do {                       \
        if (newlevel)                           \
          {                                     \
            lvl_add_new ();                     \
            newlevel = 0;                       \
            rownum = 0;                         \
          }                                     \
    } while (0)
      
      /* Remove trailing spaces. */
      p = buf;
      while (isblank ((int)(*p)))
        p++;
      
      switch (*p)
        {
        case '[':
          CHK_NEW_LVL;
          read_level_row (p, rownum);
          rownum++;
          break;
        case '#':
        case '\0':
          if (rownum)
            newlevel = 1;
          continue;
        default:
          if (rownum)
            newlevel = 1;
          CHK_NEW_LVL;
          read_level_attr (p);
          break;
        }
      
#undef CHK_NEW_LVL      
    }

  io_fclose (f);

  return 1;
}

void
game_score_inc (void)
{
  ginfo.state |= SCORE_CHANGE;
  ginfo.score += POINTS_FOR_MONEY;
}

int
game_level_num (void)
{
  return ginfo.level;
}

void
game_level_inc (void)
{
  ginfo.state |= LEVEL_CHANGE;
  ginfo.level++;
}

void
game_level_dec (void)
{
  ginfo.state |= LEVEL_CHANGE;  
  ginfo.level--;
}

void
game_lives_inc (void)
{
  ginfo.state |= LIVES_CHANGE;  
  ginfo.lives++;
}

void
game_lives_dec (void)
{
  ginfo.state |= LIVES_CHANGE;
  if (ginfo.lives == 0)
    game_lost ();
  ginfo.lives--;
}
Un proyecto texto-plano.xyz