summaryrefslogtreecommitdiffstats
path: root/Items.c4d/Sensors.c4d/ObjectSensor.c4d/Script.c
blob: a27a3985dce011a7e410937d4d17580af992e977 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#strict 2
#include LS7I

local detectIDs, exceptIDs, denyIDs, detectCats, denyCats, hostility;

func Init()
{
	detectCats = C4D_Living | C4D_Object | C4D_Vehicle;
	denyCats = 0;
	detectIDs = [C4FO_Or];
	exceptIDs = [C4FO_Or];
	denyIDs = [C4FO_Or];
	return _inherited(...);
}

func ControlUp(object caller)
{
	if(!Attached() && caller)
	{
		caller->Collect(this);
	}
}

func Attach(object caller, object contained)
{
	if(contained && GetProcedure(contained) == "WALK")
	{
		return SetPosition(GetX(), GetY() + 10);
	}
}

func Triggers() { return [["$ObjectTrigger$"], ["$NoObjectTrigger$"], ["$DeniedObjectTrigger$"]]; }

func Setup(object caller, int &menuIndex)
{
	selectCaller = caller;
	AddMenuItem("$SetupDetection$", "SetupDetection", RSR1, caller, 0, caller, 0, C4MN_Add_ForceNoDesc);
	++menuIndex;
	return true;
}

func SetupDetection(id id, object caller, int selection)
{
	if(CreateMenu(RSR1, caller, this, C4MN_Extra_None, "$SetupDetection$", 0, C4MN_Style_Context, 0))
	{
		var menuIndex = 0;
		AddMenuItem("$AddType$", "AddType", GetID(), caller, 0, caller, 0, C4MN_Add_ForceNoDesc);
		++menuIndex;
		AddMenuItem(Format("$TargetOwner$", ["$All$", "$Allied$", "$Hostile$", 0, "$DenyHostile$", "$DenyAllied$"][hostility]), "ChangeHostility", GetID(), caller, 0, caller, 0, C4MN_Add_ForceNoDesc);
		++menuIndex;
		
		for(var part in [["$DetectDef$", detectCats], ["$DetectDenial$", denyCats]])
		{
			for(var cat in [[C4D_Vehicle, "$Vehicle$", LORY], [C4D_Living, "$Living$", WIPF], [C4D_Object, "$Object$", ROCK]])
			{
				if(part[1] & cat[0])
				{
					AddMenuItem(Format(part[0], cat[1]), "RemoveCat", cat[2], caller, 0, cat[0], 0, C4MN_Add_ForceNoDesc);
					++menuIndex;
				}
			}
		}
		
		for(var part in [["$DetectDef$", detectIDs], ["$DetectException$", exceptIDs], ["$DetectDenial$", denyIDs]])
		{
			for(var i = 1; i < GetLength(part[1]); ++i)
			{
				AddMenuItem(Format(part[0], GetName(0, part[1][i][1])), "RemoveDef", part[1][i][1], caller, 0, ++menuIndex, 0, C4MN_Add_ForceNoDesc);
			}
		}
		SelectMenuItem(selection - 1, caller);
		SelectMenuItem(selection, caller);
	}
}

func RemoveCat(id id, int cat, bool right)
{
	var denial = detectCats & cat;
	detectCats &= ~cat;
	denyCats &= ~cat;
	if(right)
	{
		if(denial)
		{
			denyCats |= cat;
		}
		else
		{
			detectCats |= cat;
		}
	}
	SetupDetection(0, selectCaller, 3);
}

func RemoveDef(id id, int indexPO, bool right)
{
	var part = [C4FO_ID, id], exception, denial;
	if(right)
	{
		exception = GetIndexOf2(part, detectIDs) == -1;
		denial = GetIndexOf2(part, denyIDs) == -1;
	}
	ArrayEraseItem(detectIDs, part);
	ArrayEraseItem(exceptIDs, part);
	ArrayEraseItem(denyIDs, part);
	if(right)
	{
		if(exception)
		{
			if(denial)
			{
				denyIDs[GetLength(denyIDs)] = part;
			}
			else
			{
				detectIDs[GetLength(detectIDs)] = part;
			}
		}
		else
		{
			exceptIDs[GetLength(exceptIDs)] = part;
		}
	}
	SetupDetection(0, selectCaller, indexPO - 1);
}

