source: ttyvaders/trunk/src/tunnel.c @ 194

Last change on this file since 194 was 194, checked in by Sam Hocevar, 16 years ago
  • BUGS TODO: + Removed libcaca stuff.
  • data/bongem.txt: + Fixed sprite syntax.
  • src/: + Set framerate to 25fps instead of 10fps. + Updated code to latest libcaca changes.
  • Property svn:keywords set to Id
File size: 4.7 KB
RevLine 
[36]1/*
2 *   ttyvaders     Textmode shoot'em up
3 *   Copyright (c) 2002 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
[80]6 *   $Id: tunnel.c 194 2003-11-16 15:31:14Z sam $
[36]7 *
8 *   This program is free software; you can redistribute it and/or modify
9 *   it under the terms of the GNU General Public License as published by
10 *   the Free Software Foundation; either version 2 of the License, or
11 *   (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
[18]22
[56]23#include "config.h"
24
[18]25#include <stdlib.h>
26
27#include "common.h"
28
29/* Init tunnel */
[88]30tunnel * create_tunnel(game *g, int w, int h)
[18]31{
32    int i;
33    tunnel *t = malloc(sizeof(tunnel));
[174]34    if(t == NULL)
35        exit(1);
[18]36
37    t->left = malloc(h*sizeof(int));
[174]38    if(t->left == NULL)
39        exit(1);
[18]40    t->right = malloc(h*sizeof(int));
[174]41    if(t->right == NULL)
42        exit(1);
[18]43    t->w = w;
44    t->h = h;
45
[88]46    if(t->w >= g->w)
[18]47    {
[88]48        for(i = 0; i < g->h; i++)
[18]49        {
[50]50            t->left[i] = -10;
51            t->right[i] = g->w + 10;
[18]52        }
53    }
54    else
55    {
56        t->left[0] = (g->w - w) / 2;
57        t->right[0] = (g->w + w) / 2;
58        /* Yeah, sub-efficient, but less code to do :-) */
[88]59        for(i = 0; i < g->h; i++)
[18]60        {
[88]61            update_tunnel(g, t);
[18]62        }
63    }
64
65    return t;
66}
67
[88]68void free_tunnel(tunnel *t)
[18]69{
[88]70    free(t->left);
71    free(t->right);
72    free(t);
[18]73}
74
[88]75void draw_tunnel(game *g, tunnel *t)
[18]76{
[56]77    int i, j;
78    char c;
79
[194]80    caca_set_color(CACA_COLOR_GREEN);
[56]81
82    /* Left border */
[88]83    for(i = 0; i < g->h ; i++)
[56]84    {
[88]85        if(t->left[i] <= -10)
[56]86            continue;
87
[88]88        if(i + 1 == g->h || t->left[i] > t->left[i+1])
89            c = (i == 0 || t->left[i] > t->left[i-1]) ? '>' : '/';
[56]90        else
[88]91            c = (i == 0 || t->left[i] > t->left[i-1]) ? '\\' : '<';
[56]92
[186]93        caca_putchar(t->left[i] + 1, i, c);
[56]94
[88]95        if(i + 1 < g->h)
96            for(j = 1; j < t->left[i+1] - t->left[i]; j++)
[186]97                caca_putchar(t->left[i] + j + 1, i, '_');
[56]98    }
99
100    /* Right border */
[88]101    for(i = 0; i < g->h ; i++)
[56]102    {
[88]103        if(t->right[i] >= g->w + 10)
[56]104            continue;
105
[88]106        if(i + 1 == g->h || t->right[i] > t->right[i+1])
107            c = (i == 0 || t->right[i] > t->right[i-1]) ? '>' : '/';
[56]108        else
[88]109            c = (i == 0 || t->right[i] > t->right[i-1]) ? '\\' : '<';
[56]110
[88]111        if(i + 1 < g->h)
112            for(j = 1; j < t->right[i] - t->right[i+1]; j++)
[186]113                caca_putchar(t->right[i+1] + j - 1, i, '_');
[56]114
[186]115        caca_putchar(t->right[i] - 1, i, c);
[56]116    }
117
[194]118    caca_set_color(CACA_COLOR_RED);
[56]119
120    /* Left concrete */
[88]121    for(i = 0; i < g->h ; i++)
122        for(j = 0 ; j <= t->left[i]; j++)
[186]123            caca_putchar(j, i, '#');
[56]124
125    /* Right concrete */
[88]126    for(i = 0; i < g->h ; i++)
127        for(j = t->right[i] ; j < g->w ; j++)
[186]128            caca_putchar(j, i, '#');
[18]129}
130
[88]131void update_tunnel(game *g, tunnel *t)
[18]132{
[50]133    static int const delta[] = { -3, -2, -1, 1, 2, 3 };
[18]134    int i,j,k;
135
136    /* Slide tunnel one block vertically */
[88]137    for(i = t->h - 1; i--;)
[18]138    {
139        t->left[i+1] = t->left[i];
140        t->right[i+1] = t->right[i];
141    }
142
143    /* Generate new values */
[186]144    i = delta[caca_rand(0,5)];
145    j = delta[caca_rand(0,5)];
[18]146
147    /* Check in which direction we need to alter tunnel */
[88]148    if(t->right[1] - t->left[1] < t->w)
[18]149    {
[56]150        /* Not wide enough, make sure i <= j */
[88]151        if(i > j)
[18]152        {
153            k = j; j = i; i = k;
154        }
155    }
[88]156    else if(t->right[1] - t->left[1] - 2 > t->w)
[18]157    {
[56]158        /* Too wide, make sure i >= j */
[88]159        if(i < j)
[18]160        {
161            k = j; j = i; i = k;
162        }
163    }
164    else
165    {
166        /* No need to mess with i and j: width is OK */
167    }
168
169    /* If width doesn't exceed game size, update coords */
[88]170    if(t->w <= g->w || t->right[1] - t->left[1] < t->w)
[18]171    {
172        t->left[0] = t->left[1] + i;
173        t->right[0] = t->right[1] + j;
174    }
175    else
176    {
[50]177        t->left[0] = -10;
178        t->right[0] = g->w + 10;
[18]179    }
180
[88]181    if(t->w > g->w)
[18]182    {
[88]183        if(t->left[0] < 0 && t->right[0] < g->w - 2)
[18]184        {
185             t->left[0] = t->left[1] + 1;
186        }
187
[88]188        if(t->left[0] > 1 && t->right[0] > g->w - 1)
[18]189        {
190             t->right[0] = t->right[1] - 1;
191        }
192    }
193    else
194    {
[88]195        if(t->left[0] < 0)
[18]196        {
197            t->left[0] = t->left[1] + 1;
198        }
199
[88]200        if(t->right[0] > g->w - 1)
[18]201        {
202            t->right[0] = t->right[1] - 1;
203        }
204    }
205}
206
Note: See TracBrowser for help on using the repository browser.