summaryrefslogtreecommitdiff
path: root/src/elements/soap.c
blob: 838b16ce842bcfcfa29e058b49f5edc3c766b713 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <element.h>

void detach(int i)
{
	if ((parts[i].ctype&2) == 2)
	{
		if ((parts[parts[i].tmp].ctype&4) == 4)
			parts[parts[i].tmp].ctype ^= 4;
	}

	if ((parts[i].ctype&4) == 4)
	{
		if ((parts[parts[i].tmp2].ctype&2) == 2)
			parts[parts[i].tmp2].ctype ^= 2;
	}

	parts[i].ctype = 0;
}

int update_SOAP(UPDATE_FUNC_ARGS) 
{
	int r, rx, ry;

	//0x01 - bubble on/off
	//0x02 - first mate yes/no
	//0x04 - "back" mate yes/no

	if ((parts[i].ctype&1) == 1)
	{
		if (parts[i].life<=0)
		{
			if ((parts[i].ctype&6) == 0)
				parts[i].ctype = 0;

			if ((parts[i].ctype&6) != 6 && parts[i].ctype>1)
			{
				int target;

				target = i;

				while((parts[target].ctype&6) != 6 && parts[target].ctype>1)
				{
					if ((parts[target].ctype&2) == 2)
					{
						target = parts[target].tmp;
						detach(target);
					}

					if ((parts[target].ctype&4) == 4)
					{
						target = parts[target].tmp2;
						detach(target);
					}
				}
			}

			if ((parts[i].ctype&6) == 6 && (parts[parts[i].tmp].ctype&6) == 6 && parts[parts[i].tmp].tmp == i)
				detach(i);

		}

		parts[i].vy -= 0.1f;

		parts[i].vy *= 0.5f;
		parts[i].vx *= 0.5f;

		if((parts[i].ctype&2) != 2)
		{
			for (rx=-2; rx<3; rx++)
				for (ry=-2; ry<3; ry++)
					if (x+rx>=0 && y+ry>0 && x+rx<XRES && y+ry<YRES && (rx || ry))
					{
						r = pmap[y+ry][x+rx];
						if ((r>>8)>=NPART || !r)
							continue;

						if ((r&0xFF) != PT_SOAP || (parts[r>>8].ctype == 0 && (r&0xFF) == PT_SOAP))
						{
							detach(i);
							continue;
						}

						if ((parts[r>>8].type == PT_SOAP) && ((parts[r>>8].ctype&1) == 1) 
								&& ((parts[r>>8].ctype&4) != 4))
						{
							if ((parts[r>>8].ctype&2) == 2)
							{
								parts[i].tmp = r>>8;
								parts[r>>8].tmp2 = i;

								parts[i].ctype |= 2;
								parts[r>>8].ctype |= 4;
							}
							else
							{
								if ((parts[i].ctype&2) != 2)
								{
									parts[i].tmp = r>>8;
									parts[r>>8].tmp2 = i;

									parts[i].ctype |= 2;
									parts[r>>8].ctype |= 4;
								}
							}
						}
					}
		}

		if((parts[i].ctype&2) == 2)
		{
			float d, dx, dy;

			dx = parts[i].x - parts[parts[i].tmp].x;
			dy = parts[i].y - parts[parts[i].tmp].y;

			d = 9/(pow(dx, 2)+pow(dy, 2)+9)-0.5;

			parts[parts[i].tmp].vx -= dx*d;
			parts[parts[i].tmp].vy -= dy*d;

			parts[i].vx += dx*d;
			parts[i].vy += dy*d;

			if (((parts[parts[i].tmp].ctype&2) == 2) && ((parts[parts[i].tmp].ctype&1) == 1) 
					&& ((parts[parts[parts[i].tmp].tmp].ctype&2) == 2) && ((parts[parts[parts[i].tmp].tmp].ctype&1) == 1))
			{
				int ii;

				ii = parts[parts[parts[i].tmp].tmp].tmp;

				dx = parts[ii].x - parts[parts[i].tmp].x;
				dy = parts[ii].y - parts[parts[i].tmp].y;

				d = 81/(pow(dx, 2)+pow(dy, 2)+81)-0.5;

				parts[parts[i].tmp].vx -= dx*d*0.5f;
				parts[parts[i].tmp].vy -= dy*d*0.5f;

				parts[ii].vx += dx*d*0.5f;
				parts[ii].vy += dy*d*0.5f;
			}
		}
	}
	else
	{
		if (pv[y/CELL][x/CELL]>0.5f || pv[y/CELL][x/CELL]<(-0.5f))
		{
			parts[i].ctype = 1;
			parts[i].life = 5;
		}
	}

	return 0;
}