PageRenderTime 186ms CodeModel.GetById 60ms app.highlight 48ms RepoModel.GetById 73ms app.codeStats 0ms

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