PageRenderTime 327ms CodeModel.GetById 111ms app.highlight 88ms RepoModel.GetById 123ms app.codeStats 0ms

/FallingSandpaper/jni/update.c

http://thelements.googlecode.com/
C | 474 lines | 410 code | 30 blank | 34 comment | 221 complexity | 1941e029d693aa5b8ca4e589a8c3c411 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
 14	if (fd == 1) // if the finger is down
 15	{
 16		// If the mouse is above the menu, there are not too many points, and there isn't already sand there, then make a piece of sand
 17		if (ym != 0)
 18		{
 19			int yc;
 20			int xc;
 21			for (yc = size; yc >= -size; yc--)
 22			{
 23				for (xc = -size; xc <= size; xc++)
 24				{
 25					if ((xc * xc) + (yc * yc) <= (size * size))
 26					{
 27
 28						if (solid[celement] != 1 && celement != 16 && celement
 29								!= 3) //not wall, eraser, or plant, wind or fuse
 30						{
 31							if (xc + xm < maxx && xc + xm > 0 && yc + ym < maxy
 32									&& yc + ym > 0
 33									&& allcoords[(int) (xc + xm)][(int) (yc
 34											+ ym)] == -1 && rand() % 3 == 1)
 35							{
 36								CreatePoint(xm + xc, ym + yc, celement);
 37							}
 38						}
 39						else if (solid[celement] == 1) //wall or plant or fuse should be drawn solid
 40						{
 41							if (xc + xm < maxx && xc + xm > 0 && yc + ym < maxy
 42									&& yc + ym > 0
 43									&& allcoords[(int) (xc + xm)][(int) (yc
 44											+ ym)] == -1)
 45							{
 46								CreatePoint(xm + xc, ym + yc, celement);
 47							}
 48						}
 49						else if (celement == 16) //wind
 50						{
 51							if (xc + lmx < maxx && xc + lmx > 0 && yc + lmy
 52									< maxy && yc + lmy > 0)
 53							{
 54								if (allcoords[lmx + xc][lmy + yc] != -1)
 55								{
 56									if (fallvel[element[allcoords[lmx + xc][lmy
 57											+ yc]]] != 0)
 58									{
 59										xvel[allcoords[lmx + xc][lmy + yc]]
 60												+= (xm - lmx);
 61										yvel[allcoords[lmx + xc][lmy + yc]]
 62												+= (ym - lmy);
 63									}
 64								}
 65							}
 66						}
 67						else
 68						{ //eraser
 69
 70							if (xc + xm < maxx && xc + xm > 0 && yc + ym < maxy
 71									&& yc + ym > 0
 72									&& allcoords[(int) (xc + xm)][(int) (yc
 73											+ ym)] != -1)
 74							{
 75
 76								DeletePoint(allcoords[xm + xc][ym + yc]);
 77							}
 78
 79						}
 80					}
 81				}
 82			}
 83		}
 84
 85	}
 86
 87	if (play == 1)
 88	{
 89		int counter;
 90		int rtop; //used to prevent bugs when fire reaches the top
 91
 92		int tempx, tempy, ox, oy; //For speed we're going to create temp variables to store stuff
 93		int oelement; //these are also used to check to see if the element has changed to do stuff about freezing particles
 94
 95		//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "0");
 96
 97		// Move the particles and do collisions
 98		for (counter = 0; counter < TPoints; counter++)
 99		{
100			if (set[counter] == 1 && frozen[counter] < 4)
101			{
102				//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "1");
103
104
105				int random = rand();
106				if (element[counter] == 5 && ((random % 7) == 0))
107				{
108
109					unFreezeParticles(x[counter], y[counter]);
110					DeletePoint(counter);
111				}
112				else
113				{
114					oy = (int) y[counter];
115					ox = (int) x[counter];
116					oldy[counter] = oy;
117					oldx[counter] = ox;
118					oelement = element[counter];
119					if ((int) gravy != 0 && accelcon == 1)
120					{
121						y[counter] += ((gravy / 9.8) * fallvel[oelement]
122								+ yvel[counter]);
123					}
124					else if (accelcon == 0)
125					{ // no accelerometer control still needs to have stuff fall
126						y[counter] += fallvel[oelement] + yvel[counter];
127					}
128					else
129					{
130						y[counter] += yvel[counter]; //with accel tho, it shouldn't
131					}
132					if ((int) gravx != 0 && accelcon == 1)
133					{
134						x[counter] += ((gravx / 9.8) * fallvel[oelement]
135								+ xvel[counter]);
136					}
137					else
138					{
139						x[counter] += xvel[counter];
140					}
141					if (xvel[counter] > 0)
142					{
143						if ((oelement == 15 || oelement == 21) && xvel[counter]
144								> 5)
145						{
146							element[counter] = 0; //change particle to sand if the velocity on the wall is great enough							
147							setBitmapColor((int) x[counter], (int) y[counter],
148									0); //Set the color also
149						}
150						else if (oelement == 15 || oelement == 21)
151						{
152							x[counter] = ox;
153							y[counter] = oy;
154							xvel[counter] = 0;
155						}
156						else
157						{
158							xvel[counter] -= 1;
159						}
160					}
161					//element could have changed so don't use olement anymore
162					else if (xvel[counter] < 0)
163					{
164						if ((element[counter] == 15 || element[counter] == 21)
165								&& xvel[counter] < -5)
166						{
167							element[counter] = 0; //change particle to sand if the velocity on the wall is great enough							
168							setBitmapColor((int) x[counter], (int) y[counter],
169									0); //Set the color also
170						}
171						else if (element[counter] == 15 || element[counter]
172								== 21)
173						{
174							x[counter] = ox;
175							y[counter] = oy;
176							xvel[counter] = 0;
177						}
178						else
179						{
180							xvel[counter] += 1;
181						}
182					}
183					if (yvel[counter] > 0)
184					{
185						if ((element[counter] == 15 || element[counter] == 21)
186								&& yvel[counter] > 5)
187						{
188							element[counter] = 0; //change particle to sand if the velocity on the wall is great enough							
189							setBitmapColor((int) x[counter], (int) y[counter],
190									0); //Set the color also
191						}
192						else if (element[counter] == 15 || element[counter]
193								== 21)
194						{
195							x[counter] = ox;
196							y[counter] = oy;
197							yvel[counter] = 0;
198						}
199						else
200						{
201							yvel[counter] -= 1;
202						}
203					}
204					else if (yvel[counter] < 0)
205					{
206						if ((element[counter] == 15 || element[counter] == 21)
207								&& yvel[counter] < -5)
208						{
209							element[counter] = 0; //change particle to sand if the velocity on the wall is great enough							
210							setBitmapColor((int) x[counter], (int) y[counter],
211									0); //Set the color also
212						}
213						else if (element[counter] == 15 || element[counter]
214								== 21)
215						{
216							x[counter] = ox;
217							y[counter] = oy;
218							yvel[counter] = 0;
219						}
220						else
221						{
222							yvel[counter] += 1;
223						}
224					}
225					if ((int) y[counter] >= maxy)
226					{
227						y[counter] = oy;
228						yvel[counter] = 0;
229						xvel[counter] = 0;
230					}
231					if ((int) x[counter] >= maxx || (int) x[counter] <= 0)
232					{
233						x[counter] = ox;
234						xvel[counter] = 0;
235						yvel[counter] = 0;
236					}
237					if ((int) y[counter] <= 0) //If the particle is above the top of the screen
238					{
239						if (element[counter] == 5) //If it's fire
240						{
241							//Delete it
242							x[counter] = 0;
243							y[counter] = 0;
244							element[counter] = 0;
245							xvel[counter] = 0;
246							yvel[counter] = 0;
247							set[counter] = 0;
248							avail[loq] = counter;
249							loq++;
250							setBitmapColor(ox, oy, 3);
251							allcoords[ox][oy] = -1;
252							rtop = 1;
253						}
254						else //Not fire
255						{
256							//Just bounce it back and kill velocity
257							y[counter] = oy;
258							yvel[counter] = 0;
259							xvel[counter] = 0;
260							if (element[counter] == 18) //If it's steam
261							{
262								xvel[counter] = rand() % 3 - 1; //Add a random velocity
263							}
264						}
265					}
266					if (rtop == 0)
267					{
268						tempx = (int) x[counter];
269						tempy = (int) y[counter];
270
271						if (fireburn[element[counter]] == 1) //Fire cycle
272						{
273							frozen[counter] = 0;
274							random = rand();
275							if (collision[element[allcoords[tempx + 1][tempy]]][element[counter]]
276									== 6 && random % 3 != 0)
277							{
278								element[allcoords[tempx + 1][tempy]] = 5;
279								setBitmapColor(tempx + 1, tempy, 5);
280							}
281							random = rand();
282							if (collision[element[allcoords[tempx][tempy - 1]]][element[counter]]
283									== 6 && random % 3 != 0)
284							{
285								element[allcoords[tempx][tempy - 1]] = 5;
286								setBitmapColor(tempx, tempy - 1, 5);
287							}
288							random = rand();
289							if (collision[element[allcoords[tempx - 1][tempy]]][element[counter]]
290									== 6 && random % 3 != 0)
291							{
292								element[allcoords[tempx - 1][tempy]] = 5;
293								setBitmapColor(tempx - 1, tempy, 5);
294							}
295							random = rand();
296							if (collision[element[allcoords[tempx][tempy + 1]]][element[counter]]
297									== 6 && random % 3 != 0)
298							{
299								element[allcoords[tempx][tempy + 1]] = 5;
300								setBitmapColor(tempx, tempy + 1, 5);
301								yvel[allcoords[tempx][tempy + 1]] = 2;
302							}
303							random = rand();
304							if (collision[element[allcoords[tempx + 1][tempy
305									+ 1]]][element[counter]] == 6 && random % 3
306									!= 0)
307							{
308								element[allcoords[tempx + 1][tempy + 1]] = 5;
309								setBitmapColor(tempx + 1, tempy + 1, 5);
310								yvel[allcoords[tempx][tempy + 1]] = 2;
311							}
312							random = rand();
313							if (collision[element[allcoords[tempx - 1][tempy
314									+ 1]]][element[counter]] == 6 && random % 3
315									!= 0)
316							{
317								element[allcoords[tempx - 1][tempy + 1]] = 5;
318								setBitmapColor(tempx - 1, tempy + 1, 5);
319								yvel[allcoords[tempx][tempy + 1]] = 2;
320							}
321							random = rand();
322							if (collision[element[allcoords[tempx + 1][tempy
323									- 1]]][element[counter]] == 6 && random % 3
324									!= 0)
325							{
326								element[allcoords[tempx + 1][tempy - 1]] = 5;
327								setBitmapColor(tempx + 1, tempy - 1, 5);
328							}
329							random = rand();
330							if (collision[element[allcoords[tempx - 1][tempy
331									- 1]]][element[counter]] == 6 && random % 3
332									!= 0)
333							{
334								element[allcoords[tempx - 1][tempy - 1]] = 5;
335								setBitmapColor(tempx - 1, tempy - 1, 5);
336							}
337						}
338						if (element[counter] == 8) //Spawn cycle
339						{
340							frozen[counter] = 0;
341							int check1, check2, temp;
342							for (check1 = -2; check1 <= 2; check1++)
343							{
344								for (check2 = -2; check2 <= 2; check2++)
345								{
346									if (tempx + check1 > 1 && tempx + check1
347											< maxx && tempy + check2 >= 0
348											&& tempy + check2 < maxy)
349									{
350										temp = allcoords[tempx + check1][tempy
351												+ check2];
352										if (temp != -1 && element[temp] == 7) //There's a generator adjacent
353										{
354											element[temp] = 8; //Change the generator to a spawn
355											spawn[temp] = spawn[counter]; //Make the spawn element the same as this one
356										}
357										else if (temp == -1 && rand() % 200
358												== 0 && loq < TPoints - 1) //There's an empty spot
359										{
360											//Change the probability to change how much spawns overall
361											CreatePoint(tempx + check1, tempy
362													+ check2, spawn[counter]); //1/200 chance of spawning
363										}
364									}
365								}
366							}
367						}
368						if (growing[element[counter]] == 1) //Ice cycle
369						{
370							frozen[counter] = 0;
371							int check1, check2, temp;
372							for (check1 = -1; check1 <= 1; check1++)
373							{
374								for (check2 = -1; check2 <= 1; check2++)
375								{
376									temp = allcoords[tempx + check1][tempy
377											+ check2];
378									if (temp != -1 && element[temp] == 1
379											&& rand() % 10 == 0)
380									{
381										element[temp] = element[counter]; //Change water to ice
382									}
383								}
384							}
385						}
386						if (condensing[element[counter]] != -1) //Steam cycle
387						{
388							frozen[counter] = 0;
389							if (rand() % 200 == 0) //1/200 chance
390							{
391								element[counter] = condensing[element[counter]]; //"Condense" the steam
392								setBitmapColor(x[counter], y[counter],
393										element[counter]);
394							}
395						}
396						int atemporary = allcoords[tempx][tempy];
397
398						//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "start");
399
400						//if the space the particle is trying to move to is taken
401						if (atemporary != -1 && atemporary != counter)
402						{
403							//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "mid1");
404							int secondElementTemp = element[atemporary];
405
406							collide(counter, atemporary); //collision between the two particles
407							//if nothing has changed
408							//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "mid");
409							if (x[counter] == ox && y[counter] == oy
410									&& element[counter] == oelement
411									&& x[atemporary] == tempx && y[atemporary]
412									== tempy && element[atemporary]
413									== secondElementTemp)
414							{
415
416								frozen[counter]++; //increase freeze rounds count
417
418
419							}
420							else
421							{
422								//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "mid2");
423								if (x[counter] != ox || y[counter] != oy
424										|| element[counter] != oelement)
425								{
426									//unFreeze stuff around the primary particle because stuff has changed with it
427									//unFreezeParticles(x[counter],y[counter]);
428									unFreezeParticles(ox, oy);
429								}
430								if (x[atemporary] != tempx || y[atemporary]
431										!= tempy || element[atemporary]
432										!= secondElementTemp)
433								{
434									//unFreeze stuff around the secondary particle because stuff has changed with it
435									//unFreezeParticles(x[atemporary],y[atemporary]);
436									unFreezeParticles(tempx, tempy);
437								}
438								//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "2");
439							}
440						}
441						else
442						{
443
444							if (atemporary != counter)
445							{
446
447								//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "unfreeze stuff");
448								//Clear the old spot
449								allcoords[ox][oy] = -1;
450								setBitmapColor(ox, oy, 3);
451
452								//unfreeze particles around old spot
453
454								unFreezeParticles(ox, oy);
455
456								//Set new spot
457								allcoords[tempx][tempy] = counter;
458								setBitmapColor(tempx, tempy, element[counter]);
459
460								//unFreeze paritlces around new spot
461								//unFreezeParticles( tempx, tempy );
462								//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "3");
463
464							}
465
466						}
467						//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "end");
468					}
469				}
470				rtop = 0;
471			}
472		}
473	}
474}