/*
    Living Realms is a primate evolution simulator.
    Copyright (C) 2011  Sterling Pickens

    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/>.
*/


void SET_ZERO(unsigned char **array, size_t src_y, size_t src_x){
	size_t byte_x = src_x/8;
	unsigned char bit = src_x % 8;
	unsigned char cur = array[src_y][byte_x];
	unsigned char b0 = !(bit == 0);
	unsigned char b1 = !(bit == 1);
	unsigned char b2 = !(bit == 2);
	unsigned char b3 = !(bit == 3);
	unsigned char b4 = !(bit == 4);
	unsigned char b5 = !(bit == 5);
	unsigned char b6 = !(bit == 6);
	unsigned char b7 = !(bit == 7);

	if(b0) //if not writing this bit
		b0 = (((cur<<7) & 128) >= 128);
	if(b1)
		b1 = (((cur<<6) & 128) >= 128);
	if(b2)
		b2 = (((cur<<5) & 128) >= 128);
	if(b3)
		b3 = (((cur<<4) & 128) >= 128);
	if(b4)
		b4 = (((cur<<3) & 128) >= 128);
	if(b5)
		b5 = (((cur<<2) & 128) >= 128);
	if(b6)
		b6 = (((cur<<1) & 128) >= 128);
	if(b7)
		b7 = (((cur) & 128)  >= 128);

	array[src_y][byte_x] = (b7*128)+(b6*64)+(b5*32)+(b4*16)+(b3*8)+(b2*4)+(b1*2)+b0;
}

void SET_ONE(unsigned char **array, size_t src_y, size_t src_x){
	size_t byte_x = src_x/8;
	unsigned char bit = src_x % 8;
	unsigned char cur = array[src_y][byte_x];
	unsigned char b0 = (bit == 0);
	unsigned char b1 = (bit == 1);
	unsigned char b2 = (bit == 2);
	unsigned char b3 = (bit == 3);
	unsigned char b4 = (bit == 4);
	unsigned char b5 = (bit == 5);
	unsigned char b6 = (bit == 6);
	unsigned char b7 = (bit == 7);

	if(!b0) //if not writing this bit
		b0 = (((cur<<7) & 128) >= 128);
	if(!b1)
		b1 = (((cur<<6) & 128) >= 128);
	if(!b2)
		b2 = (((cur<<5) & 128) >= 128);
	if(!b3)
		b3 = (((cur<<4) & 128) >= 128);
	if(!b4)
		b4 = (((cur<<3) & 128) >= 128);
	if(!b5)
		b5 = (((cur<<2) & 128) >= 128);
	if(!b6)
		b6 = (((cur<<1) & 128) >= 128);
	if(!b7)
		b7 = (((cur) & 128) >= 128);

	array[src_y][byte_x] = (b7*128)+(b6*64)+(b5*32)+(b4*16)+(b3*8)+(b2*4)+(b1*2)+b0;
}

char IS_SET(unsigned char **array, size_t src_y, size_t src_x){
	unsigned char bit = src_x % 8;
	unsigned char cur = array[src_y][src_x/8];
	cur <<= (7-bit);
	cur &= 128;

	if(cur >= 128)
		return 1;
	else
		return 0;
}
