/**
* Powder Toy - particle simulation
*
* 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 .
*/
#include
#include
#include
#include
#include
#include
#include "gravity.h"
#ifdef LUACONSOLE
#include
#endif
int wire_placed = 0;
int lighting_recreate = 0;
playerst player;
playerst player2;
playerst fighters[256]; //255 is the maximum number of fighters
unsigned char fighcount = 0; //Contains the number of fighters
particle *parts;
particle *cb_parts;
int airMode = 0;
unsigned char bmap[YRES/CELL][XRES/CELL];
unsigned char emap[YRES/CELL][XRES/CELL];
unsigned char cb_bmap[YRES/CELL][XRES/CELL];
unsigned char cb_emap[YRES/CELL][XRES/CELL];
int pfree;
unsigned pmap[YRES][XRES];
unsigned cb_pmap[YRES][XRES];
unsigned photons[YRES][XRES];
static int pn_junction_sprk(int x, int y, int pt)
{
unsigned r = pmap[y][x];
if ((r & 0xFF) != pt)
return 0;
r >>= 8;
if (parts[r].type != pt)
return 0;
if (parts[r].life != 0)
return 0;
parts[r].ctype = pt;
part_change_type(r,x,y,PT_SPRK);
parts[r].life = 4;
return 1;
}
static void photoelectric_effect(int nx, int ny)//create sparks from PHOT when hitting PSCN and NSCN
{
unsigned r = pmap[ny][nx];
if ((r&0xFF) == PT_PSCN) {
if ((pmap[ny][nx-1] & 0xFF) == PT_NSCN ||
(pmap[ny][nx+1] & 0xFF) == PT_NSCN ||
(pmap[ny-1][nx] & 0xFF) == PT_NSCN ||
(pmap[ny+1][nx] & 0xFF) == PT_NSCN)
pn_junction_sprk(nx, ny, PT_PSCN);
}
}
unsigned char can_move[PT_NUM][PT_NUM];
void init_can_move()
{
// can_move[moving type][type at destination]
// 0 = No move/Bounce
// 1 = Swap
// 2 = Both particles occupy the same space.
// 3 = Varies, go run some extra checks
int t, rt;
for (rt=0;rt=XRES || ny>=YRES)
return 0;
r = pmap[ny][nx];
if (r)
r = (r&~0xFF) | parts[r>>8].type;
if (rr)
*rr = r;
if (pt>=PT_NUM || (r&0xFF)>=PT_NUM)
return 0;
result = can_move[pt][r&0xFF];
if (result==3)
{
if ((pt==PT_PHOT || pt==PT_ELEC) && (r&0xFF)==PT_LCRY)
result = (parts[r>>8].life > 5)? 2 : 0;
if ((r&0xFF)==PT_INVIS)
{
if (pv[ny/CELL][nx/CELL]>4.0f || pv[ny/CELL][nx/CELL]<-4.0f) result = 2;
else result = 0;
}
if ((r&0xFF)==PT_PVOD)
{
if (parts[r>>8].life == 10) result = 1;
else result = 0;
}
}
if (bmap[ny/CELL][nx/CELL])
{
if (bmap[ny/CELL][nx/CELL]==WL_ALLOWGAS && !(ptypes[pt].properties&TYPE_GAS))// && ptypes[pt].falldown!=0 && pt!=PT_FIRE && pt!=PT_SMKE)
return 0;
if (bmap[ny/CELL][nx/CELL]==WL_ALLOWENERGY && !(ptypes[pt].properties&TYPE_ENERGY))// && ptypes[pt].falldown!=0 && pt!=PT_FIRE && pt!=PT_SMKE)
return 0;
if (bmap[ny/CELL][nx/CELL]==WL_ALLOWLIQUID && ptypes[pt].falldown!=2)
return 0;
if (bmap[ny/CELL][nx/CELL]==WL_ALLOWSOLID && ptypes[pt].falldown!=1)
return 0;
if (bmap[ny/CELL][nx/CELL]==WL_ALLOWAIR || bmap[ny/CELL][nx/CELL]==WL_WALL || bmap[ny/CELL][nx/CELL]==WL_WALLELEC)
return 0;
if (bmap[ny/CELL][nx/CELL]==WL_EWALL && !emap[ny/CELL][nx/CELL])
return 0;
if (bmap[ny/CELL][nx/CELL]==WL_EHOLE && !emap[ny/CELL][nx/CELL])
return 2;
}
return result;
}
int try_move(int i, int x, int y, int nx, int ny)
{
unsigned r, e;
if (x==nx && y==ny)
return 1;
if (nx<0 || ny<0 || nx>=XRES || ny>=YRES)
return 1;
e = eval_move(parts[i].type, nx, ny, &r);
if ((r&0xFF)==PT_BOMB && parts[i].type==PT_BOMB && parts[i].tmp == 1)
e = 2;
/* half-silvered mirror */
if (!e && parts[i].type==PT_PHOT &&
(((r&0xFF)==PT_BMTL && rand()>8].temp = parts[i].temp;
if ((r & 0xFF) < PT_NUM && ptypes[r&0xFF].hconduct && ((r&0xFF)!=PT_HSWC||parts[r>>8].life==10) && (r&0xFF)!=PT_FILT)
parts[i].temp = parts[r>>8].temp = restrict_flt((parts[r>>8].temp+parts[i].temp)/2, MIN_TEMP, MAX_TEMP);
}
if ((parts[i].type==PT_NEUT || parts[i].type==PT_ELEC) && ((r&0xFF)==PT_CLNE || (r&0xFF)==PT_PCLN || (r&0xFF)==PT_BCLN || (r&0xFF)==PT_PBCN)) {
if (!parts[r>>8].ctype)
parts[r>>8].ctype = parts[i].type;
}
if ((r&0xFF)==PT_PRTI && (ptypes[parts[i].type].properties & TYPE_ENERGY))
{
int nnx, count;
for (count=0; count<8; count++)
{
if (isign(x-nx)==isign(portal_rx[count]) && isign(y-ny)==isign(portal_ry[count]))
break;
}
count = count%8;
parts[r>>8].tmp = (int)((parts[r>>8].temp-73.15f)/100+1);
if (parts[r>>8].tmp>=CHANNELS) parts[r>>8].tmp = CHANNELS-1;
else if (parts[r>>8].tmp<0) parts[r>>8].tmp = 0;
for ( nnx=0; nnx<80; nnx++)
if (!portalp[parts[r>>8].tmp][count][nnx].type)
{
portalp[parts[r>>8].tmp][count][nnx] = parts[i];
parts[i].type=PT_NONE;
break;
}
}
return 0;
}
if (e == 2) //if occupy same space
{
if (parts[i].type == PT_PHOT && (r&0xFF)==PT_GLOW && !parts[r>>8].life)
if (rand() < RAND_MAX/30)
{
parts[r>>8].life = 120;
create_gain_photon(i);
}
if (parts[i].type == PT_PHOT && (r&0xFF)==PT_FILT)
{
int temp_bin = (int)((parts[r>>8].temp-273.0f)*0.025f);
if (temp_bin < 0) temp_bin = 0;
if (temp_bin > 25) temp_bin = 25;
if(!parts[r>>8].tmp){
parts[i].ctype = 0x1F << temp_bin; //Assign Colour
} else if(parts[r>>8].tmp==1){
parts[i].ctype &= 0x1F << temp_bin; //Filter Colour
} else if(parts[r>>8].tmp==2){
parts[i].ctype |= 0x1F << temp_bin; //Add Colour
} else if(parts[r>>8].tmp==3){
parts[i].ctype &= ~(0x1F << temp_bin); //Subtract Colour
}
}
if (parts[i].type == PT_NEUT && (r&0xFF)==PT_GLAS) {
if (rand() < RAND_MAX/10)
create_cherenkov_photon(i);
}
if (parts[i].type == PT_PHOT && (r&0xFF)==PT_INVIS && pv[ny/CELL][nx/CELL]<=4.0f && pv[ny/CELL][nx/CELL]>=-4.0f) {
part_change_type(i,x,y,PT_NEUT);
parts[i].ctype = 0;
}
if ((parts[i].type==PT_BIZR||parts[i].type==PT_BIZRG) && (r&0xFF)==PT_FILT)
{
int temp_bin = (int)((parts[r>>8].temp-273.0f)*0.025f);
if (temp_bin < 0) temp_bin = 0;
if (temp_bin > 25) temp_bin = 25;
parts[i].ctype = 0x1F << temp_bin;
}
return 1;
}
//else e=1 , we are trying to swap the particles, return 0 no swap/move, 1 is still overlap/move, because the swap takes place later
if (parts[i].type==PT_NEUT && (ptypes[r&0xFF].properties&PROP_NEUTABSORB))
{
kill_part(i);
return 0;
}
if ((r&0xFF)==PT_VOID || (r&0xFF)==PT_PVOD) //this is where void eats particles
{
if(!parts[r>>8].ctype || (parts[r>>8].ctype==parts[i].type)!=(parts[r>>8].tmp&1))
kill_part(i);
return 0;
}
if ((r&0xFF)==PT_BHOL || (r&0xFF)==PT_NBHL) //this is where blackhole eats particles
{
kill_part(i);
if (!legacy_enable)
{
parts[r>>8].temp = restrict_flt(parts[r>>8].temp+parts[i].temp/2, MIN_TEMP, MAX_TEMP);//3.0f;
}
return 0;
}
if (((r&0xFF)==PT_WHOL||(r&0xFF)==PT_NWHL) && parts[i].type==PT_ANAR) //whitehole eats anar
{
kill_part(i);
if (!legacy_enable)
{
parts[r>>8].temp = restrict_flt(parts[r>>8].temp- (MAX_TEMP-parts[i].temp)/2, MIN_TEMP, MAX_TEMP);
}
return 0;
}
if (parts[i].type==PT_CNCT && y0)
return 0;
e = r >> 8; //e is now the particle number at r (pmap[ny][nx])
if (r)//the swap part, if we make it this far, swap
{
if (parts[i].type==PT_NEUT) {
// target material is NEUTPENETRATE, meaning it gets moved around when neutron passes
unsigned s = pmap[y][x];
if (s && !(ptypes[s&0xFF].properties&PROP_NEUTPENETRATE))
return 1; // if the element currently underneath neutron isn't NEUTPENETRATE, don't move anything except the neutron
// if nothing is currently underneath neutron, only move target particle
if (s)
{
pmap[ny][nx] = (s&~(0xFF))|parts[s>>8].type;
parts[s>>8].x = nx;
parts[s>>8].y = ny;
}
else pmap[ny][nx] = 0;
parts[e].x = x;
parts[e].y = y;
pmap[y][x] = (e<<8)|parts[e].type;
return 1;
}
if ((pmap[ny][nx]>>8)==e) pmap[ny][nx] = 0;
parts[e].x += x-nx;
parts[e].y += y-ny;
pmap[(int)(parts[e].y+0.5f)][(int)(parts[e].x+0.5f)] = (e<<8)|parts[e].type;
}
return 1;
}
// try to move particle, and if successful update pmap and parts[i].x,y
int do_move(int i, int x, int y, float nxf, float nyf)
{
int nx = (int)(nxf+0.5f), ny = (int)(nyf+0.5f), result;
if (parts[i].type == PT_NONE)
return 0;
result = try_move(i, x, y, nx, ny);
if (result)
{
int t = parts[i].type;
parts[i].x = nxf;
parts[i].y = nyf;
if (ny!=y || nx!=x)
{
if ((pmap[y][x]>>8)==i) pmap[y][x] = 0;
else if ((photons[y][x]>>8)==i) photons[y][x] = 0;
if (nx=XRES-CELL || ny=YRES-CELL)//kill_part if particle is out of bounds
{
kill_part(i);
return -1;
}
if (ptypes[t].properties & TYPE_ENERGY)
photons[ny][nx] = t|(i<<8);
else if (t)
pmap[ny][nx] = t|(i<<8);
}
}
return result;
}
static unsigned direction_to_map(float dx, float dy, int t)
{
// TODO:
// Adding extra directions causes some inaccuracies.
// Not adding them causes problems with some diagonal surfaces (photons absorbed instead of reflected).
// For now, don't add them.
// Solution may involve more intelligent setting of initial i0 value in find_next_boundary?
// or rewriting normal/boundary finding code
return (dx >= 0) |
(((dx + dy) >= 0) << 1) | /* 567 */
((dy >= 0) << 2) | /* 4+0 */
(((dy - dx) >= 0) << 3) | /* 321 */
((dx <= 0) << 4) |
(((dx + dy) <= 0) << 5) |
((dy <= 0) << 6) |
(((dy - dx) <= 0) << 7);
/*
return (dx >= -0.001) |
(((dx + dy) >= -0.001) << 1) | // 567
((dy >= -0.001) << 2) | // 4+0
(((dy - dx) >= -0.001) << 3) | // 321
((dx <= 0.001) << 4) |
(((dx + dy) <= 0.001) << 5) |
((dy <= 0.001) << 6) |
(((dy - dx) <= 0.001) << 7);
}*/
}
static int is_blocking(int t, int x, int y)
{
if (t & REFRACT) {
if (x<0 || y<0 || x>=XRES || y>=YRES)
return 0;
if ((pmap[y][x] & 0xFF) == PT_GLAS)
return 1;
return 0;
}
return !eval_move(t, x, y, NULL);
}
static int is_boundary(int pt, int x, int y)
{
if (!is_blocking(pt,x,y))
return 0;
if (is_blocking(pt,x,y-1) && is_blocking(pt,x,y+1) && is_blocking(pt,x-1,y) && is_blocking(pt,x+1,y))
return 0;
return 1;
}
static int find_next_boundary(int pt, int *x, int *y, int dm, int *em)
{
static int dx[8] = {1,1,0,-1,-1,-1,0,1};
static int dy[8] = {0,1,1,1,0,-1,-1,-1};
static int de[8] = {0x83,0x07,0x0E,0x1C,0x38,0x70,0xE0,0xC1};
int i, ii, i0;
if (*x <= 0 || *x >= XRES-1 || *y <= 0 || *y >= YRES-1)
return 0;
if (*em != -1) {
i0 = *em;
dm &= de[i0];
} else
i0 = 0;
for (ii=0; ii<8; ii++) {
i = (ii + i0) & 7;
if ((dm & (1 << i)) && is_boundary(pt, *x+dx[i], *y+dy[i])) {
*x += dx[i];
*y += dy[i];
*em = i;
return 1;
}
}
return 0;
}
int get_normal(int pt, int x, int y, float dx, float dy, float *nx, float *ny)
{
int ldm, rdm, lm, rm;
int lx, ly, lv, rx, ry, rv;
int i, j;
float r, ex, ey;
if (!dx && !dy)
return 0;
if (!is_boundary(pt, x, y))
return 0;
ldm = direction_to_map(-dy, dx, pt);
rdm = direction_to_map(dy, -dx, pt);
lx = rx = x;
ly = ry = y;
lv = rv = 1;
lm = rm = -1;
j = 0;
for (i=0; i= NORMAL_INTERP)
return 0;
if (pt == PT_PHOT)
photoelectric_effect(x, y);
return get_normal(pt, x, y, dx, dy, nx, ny);
}
//For soap only
void detach(int i)
{
if ((parts[i].ctype&2) == 2)
{
if ((parts[parts[i].tmp].ctype&4) == 4)
parts[parts[i].tmp].ctype ^= 4;
}
if ((parts[i].ctype&4) == 4)
{
if ((parts[parts[i].tmp2].ctype&2) == 2)
parts[parts[i].tmp2].ctype ^= 2;
}
parts[i].ctype = 0;
}
void kill_part(int i)//kills particle number i
{
int x, y;
// Remove from pmap even if type==0, otherwise infinite recursion occurs when flood fill deleting
// a particle which sets type to 0 without calling kill_part (such as LIFE)
x = (int)(parts[i].x+0.5f);
y = (int)(parts[i].y+0.5f);
if (x>=0 && y>=0 && x>8)==i)
pmap[y][x] = 0;
else if ((photons[y][x]>>8)==i)
photons[y][x] = 0;
}
if (parts[i].type == PT_NONE) //This shouldn't happen anymore, but it's here just in case
return;
if (parts[i].type == PT_STKM)
{
player.spwn = 0;
}
if (parts[i].type == PT_STKM2)
{
player2.spwn = 0;
}
if (parts[i].type == PT_FIGH)
{
fighters[(unsigned char)parts[i].tmp].spwn = 0;
fighcount--;
}
if (parts[i].type == PT_SPAWN)
{
ISSPAWN1 = 0;
}
if (parts[i].type == PT_SPAWN2)
{
ISSPAWN2 = 0;
}
if (parts[i].type == PT_SOAP)
{
detach(i);
}
parts[i].type = PT_NONE;
parts[i].life = pfree;
pfree = i;
}
#if defined(WIN32) && !defined(__GNUC__)
_inline void part_change_type(int i, int x, int y, int t)
#else
inline void part_change_type(int i, int x, int y, int t)//changes the type of particle number i, to t. This also changes pmap at the same time.
#endif
{
if (x<0 || y<0 || x>=XRES || y>=YRES || i>=NPART || t<0 || t>=PT_NUM)
return;
if (!ptypes[t].enabled)
t = PT_NONE;
if (parts[i].type == PT_STKM)
player.spwn = 0;
if (parts[i].type == PT_STKM2)
player2.spwn = 0;
if (parts[i].type == PT_FIGH)
{
fighters[(unsigned char)parts[i].tmp].spwn = 0;
fighcount--;
}
parts[i].type = t;
if (ptypes[t].properties & TYPE_ENERGY)
{
photons[y][x] = t|(i<<8);
if ((pmap[y][x]>>8)==i)
pmap[y][x] = 0;
}
else
{
pmap[y][x] = t|(i<<8);
if ((photons[y][x]>>8)==i)
photons[y][x] = 0;
}
}
#if defined(WIN32) && !defined(__GNUC__)
_inline int create_part(int p, int x, int y, int tv)
#else
inline int create_part(int p, int x, int y, int tv)//the function for creating a particle, use p=-1 for creating a new particle, -2 is from a brush, or a particle number to replace a particle.
#endif
{
int i;
int t = tv & 0xFF;
int v = (tv >> 8) & 0xFF;
if (x<0 || y<0 || x>=XRES || y>=YRES || ((t<0 || t>=PT_NUM)&&t!=SPC_HEAT&&t!=SPC_COOL&&t!=SPC_AIR&&t!=SPC_VACUUM&&t!=SPC_PGRV&&t!=SPC_NGRV))
return -1;
if (t>=0 && t>8].temp>8].temp = restrict_flt(parts[r>>8].temp + heatchange, MIN_TEMP, MAX_TEMP);
}
if (t==SPC_COOL&&parts[pmap[y][x]>>8].temp>MIN_TEMP)
{
float heatchange;
int r = pmap[y][x], fast = ((sdl_mod & (KMOD_SHIFT)) && (sdl_mod & (KMOD_CTRL)));
if ((r&0xFF)==PT_PUMP || (r&0xFF)==PT_GPMP)
heatchange = fast?1.0f:.1f;
else
heatchange = fast?50.0f:4.0f;
parts[r>>8].temp = restrict_flt(parts[r>>8].temp - heatchange, MIN_TEMP, MAX_TEMP);
}
return pmap[y][x]>>8;
}
else
{
return -1;
}
}
if (t==SPC_AIR)
{
pv[y/CELL][x/CELL] += 0.03f;
if (y+CELL>8].ctype=PT_DUST;
}
if (!((pmap[y][x]&0xFF)==PT_INST||(ptypes[pmap[y][x]&0xFF].properties&PROP_CONDUCTS)))
return -1;
if (parts[pmap[y][x]>>8].life!=0)
return -1;
parts[pmap[y][x]>>8].type = PT_SPRK;
parts[pmap[y][x]>>8].life = 4;
parts[pmap[y][x]>>8].ctype = pmap[y][x]&0xFF;
pmap[y][x] = (pmap[y][x]&~0xFF) | PT_SPRK;
return pmap[y][x]>>8;
}
if (t==PT_SPAWN&&ISSPAWN1)
return -1;
if (t==PT_SPAWN2&&ISSPAWN2)
return -1;
if (p==-1)//creating from anything but brush
{
// If there is a particle, only allow creation if the new particle can occupy the same space as the existing particle
// If there isn't a particle but there is a wall, check whether the new particle is allowed to be in it
// (not "!=2" for wall check because eval_move returns 1 for moving into empty space)
// If there's no particle and no wall, assume creation is allowed
if (pmap[y][x] ? (eval_move(t, x, y, NULL)!=2) : (bmap[y/CELL][x/CELL] && eval_move(t, x, y, NULL)==0))
{
if ((pmap[y][x]&0xFF)!=PT_SPAWN&&(pmap[y][x]&0xFF)!=PT_SPAWN2)
{
if (t!=PT_STKM&&t!=PT_STKM2&&t!=PT_FIGH)
{
return -1;
}
}
}
if (pfree == -1)
return -1;
i = pfree;
pfree = parts[i].life;
}
else if (p==-2)//creating from brush
{
if (pmap[y][x])
{
if ((
((pmap[y][x]&0xFF)==PT_STOR&&!(ptypes[t].properties&TYPE_SOLID))||
(pmap[y][x]&0xFF)==PT_CLNE||
(pmap[y][x]&0xFF)==PT_BCLN||
(pmap[y][x]&0xFF)==PT_CONV||
((pmap[y][x]&0xFF)==PT_PCLN&&t!=PT_PSCN&&t!=PT_NSCN)||
((pmap[y][x]&0xFF)==PT_PBCN&&t!=PT_PSCN&&t!=PT_NSCN)
)&&(
t!=PT_CLNE&&t!=PT_PCLN&&
t!=PT_BCLN&&t!=PT_STKM&&
t!=PT_STKM2&&t!=PT_PBCN&&
t!=PT_STOR&&t!=PT_FIGH)
)
{
parts[pmap[y][x]>>8].ctype = t;
if (t==PT_LIFE && v>8].tmp = v;
}
return -1;
}
if (photons[y][x] && (ptypes[t].properties & TYPE_ENERGY))
return -1;
if (pfree == -1)
return -1;
i = pfree;
pfree = parts[i].life;
}
else if (p==-3)//skip pmap checks, e.g. for sing explosion
{
if (pfree == -1)
return -1;
i = pfree;
pfree = parts[i].life;
}
else
{
int oldX = (int)(parts[p].x+0.5f);
int oldY = (int)(parts[p].y+0.5f);
if ((pmap[oldY][oldX]>>8)==p)
pmap[oldY][oldX] = 0;
if ((photons[oldY][oldX]>>8)==p)
photons[oldY][oldX] = 0;
i = p;
}
if (i>parts_lastActiveIndex) parts_lastActiveIndex = i;
parts[i].dcolour = 0;
if (t==PT_GLAS)
{
parts[i].pavg[1] = pv[y/CELL][x/CELL];
}
else if (t==PT_QRTZ)
{
parts[i].pavg[1] = pv[y/CELL][x/CELL];
}
else
{
parts[i].pavg[0] = 0.0f;
parts[i].pavg[1] = 0.0f;
}
if (t!=PT_STKM&&t!=PT_STKM2&&t!=PT_FIGH)//set everything to default values first, except for stickman.
{
parts[i].x = (float)x;
parts[i].y = (float)y;
#ifdef OGLR
parts[i].lastX = (float)x;
parts[i].lastY = (float)y;
#endif
parts[i].type = t;
parts[i].vx = 0;
parts[i].vy = 0;
parts[i].life = 0;
parts[i].ctype = 0;
parts[i].temp = ptypes[t].heat;
parts[i].tmp = 0;
parts[i].tmp2 = 0;
}
if (t==PT_LIGH && p==-2)
{
switch (gravityMode)
{
default:
case 0:
parts[i].tmp= 270+rand()%40-20;
break;
case 1:
parts[i].tmp = rand()%360;
break;
case 2:
parts[i].tmp = atan2(x-XCNTR, y-YCNTR)*(180.0f/M_PI)+90;
}
parts[i].tmp2 = 4;
}
if (t==PT_SOAP)
{
parts[i].tmp = -1;
parts[i].tmp2 = -1;
}
//now set various properties that we want at spawn.
if (t==PT_ACID || t==PT_CAUS)
{
parts[i].life = 75;
}
/*Testing
if(t==PT_WOOD){
parts[i].life = 150;
}
End Testing*/
if (t==PT_WARP) {
parts[i].life = rand()%95+70;
}
if (t==PT_FUSE) {
parts[i].life = 50;
parts[i].tmp = 50;
}
/*if (ptypes[t].properties&PROP_LIFE) {
int r;
for (r = 0; r255 ? 255 : (colr<0 ? 0 : colr);
colg = colg>255 ? 255 : (colg<0 ? 0 : colg);
colb = colb>255 ? 255 : (colb<0 ? 0 : colb);
parts[i].dcolour = 0xFF000000 | (colr<<16) | (colg<<8) | colb;
}
return i;
}
static void create_gain_photon(int pp)//photons from PHOT going through GLOW
{
float xx, yy;
int i, lr, temp_bin, nx, ny;
if (pfree == -1)
return;
i = pfree;
lr = rand() % 2;
if (lr) {
xx = parts[pp].x - 0.3*parts[pp].vy;
yy = parts[pp].y + 0.3*parts[pp].vx;
} else {
xx = parts[pp].x + 0.3*parts[pp].vy;
yy = parts[pp].y - 0.3*parts[pp].vx;
}
nx = (int)(xx + 0.5f);
ny = (int)(yy + 0.5f);
if (nx<0 || ny<0 || nx>=XRES || ny>=YRES)
return;
if ((pmap[ny][nx] & 0xFF) != PT_GLOW)
return;
pfree = parts[i].life;
if (i>parts_lastActiveIndex) parts_lastActiveIndex = i;
parts[i].type = PT_PHOT;
parts[i].life = 680;
parts[i].x = xx;
parts[i].y = yy;
parts[i].vx = parts[pp].vx;
parts[i].vy = parts[pp].vy;
parts[i].temp = parts[pmap[ny][nx] >> 8].temp;
parts[i].tmp = 0;
parts[i].pavg[0] = parts[i].pavg[1] = 0.0f;
photons[ny][nx] = PT_PHOT|(i<<8);
temp_bin = (int)((parts[i].temp-273.0f)*0.25f);
if (temp_bin < 0) temp_bin = 0;
if (temp_bin > 25) temp_bin = 25;
parts[i].ctype = 0x1F << temp_bin;
}
static void create_cherenkov_photon(int pp)//photons from NEUT going through GLAS
{
int i, lr, nx, ny;
float r, eff_ior;
if (pfree == -1)
return;
i = pfree;
nx = (int)(parts[pp].x + 0.5f);
ny = (int)(parts[pp].y + 0.5f);
if ((pmap[ny][nx] & 0xFF) != PT_GLAS)
return;
if (hypotf(parts[pp].vx, parts[pp].vy) < 1.44f)
return;
pfree = parts[i].life;
if (i>parts_lastActiveIndex) parts_lastActiveIndex = i;
lr = rand() % 2;
parts[i].type = PT_PHOT;
parts[i].ctype = 0x00000F80;
parts[i].life = 680;
parts[i].x = parts[pp].x;
parts[i].y = parts[pp].y;
parts[i].temp = parts[pmap[ny][nx] >> 8].temp;
parts[i].tmp = 0;
parts[i].pavg[0] = parts[i].pavg[1] = 0.0f;
photons[ny][nx] = PT_PHOT|(i<<8);
if (lr) {
parts[i].vx = parts[pp].vx - 2.5f*parts[pp].vy;
parts[i].vy = parts[pp].vy + 2.5f*parts[pp].vx;
} else {
parts[i].vx = parts[pp].vx + 2.5f*parts[pp].vy;
parts[i].vy = parts[pp].vy - 2.5f*parts[pp].vx;
}
/* photons have speed of light. no discussion. */
r = 1.269 / hypotf(parts[i].vx, parts[i].vy);
parts[i].vx *= r;
parts[i].vy *= r;
}
#if defined(WIN32) && !defined(__GNUC__)
_inline void delete_part(int x, int y, int flags)//calls kill_part with the particle located at x,y
#else
inline void delete_part(int x, int y, int flags)//calls kill_part with the particle located at x,y
#endif
{
unsigned i;
if (x<0 || y<0 || x>=XRES || y>=YRES)
return;
if (photons[y][x]) {
i = photons[y][x];
} else {
i = pmap[y][x];
}
if (!i)
return;
if (!(flags&BRUSH_SPECIFIC_DELETE) || parts[i>>8].type==SLALT || SLALT==0)//specific deletiom
{
kill_part(i>>8);
}
else if (ptypes[parts[i>>8].type].menusection==SEC)//specific menu deletion
{
kill_part(i>>8);
}
else
return;
}
#if defined(WIN32) && !defined(__GNUC__)
_inline int is_wire(int x, int y)
#else
inline int is_wire(int x, int y)
#endif
{
return bmap[y][x]==WL_DETECT || bmap[y][x]==WL_EWALL || bmap[y][x]==WL_ALLOWLIQUID || bmap[y][x]==WL_WALLELEC || bmap[y][x]==WL_ALLOWALLELEC || bmap[y][x]==WL_EHOLE;
}
#if defined(WIN32) && !defined(__GNUC__)
_inline int is_wire_off(int x, int y)
#else
inline int is_wire_off(int x, int y)
#endif
{
return (bmap[y][x]==WL_DETECT || bmap[y][x]==WL_EWALL || bmap[y][x]==WL_ALLOWLIQUID || bmap[y][x]==WL_WALLELEC || bmap[y][x]==WL_ALLOWALLELEC || bmap[y][x]==WL_EHOLE) && emap[y][x]<8;
}
int get_wavelength_bin(int *wm)
{
int i, w0=30, wM=0;
if (!*wm)
return -1;
for (i=0; i<30; i++)
if (*wm & (1< wM)
wM = i;
}
if (wM-w0 < 5)
return (wM+w0)/2;
i = rand() % (wM-w0-3);
i += w0;
*wm &= 0x1F << i;
return i + 2;
}
void set_emap(int x, int y)
{
int x1, x2;
if (!is_wire_off(x, y))
return;
// go left as far as possible
x1 = x2 = x;
while (x1>0)
{
if (!is_wire_off(x1-1, y))
break;
x1--;
}
while (x21 && x1==x2 &&
is_wire(x1-1, y-1) && is_wire(x1, y-1) && is_wire(x1+1, y-1) &&
!is_wire(x1-1, y-2) && is_wire(x1, y-2) && !is_wire(x1+1, y-2))
set_emap(x1, y-2);
else if (y>0)
for (x=x1; x<=x2; x++)
if (is_wire_off(x, y-1))
{
if (x==x1 || x==x2 || y>=YRES/CELL-1 ||
is_wire(x-1, y-1) || is_wire(x+1, y-1) ||
is_wire(x-1, y+1) || !is_wire(x, y+1) || is_wire(x+1, y+1))
set_emap(x, y-1);
}
if (y>8].type;
else
return PT_NONE;
}
else
{
int pmr2 = pmap[(int)((parts[ci].y + parts[ni].y)/2+0.5f)][(int)((parts[ci].x + parts[ni].x)/2+0.5f)];//seems to be more accurate.
if (pmr2)
{
if (parts[pmr2>>8].type==t)
return t;
}
else
return PT_NONE;
}
return PT_NONE;
}
int nearest_part(int ci, int t, int max_d)
{
int distance = (max_d!=-1)?max_d:MAX_DISTANCE;
int ndistance = 0;
int id = -1;
int i = 0;
int cx = (int)parts[ci].x;
int cy = (int)parts[ci].y;
for (i=0; i<=parts_lastActiveIndex; i++)
{
if ((parts[i].type==t||(t==-1&&parts[i].type))&&!parts[i].life&&i!=ci)
{
ndistance = abs(cx-parts[i].x)+abs(cy-parts[i].y);// Faster but less accurate Older: sqrt(pow(cx-parts[i].x, 2)+pow(cy-parts[i].y, 2));
if (ndistance0)
{
for (i=0; i<=parts_lastActiveIndex; i++)
{
if (parts[i].type==PT_LIGH && parts[i].tmp2>0)
{
lighting_ok=0;
break;
}
}
}
if (lighting_ok)
lighting_recreate--;
if (lighting_recreate<0)
lighting_recreate=1;
if (lighting_recreate>21)
lighting_recreate=21;
if (sys_pause&&!framerender)//do nothing if paused
return;
if (ISGRAV==1)//crappy grav color handling, i will change this someday
{
ISGRAV = 0;
GRAV ++;
GRAV_R = 60;
GRAV_G = 0;
GRAV_B = 0;
GRAV_R2 = 30;
GRAV_G2 = 30;
GRAV_B2 = 0;
for ( q = 0; q <= GRAV; q++)
{
if (GRAV_R >0 && GRAV_G==0)
{
GRAV_R--;
GRAV_B++;
}
if (GRAV_B >0 && GRAV_R==0)
{
GRAV_B--;
GRAV_G++;
}
if (GRAV_G >0 && GRAV_B==0)
{
GRAV_G--;
GRAV_R++;
}
if (GRAV_R2 >0 && GRAV_G2==0)
{
GRAV_R2--;
GRAV_B2++;
}
if (GRAV_B2 >0 && GRAV_R2==0)
{
GRAV_B2--;
GRAV_G2++;
}
if (GRAV_G2 >0 && GRAV_B2==0)
{
GRAV_G2--;
GRAV_R2++;
}
}
if (GRAV>180) GRAV = 0;
}
if (ISLOVE==1)//LOVE element handling
{
ISLOVE = 0;
for (ny=0; nyYRES-7||nx>XRES-10)&&parts[r>>8].type==PT_LOVE)
kill_part(r>>8);
else if (parts[r>>8].type==PT_LOVE)
{
love[nx/9][ny/9] = 1;
}
}
}
for (nx=9; nx<=XRES-18; nx++)
{
for (ny=9; ny<=YRES-7; ny++)
{
if (love[nx/9][ny/9]==1)
{
for ( nnx=0; nnx<9; nnx++)
for ( nny=0; nny<9; nny++)
{
if (ny+nny>0&&ny+nny=0&&nx+nnx>8].type==PT_LOVE&&loverule[nnx][nny]==0)
kill_part(rt>>8);
}
}
}
love[nx/9][ny/9]=0;
}
}
}
if (ISLOLZ==1)//LOLZ element handling
{
ISLOLZ = 0;
for (ny=0; nyYRES-7||nx>XRES-10)&&parts[r>>8].type==PT_LOLZ)
kill_part(r>>8);
else if (parts[r>>8].type==PT_LOLZ)
{
lolz[nx/9][ny/9] = 1;
}
}
}
for (nx=9; nx<=XRES-18; nx++)
{
for (ny=9; ny<=YRES-7; ny++)
{
if (lolz[nx/9][ny/9]==1)
{
for ( nnx=0; nnx<9; nnx++)
for ( nny=0; nny<9; nny++)
{
if (ny+nny>0&&ny+nny=0&&nx+nnx>8].type==PT_LOLZ&&lolzrule[nny][nnx]==0)
kill_part(rt>>8);
}
}
}
lolz[nx/9][ny/9]=0;
}
}
}
//wire!
if(wire_placed == 1)
{
wire_placed = 0;
for (nx=0; nx>8].type==PT_WIRE)
parts[r>>8].tmp=parts[r>>8].ctype;
}
}
}
//game of life!
if (ISGOL==1&&++CGOL>=GSPEED)//GSPEED is frames per generation
{
int createdsomething = 0;
CGOL=0;
ISGOL=0;
for (nx=CELL; nx>8].type==PT_LIFE/* && parts[r>>8].ctype==golnum-1*/)
{
golnum = parts[r>>8].ctype+1;
if (golnum<=0 || golnum>NGOLALT) {
parts[r>>8].type = PT_NONE;
continue;
}
if (parts[r>>8].tmp == grule[golnum][9]-1) {
gol[nx][ny] = golnum;
for ( nnx=-1; nnx<2; nnx++)
{
for ( nny=-1; nny<2; nny++)//it will count itself as its own neighbor, which is needed, but will have 1 extra for delete check
{
rt = pmap[((ny+nny+YRES-3*CELL)%(YRES-2*CELL))+CELL][((nx+nnx+XRES-3*CELL)%(XRES-2*CELL))+CELL];
if (!rt || (rt&0xFF)==PT_LIFE)
{
gol2[((nx+nnx+XRES-3*CELL)%(XRES-2*CELL))+CELL][((ny+nny+YRES-3*CELL)%(YRES-2*CELL))+CELL][golnum] ++;
gol2[((nx+nnx+XRES-3*CELL)%(XRES-2*CELL))+CELL][((ny+nny+YRES-3*CELL)%(YRES-2*CELL))+CELL][0] ++;
}
}
}
} else {
parts[r>>8].tmp --;
if (parts[r>>8].tmp<=0)
parts[r>>8].type = PT_NONE;//using kill_part makes it not work
}
}
//}
}
}
}
for (nx=CELL; nx=2&&gol2[nx][ny][golnum]>=(goldelete%2)+goldelete/2)
{
if (create_part(-1, nx, ny, PT_LIFE|((golnum-1)<<8)))
createdsomething = 1;
}
else if (gol[nx][ny]==golnum&&(grule[golnum][goldelete-1]==0||grule[golnum][goldelete-1]==2))//subtract 1 because it counted itself
{
if (parts[r>>8].tmp==grule[golnum][9]-1)
parts[r>>8].tmp --;
}
if (r && parts[r>>8].tmp<=0)
parts[r>>8].type = PT_NONE;//using kill_part makes it not work
}
for ( z = 0; z<=NGOL; z++)
gol2[nx][ny][z] = 0;//this improves performance A LOT compared to the memset, i was getting ~23 more fps with this.
}
}
if (createdsomething)
GENERATION ++;
//memset(gol2, 0, sizeof(gol2));
}
if (ISWIRE>0)//wifi channel reseting
{
for ( q = 0; q<(int)(MAX_TEMP-73.15f)/100+2; q++)
{
wireless[q][0] = wireless[q][1];
wireless[q][1] = 0;
}
ISWIRE--;
}
for (i=0; i<=parts_lastActiveIndex; i++)
if (parts[i].type)
{
t = parts[i].type;
#ifdef OGLR
parts[i].lastX = parts[i].x;
parts[i].lastY = parts[i].y;
#endif
if (t<0 || t>=PT_NUM)
{
kill_part(i);
continue;
}
elem_properties = ptypes[t].properties;
if (parts[i].life>0 && (elem_properties&PROP_LIFE_DEC))
{
// automatically decrease life
parts[i].life--;
if (parts[i].life<=0 && (elem_properties&(PROP_LIFE_KILL_DEC|PROP_LIFE_KILL)))
{
// kill on change to no life
kill_part(i);
continue;
}
}
else if (parts[i].life<=0 && (elem_properties&PROP_LIFE_KILL))
{
// kill if no life
kill_part(i);
continue;
}
}
//the main particle loop function, goes over all particles.
for (i=0; i<=parts_lastActiveIndex; i++)
if (parts[i].type)
{
t = parts[i].type;
x = (int)(parts[i].x+0.5f);
y = (int)(parts[i].y+0.5f);
//this kills any particle out of the screen, or in a wall where it isn't supposed to go
if (x=XRES-CELL || y>=YRES-CELL ||
(bmap[y/CELL][x/CELL] &&
(bmap[y/CELL][x/CELL]==WL_WALL ||
bmap[y/CELL][x/CELL]==WL_WALLELEC ||
bmap[y/CELL][x/CELL]==WL_ALLOWAIR ||
(bmap[y/CELL][x/CELL]==WL_DESTROYALL) ||
(bmap[y/CELL][x/CELL]==WL_ALLOWLIQUID && ptypes[t].falldown!=2) ||
(bmap[y/CELL][x/CELL]==WL_ALLOWSOLID && ptypes[t].falldown!=1) ||
(bmap[y/CELL][x/CELL]==WL_ALLOWGAS && !(ptypes[t].properties&TYPE_GAS)) || //&& ptypes[t].falldown!=0 && parts[i].type!=PT_FIRE && parts[i].type!=PT_SMKE && parts[i].type!=PT_HFLM) ||
(bmap[y/CELL][x/CELL]==WL_ALLOWENERGY && !(ptypes[t].properties&TYPE_ENERGY)) ||
(bmap[y/CELL][x/CELL]==WL_DETECT && (t==PT_METL || t==PT_SPRK)) ||
(bmap[y/CELL][x/CELL]==WL_EWALL && !emap[y/CELL][x/CELL])) && (t!=PT_STKM) && (t!=PT_STKM2) && (t!=PT_FIGH)))
{
kill_part(i);
continue;
}
if (bmap[y/CELL][x/CELL]==WL_DETECT && emap[y/CELL][x/CELL]<8)
set_emap(x/CELL, y/CELL);
//adding to velocity from the particle's velocity
vx[y/CELL][x/CELL] = vx[y/CELL][x/CELL]*ptypes[t].airloss + ptypes[t].airdrag*parts[i].vx;
vy[y/CELL][x/CELL] = vy[y/CELL][x/CELL]*ptypes[t].airloss + ptypes[t].airdrag*parts[i].vy;
if (t==PT_GAS||t==PT_NBLE)
{
if (pv[y/CELL][x/CELL]<3.5f)
pv[y/CELL][x/CELL] += ptypes[t].hotair*(3.5f-pv[y/CELL][x/CELL]);
if (y+CELL= 0 && y-2 < YRES && (ptypes[t].properties&TYPE_LIQUID) && (t!=PT_GEL || gel_scale>(1+rand()%255))) {//some heat convection for liquids
r = pmap[y-2][x];
if (!(!r || parts[i].type != (r&0xFF))) {
if (parts[i].temp>parts[r>>8].temp) {
swappage = parts[i].temp;
parts[i].temp = parts[r>>8].temp;
parts[r>>8].temp = swappage;
}
}
}
//heat transfer code
h_count = 0;
#ifdef REALISTIC
if (t&&(t!=PT_HSWC||parts[i].life==10)&&(ptypes[t].hconduct*gel_scale))
{
float c_Cm = 0.0f;
#else
if (t&&(t!=PT_HSWC||parts[i].life==10)&&(ptypes[t].hconduct*gel_scale)>(rand()%250))
{
float c_Cm = 0.0f;
#endif
if (aheat_enable && !(ptypes[t].properties&PROP_NOAMBHEAT))
{
#ifdef REALISTIC
c_heat = parts[i].temp*96.645/ptypes[t].hconduct*gel_scale*fabs(ptypes[t].weight)
+ hv[y/CELL][x/CELL]*100*(pv[y/CELL][x/CELL]+273.15f)/256;
c_Cm = 96.645/ptypes[t].hconduct*gel_scale*fabs(ptypes[t].weight)
+ 100*(pv[y/CELL][x/CELL]+273.15f)/256;
pt = c_heat/c_Cm;
pt = restrict_flt(pt, -MAX_TEMP+MIN_TEMP, MAX_TEMP-MIN_TEMP);
parts[i].temp = pt;
//Pressure increase from heat (temporary)
pv[y/CELL][x/CELL] += (pt-hv[y/CELL][x/CELL])*0.004;
hv[y/CELL][x/CELL] = pt;
#else
c_heat = (hv[y/CELL][x/CELL]-parts[i].temp)*0.04;
c_heat = restrict_flt(c_heat, -MAX_TEMP+MIN_TEMP, MAX_TEMP-MIN_TEMP);
parts[i].temp += c_heat;
hv[y/CELL][x/CELL] -= c_heat;
#endif
}
c_heat = 0.0f; c_Cm = 0.0f;
for (j=0; j<8; j++)
{
surround_hconduct[j] = i;
r = surround[j];
if (!r)
continue;
rt = r&0xFF;
if (rt&&ptypes[rt].hconduct&&(rt!=PT_HSWC||parts[r>>8].life==10)
&&(t!=PT_FILT||(rt!=PT_BRAY&&rt!=PT_BIZR&&rt!=PT_BIZRG))
&&(rt!=PT_FILT||(t!=PT_BRAY&&t!=PT_PHOT&&t!=PT_BIZR&&t!=PT_BIZRG)))
{
surround_hconduct[j] = r>>8;
#ifdef REALISTIC
if (rt==PT_GEL)
gel_scale = parts[r>>8].tmp*2.55f;
else gel_scale = 1.0f;
c_heat += parts[r>>8].temp*96.645/ptypes[rt].hconduct*gel_scale*fabs(ptypes[rt].weight);
c_Cm += 96.645/ptypes[rt].hconduct*gel_scale*fabs(ptypes[rt].weight);
#else
c_heat += parts[r>>8].temp;
#endif
h_count++;
}
}
#ifdef REALISTIC
if (t==PT_GEL)
gel_scale = parts[i].tmp*2.55f;
else gel_scale = 1.0f;
if (t == PT_PHOT)
pt = (c_heat+parts[i].temp*96.645)/(c_Cm+96.645);
else
pt = (c_heat+parts[i].temp*96.645/ptypes[t].hconduct*gel_scale*fabs(ptypes[t].weight))/(c_Cm+96.645/ptypes[t].hconduct*gel_scale*fabs(ptypes[t].weight));
c_heat += parts[i].temp*96.645/ptypes[t].hconduct*gel_scale*fabs(ptypes[t].weight);
c_Cm += 96.645/ptypes[t].hconduct*gel_scale*fabs(ptypes[t].weight);
parts[i].temp = restrict_flt(pt, MIN_TEMP, MAX_TEMP);
#else
pt = (c_heat+parts[i].temp)/(h_count+1);
pt = parts[i].temp = restrict_flt(pt, MIN_TEMP, MAX_TEMP);
for (j=0; j<8; j++)
{
parts[surround_hconduct[j]].temp = pt;
}
#endif
ctemph = ctempl = pt;
// change boiling point with pressure
if ((ptypes[t].state==ST_LIQUID && ptransitions[t].tht>-1 && ptransitions[t].tht-1 && ptransitions[t].tlt=PT_NUM || parts[i].ctype==PT_ICEI || parts[i].ctype==PT_SNOW))
parts[i].ctype = PT_WATR;
if (ctemph>ptransitions[t].thv&&ptransitions[t].tht>-1) {
// particle type change due to high temperature
#ifdef REALISTIC
float dbt = ctempl - pt;
if (ptransitions[t].tht!=PT_NUM)
{
if (platent[t] <= (c_heat - (ptransitions[t].thv - dbt)*c_Cm))
{
pt = (c_heat - platent[t])/c_Cm;
t = ptransitions[t].tht;
}
else
{
parts[i].temp = restrict_flt(ptransitions[t].thv - dbt, MIN_TEMP, MAX_TEMP);
s = 0;
}
}
#else
if (ptransitions[t].tht!=PT_NUM)
t = ptransitions[t].tht;
#endif
else if (t==PT_ICEI || t==PT_SNOW) {
if (parts[i].ctyperand()%6) t = PT_SALT;
else t = PT_WTRV;
}
else
{
parts[i].temp = restrict_flt(ptransitions[t].thv - dbt, MIN_TEMP, MAX_TEMP);
s = 0;
}
#else
if (1>rand()%6) t = PT_SALT;
else t = PT_WTRV;
#endif
}
else s = 0;
} else if (ctempl-1) {
// particle type change due to low temperature
#ifdef REALISTIC
float dbt = ctempl - pt;
if (ptransitions[t].tlt!=PT_NUM)
{
if (platent[ptransitions[t].tlt] >= (c_heat - (ptransitions[t].tlv - dbt)*c_Cm))
{
pt = (c_heat + platent[ptransitions[t].tlt])/c_Cm;
t = ptransitions[t].tlt;
}
else
{
parts[i].temp = restrict_flt(ptransitions[t].tlv - dbt, MIN_TEMP, MAX_TEMP);
s = 0;
}
}
#else
if (ptransitions[t].tlt!=PT_NUM)
t = ptransitions[t].tlt;
#endif
else if (t==PT_WTRV) {
if (pt<273.0f) t = PT_RIME;
else t = PT_DSTW;
}
else if (t==PT_LAVA) {
if (parts[i].ctype>0 && parts[i].ctype=ptransitions[PT_BMTL].thv) s = 0;
else if (ptransitions[parts[i].ctype].tht==PT_LAVA) {
if (pt>=ptransitions[parts[i].ctype].thv) s = 0;
}
else if (pt>=973.0f) s = 0; // freezing point for lava with any other (not listed in ptransitions as turning into lava) ctype
if (s) {
t = parts[i].ctype;
parts[i].ctype = PT_NONE;
if (t==PT_THRM) {
parts[i].tmp = 0;
t = PT_BMTL;
}
if (t==PT_PLUT)
{
parts[i].tmp = 0;
t = PT_LAVA;
}
}
}
else if (pt<973.0f) t = PT_STNE;
else s = 0;
}
else s = 0;
}
else s = 0;
#ifdef REALISTIC
pt = restrict_flt(pt, MIN_TEMP, MAX_TEMP);
for (j=0; j<8; j++)
{
parts[surround_hconduct[j]].temp = pt;
}
#endif
if (s) { // particle type change occurred
if (t==PT_ICEI||t==PT_LAVA||t==PT_SNOW)
parts[i].ctype = parts[i].type;
if (!(t==PT_ICEI&&parts[i].ctype==PT_FRZW)) parts[i].life = 0;
if (ptypes[t].state==ST_GAS&&ptypes[parts[i].type].state!=ST_GAS)
pv[y/CELL][x/CELL] += 0.50f;
part_change_type(i,x,y,t);
if (t==PT_FIRE||t==PT_PLSM||t==PT_HFLM)
parts[i].life = rand()%50+120;
if (t==PT_LAVA) {
if (parts[i].ctype==PT_BRMT) parts[i].ctype = PT_BMTL;
else if (parts[i].ctype==PT_SAND) parts[i].ctype = PT_GLAS;
else if (parts[i].ctype==PT_BGLA) parts[i].ctype = PT_GLAS;
else if (parts[i].ctype==PT_PQRT) parts[i].ctype = PT_QRTZ;
parts[i].life = rand()%120+240;
}
if (t==PT_NONE) {
kill_part(i);
goto killed;
}
}
pt = parts[i].temp = restrict_flt(parts[i].temp, MIN_TEMP, MAX_TEMP);
if (t==PT_LAVA) {
parts[i].life = restrict_flt((parts[i].temp-700)/7, 0.0f, 400.0f);
if (parts[i].ctype==PT_THRM&&parts[i].tmp>0)
{
parts[i].tmp--;
parts[i].temp = 3500;
}
if (parts[i].ctype==PT_PLUT&&parts[i].tmp>0)
{
parts[i].tmp--;
parts[i].temp = MAX_TEMP;
}
}
}
else parts[i].temp = restrict_flt(parts[i].temp, MIN_TEMP, MAX_TEMP);
}
if (t==PT_LIFE)
{
parts[i].temp = restrict_flt(parts[i].temp-50.0f, MIN_TEMP, MAX_TEMP);
ISGOL=1;//means there is a life particle on screen
}
if (t==PT_WIRE)
{
wire_placed = 1;
}
//spark updates from walls
if ((ptypes[t].properties&PROP_CONDUCTS) || t==PT_SPRK)
{
nx = x % CELL;
if (nx == 0)
nx = x/CELL - 1;
else if (nx == CELL-1)
nx = x/CELL + 1;
else
nx = x/CELL;
ny = y % CELL;
if (ny == 0)
ny = y/CELL - 1;
else if (ny == CELL-1)
ny = y/CELL + 1;
else
ny = y/CELL;
if (nx>=0 && ny>=0 && nx2.5f)
{
parts[i].life = rand()%80+180;
parts[i].temp = restrict_flt(ptypes[PT_FIRE].heat + (ptypes[t].flammable/2), MIN_TEMP, MAX_TEMP);
t = PT_FIRE;
part_change_type(i,x,y,t);
pv[y/CELL][x/CELL] += 0.25f * CFDS;
}
s = 1;
gravtot = fabs(gravy[(y/CELL)*(XRES/CELL)+(x/CELL)])+fabs(gravx[(y/CELL)*(XRES/CELL)+(x/CELL)]);
if (pv[y/CELL][x/CELL]>ptransitions[t].phv&&ptransitions[t].pht>-1) {
// particle type change due to high pressure
if (ptransitions[t].pht!=PT_NUM)
t = ptransitions[t].pht;
else if (t==PT_BMTL) {
if (pv[y/CELL][x/CELL]>2.5f)
t = PT_BRMT;
else if (pv[y/CELL][x/CELL]>1.0f && parts[i].tmp==1)
t = PT_BRMT;
else s = 0;
}
else s = 0;
} else if (pv[y/CELL][x/CELL]-1) {
// particle type change due to low pressure
if (ptransitions[t].plt!=PT_NUM)
t = ptransitions[t].plt;
else s = 0;
} else if (gravtot>(ptransitions[t].phv/4.0f)&&ptransitions[t].pht>-1) {
// particle type change due to high gravity
if (ptransitions[t].pht!=PT_NUM)
t = ptransitions[t].pht;
else if (t==PT_BMTL) {
if (gravtot>0.625f)
t = PT_BRMT;
else if (gravtot>0.25f && parts[i].tmp==1)
t = PT_BRMT;
else s = 0;
}
else s = 0;
} else s = 0;
if (s) { // particle type change occurred
parts[i].life = 0;
part_change_type(i,x,y,t);
if (t==PT_FIRE)
parts[i].life = rand()%50+120;
if (t==PT_NONE) {
kill_part(i);
goto killed;
}
}
//call the particle update function, if there is one
#ifdef LUACONSOLE
if (ptypes[t].update_func && lua_el_mode[t] != 2)
#else
if (ptypes[t].update_func)
#endif
{
if ((*(ptypes[t].update_func))(i,x,y,surround_space,nt))
continue;
else if (t==PT_WARP)
{
// Warp does some movement in its update func, update variables to avoid incorrect data in pmap
x = (int)(parts[i].x+0.5f);
y = (int)(parts[i].y+0.5f);
}
}
#ifdef LUACONSOLE
if(lua_el_mode[t])
{
if(luacon_part_update(t,i,x,y,surround_space,nt))
continue;
// Need to update variables, in case they've been changed by Lua
x = (int)(parts[i].x+0.5f);
y = (int)(parts[i].y+0.5f);
}
#endif
if (legacy_enable)//if heat sim is off
update_legacy_all(i,x,y,surround_space,nt);
killed:
if (parts[i].type == PT_NONE)//if its dead, skip to next particle
continue;
if (!parts[i].vx&&!parts[i].vy)//if its not moving, skip to next particle, movement code it next
continue;
#if defined(WIN32) && !defined(__GNUC__)
mv = max(fabsf(parts[i].vx), fabsf(parts[i].vy));
#else
mv = fmaxf(fabsf(parts[i].vx), fabsf(parts[i].vy));
#endif
if (mv < ISTP)
{
clear_x = x;
clear_y = y;
clear_xf = parts[i].x;
clear_yf = parts[i].y;
fin_xf = clear_xf + parts[i].vx;
fin_yf = clear_yf + parts[i].vy;
fin_x = (int)(fin_xf+0.5f);
fin_y = (int)(fin_yf+0.5f);
}
else
{
// interpolate to see if there is anything in the way
dx = parts[i].vx*ISTP/mv;
dy = parts[i].vy*ISTP/mv;
fin_xf = parts[i].x;
fin_yf = parts[i].y;
while (1)
{
mv -= ISTP;
fin_xf += dx;
fin_yf += dy;
fin_x = (int)(fin_xf+0.5f);
fin_y = (int)(fin_yf+0.5f);
if (mv <= 0.0f)
{
// nothing found
fin_xf = parts[i].x + parts[i].vx;
fin_yf = parts[i].y + parts[i].vy;
fin_x = (int)(fin_xf+0.5f);
fin_y = (int)(fin_yf+0.5f);
clear_xf = fin_xf-dx;
clear_yf = fin_yf-dy;
clear_x = (int)(clear_xf+0.5f);
clear_y = (int)(clear_yf+0.5f);
break;
}
if (fin_x=XRES-CELL || fin_y>=YRES-CELL || pmap[fin_y][fin_x] || (bmap[fin_y/CELL][fin_x/CELL] && (bmap[fin_y/CELL][fin_x/CELL]==WL_DESTROYALL || !eval_move(t,fin_x,fin_y,NULL))))
{
// found an obstacle
clear_xf = fin_xf-dx;
clear_yf = fin_yf-dy;
clear_x = (int)(clear_xf+0.5f);
clear_y = (int)(clear_yf+0.5f);
break;
}
if (bmap[fin_y/CELL][fin_x/CELL]==WL_DETECT && emap[fin_y/CELL][fin_x/CELL]<8)
set_emap(fin_x/CELL, fin_y/CELL);
}
}
stagnant = parts[i].flags & FLAG_STAGNANT;
parts[i].flags &= ~FLAG_STAGNANT;
if (ptypes[t].properties & TYPE_ENERGY) {
if (t == PT_PHOT) {
if (parts[i].flags&FLAG_SKIPMOVE)
{
parts[i].flags &= ~FLAG_SKIPMOVE;
continue;
}
rt = pmap[fin_y][fin_x] & 0xFF;
lt = pmap[y][x] & 0xFF;
r = eval_move(PT_PHOT, fin_x, fin_y, NULL);
if (((rt==PT_GLAS && lt!=PT_GLAS) || (rt!=PT_GLAS && lt==PT_GLAS)) && r) {
if (!get_normal_interp(REFRACT|t, parts[i].x, parts[i].y, parts[i].vx, parts[i].vy, &nrx, &nry)) {
kill_part(i);
continue;
}
r = get_wavelength_bin(&parts[i].ctype);
if (r == -1) {
kill_part(i);
continue;
}
nn = GLASS_IOR - GLASS_DISP*(r-15)/15.0f;
nn *= nn;
nrx = -nrx;
nry = -nry;
if (rt==PT_GLAS && lt!=PT_GLAS)
nn = 1.0f/nn;
ct1 = parts[i].vx*nrx + parts[i].vy*nry;
ct2 = 1.0f - (nn*nn)*(1.0f-(ct1*ct1));
if (ct2 < 0.0f) {
// total internal reflection
parts[i].vx -= 2.0f*ct1*nrx;
parts[i].vy -= 2.0f*ct1*nry;
fin_xf = parts[i].x;
fin_yf = parts[i].y;
fin_x = x;
fin_y = y;
} else {
// refraction
ct2 = sqrtf(ct2);
ct2 = ct2 - nn*ct1;
parts[i].vx = nn*parts[i].vx + ct2*nrx;
parts[i].vy = nn*parts[i].vy + ct2*nry;
}
}
}
if (stagnant)//FLAG_STAGNANT set, was reflected on previous frame
{
// cast coords as int then back to float for compatibility with existing saves
if (!do_move(i, x, y, (float)fin_x, (float)fin_y) && parts[i].type) {
kill_part(i);
continue;
}
}
else if (!do_move(i, x, y, fin_xf, fin_yf))
{
if (parts[i].type == PT_NONE)
continue;
// reflection
parts[i].flags |= FLAG_STAGNANT;
if (t==PT_NEUT && 100>(rand()%1000))
{
kill_part(i);
continue;
}
r = pmap[fin_y][fin_x];
if ((r & 0xFF) == PT_PIPE && !(parts[r>>8].tmp&0xFF))
{
parts[r>>8].tmp = (parts[r>>8].tmp&~0xFF) | parts[i].type;
parts[r>>8].temp = parts[i].temp;
parts[r>>8].flags = parts[i].life;
parts[r>>8].pavg[0] = parts[i].tmp;
parts[r>>8].pavg[1] = parts[i].ctype;
kill_part(i);
continue;
}
// this should be replaced with a particle type attribute ("photwl" or something)
if ((r & 0xFF) == PT_PSCN) parts[i].ctype = 0x00000000;
if ((r & 0xFF) == PT_NSCN) parts[i].ctype = 0x00000000;
if ((r & 0xFF) == PT_SPRK) parts[i].ctype = 0x00000000;
if ((r & 0xFF) == PT_COAL) parts[i].ctype = 0x00000000;
if ((r & 0xFF) == PT_BCOL) parts[i].ctype = 0x00000000;
if ((r & 0xFF) == PT_PLEX) parts[i].ctype &= 0x1F00003E;
if ((r & 0xFF) == PT_NITR) parts[i].ctype &= 0x0007C000;
if ((r & 0xFF) == PT_NBLE) parts[i].ctype &= 0x3FFF8000;
if ((r & 0xFF) == PT_LAVA) parts[i].ctype &= 0x3FF00000;
if ((r & 0xFF) == PT_ACID) parts[i].ctype &= 0x1FE001FE;
if ((r & 0xFF) == PT_DUST) parts[i].ctype &= 0x3FFFFFC0;
if ((r & 0xFF) == PT_SNOW) parts[i].ctype &= 0x03FFFFFF;
if ((r & 0xFF) == PT_GOO) parts[i].ctype &= 0x3FFAAA00;
if ((r & 0xFF) == PT_PLNT) parts[i].ctype &= 0x0007C000;
if ((r & 0xFF) == PT_PLUT) parts[i].ctype &= 0x001FCE00;
if ((r & 0xFF) == PT_URAN) parts[i].ctype &= 0x003FC000;
if (get_normal_interp(t, parts[i].x, parts[i].y, parts[i].vx, parts[i].vy, &nrx, &nry)) {
dp = nrx*parts[i].vx + nry*parts[i].vy;
parts[i].vx -= 2.0f*dp*nrx;
parts[i].vy -= 2.0f*dp*nry;
// leave the actual movement until next frame so that reflection of fast particles and refraction happen correctly
} else {
if (t!=PT_NEUT)
kill_part(i);
continue;
}
if (!(parts[i].ctype&0x3FFFFFFF)&&t!=PT_NEUT&&t!=PT_ELEC) {
kill_part(i);
continue;
}
}
}
else if (ptypes[t].falldown==0)
{
// gases and solids (but not powders)
if (!do_move(i, x, y, fin_xf, fin_yf))
{
if (parts[i].type == PT_NONE)
continue;
// can't move there, so bounce off
// TODO
if (fin_x>x+ISTP) fin_x=x+ISTP;
if (fin_xy+ISTP) fin_y=y+ISTP;
if (fin_y= rand()%400)//checking stagnant is cool, but then it doesn't update when you change it later.
{
if (!flood_water(x,y,i,y, parts[i].tmp2))
goto movedone;
}
// liquids and powders
if (!do_move(i, x, y, fin_xf, fin_yf))
{
if (parts[i].type == PT_NONE)
continue;
if (fin_x!=x && do_move(i, x, y, fin_xf, clear_yf))
{
parts[i].vx *= ptypes[t].collision;
parts[i].vy *= ptypes[t].collision;
}
else if (fin_y!=y && do_move(i, x, y, clear_xf, fin_yf))
{
parts[i].vx *= ptypes[t].collision;
parts[i].vy *= ptypes[t].collision;
}
else
{
s = 1;
r = (rand()%2)*2-1;
if ((clear_x!=x || clear_y!=y || nt || surround_space) &&
(fabsf(parts[i].vx)>0.01f || fabsf(parts[i].vy)>0.01f))
{
// allow diagonal movement if target position is blocked
// but no point trying this if particle is stuck in a block of identical particles
dx = parts[i].vx - parts[i].vy*r;
dy = parts[i].vy + parts[i].vx*r;
if (fabsf(dy)>fabsf(dx))
mv = fabsf(dy);
else
mv = fabsf(dx);
dx /= mv;
dy /= mv;
if (do_move(i, x, y, clear_xf+dx, clear_yf+dy))
{
parts[i].vx *= ptypes[t].collision;
parts[i].vy *= ptypes[t].collision;
goto movedone;
}
swappage = dx;
dx = dy*r;
dy = -swappage*r;
if (do_move(i, x, y, clear_xf+dx, clear_yf+dy))
{
parts[i].vx *= ptypes[t].collision;
parts[i].vy *= ptypes[t].collision;
goto movedone;
}
}
if (ptypes[t].falldown>1 && !ngrav_enable && gravityMode==0 && parts[i].vy>fabsf(parts[i].vx))
{
s = 0;
// stagnant is true if FLAG_STAGNANT was set for this particle in previous frame
if (!stagnant || nt) //nt is if there is an something else besides the current particle type, around the particle
rt = 30;//slight less water lag, although it changes how it moves a lot
else
rt = 10;
if (t==PT_GEL)
rt = parts[i].tmp*0.20f+5.0f;
for (j=clear_x+r; j>=0 && j>=clear_x-rt && j0)
r = 1;
else
r = -1;
if (s==1)
for (j=ny+r; j>=0 && j=ny-rt && j1 && fabsf(pGravX*parts[i].vx+pGravY*parts[i].vy)>fabsf(pGravY*parts[i].vx-pGravX*parts[i].vy))
{
float nxf, nyf, prev_pGravX, prev_pGravY, ptGrav = ptypes[t].gravity;
s = 0;
// stagnant is true if FLAG_STAGNANT was set for this particle in previous frame
if (!stagnant || nt) //nt is if there is an something else besides the current particle type, around the particle
rt = 30;//slight less water lag, although it changes how it moves a lot
else
rt = 10;
nxf = clear_xf;
nyf = clear_yf;
for (j=0;j | | | |