summaryrefslogtreecommitdiff
path: root/src/elements
diff options
context:
space:
mode:
authorjacksonmj <mj-pt@jacksonmj.co.uk>2011-06-01 00:07:04 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2011-06-02 19:19:28 (GMT)
commit744854ef3691002b7049cb5c48fb89f86bed5320 (patch)
tree50f02c6d787fdb065a1a1dca8211495e7942e8fd /src/elements
parent099ab10a3775cd7746d8796f689ea2f81dbbc29b (diff)
downloadpowder-744854ef3691002b7049cb5c48fb89f86bed5320.zip
powder-744854ef3691002b7049cb5c48fb89f86bed5320.tar.gz
Speed up update_PIPE
By making it call rand() once instead of 6 times.
Diffstat (limited to 'src/elements')
-rw-r--r--src/elements/pipe.c163
1 files changed, 85 insertions, 78 deletions
diff --git a/src/elements/pipe.c b/src/elements/pipe.c
index 849d2ea..bc7ff00 100644
--- a/src/elements/pipe.c
+++ b/src/elements/pipe.c
@@ -1,8 +1,78 @@
#include <element.h>
+signed char pos_1_rx[] = {-1,-1,-1, 0, 0, 1, 1, 1};
+signed char pos_1_ry[] = {-1, 0, 1,-1, 1,-1, 0, 1};
+
int update_PIPE(UPDATE_FUNC_ARGS) {
- int r, rx, ry, np, trade, q, ctype;
- if (!parts[i].ctype && parts[i].life<=10)
+ int r, rx, ry, np;
+ if (parts[i].ctype>=2 && parts[i].ctype<=4)
+ {
+ if (parts[i].life==3)
+ {
+ // make automatic pipe pattern
+ for (rx=-1; rx<2; rx++)
+ for (ry=-1; ry<2; ry++)
+ if (x+rx>=0 && y+ry>0 && x+rx<XRES && y+ry<YRES && (rx || ry))
+ {
+ r = pmap[y+ry][x+rx];
+ if ((r>>8)>=NPART || !r)
+ continue;
+ if ((r&0xFF)==PT_PIPE&&parts[r>>8].ctype==1)
+ {
+ parts[r>>8].ctype = (((parts[i].ctype)%3)+2);//reverse
+ parts[r>>8].life = 6;
+ }
+ }
+ }
+ else
+ {
+ int rndstore, rnd, q, ctype=(((parts[i].ctype)%3)+2);
+ rndstore = rand();
+ // RAND_MAX is at least 32767 on all platforms i.e. pow(8,5)-1
+ // so can go 5 cycles without regenerating rndstore
+ for (q=0; q<3; q++)
+ {
+ rnd = rndstore&7;
+ rndstore = rndstore>>3;
+ rx = pos_1_rx[rnd];
+ ry = pos_1_ry[rnd];
+ if (x+rx>=0 && y+ry>=0 && x+rx<XRES && y+ry<YRES)
+ {
+ r = pmap[y+ry][x+rx];
+ if ((r>>8)>=NPART)
+ continue;
+ else if (!r&&parts[i].tmp!=0)
+ {
+ np = create_part(-1,x+rx,y+ry,parts[i].tmp);
+ if (np!=-1)
+ {
+ parts[np].temp = parts[i].temp;//pipe saves temp and life now
+ parts[np].life = parts[i].flags;
+ }
+ parts[i].tmp = 0;
+ continue;
+ }
+ else if (!r)
+ continue;
+ else if (parts[i].tmp == 0 && (ptypes[r&0xFF].falldown!= 0 || ptypes[r&0xFF].state == ST_GAS))
+ {
+ parts[i].tmp = parts[r>>8].type;
+ parts[i].temp = parts[r>>8].temp;
+ parts[i].flags = parts[r>>8].life;
+ kill_part(r>>8);
+ }
+ else if ((r&0xFF)==PT_PIPE && parts[r>>8].ctype!=ctype && parts[r>>8].tmp==0&&parts[i].tmp>0)
+ {
+ parts[r>>8].tmp = parts[i].tmp;
+ parts[r>>8].temp = parts[i].temp;
+ parts[r>>8].flags = parts[i].flags;
+ parts[i].tmp = 0;
+ }
+ }
+ }
+ }
+ }
+ else if (!parts[i].ctype && parts[i].life<=10)
{
if (parts[i].temp<272.15)//manual pipe colors
{
@@ -24,13 +94,12 @@ int update_PIPE(UPDATE_FUNC_ARGS) {
}
else
{
+ // make a border
for (rx=-2; rx<3; rx++)
for (ry=-2; ry<3; ry++)
if (x+rx>=0 && y+ry>0 && x+rx<XRES && y+ry<YRES && (rx || ry))
{
r = pmap[y+ry][x+rx];
- if ((r>>8)>=NPART )
- continue;
if (!r)
create_part(-1,x+rx,y+ry,PT_BRCK);//BRCK border, people didn't like DMND
}
@@ -38,85 +107,23 @@ int update_PIPE(UPDATE_FUNC_ARGS) {
parts[i].ctype = 1;
}
}
- if (parts[i].ctype==1)//waiting for empty space
+ else if (parts[i].ctype==1)//wait for empty space before starting to generate automatic pipe pattern
{
- for (rx=-1; rx<2; rx++)
- for (ry=-1; ry<2; ry++)
- if (x+rx>=0 && y+ry>0 && x+rx<XRES && y+ry<YRES && (rx || ry))
- {
- r = pmap[y+ry][x+rx];
- if ((r>>8)>=NPART)
- continue;
- if (!r&&!parts[i].life)
- parts[i].life=50;
- }
- if (parts[i].life==2)
+ if (!parts[i].life)
+ {
+ for (rx=-1; rx<2; rx++)
+ for (ry=-1; ry<2; ry++)
+ if (x+rx>=0 && y+ry>0 && x+rx<XRES && y+ry<YRES && (rx || ry))
+ {
+ if (!pmap[y+ry][x+rx])
+ parts[i].life=50;
+ }
+ }
+ else if (parts[i].life==2)
{
parts[i].ctype = 2;
parts[i].life = 6;
}
}
- if (parts[i].ctype>1)
- for ( q = 0; q<3; q++)
- for ( ctype = 2; ctype<5; ctype++)
- {
- if (parts[i].ctype==ctype)
- {
- if (parts[i].life==3)
- {
- for (rx=-1; rx<2; rx++)
- for (ry=-1; ry<2; ry++)
- if (x+rx>=0 && y+ry>0 && x+rx<XRES && y+ry<YRES && (rx || ry))
- {
- r = pmap[y+ry][x+rx];
- if ((r>>8)>=NPART || !r)
- continue;
- if ((r&0xFF)==PT_PIPE&&parts[r>>8].ctype==1)
- {
- parts[r>>8].ctype = (((ctype)%3)+2);//reverse
- parts[r>>8].life = 6;
- }
- }
- }
- else
- {
- rx = rand()%3-1;
- ry = rand()%3-1;
- if (x+rx>=0 && y+ry>0 && x+rx<XRES && y+ry<YRES && (rx || ry))
- {
- r = pmap[y+ry][x+rx];
- if ((r>>8)>=NPART)
- continue;
- else if (!r&&parts[i].tmp!=0)
- {
- np = create_part(-1,x+rx,y+ry,parts[i].tmp);
- if (np!=-1)
- {
- parts[np].temp = parts[i].temp;//pipe saves temp and life now
- parts[np].life = parts[i].flags;
- }
- parts[i].tmp = 0;
- continue;
- }
- else if (!r)
- continue;
- else if (parts[i].tmp == 0 && (ptypes[r&0xFF].falldown!= 0 || ptypes[r&0xFF].state == ST_GAS))
- {
- parts[i].tmp = parts[r>>8].type;
- parts[i].temp = parts[r>>8].temp;
- parts[i].flags = parts[r>>8].life;
- kill_part(r>>8);
- }
- else if ((r&0xFF)==PT_PIPE && parts[r>>8].ctype!=(((ctype)%3)+2) && parts[r>>8].tmp==0&&parts[i].tmp>0)
- {
- parts[r>>8].tmp = parts[i].tmp;
- parts[r>>8].temp = parts[i].temp;
- parts[r>>8].flags = parts[i].flags;
- parts[i].tmp = 0;
- }
- }
- }
- }
- }
return 0;
}