BlitzSand Final Source

0 views
Skip to first unread message

inferno

unread,
Sep 2, 2008, 8:55:09 PM9/2/08
to Falling Sand Games Development
For those that are interested, here is BlitzSand's source, in all its
horrifying, incomplete horror:

/* By viewing and/or using the following code, you adhere to the
following:
1. Any and all code within this will not be used to compile a program
for any Apple-related products, including hardware,
software, and any Apple-associated operating systems.
2. All software derived from the following will be open-source.
3. Credit will be given to this program's creator, inferno, AKA
Fission.*/
#include <allegro.h>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#define ELEMENTCOUNT 50
#define width 380
#define height 380
using namespace std;

int close_b = 0;
void close_button_handler(void){
close_b = 1;}

int FRAMES_PER_SECOND = 60;
unsigned int x,y;
int *draw = NULL,*brushsize = NULL,e,oldz,*test =
NULL,tmp,tmpx,tmpy,drawtype = 0,scrolly,clicked;
unsigned int oldx,i,oldy,frame = 1;
int vleft,vright,up,down,elem;
int spot,charspot;
char tline[1000];
// element[elementnumber]["0=element number" "1,2,3=color "4=weight"
"5=slide" "6=deathrate" "7 = dieto" "8 = visible" "9 = icontype" "10 =
file/text"]
// if element[x][8] = 0, then it will not be visible.
signed int element[ELEMENTCOUNT][10];
unsigned int color[ELEMENTCOUNT];
string elementname[ELEMENTCOUNT],overelement,scan;
// interaction[element1][element2][0 = rate, 1 = result1, 2 = result2]
// needs work, possibly interaction[element1][element2][0 = result1, 1
= result2][0 = rate]
int interaction[ELEMENTCOUNT][ELEMENTCOUNT][2];
int sandbox[1+width+height*width],*pressure = NULL;
unsigned int ELEMENT1 = 1,ELEMENT2 = 0,maxhistory = 50;
int history[(width+height*width)][50];

// control variables
unsigned int PHYSICS = 1, GRAVITY = 1,INTERACTIONS = 1,REVERSE =
0,BORDER = 0;
int dir = 1,bias=1,X,Y,*pressed = NULL;

// Error handling

void error(int errnum,BITMAP *bmp,char str,int num){
switch(errnum){
case 0:
break;
default:
// Not a real error!
break;
}
}

