/* GEM Worm * Copyright (C) 2009 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 . * * A full version of the license terms is available in LICENSE.txt. */ #include #ifdef __GNUC__ #include #else #include #include #include #endif #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) xhead->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 0; } void field_init() { char i,j; for(j=0;jhead->c_x][player->head->c_y] == CELLWALL ? DEAD : ALIVE; } 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) { char i,j; WORD pts[4]; int cellwidth, cellheight; #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); 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; 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;ig_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; }