Advertisement
tdog442

Plasma Ball prototype

Jun 12th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. #include "simulation/Elements.h"
  2. //#include "mover.h"
  3. //spawns with random life between 200 and 800 frames
  4.  
  5. Element_PBAL::Element_PBAL()
  6. {
  7. Identifier = "DEFAULT_PT_PBAL";
  8. Name = "PBAL";
  9. Colour = PIXPACK(0x7DF9FF);
  10. MenuVisible = 1;
  11. MenuSection = SC_SPECIAL;
  12. Enabled = 1;
  13.  
  14. Advection = 0.0f;
  15. AirDrag = 0.001f * CFDS;
  16. AirLoss = 0.97f;
  17. Loss = 0.20f;
  18. Collision = 0.0f;
  19. Gravity = 0.0f;
  20. Diffusion = 0.00f;
  21. HotAir = 0.000f * CFDS;
  22. Falldown = 0;
  23.  
  24. Flammable = 0;
  25. Explosive = 0;
  26. Meltable = 0;
  27. Hardness = 0;
  28.  
  29. Weight = 10;
  30.  
  31. Temperature = R_TEMP+MAX_TEMP;
  32. HeatConduct = 251;
  33. Description = "Plasma Ball. Floats and behaves like a moving solid. Explosive.";
  34.  
  35. State = ST_SOLID;
  36. Properties = TYPE_SOLID|PROP_LIFE_DEC;
  37.  
  38. LowPressure = IPL;
  39. LowPressureTransition = NT;
  40. HighPressure = IPH;
  41. HighPressureTransition = NT;
  42. LowTemperature = ITL;
  43. LowTemperatureTransition = NT;
  44. HighTemperature = ITH;
  45. HighTemperatureTransition = NT;
  46.  
  47. Update = &Element_PBAL::update;
  48. Graphics = &Element_PBAL::graphics;
  49. }
  50.  
  51.  
  52. int Element_PBAL::update(UPDATE_FUNC_ARGS)
  53. {
  54. int msindex[256];
  55. int msnum[256];
  56. float msvx[256];
  57. float msvy[256];
  58. float msrotation[256];
  59. float newmsrotation[256];
  60. int numballs;
  61. int ms_rotation;
  62. int r, rt, rx, ry;
  63. for (rx=-2; rx<=3; rx++)
  64. for (ry=-2; ry<=3; ry++)
  65. if (BOUNDS_CHECK) {
  66. r = pmap[y+ry][x+rx];
  67. if (!r)
  68. continue;
  69. int rt = r&0xFF;
  70. if(parts[i].tmp==0)
  71. {
  72. parts[i].temp==MAX_TEMP;
  73. if (((r&0xFF)!=PT_PBAL))
  74. {
  75. if(!(rand()%500))
  76. {
  77. sim->create_part(-1,x+rand()%3-1,y+rand()%3-1,PT_PLSM);
  78. }
  79. }
  80. if ((sim->elements[rt].Properties&PROP_CONDUCTS) && !(rt==PT_WATR||rt==PT_SLTW))
  81. {
  82. parts[r>>8].life = 4;
  83. parts[r>>8].ctype = rt;
  84. sim->part_change_type(r>>8,x+rx,y+ry,PT_SPRK);
  85. }
  86. }
  87. if(parts[i].tmp>=1)
  88. {
  89. parts[i].temp= R_TEMP+0.0f +273.15f;
  90. if ((sim->elements[rt].Properties&PROP_CONDUCTS) && !(rt==PT_WATR||rt==PT_SLTW))
  91. {
  92. parts[r>>8].life = 4;
  93. parts[r>>8].ctype = rt;
  94. sim->part_change_type(r>>8,x+rx,y+ry,PT_SPRK);
  95. }
  96. }
  97. }
  98. int bn = parts[i].tmp2, type, bounce = 4;
  99. float tmp = 0, tmp2 = 0;
  100. bool pop = false;
  101. if (bn < 0 || bn > 255)
  102. return 0;
  103. //kill moving solid control particle with a lot of pressure (other ones dissapear at 30 pressure)
  104. if (!tmp && !tmp2 && sim->pv[y/CELL][x/CELL] > 10 || sim->pv[y/CELL][x/CELL] < -10)
  105. {
  106. parts[bn].ctype = PT_BANG;
  107. if ((pmap[y][x]>>8 == i))
  108. {
  109. int tempvalue = 2;
  110. sim->flood_prop(x, y, offsetof(Particle, tmp), &tempvalue, StructProperty::Integer);
  111. }
  112. float otemp = parts[i].temp-273.15f;
  113. //Explode!!
  114. sim->pv[y/CELL][x/CELL] += 0.5f;
  115. if(!(rand()%3))
  116. {
  117. if(!(rand()%2))
  118. {
  119. sim->create_part(i, x, y, PT_PLSM);
  120. }
  121. parts[i].temp = restrict_flt((MAX_TEMP/4)+otemp, MIN_TEMP, MAX_TEMP);
  122. }
  123. else
  124. {
  125. if(!(rand()%15))
  126. {
  127. sim->create_part(i, x, y, PT_EMBR);
  128. parts[i].tmp = 0;
  129. parts[i].life = 50;
  130. parts[i].temp = restrict_flt((MAX_TEMP/3)+otemp, MIN_TEMP, MAX_TEMP);
  131. parts[i].vx = rand()%20-10;
  132. parts[i].vy = rand()%20-10;
  133. }
  134. }
  135. msindex[bn]-bn;
  136. msindex[bn]--;
  137. return 1;
  138. }
  139. if(parts[i].life<=0&&parts[i].tmp==0)
  140. {
  141. pop=true;
  142. }
  143. //center control particle was killed, ball explodes
  144. if ((!msindex[bn] && parts[i].tmp==0) || (parts[bn].ctype==PT_BANG && parts[i].tmp==0) || pop==true)
  145. {
  146. if ((pmap[y][x]>>8 == i))
  147. {
  148. int tempvalue = 2;
  149. sim->flood_prop(x, y, offsetof(Particle, tmp), &tempvalue, StructProperty::Integer);
  150. }
  151. else
  152. {
  153. float otemp = parts[i].temp-273.15f;
  154. //Explode!!
  155. sim->pv[y/CELL][x/CELL] += 0.5f;
  156. if(!(rand()%3))
  157. {
  158. if(!(rand()%2))
  159. {
  160. sim->create_part(i, x, y, PT_PLSM);
  161. }
  162. parts[i].temp = restrict_flt((MAX_TEMP/4)+otemp, MIN_TEMP, MAX_TEMP);
  163. }
  164. else
  165. {
  166. if(!(rand()%15))
  167. {
  168. sim->create_part(i, x, y, PT_EMBR);
  169. parts[i].tmp = 0;
  170. parts[i].life = 50;
  171. parts[i].temp = restrict_flt((MAX_TEMP/3)+otemp, MIN_TEMP, MAX_TEMP);
  172. parts[i].vx = rand()%20-10;
  173. parts[i].vy = rand()%20-10;
  174. }
  175. else
  176. {
  177. sim->kill_part(i);
  178. }
  179. }
  180. return 1;
  181. }
  182. }
  183. if (!msindex[bn] && parts[i].tmp>=1)
  184. {
  185. sim->kill_part(i);
  186. return 1;
  187. }
  188. //speed improvement if rotation disabled, no need to do trigonometry
  189. else if (!ms_rotation)
  190. {
  191. tmp = parts[i].pavg[0];
  192. tmp2 = parts[i].pavg[1];
  193. }
  194. //determine rotated x and y coordinates relative to center
  195. else if (parts[i].pavg[0] != 0)
  196. {
  197. float angle = atan((float)parts[i].pavg[1]/parts[i].pavg[0]);
  198. float distance = sqrt(pow((float)parts[i].pavg[0],2)+pow((float)parts[i].pavg[1],2));
  199. if (parts[i].pavg[0] < 0)
  200. angle += M_PI;
  201. tmp = distance*cos(angle+msrotation[bn]);
  202. tmp2 = distance*sin(angle+msrotation[bn]);
  203. }
  204. else if (parts[i].pavg[1] != 0)
  205. {
  206. float angle = M_PI/2;
  207. tmp = parts[msindex[bn]-1].x + parts[i].pavg[1]*cos(angle+msrotation[bn]);
  208. if (parts[i].pavg[1] < 0)
  209. tmp2 = parts[i].pavg[1]*sin(angle+msrotation[bn]);
  210. else
  211. tmp2 = -1*parts[i].pavg[1]*sin(angle+msrotation[bn]);
  212. }
  213. type = pmap[y+1][x]&0xFF;
  214. //bottom side collision
  215. if (tmp2 > 0 && type && y+1 < YRES && ((type != parts[i].type && !sim->eval_move(parts[i].type,x,y+1,NULL)) || (type == parts[i].type && parts[pmap[y+1][x]>>8].tmp2 != bn)))
  216. {
  217. parts[i].vy -= tmp2*bounce;
  218. newmsrotation[bn] -= tmp/50000;
  219. }
  220. type = pmap[y-1][x]&0xFF;
  221. //top side collision
  222. if (tmp2 < 0 && type && y-1 >= 0 && ((type != parts[i].type && !sim->eval_move(parts[i].type,x,y-1,NULL)) || (type == parts[i].type && parts[pmap[y-1][x]>>8].tmp2 != bn)))
  223. {
  224. parts[i].vy -= tmp2*bounce;
  225. newmsrotation[bn] -= tmp/50000;
  226. }
  227. type = pmap[y][x+1]&0xFF;
  228. //right side collision
  229. if (tmp > 0 && type && x+1 < XRES && ((type != parts[i].type && !sim->eval_move(parts[i].type,x+1,y,NULL)) || (type == parts[i].type && parts[pmap[y][x+1]>>8].tmp2 != bn)))
  230. {
  231. parts[i].vx -= tmp*bounce;
  232. newmsrotation[bn] -= tmp/50000;
  233. }
  234. type = pmap[y][x-1]&0xFF;
  235. //left side collision
  236. if (tmp < 0 && type && x-1 >= 0 && ((type != parts[i].type && !sim->eval_move(parts[i].type,x-1,y,NULL)) || (type == parts[i].type && parts[pmap[y][x-1]>>8].tmp2 != bn)))
  237. {
  238. parts[i].vx -= tmp*bounce;
  239. newmsrotation[bn] -= tmp/50000;
  240. }
  241. if(sim->pv[x/CELL][y/CELL] >= 0.3)
  242. {
  243. parts[bn].vx = sim->vx[y/CELL][x/CELL];
  244. parts[bn].vy = sim->vy[y/CELL][x/CELL];
  245. newmsrotation[bn] -= tmp/50000;
  246. }
  247. return(0);
  248. }
  249.  
  250.  
  251. int Element_PBAL::graphics(GRAPHICS_FUNC_ARGS)
  252. {
  253. *firea = 160;
  254. *fireg = 192;
  255. *fireb = 255;
  256. *firer = 144;
  257. *pixel_mode |= FIRE_ADD;
  258. return NULL;
  259. }
  260.  
  261.  
  262. Element_PBAL::~Element_PBAL() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement