aboutsummaryrefslogtreecommitdiff
path: root/field.c
blob: fee076b1142741ede9857543d14087fdfbe38c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* GEM Worm
 * Copyright (C) 2009, 2020 Jeffrey Armstrong
 * http://jeff.rainbow-100.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * A full version of the license terms is available in LICENSE.txt.
 */

#include <stdio.h>
#include <stdlib.h>

#ifdef __GNUC__

#include <gem.h>

#else

#include <portab.h>
#include <aes.h>
#include <vdi.h>

#endif

#include "util.h"
#include "field.h"
#include "player.h"

#ifdef __GEMLIB__
#define RED     G_RED
#define GREEN   G_GREEN
#define WHITE   G_WHITE
#define BLACK   G_BLACK
#endif

/* Defines the play field */
char field[CELLW][CELLH];

#define CELLBLANK       0
#define CELLPLAYER      1
#define CELLWALL        2
#define CELLFOOD        3

#define max(x,y)    x>y ? x : y
#define min(x,y)    x<y ? x : y

struct _food{
    WORD c_x;
    WORD c_y;
    int count;
} food;

void empty_field(char celltype)
{
char i,j;

    for(i=0;i<CELLW;i++) {
        field[i][0] = CELLWALL;
        field[i][CELLH-1] = CELLWALL;
    }
    for(j=0;j<CELLH;j++) {
        field[0][j] = CELLWALL;
        field[CELLW-1][j] = CELLWALL;
    }

    for(i=1;i<CELLW-1;i++) {
        for(j=1;j<CELLH-1;j++) {
            if(field[i][j] == celltype) 
                field[i][j] = CELLBLANK;
        }
    }
    
}

void food_reset()
{
    
    field[food.c_x][food.c_y] = CELLBLANK;
    food.c_x = rand() % CELLW;
    food.c_y = rand() % CELLH;
    while(field[food.c_x][food.c_y] != CELLBLANK) {
        food.c_x = rand() % CELLW;
        food.c_y = rand() % CELLH;
    }
    food.count = 0;
    field[food.c_x][food.c_y] = CELLFOOD;
}

void food_init()
{
    food.c_x = 10;
    food.c_y = 10;
    food_reset();
    field[food.c_x][food.c_y] = CELLFOOD;
}

int food_check(WORD h_vdi, GRECT *play, WPLAYER *player) 
{
int oldx,oldy;

    if(player->head->c_x == food.c_x && player->head->c_y == food.c_y) {
        player->grow += FOODADDITION;
        oldx = food.c_x; oldy = food.c_y;
        food_reset();
        incremental_draw(h_vdi, play, food.c_x, food.c_y);
        field[oldx][oldy] = CELLPLAYER;
        return FOODADDITION;
    }
    
    food.count++;
    if(food.count > FOODRESETCOUNT) {
        oldx = food.c_x; oldy = food.c_y;
        food_reset();
        incremental_draw(h_vdi, play, food.c_x, food.c_y);
        incremental_draw(h_vdi, play, oldx, oldy);
        return FOODRESETCOUNT;
    }
    return 0;
}

void field_init() 
{
char i,j;

    for(j=0;j<CELLH;j++) {
        field[0][j] = CELLWALL;
        field[CELLW-1][j] = CELLWALL;
    }

    for(i=1;i<CELLW-1;i++) {
        field[i][0] = CELLWALL;
        field[i][CELLH-1] = CELLWALL; 
        for(j=1;j<CELLH-2;j++)
            field[i][j] = CELLBLANK;
    }

}

int check_wall(WPLAYER *player) 
{
    return field[player->head->c_x][player->head->c_y] == CELLWALL ? DEAD : ALIVE;  
}

static void getcellrect(char cx, char cy, int xoffset, int yoffset, 
                 int cellwidth, int cellheight, GRECT *rc)
{
    rc->g_x = xoffset + cx*cellwidth;
    rc->g_y = yoffset + cy*cellheight;
    rc->g_w = cellwidth;
    rc->g_h = cellheight;
}