func AddType(id id, object caller, int selection)
{
	var menuIndex = 0;
	if(CreateMenu(RSR1, caller, this, C4MN_Extra_None, "$AddType$", 0, C4MN_Style_Context, 0))
	{
		for(var cat in [[C4D_Vehicle, "$Vehicle$", LORY], [C4D_Living, "$Living$", WIPF], [C4D_Object, "$Object$", ROCK]])
		{
			if(!(detectCats & cat[0]))
			{
				AddMenuItem(cat[1], "AddCat", cat[2], caller, 0, cat[0], 0, C4MN_Add_ForceNoDesc);
				++menuIndex;
			}
		}
		var defs = [];
		for(var obj in FindObjects(Find_Exclude(this), (caller || this)->Find_AtPoint(), IgnoreSensorIgnored(), Find_Category(C4D_Living | C4D_Object | C4D_Vehicle)))
		{
			var def = GetID(obj);
			if(GetIndexOf(def, defs) == -1 && GetIndexOf2([C4FO_ID, def], detectIDs) == -1 && GetIndexOf2([C4FO_ID, def], exceptIDs) == -1)
			{
				defs[GetLength(defs)] = def;
			}
		}
		
		for(def in defs)
		{
			AddMenuItem(GetName(0, def), "AddDef", def, caller, 0, ++menuIndex, 0, C4MN_Add_ForceNoDesc);
		}
		
		SelectMenuItem(selection - 1, caller);
		SelectMenuItem(selection, caller);
	}
}

func AddCat(id id, int cat, bool right)
{
	if(right)
	{
		denyCats |= cat;
	}
	else
	{
		detectCats |= cat;
	}
	AddType(0, selectCaller, 1);
}

func AddDef(id id, int indexPO, bool right)
{
	if(right)
	{
		exceptIDs[GetLength(exceptIDs)] = [C4FO_ID, id];
	}
	else
	{
		detectIDs[GetLength(detectIDs)] = [C4FO_ID, id];
	}
	AddType(0, selectCaller, indexPO - 1);
}

func ChangeHostility(id id, object caller, bool right)
{
	if(right)
	{
		if(hostility)
		{
			if(hostility > 2)
			{
				hostility -= 3;
			}
			else
			{
				hostility += 3;
			}
		}
		else
		{
			hostility = 4;
		}
	}
	else
	{
		var denyOther = (hostility > 2) * 4;
		++hostility;
		if((!denyOther && hostility > 2) || (denyOther && hostility > 5))
		{
			hostility = denyOther;
		}
	}

	SetupDetection(0, caller, 1);
}

func Check(bool retrigger)
{
	var posCond;
	if(Attached() && !GBackSemiSolid(0, 2))
	{
		SetCategory(C4D_Structure);
		SetAction("Wall");
		SetSolidMask();
		posCond = Find_AtRect(-3, 0, 8, 8);
	}
	else
	{
		SetCategory(C4D_Object);
		SetAction("Idle");
		posCond = Find_AtPoint(0, -3);
		if(!Contained() && GetSpeed() < 5)
		{
			SetSolidMask(0, 0, 10, 1);
		}
		else
		{
			SetSolidMask();
		}
	}
	var obj;
	var additionalConds = [C4FO_And];
	var hostilityCond = [0, Find_Not(Find_Hostile(GetOwner(this))), Find_Hostile(GetOwner(this))][hostility % 3];
	var hostilityDenyCond = [0, Find_Hostile(GetOwner(this)), Find_Not(Find_Hostile(GetOwner(this)))][hostility - 3];
	if(GetLength(exceptIDs) > 1)
	{
		additionalConds[GetLength(additionalConds)] = Find_Not(exceptIDs);
	}
	if(hostilityCond)
	{
		additionalConds[GetLength(additionalConds)] = hostilityCond;
	}
	var additionalDenyConds = [C4FO_Or];
	if(denyIDs)
	{
		additionalDenyConds[GetLength(additionalDenyConds)] = denyIDs;
	}
	if(denyCats)
	{
		additionalDenyConds[GetLength(additionalDenyConds)] = Find_Category(denyCats);
	}
	if(hostilityDenyCond)
	{
		additionalDenyConds[GetLength(additionalDenyConds)] = Find_And(Find_Or(detectIDs, detectCats && Find_Category(detectCats)), hostilityDenyCond);
	}
	ClearParticles("PSpark", this);
	if(GetLength(additionalDenyConds) > 1 && (obj = FindObject2(Find_Exclude(this), posCond, Find_NoContainer(), Find_Category(C4D_Living | C4D_Object | C4D_Vehicle), IgnoreSensorIgnored(), additionalDenyConds)))
	{
		CreateParticle("PSpark", 0, 0, 0, 0, 50, RGB(192, 0, 0), this);
		if(triggered != 2 || retrigger)
		{
			triggered = 2;
			return Trigger(2, obj);
		}
	}
	else if(obj = FindObject2(Find_Exclude(this), posCond, Find_NoContainer(), IgnoreSensorIgnored(), Find_Or(detectIDs, detectCats && Find_Category(detectCats)), additionalConds))
	{
		CreateParticle("PSpark", 0, 0, 0, 0, 50, RGB(0, 192, 0), this);
		if(!triggered || retrigger)
		{
			triggered = true;
			return Trigger(0, obj);
		}
	}
	else if(triggered || retrigger)
	{
		triggered = false;
		return Trigger(1);
	}
}