From 31ce22f122111267f7e2989a6eaf0e627ea7d2a7 Mon Sep 17 00:00:00 2001 From: jacksonmj Date: Thu, 21 Jun 2012 13:31:35 +0100 Subject: [PATCH] Gravity modes and Newtonian gravity for fireworks Resolves issue #59 --- includes/powder.h | 2 ++ src/elementdata.c | 2 +- src/elements/firw.c | 20 +++++++++++++++----- src/elements/fwrk.c | 44 ++++++++++++++++++++++++++++++++------------ src/powder.c | 22 ++++++++++++++++++++++ 5 files changed, 72 insertions(+), 18 deletions(-) diff --git a/includes/powder.h b/includes/powder.h index 62cbd9f00..f47eacefe 100644 --- a/includes/powder.h +++ b/includes/powder.h @@ -804,6 +804,8 @@ void detach(int i); void part_change_type(int i, int x, int y, int t); +void get_gravity_field(int x, int y, float particleGrav, float newtonGrav, float *pGravX, float *pGravY); + int InCurrentBrush(int i, int j, int rx, int ry); int get_brush_flags(); diff --git a/src/elementdata.c b/src/elementdata.c index ddcb9b2a6..f4429229b 100644 --- a/src/elementdata.c +++ b/src/elementdata.c @@ -100,7 +100,7 @@ part_type ptypes[PT_NUM] = {"GLOW", PIXPACK(0x445464), 0.3f, 0.02f * CFDS, 0.98f, 0.80f, 0.0f, 0.15f, 0.00f, 0.000f * CFDS, 2, 0, 0, 0, 2, 1, 1, 40, SC_LIQUID, R_TEMP+20.0f+273.15f, 44, "Glow, Glows under pressure", ST_LIQUID, TYPE_LIQUID|PROP_LIFE_DEC, &update_GLOW, &graphics_GLOW}, {"BRCK", PIXPACK(0x808080), 0.0f, 0.00f * CFDS, 0.90f, 0.00f, 0.0f, 0.0f, 0.00f, 0.000f * CFDS, 0, 0, 0, 0, 1, 1, 1, 100, SC_SOLIDS, R_TEMP+0.0f +273.15f, 251, "Brick, breakable building material.", ST_SOLID, TYPE_SOLID|PROP_HOT_GLOW, NULL, NULL}, {"CFLM", PIXPACK(0x8080FF), 0.9f, 0.04f * CFDS, 0.97f, 0.20f, 0.0f, -0.1f, 0.00f, 0.0005f * CFDS, 1, 0, 0, 0, 1, 1, 1, 2, SC_EXPLOSIVE, 0.0f, 88, "Sub-zero flame.", ST_LIQUID, TYPE_GAS|PROP_LIFE_DEC|PROP_LIFE_KILL, NULL, &graphics_HFLM}, - {"FIRW", PIXPACK(0xFFA040), 0.7f, 0.02f * CFDS, 0.96f, 0.80f, -0.99f, 0.1f, 0.00f, 0.000f * CFDS, 1, 0, 0, 0, 30, 1, 1, 55, SC_EXPLOSIVE, R_TEMP+0.0f +273.15f, 70, "Fireworks!", ST_SOLID, TYPE_PART|PROP_LIFE_DEC, &update_FIRW, &graphics_FIRW}, + {"FIRW", PIXPACK(0xFFA040), 0.4f, 0.02f * CFDS, 0.96f, 0.95f, -0.5f, 0.1f, 0.00f, 0.000f * CFDS, 1, 0, 0, 0, 30, 1, 1, 55, SC_EXPLOSIVE, R_TEMP+0.0f +273.15f, 70, "Fireworks!", ST_SOLID, TYPE_PART|PROP_LIFE_DEC, &update_FIRW, &graphics_FIRW}, {"FUSE", PIXPACK(0x0A5706), 0.0f, 0.00f * CFDS, 0.90f, 0.00f, 0.0f, 0.0f, 0.0f, 0.0f * CFDS, 0, 0, 0, 0, 20, 1, 1, 100, SC_SOLIDS, R_TEMP+0.0f +273.15f, 200, "Solid. Burns slowly. Ignites at somewhat high temperatures and electricity.", ST_SOLID, TYPE_SOLID, &update_FUSE, NULL}, {"FSEP", PIXPACK(0x63AD5F), 0.7f, 0.02f * CFDS, 0.96f, 0.80f, 0.0f, 0.1f, 0.00f, 0.000f * CFDS, 1, 0, 0, 0, 30, 1, 1, 70, SC_POWDERS, R_TEMP+0.0f +273.15f, 70, "Fuse Powder. See FUSE.", ST_SOLID, TYPE_PART, &update_FSEP, NULL}, {"AMTR", PIXPACK(0x808080), 0.7f, 0.02f * CFDS, 0.96f, 0.80f, 0.00f, 0.10f, 1.00f, 0.0000f * CFDS, 0, 0, 0, 0, 0, 1, 1, 100, SC_NUCLEAR, R_TEMP+0.0f +273.15f, 70, "Anti-Matter, Destroys a majority of particles", ST_NONE, TYPE_PART, &update_AMTR, NULL}, //Maybe TYPE_ENERGY? diff --git a/src/elements/firw.c b/src/elements/firw.c index 3f392b69d..d0e863ee5 100644 --- a/src/elements/firw.c +++ b/src/elements/firw.c @@ -29,8 +29,19 @@ int update_FIRW(UPDATE_FUNC_ARGS) { rt = parts[r>>8].type; if (rt==PT_FIRE||rt==PT_PLSM||rt==PT_THDR) { + float gx, gy, multiplier; + get_gravity_field(x, y, ptypes[PT_FIRW].gravity, 1.0f, &gx, &gy); + if (gx*gx+gy*gy < 0.001f) + { + float angle = (rand()%6284)*0.001f;//(in radians, between 0 and 2*pi) + gx += sinf(angle)*ptypes[PT_FIRW].gravity*0.5f; + gy += cosf(angle)*ptypes[PT_FIRW].gravity*0.5f; + } parts[i].tmp = 1; - parts[i].life = rand()%40+60; + parts[i].life = rand()%10+20; + multiplier = (parts[i].life+20)*0.2f/sqrtf(gx*gx+gy*gy); + parts[i].vx -= gx*multiplier; + parts[i].vy -= gy*multiplier; } } } @@ -38,8 +49,7 @@ int update_FIRW(UPDATE_FUNC_ARGS) { if (parts[i].life<=0) { parts[i].tmp=2; } else { - // TODO: different gravity modes + Newtonian gravity - parts[i].vy = -parts[i].life*0.04f - 0.1f; + parts[i].flags &= ~FLAG_STAGNANT; } } else if (parts[i].tmp>=2) { @@ -54,8 +64,8 @@ int update_FIRW(UPDATE_FUNC_ARGS) { { magnitude = ((rand()%60)+40)*0.05f; angle = (rand()%6284)*0.001f;//(in radians, between 0 and 2*pi) - parts[np].vx = parts[i].vx + cosf(angle)*magnitude; - parts[np].vy = parts[i].vy + sinf(angle)*magnitude - 2.5f; + parts[np].vx = parts[i].vx*0.5f + cosf(angle)*magnitude; + parts[np].vy = parts[i].vy*0.5f + sinf(angle)*magnitude; parts[np].ctype = col; parts[np].tmp = 1; parts[np].life = rand()%40+70; diff --git a/src/elements/fwrk.c b/src/elements/fwrk.c index f54877b10..d6dc59c35 100644 --- a/src/elements/fwrk.c +++ b/src/elements/fwrk.c @@ -17,22 +17,42 @@ int update_FWRK(UPDATE_FUNC_ARGS) { int r, rx, ry, np; - if ((parts[i].temp>400&&(9+parts[i].temp/40)>rand()%100000&&parts[i].life==0&&!pmap[y-1][x])||parts[i].ctype==PT_DUST) + if (parts[i].life==0 && ((parts[i].temp>400&&(9+parts[i].temp/40)>rand()%100000&&surround_space)||parts[i].ctype==PT_DUST)) { - np = create_part(-1, x , y-1 , PT_FWRK); - if (np!=-1) + float gx, gy, multiplier, gmax; + int randTmp; + get_gravity_field(x, y, ptypes[PT_FWRK].gravity, 1.0f, &gx, &gy); + if (gx*gx+gy*gy < 0.001f) { - parts[np].vy = rand()%8-22; - parts[np].vx = rand()%20-rand()%20; - parts[np].life=rand()%15+25; - parts[np].dcolour = parts[i].dcolour; - kill_part(i); - return 1; + float angle = (rand()%6284)*0.001f;//(in radians, between 0 and 2*pi) + gx += sinf(angle)*ptypes[PT_FWRK].gravity*0.5f; + gy += cosf(angle)*ptypes[PT_FWRK].gravity*0.5f; + } + gmax = fmaxf(fabsf(gx), fabsf(gy)); + if (eval_move(PT_FWRK, (int)(x-(gx/gmax)+0.5f), (int)(y-(gy/gmax)+0.5f), NULL)) + { + multiplier = 15.0f/sqrtf(gx*gx+gy*gy); + + //Some variation in speed parallel to gravity direction + randTmp = (rand()%200)-100; + gx += gx*randTmp*0.002f; + gy += gy*randTmp*0.002f; + //and a bit more variation in speed perpendicular to gravity direction + randTmp = (rand()%200)-100; + gx += -gy*randTmp*0.005f; + gy += gx*randTmp*0.005f; + + parts[i].life=rand()%10+18; + parts[i].ctype=0; + parts[i].vx -= gx*multiplier; + parts[i].vy -= gy*multiplier; + parts[i].dcolour = parts[i].dcolour; + return 0; } } if (parts[i].life>=45) parts[i].life=0; - if ((parts[i].life<3&&parts[i].life>0)||(parts[i].vy>6&&parts[i].life>0)) + if (parts[i].life<3&&parts[i].life>0) { int r = (rand()%245+11); int g = (rand()%245+11); @@ -47,8 +67,8 @@ int update_FWRK(UPDATE_FUNC_ARGS) { { magnitude = ((rand()%60)+40)*0.05f; angle = (rand()%6284)*0.001f;//(in radians, between 0 and 2*pi) - parts[np].vx = parts[i].vx + cosf(angle)*magnitude; - parts[np].vy = parts[i].vy + sinf(angle)*magnitude - 2.5f; + parts[np].vx = parts[i].vx*0.5f + cosf(angle)*magnitude; + parts[np].vy = parts[i].vy*0.5f + sinf(angle)*magnitude; parts[np].ctype = col; parts[np].tmp = 1; parts[np].life = rand()%40+70; diff --git a/src/powder.c b/src/powder.c index da715f6f0..c7d3e64d9 100644 --- a/src/powder.c +++ b/src/powder.c @@ -56,6 +56,28 @@ int pmap_count[YRES][XRES]; unsigned cb_pmap[YRES][XRES]; unsigned photons[YRES][XRES]; +void get_gravity_field(int x, int y, float particleGrav, float newtonGrav, float *pGravX, float *pGravY) +{ + *pGravX = newtonGrav*gravx[(y/CELL)*(XRES/CELL)+(x/CELL)]; + *pGravY = newtonGrav*gravy[(y/CELL)*(XRES/CELL)+(x/CELL)]; + switch (gravityMode) + { + default: + case 0: //normal, vertical gravity + *pGravY += particleGrav; + break; + case 1: //no gravity + break; + case 2: //radial gravity + if (x-XCNTR != 0 || y-YCNTR != 0) + { + float pGravMult = particleGrav/sqrtf((x-XCNTR)*(x-XCNTR) + (y-YCNTR)*(y-YCNTR)); + *pGravX -= pGravMult * (float)(x - XCNTR); + *pGravY -= pGravMult * (float)(y - YCNTR); + } + } +} + static int pn_junction_sprk(int x, int y, int pt) { unsigned r = pmap[y][x];