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

Last change on this file since 38 was 38, checked in by Sam Hocevar, 17 years ago
  • I think I fucked up the $Id tags on my previous commit.
File size: 4.1 KB
Line 
1/*
2 *   ttyvaders     Textmode shoot'em up
3 *   Copyright (c) 2002 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: tunnel.c,v 1.4 2002/12/22 18:44:12 sam Exp $
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 */
22
23#include <stdlib.h>
24
25#include "common.h"
26
27static void draw_wall( game *g, int *wall );
28
29/* Init tunnel */
30tunnel * create_tunnel( game *g, int w, int h )
31{
32    int i;
33    tunnel *t = malloc(sizeof(tunnel));
34
35    t->left = malloc(h*sizeof(int));
36    t->right = malloc(h*sizeof(int));
37    t->w = w;
38    t->h = h;
39
40    if( t->w >= g->w )
41    {
42        t->left[0] = -1;
43        t->right[0] = g->w;
44        for( i = 0; i < g->h; i++ )
45        {
46            t->left[i] = -1;
47            t->right[i] = g->w;
48        }
49    }
50    else
51    {
52        t->left[0] = (g->w - w) / 2;
53        t->right[0] = (g->w + w) / 2;
54        /* Yeah, sub-efficient, but less code to do :-) */
55        for( i = 0; i < g->h; i++ )
56        {
57            update_tunnel( g, t );
58        }
59    }
60
61    return t;
62}
63
64void free_tunnel( tunnel *t )
65{
66    free( t->left );
67    free( t->right );
68    free( t );
69}
70
71void draw_tunnel( game *g, tunnel *t )
72{
73    /* Print tunnel */
74    draw_wall( g, t->left );
75    draw_wall( g, t->right );
76}
77
78void update_tunnel( game *g, tunnel *t )
79{
80    static int const delta[] = { -2, -1, 1, 2 };
81    int i,j,k;
82
83    /* Slide tunnel one block vertically */
84    for( i = t->h; i--; )
85    {
86        t->left[i+1] = t->left[i];
87        t->right[i+1] = t->right[i];
88    }
89
90    /* Generate new values */
91    i = delta[GET_RAND(0,4)];
92    j = delta[GET_RAND(0,4)];
93
94    /* Check in which direction we need to alter tunnel */
95    if( t->right[1] - t->left[1] < t->w )
96    {
97        /* Not wide enough */
98        if( i > j )
99        {
100            k = j; j = i; i = k;
101        }
102    }
103    else if( t->right[1] - t->left[1] - 2 > t->w )
104    {
105        /* Too wide */
106        if( i < j )
107        {
108            k = j; j = i; i = k;
109        }
110    }
111    else
112    {
113        /* No need to mess with i and j: width is OK */
114    }
115
116    /* If width doesn't exceed game size, update coords */
117    if( t->w <= g->w || t->right[1] - t->left[1] < t->w )
118    {
119        t->left[0] = t->left[1] + i;
120        t->right[0] = t->right[1] + j;
121    }
122    else
123    {
124        t->left[0] = -1;
125        t->right[0] = g->w;
126    }
127
128    if( t->w > g->w )
129    {
130        if( t->left[0] < 0 && t->right[0] < g->w - 2 )
131        {
132             t->left[0] = t->left[1] + 1;
133        }
134
135        if( t->left[0] > 1 && t->right[0] > g->w - 1 )
136        {
137             t->right[0] = t->right[1] - 1;
138        }
139    }
140    else
141    {
142        if( t->left[0] < 0 )
143        {
144            t->left[0] = t->left[1] + 1;
145        }
146
147        if( t->right[0] > g->w - 1 )
148        {
149            t->right[0] = t->right[1] - 1;
150        }
151    }
152}
153
154static void draw_wall( game *g, int *wall )
155{
156    int i;
157
158    gfx_color( RED );
159
160    for( i = 0; i < g->h ; i++ )
161    {
162        char *str;
163
164        if( wall[i] < 0 || wall[i] >= g->w )
165        {
166            continue;
167        }
168
169        if( wall[i] > wall[i+1] )
170        {
171            str = wall[i] > wall[i-1] ? ">##>" : "/##/";
172        }
173        else
174        {
175            str = wall[i] > wall[i-1] ? "\\##\\" : "<##<";
176        }
177
178        if( wall[i] == wall[i+1] + 2 )
179        {
180            gfx_goto( wall[i] - 1, i );
181            gfx_putchar( '_' );
182        }
183
184        gfx_goto( wall[i], i );
185        gfx_putstr( str );
186        if( wall[i] == wall[i+1] - 2 ) gfx_putchar( '_' );
187    }
188}
189
Note: See TracBrowser for help on using the repository browser.