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 174 2003-11-14 17:02:36Z sam $ |
---|
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 "config.h" |
---|
24 | |
---|
25 | #include <stdlib.h> |
---|
26 | |
---|
27 | #include "common.h" |
---|
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 | if(t == NULL) |
---|
35 | exit(1); |
---|
36 | |
---|
37 | t->left = malloc(h*sizeof(int)); |
---|
38 | if(t->left == NULL) |
---|
39 | exit(1); |
---|
40 | t->right = malloc(h*sizeof(int)); |
---|
41 | if(t->right == NULL) |
---|
42 | exit(1); |
---|
43 | t->w = w; |
---|
44 | t->h = h; |
---|
45 | |
---|
46 | if(t->w >= g->w) |
---|
47 | { |
---|
48 | for(i = 0; i < g->h; i++) |
---|
49 | { |
---|
50 | t->left[i] = -10; |
---|
51 | t->right[i] = g->w + 10; |
---|
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 :-) */ |
---|
59 | for(i = 0; i < g->h; i++) |
---|
60 | { |
---|
61 | update_tunnel(g, t); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | return t; |
---|
66 | } |
---|
67 | |
---|
68 | void free_tunnel(tunnel *t) |
---|
69 | { |
---|
70 | free(t->left); |
---|
71 | free(t->right); |
---|
72 | free(t); |
---|
73 | } |
---|
74 | |
---|
75 | void draw_tunnel(game *g, tunnel *t) |
---|
76 | { |
---|
77 | int i, j; |
---|
78 | char c; |
---|
79 | |
---|
80 | ee_set_color(EE_GREEN); |
---|
81 | |
---|
82 | /* Left border */ |
---|
83 | for(i = 0; i < g->h ; i++) |
---|
84 | { |
---|
85 | if(t->left[i] <= -10) |
---|
86 | continue; |
---|
87 | |
---|
88 | if(i + 1 == g->h || t->left[i] > t->left[i+1]) |
---|
89 | c = (i == 0 || t->left[i] > t->left[i-1]) ? '>' : '/'; |
---|
90 | else |
---|
91 | c = (i == 0 || t->left[i] > t->left[i-1]) ? '\\' : '<'; |
---|
92 | |
---|
93 | ee_putchar(t->left[i] + 1, i, c); |
---|
94 | |
---|
95 | if(i + 1 < g->h) |
---|
96 | for(j = 1; j < t->left[i+1] - t->left[i]; j++) |
---|
97 | ee_putchar(t->left[i] + j + 1, i, '_'); |
---|
98 | } |
---|
99 | |
---|
100 | /* Right border */ |
---|
101 | for(i = 0; i < g->h ; i++) |
---|
102 | { |
---|
103 | if(t->right[i] >= g->w + 10) |
---|
104 | continue; |
---|
105 | |
---|
106 | if(i + 1 == g->h || t->right[i] > t->right[i+1]) |
---|
107 | c = (i == 0 || t->right[i] > t->right[i-1]) ? '>' : '/'; |
---|
108 | else |
---|
109 | c = (i == 0 || t->right[i] > t->right[i-1]) ? '\\' : '<'; |
---|
110 | |
---|
111 | if(i + 1 < g->h) |
---|
112 | for(j = 1; j < t->right[i] - t->right[i+1]; j++) |
---|
113 | ee_putchar(t->right[i+1] + j - 1, i, '_'); |
---|
114 | |
---|
115 | ee_putchar(t->right[i] - 1, i, c); |
---|
116 | } |
---|
117 | |
---|
118 | ee_set_color(EE_RED); |
---|
119 | |
---|
120 | /* Left concrete */ |
---|
121 | for(i = 0; i < g->h ; i++) |
---|
122 | for(j = 0 ; j <= t->left[i]; j++) |
---|
123 | ee_putchar(j, i, '#'); |
---|
124 | |
---|
125 | /* Right concrete */ |
---|
126 | for(i = 0; i < g->h ; i++) |
---|
127 | for(j = t->right[i] ; j < g->w ; j++) |
---|
128 | ee_putchar(j, i, '#'); |
---|
129 | } |
---|
130 | |
---|
131 | void update_tunnel(game *g, tunnel *t) |
---|
132 | { |
---|
133 | static int const delta[] = { -3, -2, -1, 1, 2, 3 }; |
---|
134 | int i,j,k; |
---|
135 | |
---|
136 | /* Slide tunnel one block vertically */ |
---|
137 | for(i = t->h - 1; i--;) |
---|
138 | { |
---|
139 | t->left[i+1] = t->left[i]; |
---|
140 | t->right[i+1] = t->right[i]; |
---|
141 | } |
---|
142 | |
---|
143 | /* Generate new values */ |
---|
144 | i = delta[ee_rand(0,5)]; |
---|
145 | j = delta[ee_rand(0,5)]; |
---|
146 | |
---|
147 | /* Check in which direction we need to alter tunnel */ |
---|
148 | if(t->right[1] - t->left[1] < t->w) |
---|
149 | { |
---|
150 | /* Not wide enough, make sure i <= j */ |
---|
151 | if(i > j) |
---|
152 | { |
---|
153 | k = j; j = i; i = k; |
---|
154 | } |
---|
155 | } |
---|
156 | else if(t->right[1] - t->left[1] - 2 > t->w) |
---|
157 | { |
---|
158 | /* Too wide, make sure i >= j */ |
---|
159 | if(i < j) |
---|
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 */ |
---|
170 | if(t->w <= g->w || t->right[1] - t->left[1] < t->w) |
---|
171 | { |
---|
172 | t->left[0] = t->left[1] + i; |
---|
173 | t->right[0] = t->right[1] + j; |
---|
174 | } |
---|
175 | else |
---|
176 | { |
---|
177 | t->left[0] = -10; |
---|
178 | t->right[0] = g->w + 10; |
---|
179 | } |
---|
180 | |
---|
181 | if(t->w > g->w) |
---|
182 | { |
---|
183 | if(t->left[0] < 0 && t->right[0] < g->w - 2) |
---|
184 | { |
---|
185 | t->left[0] = t->left[1] + 1; |
---|
186 | } |
---|
187 | |
---|
188 | if(t->left[0] > 1 && t->right[0] > g->w - 1) |
---|
189 | { |
---|
190 | t->right[0] = t->right[1] - 1; |
---|
191 | } |
---|
192 | } |
---|
193 | else |
---|
194 | { |
---|
195 | if(t->left[0] < 0) |
---|
196 | { |
---|
197 | t->left[0] = t->left[1] + 1; |
---|
198 | } |
---|
199 | |
---|
200 | if(t->right[0] > g->w - 1) |
---|
201 | { |
---|
202 | t->right[0] = t->right[1] - 1; |
---|
203 | } |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|