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 148 2003-11-10 23:38:50Z 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 | |
---|
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 | for(i = 0; i < g->h; i++) |
---|
43 | { |
---|
44 | t->left[i] = -10; |
---|
45 | t->right[i] = g->w + 10; |
---|
46 | } |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | t->left[0] = (g->w - w) / 2; |
---|
51 | t->right[0] = (g->w + w) / 2; |
---|
52 | /* Yeah, sub-efficient, but less code to do :-) */ |
---|
53 | for(i = 0; i < g->h; i++) |
---|
54 | { |
---|
55 | update_tunnel(g, t); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | return t; |
---|
60 | } |
---|
61 | |
---|
62 | void free_tunnel(tunnel *t) |
---|
63 | { |
---|
64 | free(t->left); |
---|
65 | free(t->right); |
---|
66 | free(t); |
---|
67 | } |
---|
68 | |
---|
69 | void draw_tunnel(game *g, tunnel *t) |
---|
70 | { |
---|
71 | int i, j; |
---|
72 | char c; |
---|
73 | |
---|
74 | ee_color(EE_GREEN); |
---|
75 | |
---|
76 | /* Left border */ |
---|
77 | for(i = 0; i < g->h ; i++) |
---|
78 | { |
---|
79 | if(t->left[i] <= -10) |
---|
80 | continue; |
---|
81 | |
---|
82 | if(i + 1 == g->h || t->left[i] > t->left[i+1]) |
---|
83 | c = (i == 0 || t->left[i] > t->left[i-1]) ? '>' : '/'; |
---|
84 | else |
---|
85 | c = (i == 0 || t->left[i] > t->left[i-1]) ? '\\' : '<'; |
---|
86 | |
---|
87 | ee_putchar(t->left[i] + 1, i, c); |
---|
88 | |
---|
89 | if(i + 1 < g->h) |
---|
90 | for(j = 1; j < t->left[i+1] - t->left[i]; j++) |
---|
91 | ee_putchar(t->left[i] + j + 1, i, '_'); |
---|
92 | } |
---|
93 | |
---|
94 | /* Right border */ |
---|
95 | for(i = 0; i < g->h ; i++) |
---|
96 | { |
---|
97 | if(t->right[i] >= g->w + 10) |
---|
98 | continue; |
---|
99 | |
---|
100 | if(i + 1 == g->h || t->right[i] > t->right[i+1]) |
---|
101 | c = (i == 0 || t->right[i] > t->right[i-1]) ? '>' : '/'; |
---|
102 | else |
---|
103 | c = (i == 0 || t->right[i] > t->right[i-1]) ? '\\' : '<'; |
---|
104 | |
---|
105 | if(i + 1 < g->h) |
---|
106 | for(j = 1; j < t->right[i] - t->right[i+1]; j++) |
---|
107 | ee_putchar(t->right[i+1] + j - 1, i, '_'); |
---|
108 | |
---|
109 | ee_putchar(t->right[i] - 1, i, c); |
---|
110 | } |
---|
111 | |
---|
112 | ee_color(EE_RED); |
---|
113 | |
---|
114 | /* Left concrete */ |
---|
115 | for(i = 0; i < g->h ; i++) |
---|
116 | for(j = 0 ; j <= t->left[i]; j++) |
---|
117 | ee_putchar(j, i, '#'); |
---|
118 | |
---|
119 | /* Right concrete */ |
---|
120 | for(i = 0; i < g->h ; i++) |
---|
121 | for(j = t->right[i] ; j < g->w ; j++) |
---|
122 | ee_putchar(j, i, '#'); |
---|
123 | } |
---|
124 | |
---|
125 | void update_tunnel(game *g, tunnel *t) |
---|
126 | { |
---|
127 | static int const delta[] = { -3, -2, -1, 1, 2, 3 }; |
---|
128 | int i,j,k; |
---|
129 | |
---|
130 | /* Slide tunnel one block vertically */ |
---|
131 | for(i = t->h - 1; i--;) |
---|
132 | { |
---|
133 | t->left[i+1] = t->left[i]; |
---|
134 | t->right[i+1] = t->right[i]; |
---|
135 | } |
---|
136 | |
---|
137 | /* Generate new values */ |
---|
138 | i = delta[ee_rand(0,5)]; |
---|
139 | j = delta[ee_rand(0,5)]; |
---|
140 | |
---|
141 | /* Check in which direction we need to alter tunnel */ |
---|
142 | if(t->right[1] - t->left[1] < t->w) |
---|
143 | { |
---|
144 | /* Not wide enough, make sure i <= j */ |
---|
145 | if(i > j) |
---|
146 | { |
---|
147 | k = j; j = i; i = k; |
---|
148 | } |
---|
149 | } |
---|
150 | else if(t->right[1] - t->left[1] - 2 > t->w) |
---|
151 | { |
---|
152 | /* Too wide, make sure i >= j */ |
---|
153 | if(i < j) |
---|
154 | { |
---|
155 | k = j; j = i; i = k; |
---|
156 | } |
---|
157 | } |
---|
158 | else |
---|
159 | { |
---|
160 | /* No need to mess with i and j: width is OK */ |
---|
161 | } |
---|
162 | |
---|
163 | /* If width doesn't exceed game size, update coords */ |
---|
164 | if(t->w <= g->w || t->right[1] - t->left[1] < t->w) |
---|
165 | { |
---|
166 | t->left[0] = t->left[1] + i; |
---|
167 | t->right[0] = t->right[1] + j; |
---|
168 | } |
---|
169 | else |
---|
170 | { |
---|
171 | t->left[0] = -10; |
---|
172 | t->right[0] = g->w + 10; |
---|
173 | } |
---|
174 | |
---|
175 | if(t->w > g->w) |
---|
176 | { |
---|
177 | if(t->left[0] < 0 && t->right[0] < g->w - 2) |
---|
178 | { |
---|
179 | t->left[0] = t->left[1] + 1; |
---|
180 | } |
---|
181 | |
---|
182 | if(t->left[0] > 1 && t->right[0] > g->w - 1) |
---|
183 | { |
---|
184 | t->right[0] = t->right[1] - 1; |
---|
185 | } |
---|
186 | } |
---|
187 | else |
---|
188 | { |
---|
189 | if(t->left[0] < 0) |
---|
190 | { |
---|
191 | t->left[0] = t->left[1] + 1; |
---|
192 | } |
---|
193 | |
---|
194 | if(t->right[0] > g->w - 1) |
---|
195 | { |
---|
196 | t->right[0] = t->right[1] - 1; |
---|
197 | } |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|