c - Minecraft Flood fill -
i trying calculate minecraft's light values, algorithm use slow.
what better way calculate lighting array?
the code looks this:
struct chunk_data { char light[16*16*256]; }; int j; void fill(chunk_data* c, int i, int l) { ++j; if(c->light[i] > l) return; c->light[i] = l; if(!--l) return; if((i&0x0f) != 0x0f) fill(c, + 0x01, l); if((i&0x0f) != 0x00) fill(c, - 0x01, l); if((i&0xf0) != 0xf0) fill(c, + 0x10, l); if((i&0xf0) != 0x00) fill(c, - 0x10, l); if((i&0xff00) != 0x0000) fill(c, - 0x0100, l); if((i&0xff00) != 0xff00) fill(c, + 0x0100, l); }
when move check before call recursion, number of stack pushes reduced. reduces running time 250ms 23us.
improved code:
void fill(chunk_data* c, int i, int l) { c->light[i] = l; if(!--l) return; if((i&0x0f) != 0x0f && c->light[i + 0x01] < l) fill(c, + 0x01, l); if((i&0x0f) != 0x00 && c->light[i - 0x01] < l) fill(c, - 0x01, l); if((i&0xf0) != 0xf0 && c->light[i + 0x10] < l) fill(c, + 0x10, l); if((i&0xf0) != 0x00 && c->light[i - 0x10] < l) fill(c, - 0x10, l); if((i&0xff00) != 0x0000 && c->light[i - 0x0100] < l) fill(c, - 0x0100, l); if((i&0xff00) != 0xff00 && c->light[i + 0x0100] < l) fill(c, + 0x0100, l); }
Comments
Post a Comment