source: ttyvaders/trunk/tunnel.c @ 12

Last change on this file since 12 was 12, checked in by Sam Hocevar, 20 years ago
  • First commit. Scrolling works, some weaponry, controls, tunnel blowing, the nuke is still ugly, almost no collision detection. Nice demo :)
File size: 3.2 KB
Line 
1
2#include <stdlib.h>
3
4#include "common.h"
5
6static void draw_wall( game *g, int *wall );
7
8/* Init tunnel */
9tunnel * create_tunnel( game *g, int w, int h )
10{
11    int i;
12    tunnel *t = malloc(sizeof(tunnel));
13
14    t->left = malloc(h*sizeof(int));
15    t->right = malloc(h*sizeof(int));
16    t->w = w;
17    t->h = h;
18
19    if( t->w >= g->w )
20    {
21        t->left[0] = -1;
22        t->right[0] = g->w;
23        for( i = 0; i < g->h; i++ )
24        {
25            t->left[i] = -1;
26            t->right[i] = g->w;
27        }
28    }
29    else
30    {
31        t->left[0] = (g->w - w) / 2;
32        t->right[0] = (g->w + w) / 2;
33        /* Yeah, sub-efficient, but less code to do :-) */
34        for( i = 0; i < g->h; i++ )
35        {
36            update_tunnel( g, t );
37        }
38    }
39
40    return t;
41}
42
43void free_tunnel( tunnel *t )
44{
45    free( t->left );
46    free( t->right );
47    free( t );
48}
49
50void draw_tunnel( game *g, tunnel *t )
51{
52    /* Print tunnel */
53    draw_wall( g, t->left );
54    draw_wall( g, t->right );
55}
56
57void update_tunnel( game *g, tunnel *t )
58{
59    static int const delta[] = { -2, -1, 1, 2 };
60    int i,j,k;
61
62    /* Slide tunnel one block vertically */
63    for( i = t->h; i--; )
64    {
65        t->left[i+1] = t->left[i];
66        t->right[i+1] = t->right[i];
67    }
68
69    /* Generate new values */
70    i = delta[GET_RAND(0,4)];
71    j = delta[GET_RAND(0,4)];
72
73    /* Check in which direction we need to alter tunnel */
74    if( t->right[1] - t->left[1] < t->w )
75    {
76        /* Not wide enough */
77        if( i > j )
78        {
79            k = j; j = i; i = k;
80        }
81    }
82    else if( t->right[1] - t->left[1] - 2 > t->w )
83    {
84        /* Too wide */
85        if( i < j )
86        {
87            k = j; j = i; i = k;
88        }
89    }
90    else
91    {
92        /* No need to mess with i and j: width is OK */
93    }
94
95    /* If width doesn't exceed game size, update coords */
96    if( t->w <= g->w || t->right[1] - t->left[1] < t->w )
97    {
98        t->left[0] = t->left[1] + i;
99        t->right[0] = t->right[1] + j;
100    }
101    else
102    {
103        t->left[0] = -1;
104        t->right[0] = g->w;
105    }
106
107    if( t->w > g->w )
108    {
109        if( t->left[0] < 0 && t->right[0] < g->w - 2 )
110        {
111             t->left[0] = t->left[1] + 1;
112        }
113
114        if( t->left[0] > 1 && t->right[0] > g->w - 1 )
115        {
116             t->right[0] = t->right[1] - 1;
117        }
118    }
119    else
120    {
121        if( t->left[0] < 0 )
122        {
123            t->left[0] = t->left[1] + 1;
124        }
125
126        if( t->right[0] > g->w - 1 )
127        {
128            t->right[0] = t->right[1] - 1;
129        }
130    }
131}
132
133static void draw_wall( game *g, int *wall )
134{
135    int i;
136
137    for( i = 0; i < g->h ; i++ )
138    {
139        char c;
140
141        if( wall[i] < 0 || wall[i] >= g->w )
142        {
143            continue;
144        }
145
146        if( wall[i] > wall[i+1] )
147        {
148            c = wall[i] > wall[i-1] ? '>' : '/';
149        }
150        else
151        {
152            c = wall[i] > wall[i-1] ? '\\' : '<';
153        }
154
155        GFX_COLOR( RED );
156        if( wall[i] == wall[i+1] + 2 )
157        {
158            GFX_GOTO( wall[i] - 1, i );
159            GFX_WRITE( '_' );
160        }
161        else
162        {
163            GFX_GOTO( wall[i], i );
164        }
165        GFX_WRITE( c );
166        GFX_WRITE( '#' );
167        GFX_WRITE( c );
168        if( wall[i] == wall[i+1] - 2 ) GFX_WRITE( '_' );
169    }
170}
171
Note: See TracBrowser for help on using the repository browser.