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.5 2002/12/23 09:28:37 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 | |
---|
27 | static void draw_wall( game *g, int *wall, int delta ); |
---|
28 | |
---|
29 | /* Init tunnel */ |
---|
30 | tunnel * 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] = -10; |
---|
43 | t->right[0] = g->w + 10; |
---|
44 | for( i = 0; i < g->h; i++ ) |
---|
45 | { |
---|
46 | t->left[i] = -10; |
---|
47 | t->right[i] = g->w + 10; |
---|
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 | |
---|
64 | void free_tunnel( tunnel *t ) |
---|
65 | { |
---|
66 | free( t->left ); |
---|
67 | free( t->right ); |
---|
68 | free( t ); |
---|
69 | } |
---|
70 | |
---|
71 | void draw_tunnel( game *g, tunnel *t ) |
---|
72 | { |
---|
73 | /* Print tunnel */ |
---|
74 | draw_wall( g, t->left, -2 ); |
---|
75 | draw_wall( g, t->right, -1 ); |
---|
76 | } |
---|
77 | |
---|
78 | void update_tunnel( game *g, tunnel *t ) |
---|
79 | { |
---|
80 | static int const delta[] = { -3, -2, -1, 1, 2, 3 }; |
---|
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,6)]; |
---|
92 | j = delta[GET_RAND(0,6)]; |
---|
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] = -10; |
---|
125 | t->right[0] = g->w + 10; |
---|
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 | |
---|
154 | static void draw_wall( game *g, int *wall, int delta ) |
---|
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] < -10 || wall[i] >= g->w + 10 ) |
---|
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 + delta, i ); |
---|
181 | gfx_putchar( '_' ); |
---|
182 | } |
---|
183 | |
---|
184 | gfx_goto( wall[i] + delta, i ); |
---|
185 | gfx_putstr( str ); |
---|
186 | if( wall[i] == wall[i+1] - 2 ) gfx_putchar( '_' ); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|