/TheElements/jni/update.c

http://thelements.googlecode.com/ · C · 407 lines · 340 code · 22 blank · 45 comment · 220 complexity · 6a4f35448fa566ce154cd38424cb119a MD5 · raw file

  1. /*
  2. * update.c
  3. * -----------------------------------
  4. * Defines the function UpdateView(), which
  5. * is called every frame to update all the
  6. * particles' positions.
  7. */
  8. #include "update.h"
  9. void UpdateView(void)
  10. {
  11. if (shouldClear)
  12. {
  13. rsetup();
  14. shouldClear = 0;
  15. }
  16. //If the finger is down
  17. if (fd == 1)
  18. {
  19. if (ym != 0)
  20. {
  21. int yc;
  22. int xc;
  23. for (yc = size; yc >= -size; yc--)
  24. {
  25. for (xc = -size; xc <= size; xc++)
  26. {
  27. if ((xc * xc) + (yc * yc) <= (size * size))
  28. {
  29. //Draw randomized for anything other than non-moving, Eraser, and Drag
  30. if (!solid[celement] && celement != 16 && celement!= 3)
  31. {
  32. if (rand() % 3 == 1 && xc + xm < maxx && xc + xm > 0 && yc + ym < maxy && yc + ym > 0 && allcoords[(int) (xc + xm)][(int) (yc + ym)] == -1)
  33. {
  34. CreatePoint(xm + xc, ym + yc, celement);
  35. }
  36. }
  37. //Draw fully for anything non-moving
  38. else if (solid[celement])
  39. {
  40. if (xc + xm < maxx && xc + xm > 0 && yc + ym < maxy && yc + ym > 0 && allcoords[(int) (xc + xm)][(int) (yc + ym)] == -1)
  41. {
  42. CreatePoint(xm + xc, ym + yc, celement);
  43. }
  44. }
  45. //Special Drag case
  46. else if (celement == 16)
  47. {
  48. if (allcoords[lmx + xc][lmy + yc] != -1 && fallvel[element[allcoords[lmx + xc][lmy + yc]]] != 0 && xc + lmx < maxx && xc + lmx > 0 && yc + lmy < maxy && yc + lmy > 0)
  49. {
  50. xvel[allcoords[lmx + xc][lmy + yc]] += (xm - lmx);
  51. yvel[allcoords[lmx + xc][lmy + yc]] += (ym - lmy);
  52. }
  53. }
  54. //Special Eraser case
  55. else
  56. {
  57. if (allcoords[(int) (xc + xm)][(int) (yc + ym)] != -1 && xc + xm < maxx && xc + xm > 0 && yc + ym < maxy && yc + ym > 0)
  58. {
  59. DeletePoint(allcoords[xm + xc][ym + yc]);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. if (play == 1)
  68. {
  69. int counter;
  70. int rtop; //used to prevent bugs when fire reaches the top
  71. int tempx, tempy, ox, oy; //For speed we're going to create temp variables to store stuff
  72. int oelement; //these are also used to check to see if the element has changed to do stuff about freezing particles
  73. // Move the particles and do collisions
  74. for (counter = 0; counter < TPoints; counter++)
  75. {
  76. //If the particle is set and unfrozen
  77. if (set[counter] && frozen[counter] < 4)
  78. {
  79. //Random delete of fire
  80. int random = rand();
  81. if (element[counter] == 5 && ((random % 7) == 0))
  82. {
  83. DeletePoint(counter);
  84. continue;
  85. }
  86. //Set the temp and old variables
  87. oy = (int) y[counter];
  88. ox = (int) x[counter];
  89. oldy[counter] = oy;
  90. oldx[counter] = ox;
  91. oelement = element[counter];
  92. //If accel control, do yvel based on that
  93. if ((int) gravy != 0 && accelcon)
  94. {
  95. y[counter] += ((gravy / 9.8) * fallvel[oelement] + yvel[counter]);
  96. }
  97. //Otherwise, just do the fallvelocity
  98. else if (accelcon == 0)
  99. {
  100. y[counter] += fallvel[oelement] + yvel[counter];
  101. }
  102. //Accelerometer control, but no gravity (held horizontal)
  103. else
  104. {
  105. y[counter] += yvel[counter];
  106. }
  107. //If accel control is on, calculate new x using the gravity set
  108. if ((int) gravx != 0 && accelcon == 1)
  109. {
  110. x[counter] += ((gravx / 9.8) * fallvel[oelement] + xvel[counter]);
  111. }
  112. //Otherwise, just add xvel
  113. else
  114. {
  115. x[counter] += xvel[counter];
  116. }
  117. if (xvel[counter] > 0)
  118. {
  119. //Check for breaking
  120. if ((oelement == 15 || oelement == 21) && xvel[counter] > 5)
  121. {
  122. element[counter] = 0; //change particle to sand if the velocity on the wall is great enough
  123. setBitmapColor((int) x[counter], (int) y[counter], 0); //Set the color also
  124. }
  125. else if (oelement == 15 || oelement == 21)
  126. {
  127. x[counter] = ox;
  128. y[counter] = oy;
  129. xvel[counter] = 0;
  130. }
  131. else
  132. {
  133. //Default case: reduce velocity
  134. xvel[counter] -= 1;
  135. }
  136. }
  137. else if (xvel[counter] < 0)
  138. {
  139. //Check for breaking
  140. if ((oelement == 15 || oelement == 21) && xvel[counter] < -5)
  141. {
  142. element[counter] = 0; //change particle to sand if the velocity on the wall is great enough
  143. setBitmapColor((int) x[counter], (int) y[counter] ,0); //Set the color also
  144. }
  145. else if (oelement == 15 || oelement == 21)
  146. {
  147. x[counter] = ox;
  148. y[counter] = oy;
  149. xvel[counter] = 0;
  150. }
  151. else
  152. {
  153. //Default case: decrease speed by one
  154. xvel[counter] += 1;
  155. }
  156. }
  157. if (yvel[counter] > 0)
  158. {
  159. //Check for breaking
  160. if ((oelement == 15 || oelement == 21) && yvel[counter] > 5)
  161. {
  162. element[counter] = 0; //change particle to sand if the velocity on the wall is great enough
  163. setBitmapColor((int) x[counter], (int) y[counter], 0); //Set the color also
  164. }
  165. else if (oelement == 15 || oelement == 21)
  166. {
  167. x[counter] = ox;
  168. y[counter] = oy;
  169. yvel[counter] = 0;
  170. }
  171. else
  172. {
  173. //Default case: decrease speed by 1
  174. yvel[counter] -= 1;
  175. }
  176. }
  177. else if (yvel[counter] < 0)
  178. {
  179. //Check for breaking
  180. if ((oelement == 15 || oelement == 21) && yvel[counter] < -5)
  181. {
  182. element[counter] = 0; //change particle to sand if the velocity on the wall is great enough
  183. setBitmapColor((int) x[counter], (int) y[counter], 0); //Set the color also
  184. }
  185. else if (oelement == 15 || oelement == 21)
  186. {
  187. x[counter] = ox;
  188. y[counter] = oy;
  189. yvel[counter] = 0;
  190. }
  191. else
  192. {
  193. //Default case: decrease speed by 1
  194. yvel[counter] += 1;
  195. }
  196. }
  197. //Boundary checking
  198. if ((int) y[counter] >= maxy)
  199. {
  200. y[counter] = oy;
  201. yvel[counter] = 0;
  202. xvel[counter] = 0;
  203. }
  204. if ((int) x[counter] >= maxx || (int) x[counter] <= 0)
  205. {
  206. x[counter] = ox;
  207. xvel[counter] = 0;
  208. yvel[counter] = 0;
  209. }
  210. if ((int) y[counter] <= 0) //If the particle is above the top of the screen
  211. {
  212. if (element[counter] == 5) //If it's fire
  213. {
  214. //Move it back
  215. y[counter] = oy;
  216. x[counter] = ox;
  217. //Delete it
  218. DeletePoint(counter);
  219. continue;
  220. }
  221. else //Not fire
  222. {
  223. //Just bounce it back and kill velocity
  224. y[counter] = oy;
  225. yvel[counter] = 0;
  226. xvel[counter] = 0;
  227. if (element[counter] == 18) //If it's steam
  228. {
  229. xvel[counter] = rand() % 3 - 1; //Add a random velocity
  230. }
  231. }
  232. }
  233. //Set other temp variables for next section
  234. tempx = (int) x[counter];
  235. tempy = (int) y[counter];
  236. //Special cycles
  237. if (fireburn[element[counter]] == 1) //Fire cycle
  238. {
  239. frozen[counter] = 0;
  240. random = rand();
  241. if (collision[element[allcoords[tempx + 1][tempy]]][element[counter]] == 6 && random % 3 != 0)
  242. {
  243. setElement(allcoords[tempx + 1][tempy], 5);
  244. }
  245. random = rand();
  246. if (collision[element[allcoords[tempx][tempy - 1]]][element[counter]] == 6 && random % 3 != 0)
  247. {
  248. setElement(allcoords[tempx][tempy - 1], 5);
  249. }
  250. random = rand();
  251. if (collision[element[allcoords[tempx - 1][tempy]]][element[counter]] == 6 && random % 3 != 0)
  252. {
  253. setElement(allcoords[tempx - 1][tempy], 5);
  254. }
  255. random = rand();
  256. if (collision[element[allcoords[tempx][tempy + 1]]][element[counter]] == 6 && random % 3 != 0)
  257. {
  258. setElement(allcoords[tempx][tempy + 1], 5);
  259. yvel[allcoords[tempx][tempy + 1]] = 2;
  260. }
  261. random = rand();
  262. if (collision[element[allcoords[tempx + 1][tempy + 1]]][element[counter]] == 6 && random % 3 != 0)
  263. {
  264. setElement(allcoords[tempx + 1][tempy + 1], 5);
  265. yvel[allcoords[tempx][tempy + 1]] = 2;
  266. }
  267. random = rand();
  268. if (collision[element[allcoords[tempx - 1][tempy + 1]]][element[counter]] == 6 && random % 3 != 0)
  269. {
  270. setElement(allcoords[tempx - 1][tempy + 1], 5);
  271. yvel[allcoords[tempx][tempy + 1]] = 2;
  272. }
  273. random = rand();
  274. if (collision[element[allcoords[tempx + 1][tempy - 1]]][element[counter]] == 6 && random % 3 != 0)
  275. {
  276. setElement(allcoords[tempx + 1][tempy - 1], 5);
  277. }
  278. random = rand();
  279. if (collision[element[allcoords[tempx - 1][tempy - 1]]][element[counter]] == 6 && random % 3 != 0)
  280. {
  281. setElement(allcoords[tempx - 1][tempy - 1], 5);
  282. }
  283. }
  284. if (element[counter] == 8) //Spawn cycle
  285. {
  286. frozen[counter] = 0;
  287. int check1, check2, temp;
  288. for (check1 = -2; check1 <= 2; check1++)
  289. {
  290. for (check2 = -2; check2 <= 2; check2++)
  291. {
  292. if (tempx + check1 > 1 && tempx + check1 < maxx && tempy + check2 >= 0 && tempy + check2 < maxy)
  293. {
  294. temp = allcoords[tempx + check1][tempy + check2];
  295. if (temp != -1 && element[temp] == 7) //There's a generator adjacent
  296. {
  297. element[temp] = 8; //Change the generator to a spawn
  298. spawn[temp] = spawn[counter]; //Make the spawn element the same as this one
  299. }
  300. else if (temp == -1 && rand() % 200 == 0 && loq < TPoints - 1) //There's an empty spot
  301. {
  302. CreatePoint(tempx + check1, tempy + check2, spawn[counter]); //1/200 chance of spawning
  303. }
  304. }
  305. }
  306. }
  307. }
  308. if (growing[element[counter]] == 1) //Ice cycle
  309. {
  310. frozen[counter] = 0;
  311. int check1, check2, temp;
  312. for (check1 = -1; check1 <= 1; check1++)
  313. {
  314. for (check2 = -1; check2 <= 1; check2++)
  315. {
  316. temp = allcoords[tempx + check1][tempy + check2];
  317. if (temp != -1 && element[temp] == 1 && rand() % 10 == 0)
  318. {
  319. //Change the water to ice
  320. setElement(temp, element[counter]);
  321. frozen[temp] = 0;
  322. }
  323. }
  324. }
  325. }
  326. if (condensing[element[counter]] != -1) //Steam cycle
  327. {
  328. frozen[counter] = 0;
  329. if (rand() % 200 == 0) //1/200 chance
  330. {
  331. setElement(counter, condensing[element[counter]]); //"Condense" the steam
  332. }
  333. }
  334. //Updating allcoords and collision resolution
  335. int atemporary = allcoords[tempx][tempy];
  336. //If the space the particle is trying to move to is taken
  337. if (atemporary != -1 && atemporary != counter)
  338. {
  339. int secondElementTemp = element[atemporary];
  340. collide(counter, atemporary); //Call collision() on the two particles
  341. //If nothing has changed
  342. if (x[counter] == ox
  343. && y[counter] == oy
  344. && element[counter] == oelement
  345. && x[atemporary] == tempx
  346. && y[atemporary] == tempy
  347. && element[atemporary] == secondElementTemp
  348. && xvel[counter] == 0
  349. && yvel[counter] == 0)
  350. {
  351. frozen[counter]++; //Increment the frozen count
  352. }
  353. else
  354. {
  355. if (x[counter] != ox || y[counter] != oy || element[counter] != oelement)
  356. {
  357. //unFreeze stuff around the primary particle because stuff has changed with it
  358. unFreezeParticles(ox, oy);
  359. }
  360. if (x[atemporary] != tempx || y[atemporary] != tempy || element[atemporary] != secondElementTemp)
  361. {
  362. //unFreeze stuff around the secondary particle because stuff has changed with it
  363. unFreezeParticles(tempx, tempy);
  364. }
  365. }
  366. }
  367. else //Space particle is trying to move to is free or is itself
  368. {
  369. //Space particle is trying to move to is free
  370. if (atemporary != counter)
  371. {
  372. //Clear the old spot
  373. allcoords[ox][oy] = -1;
  374. setBitmapColor(ox, oy, 3);
  375. //Unfreeze particles around old spot
  376. unFreezeParticles(ox, oy);
  377. //Set new spot
  378. allcoords[tempx][tempy] = counter;
  379. setBitmapColor(tempx, tempy, element[counter]);
  380. }
  381. }
  382. }
  383. }
  384. }
  385. }