// Mod loading
void loadmod() {/*

}

void historysave(BITMAP *bmp) {
if(frame <= maxhistory){
for(y = 0;++y < height;){
for(x = 0;++x < width;){
history[x+y*width][frame] = sandbox[x+y*width];
}}
++frame;
}
x = 0;y = 0;
}

void historyload(BITMAP *bmp) {
if(frame > 1){
--frame;
for(y = 0;++y < height;){
for(x = 0;++x < width;){
sandbox[x+y*width] = history[x+y*width][frame];
bmp->line[y][x] = color[sandbox[x+y*width]];
history[x+y*width][frame] = 0;
}}
}
x = 0;y = 0;
}

void drawrect(BITMAP *bmp,int x,int y,int d){
for(tmpx = x - *brushsize;tmpx <= x + *brushsize;tmpx++){
for(tmpy = y - *brushsize;tmpy <= y + *brushsize;tmpy++){
if(is_inside_bitmap(bmp,tmpx,tmpy,0)){
sandbox[tmpx+tmpy*width] = d;
bmp->line[tmpy][tmpx] = color[d];
}}}
}

void drawcircle(BITMAP *bmp,int x,int y,int d){
if(is_inside_bitmap(bmp,x,y,0)){
sandbox[x+y*width] = d;
bmp->line[y][x] = color[d];}
}

void drawcircle2(BITMAP *bmp,int x,int y,int d){
for(tmp=0;++tmp<=*brushsize;)
do_circle(bmp,x,y,tmp,d,drawcircle);
}

volatile int last_fps;
volatile int fps;
volatile int wait;
void fps_timer(){
last_fps=fps;
fps=0;
}
END_OF_FUNCTION(fps_timer);
void increment_speed_counter(){
++wait;
}
END_OF_FUNCTION(increment_speed_counter);

int checkgrav(BITMAP *canvas,int x,int y,int elem){

if(element[elem][4] != 0){
if(rand()%1000 < element[elem][4]-element[down][4] && element[down]
[4] != 0){
sandbox[x+y*width]=down;sandbox[x+(y+dir)*width]=elem;
return dir;} else
if(-(rand()%1000) > element[elem][4]-element[up][4] && element[up][4] !
= 0){
sandbox[x+y*width]=up;sandbox[x+(y-dir)*width]=elem;
return -dir;}else return 0;
}else return 0;
}

int checkslip(BITMAP *canvas,int x,int y,int elem){
if(is_inside_bitmap(canvas,x-1,y,0)) canvas->line[y]
[x-1]=color[sandbox[(x-1)+y*width]];
if(is_inside_bitmap(canvas,x+1,y,0)) canvas->line[y][x
+1]=color[sandbox[(x+1)+y*width]];
if(element[elem][5] !=0 && element[elem][4] != 0){
switch(rand()%2){
case 0:
if(rand()%32768>(element[elem][5]-element[vleft][5]) && element[vleft]
[4] != 0 && element[vleft][5] != 0){
sandbox[x+y*width]=vleft;sandbox[(x-1)+y*width]=elem;
return -1;}else return 0;
break;
case 1:
if(rand()%32768>(element[elem][5]-element[vright][5]) &&
element[vright][4] != 0 && element[vright][5] != 0){
sandbox[x+y*width]=vright;sandbox[(x+1)+y*width]=elem;
return 1;}else return 0;
break;
}
}else return 0;
}

void checkinteraction(BITMAP *canvas,int elem,int x,int y,int test,int
tmp){
e=sandbox[(x+test)+(y+tmp)*width];
if(rand()%32768<interaction[elem][e][0]){
sandbox[x+y*width]=interaction[elem][e][1];sandbox[(x+test)+(y
+tmp)*width]=interaction[elem][e][2];
if(is_inside_bitmap(canvas,x+test,y+tmp,0)) canvas->line[y+tmp][x
+test]=color[interaction[elem][e][2]];}
}

void physics(int x,int y,BITMAP *canvas){
// Saves some CPU usage
elem = sandbox[x+y*width];
vleft = sandbox[(x-1)+y*width];
vright = sandbox[(x+1)+y*width];
down = sandbox[x+(y+dir)*width];
up = sandbox[x+(y-dir)*width];

canvas->line[y][x] = color[sandbox[x+y*width]]; // Video memory
pointer for speed
// PHYSICS

// Borders
switch(BORDER){
case 0:sandbox[x+height*width] = 0;sandbox[x] = 0;break;
case 1:sandbox[x+height*width] = 1;sandbox[x] = 1;break;
case 2:sandbox[x+height*width] = sandbox[x];sandbox[x] = sandbox[x
+y*height];break;
}
// Vertical/horizontal movement
/*
if(GRAVITY != 0)
switch(rand()%2){
case 0:
y+=checkgrav(canvas,x,y,*elem);
x+=checkslip(canvas,x,y,*elem);
break;
case 1:
x+=checkslip(canvas,x,y,*elem);
y+=checkgrav(canvas,x,y,*elem);
break;
}*/

switch(rand()%2){
case 0:
if(GRAVITY != 0){
switch(rand()%2){
case 0:
y+=checkgrav(canvas,x,y,sandbox[x+y*width]);
if(is_inside_bitmap(canvas,x,y+dir,0)) canvas->line[y+dir]
[x]=color[sandbox[x+(y+dir)*width]];
if(is_inside_bitmap(canvas,x,y-dir,0)) canvas->line[y-dir]
[x]=color[sandbox[x+(y-dir)*width]];
vleft = sandbox[(x-1)+y*width];
vright = sandbox[(x+1)+y*width];
checkslip(canvas,x,y,sandbox[x+y*width]);
if(is_inside_bitmap(canvas,x-1,y,0)) canvas->line[y]
[x-1]=color[sandbox[(x-1)+y*width]];
if(is_inside_bitmap(canvas,x+1,y,0)) canvas->line[y][x
+1]=color[sandbox[(x+1)+y*width]];
break;
case 1:
x+=checkslip(canvas,x,y,sandbox[x+y*width]);
if(is_inside_bitmap(canvas,x-1,y,0)) canvas->line[y]
[x-1]=color[sandbox[(x-1)+y*width]];
if(is_inside_bitmap(canvas,x+1,y,0)) canvas->line[y][x
+1]=color[sandbox[(x+1)+y*width]];
down = sandbox[x+(y+dir)*width];
up = sandbox[x+(y-dir)*width];
checkgrav(canvas,x,y,sandbox[x+y*width]);
if(is_inside_bitmap(canvas,x,y+dir,0)) canvas->line[y+dir]
[x]=color[sandbox[x+(y+dir)*width]];
if(is_inside_bitmap(canvas,x,y-dir,0)) canvas->line[y-dir]
[x]=color[sandbox[x+(y-dir)*width]];
break;
}
}
break;

case 1:
// Pressure
// Not even started yet... -__-
// Interactions
if(rand()%32768 < element[elem][6]){
sandbox[x+y*width]=element[elem][7];} else
if(INTERACTIONS != 0){
switch(rand()%6){
case 0:
case 4:
checkinteraction(canvas,elem,x,y,-1,0);
checkinteraction(canvas,elem,x,y,1,0);
break;
case 1:
case 5:
checkinteraction(canvas,elem,x,y,1,0);
checkinteraction(canvas,elem,x,y,-1,0);
break;
case 2:
checkinteraction(canvas,elem,x,y,0,1);
checkinteraction(canvas,elem,x,y,0,-1);
break;
case 3:
checkinteraction(canvas,elem,x,y,0,-1);
checkinteraction(canvas,elem,x,y,0,1);
break;
}
}
break;
}

canvas->line[y][x] = color[sandbox[x+y*width]]; // Video memory
pointer for speed
}

int main(){
allegro_init();
install_keyboard();
install_mouse();
install_timer();
install_int_ex(fps_timer,SECS_TO_TIMER(1));
install_int_ex(increment_speed_counter,BPS_TO_TIMER(FRAMES_PER_SECOND));
set_color_depth(8);
set_gfx_mode(GFX_AUTODETECT_WINDOWED,500,500,0,0);
set_close_button_callback(close_button_handler);
set_window_title("BlitzSand by inferno - 5.0 Alpha");
set_display_switch_mode(SWITCH_BACKGROUND);
show_mouse(screen);

BITMAP *buffer = create_system_bitmap(500,500);
BITMAP *canvas = create_bitmap(width,height);
BITMAP *elementbar = create_system_bitmap(20,380);
BITMAP *menu[21];
BITMAP *elementicon[ELEMENTCOUNT];
menu[0] = load_bmp("img/cls.bmp", 0);
menu[1] = load_bmp("img/load.bmp",0);
menu[2] = load_bmp("img/save.bmp",0);
menu[5] = load_bmp("img/reverse.bmp",0);
menu[6] = load_bmp("img/physics.bmp",0);
menu[7] = load_bmp("img/gravity.bmp",0);
menu[8] = load_bmp("img/rect.bmp",0);
menu[9] = load_bmp("img/circle.bmp",0);
menu[10] = load_bmp("img/interactions.bmp",0);
menu[11] = load_bmp("img/slider.bmp",0);
menu[14] = load_bmp("img/borderopen.bmp",0);
menu[15] = load_bmp("img/borderclose.bmp",0);
menu[16] = load_bmp("img/fpsslider.bmp",0);
menu[17] = load_bmp("img/slow.bmp",0);
menu[18] = load_bmp("img/pause.bmp",0);
menu[19] = load_bmp("img/play.bmp",0);
menu[20] = load_bmp("img/fast.bmp",0);
BITMAP *statusbar = create_bitmap(500,10);
clear(buffer);clear(canvas);
pressure = new int[1+width+height*width];
draw = new int[3];
pressed = new int[10];
overelement = new char[ELEMENTCOUNT];
brushsize = new int(15);
test = new int;

for(x = 0;++x < width;){for(y = 0;++y < height;){
sandbox[x + y * width] = element[0][0];
}}

element[0][1] = 0;
element[0][2] = 0;
element[0][3] = 0;
element[0][4] = 1;
element[0][5] = 1;
element[0][6] = 0;
elementname[0]="Clear";
elementicon[0] = load_bitmap("img/clear.bmp",0);

element[1][1] = 120;
element[1][2] = 120;
element[1][3] = 120;
element[1][4] = 0;
element[1][5] = 0;
element[1][6] = 0;
element[1][7] = 0;
elementname[1]="Wall";

element[2][1] = 255;
element[2][2] = 0;
element[2][3] = 0;
element[2][4] = -10000;
element[2][5] = 10000;
element[2][6] = 1000;
elementname[2]="Fire";

element[3][1] = 0;
element[3][2] = 0;
element[3][3] = 255;
element[3][4] = 10000;
element[3][5] = 20000;
element[3][6] = 0;
elementname[3]="Water";

element[4][1] = 40;
element[4][2] = 40;
element[4][3] = 255;
element[4][4] = -10000;
element[4][5] = 10000;
element[4][6] = 1000;
element[4][7] = 3;
elementname[4]="Steam";

element[5][1] = 0;
element[5][2] = 255;
element[5][3] = 0;
element[5][4] = 0;
element[5][5] = 0;
elementname[5]="Plant";

element[6][1] = 255;
element[6][2] = 0;
element[6][3] = 100;
element[6][6] = 4000;
element[6][7] = 7;
elementname[6]="???";

element[7][1] = 251;
element[7][2] = 0;
element[7][3] = 0;
element[7][4] = 0;
element[7][5] = 0;
element[7][6] = 1000;
element[7][7] = 0;
elementname[7]="?";

element[8][1] = 230;
element[8][2] = 180;
element[8][3] = 0;
element[8][4] = 25000;
element[8][5] = 1000;
element[8][6] = 0;
elementname[8]="Sand";

interaction[2][3][0] = 1000;
interaction[2][3][2] = 4;

interaction[2][5][0] = 30000;
interaction[2][5][1] = 2;
interaction[2][5][2] = 2;

interaction[3][5][0] = 10000;
interaction[3][5][1] = 5;
interaction[3][5][2] = 5;

interaction[6][0][0] = 32768;
interaction[6][0][1] = 6;
interaction[6][0][2] = 6;

interaction[6][2][0] = 32768;
interaction[6][2][1] = 6;
interaction[6][2][2] = 6;

interaction[6][4][0] = 32000;
interaction[6][4][1] = 6;
interaction[6][4][2] = 6;

interaction[6][8][0] = 32768;
interaction[6][8][1] = 6;
interaction[6][8][2] = 6;

interaction[7][6][0] = 10000;
interaction[7][6][1] = 7;
interaction[7][6][2] = 7;

draw_sprite(buffer,menu[0],401,0);
draw_sprite(buffer,menu[1],421,0);
draw_sprite(buffer,menu[2],441,0);
draw_sprite(buffer,menu[5],401,80);
draw_sprite(buffer,menu[6],421,20);
draw_sprite(buffer,menu[7],441,20);
draw_sprite(buffer,menu[8],10,410);
draw_sprite(buffer,menu[9],30,410);
draw_sprite(buffer,menu[10],461,20);
draw_sprite(buffer,menu[14],421,80);
// draw_sprite(buffer,menu[16],401,60);
draw_sprite(buffer,menu[17],421,40);
draw_sprite(buffer,menu[18],441,40);
draw_sprite(buffer,menu[19],461,40);
draw_sprite(buffer,menu[20],481,40);

for(tmp=0;++tmp <= ELEMENTCOUNT;){
element[tmp][0] = tmp;
color[tmp] = makecol(element[tmp][1],element[tmp][2],element[tmp][3]);
/*if(element[tmp][8]==0){
draw_sprite(elementbar,elementicon[tmp],0,scrolly);
scrolly+=21;}*/
}

while(!key[KEY_ESC] && !close_b){
if(wait > 0 && FRAMES_PER_SECOND > 0){
if(PHYSICS != 0){

for(int var=0;++var<4;)
if(bias==0){
for(y=0;++y!=height;)
for(x=1;++x<width;)
if(sandbox[x+y*width] != 0 && is_inside_bitmap(canvas,x,y,0))
physics(x,y,canvas);
}else if(bias==1){
for(y=height;--y!=0;)
for(x=width;--x>0;)
if(sandbox[x+y*width] != 0 && is_inside_bitmap(canvas,x,y,0))
physics(x,y,canvas);
}

// END OF PHYSICS

bias=1-bias;
}
++fps;--wait;
}

/* ====mouse/key input==== */

// Always check
if(is_inside_bitmap(canvas,mouse_x-21,mouse_y,0)){
overelement =
elementname[sandbox[(mouse_x-21)+mouse_y*width]];
}
else {
overelement = "Nothing";
}

// Any click
if(mouse_b){
if(is_inside_bitmap(menu[11],mouse_x-10,mouse_y-440,0)){
*brushsize = (mouse_x-10);}
if(clicked == 0){ // Prevents multiple occurences per click
if(is_inside_bitmap(canvas,mouse_x-21,mouse_y,0)){
historysave(canvas);} // History saving
if(is_inside_bitmap(menu[5],mouse_x-401,mouse_y-80,0)){
REVERSE = 1 - REVERSE;
if(REVERSE == 1){dir = -1;} else {dir = 1;}
}
if(is_inside_bitmap(menu[6],mouse_x-421,mouse_y-20,0)){
PHYSICS = 1 - PHYSICS;}
if(is_inside_bitmap(menu[7],mouse_x-441,mouse_y-20,0)){
GRAVITY = 1 - GRAVITY;}
if(is_inside_bitmap(menu[10],mouse_x-461,mouse_y-20,0)){
INTERACTIONS = 1 - INTERACTIONS;}
if(is_inside_bitmap(menu[17],mouse_x-421,mouse_y-40,0)){
FRAMES_PER_SECOND = 30;install_int_ex(increment_speed_counter,
BPS_TO_TIMER(FRAMES_PER_SECOND));}
if(is_inside_bitmap(menu[18],mouse_x-441,mouse_y-40,0)){
FRAMES_PER_SECOND = 0;}
if(is_inside_bitmap(menu[19],mouse_x-461,mouse_y-40,0)){
FRAMES_PER_SECOND = 60;install_int_ex(increment_speed_counter,
BPS_TO_TIMER(FRAMES_PER_SECOND));}
if(is_inside_bitmap(menu[20],mouse_x-481,mouse_y-40,0)){
FRAMES_PER_SECOND = 9999;install_int_ex(increment_speed_counter,
BPS_TO_TIMER(FRAMES_PER_SECOND));}
if(is_inside_bitmap(menu[13],mouse_x,mouse_y-380,0) && scrolly >= 0 &&
scrolly <= 400){
++scrolly;}
if(is_inside_bitmap(menu[14],mouse_x-421,mouse_y-80,0)){
BORDER = 1 - BORDER;}
if(is_inside_bitmap(menu[12],mouse_x,mouse_y,0) && scrolly <= 400 &&
scrolly >= 0){
--scrolly;}
}clicked = 1;
}else{clicked = 0;}

// left click
if(mouse_b & 1)
{
if(is_inside_bitmap(canvas,mouse_x-21,mouse_y,0)){
if(drawtype == 0){
do_line(canvas,mouse_x-20,mouse_y,mouse_x-21,mouse_y,ELEMENT1,drawcircle2);
if(mouse_x-21 != oldx || mouse_y != oldy){
do_line(canvas,oldx,oldy,mouse_x-21,mouse_y,ELEMENT1,drawcircle2);
}}
if(drawtype == 1){
drawrect(canvas,mouse_x-21,mouse_y,ELEMENT1);
if(mouse_x-21 != oldx || mouse_y != oldy){
do_line(canvas,oldx,oldy,mouse_x-21,mouse_y,ELEMENT1,drawrect);
}
}}
if(is_inside_bitmap(menu[0],mouse_x-401,mouse_y,0)){
clear(canvas);
for(x = 0;++x <= width;){for(y = 0;++y <= height;){
sandbox[x+y*width] = 0;}}
}
if(is_inside_bitmap(menu[8],mouse_x-10,mouse_y-410,0)){
drawtype = 1;}
if(is_inside_bitmap(menu[9],mouse_x-30,mouse_y-410,0)){
drawtype = 0;}

// right click
} else if(mouse_b & 2){
if(is_inside_bitmap(canvas,mouse_x-21,mouse_y,0)){
if(drawtype == 0){
do_line(canvas,mouse_x-20,mouse_y,mouse_x-21,mouse_y,ELEMENT2,drawcircle2);
if(mouse_x-21 != oldx || mouse_y != oldy){
do_line(canvas,oldx,oldy,mouse_x-21,mouse_y,ELEMENT2,drawcircle2);
}
}
if(drawtype == 1){
drawrect(canvas,mouse_x-21,mouse_y,ELEMENT2);
if(mouse_x-21 != oldx || mouse_y != oldy){
do_line(canvas,oldx,oldy,mouse_x-21,mouse_y,ELEMENT2,drawrect);
}}}
}

// Mouse wheel
if(mouse_z > oldz)
{*brushsize = (int)round(*brushsize * 11 / 10 + 1);}
if(mouse_z < oldz && *brushsize-1 > 0)
{*brushsize = (int)round(*brushsize * 10 / 11 - 1);}

// Keys
if(key[KEY_1])
{ELEMENT1 = 1;}
if(key[KEY_2])
{ELEMENT1 = 2;}
if(key[KEY_3])
{ELEMENT1 = 3;}
if(key[KEY_4])
{ELEMENT1 = 5;}
if(key[KEY_5])
{ELEMENT1 = 6;}
if(key[KEY_6])
{ELEMENT1 = 8;}
if(key[KEY_SPACE]){
loadmod();
}
if(key[KEY_BACKSPACE]){
if(pressed[1] == 0){
historyload(canvas);
pressed[1] = 1;}
}else {pressed[1] = 0;}

if(is_inside_bitmap(canvas,mouse_x-21,mouse_y,0)){
X = mouse_x-21;Y = mouse_y;}
else{
X = 0;Y = 0;}

oldz = mouse_z;
oldx = mouse_x-21;
oldy = mouse_y;

blit(canvas, buffer, 1, 1, 21, 1, width, height);
rectfill(statusbar,0,0,500,10,makecol(0,0,0));
textprintf_ex(statusbar, font,0,0,makecol(255,255,255), -1, "FPS:%i
X/Y:%i,%i Over:%s History:
%i",last_fps,X,Y,overelement.c_str(),maxhistory-frame+1);
draw_sprite(buffer,statusbar,0,490);
rect(buffer,21,0,width+19,height,makecol(255,255,255));
blit(elementbar,buffer,0,0,0,0,20,380);
draw_sprite(buffer,menu[11],10,440);
textprintf_ex(buffer, font,10,440,makecol(255,255,255), -1,
"%i",*brushsize);
blit(buffer,screen,0,0,0,0,500,500);
}
// End functions
destroy_bitmap(menu[0]);
destroy_bitmap(menu[1]);
destroy_bitmap(menu[2]);/*
destroy_bitmap(menu[3]);
destroy_bitmap(menu[4]);*/
destroy_bitmap(menu[5]);
destroy_bitmap(menu[6]);
destroy_bitmap(menu[7]);
destroy_bitmap(menu[8]);
destroy_bitmap(menu[9]);
destroy_bitmap(menu[10]);
destroy_bitmap(menu[11]);/*
destroy_bitmap(menu[12]);
destroy_bitmap(menu[13]);*/
destroy_bitmap(menu[14]);
destroy_bitmap(menu[15]);
destroy_bitmap(menu[16]);
destroy_bitmap(menu[17]);
destroy_bitmap(menu[18]);
destroy_bitmap(menu[19]);
destroy_bitmap(menu[20]);
destroy_bitmap(elementicon[0]);
destroy_bitmap(canvas);
destroy_bitmap(buffer);
destroy_bitmap(elementbar);
destroy_bitmap(statusbar);
delete [] pressure;
delete [] draw;
delete brushsize;
delete test;

return 0;
}
END_OF_MAIN()

tehCheese

unread,
Sep 2, 2008, 9:27:45 PM9/2/08
to Falling Sand Games Development
AHHH! ALLEGRO!!! BURN!!!
Reply all
Reply to author
Forward
0 new messages