void drawcell(WORD h_vdi, int type, char cx, char cy, 
              int xoffset, int yoffset, int cellwidth, int cellheight)
{
WORD pts[4];
int vx,vy;

    vx = xoffset + cx*cellwidth;
    vy = yoffset + cy*cellheight;

    switch(type) {
        case CELLWALL:
            vsf_color(h_vdi, (WORD)RED);
            pts[0] = vx; pts[1] = vy;
            pts[2] = vx+cellwidth; pts[3] = vy+cellheight;
            v_bar(h_vdi,pts);
            break;
        case CELLBLANK:
            vsf_color(h_vdi, (WORD)WHITE);
            pts[0] = vx; pts[1] = vy;
            pts[2] = vx+cellwidth; pts[3] = vy+cellheight;
            v_bar(h_vdi,pts);
            break;
        case CELLPLAYER:
            vsf_color(h_vdi,(WORD)BLACK);
            v_ellipse(h_vdi,(WORD)(vx+cellwidth/2),
                     (WORD)(vy+cellheight/2),
                 (WORD)(max(cellwidth/2-1,1)),
                 (WORD)(max(cellheight/2-1,1)));
            break;
        case CELLFOOD:
            vsf_color(h_vdi,(WORD)GREEN);
            v_ellipse(h_vdi,(WORD)(vx+cellwidth/2),
                     (WORD)(vy+2*cellheight/3),
                 (WORD)(cellwidth/2),
                 (WORD)(cellheight/3));
            vsf_color(h_vdi,(WORD)BLACK);
            v_pieslice(h_vdi,(WORD)(vx+cellwidth/2),
                     (WORD)(vy+cellheight/2),
                 (WORD)(cellheight/2),
                 (WORD)(500),
                 (WORD)(1300));
            break;
    }
    
}

void incremental_draw(WORD h_vdi, GRECT *play, int x, int y)
{
int cellwidth, cellheight;

WUNIT *walker;

    cellwidth = max(1,play->g_w/CELLW);
    cellheight = max(1,play->g_h/CELLH);

    drawcell(h_vdi, field[x][y], 
             x, y, 
             play->g_x, play->g_y,
             cellwidth, cellheight);
}

void draw_field(WORD h_vdi, GRECT *play, GRECT *dirty)
{
char i,j;
WORD pts[4];
int cellwidth, cellheight;
GRECT crc;

#ifdef VDEBUG
    printf("%d: %d %d %d %d\n",h_vdi,play->g_x,play->g_y,play->g_w,play->g_h);
#endif

    vsf_interior(h_vdi, 1);

    /* Fill background */
    vsf_color(h_vdi, (WORD)WHITE);
    
    if(dirty == NULL) {
        pts[0] = play->g_x; pts[1] = play->g_y;
        pts[2] = play->g_x+play->g_w; pts[3] = play->g_y+play->g_h;
    } else {
        pts[0] = dirty->g_x; pts[1] = dirty->g_y;
        pts[2] = dirty->g_x+dirty->g_w; pts[3] = dirty->g_y+dirty->g_h;
    }
    v_bar(h_vdi,pts);

    cellwidth = max(1,play->g_w/CELLW);
    cellheight = max(1,play->g_h/CELLH);
    
    /* Draw all cells */
    for(i=0;i<CELLW;i++) {
        for(j=0;j<CELLH;j++) {
            if(field[i][j] != CELLBLANK) {
                if(dirty == NULL)
                    drawcell(h_vdi, field[i][j], i, j, 
                             play->g_x, play->g_y,
                             cellwidth, cellheight);
                else {
                    getcellrect(i, j, 
                                play->g_x, play->g_y,
                                cellwidth, cellheight,
                                &crc);
                                
                    /* Only draw if inside the dirty */
                    if(rc_intersect(dirty, &crc)) {
                        drawcell(h_vdi, field[i][j], i, j, 
                                 play->g_x, play->g_y,
                                 cellwidth, cellheight);

                    }
                }
            }
        }
    }

    /* Done! */

}


int update_field(WPLAYER *player) 
{
WUNIT *walker;
int da;
    
    /* Check the wall now */
    da = check_wall(player);
    
    /* First empty the player from the field */
    empty_field(CELLPLAYER);
    
    /* Now reinsert all player values */
    walker = player->head;
    while(walker != NULL) {
        field[walker->c_x][walker->c_y] = CELLPLAYER;
        walker = (WUNIT *)walker->next;
    }
    return da;
}