summaryrefslogtreecommitdiffstats
path: root/Items.c4d/Utility.c4d/Script.c
blob: 6e44f7edf338348b557e3a3a4d84947047c62519 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#strict 2

func AttachVertex() { return 3; }

func Detach()
{
	if(Attached() || Floating())
	{
		SetAction("Idle");
		return true;
	}
	return false;
}

func AttachObjectCondition(object caller)
{
	return Find_And(Find_Or(Find_OCF(OCF_Entrance | OCF_Collection), ...), Find_Not(Find_Category(C4D_Living)));
}

func AttachMenu(object caller, bool allowFloating)
{
	var targets = FindObjects(Find_AtPoint(), Find_Exclude(caller), Find_Exclude(this), AttachObjectCondition(caller));
	if(allowFloating)
	{
		targets[] = [0, "$AttachWall$", GetID()];
	}
	return caller->~CreateSelectionMenu("AttachEntry", targets, GetID(), "$SelectTarget$", this);
}

func AttachEntry(id id, object target)
{
	if(target)
	{
		return Attach(target);
	}
	else
	{
		if(Contained())
		{
			Contained()->Exit(this); // TODO: maybe use scripted Float instead
		}
		return SetAction("Float");
	}
}

func Attach(object target) // TODO: maybe use scripted Attach instead; check if target shape is still at own position; At best implement FilterObject(s) using Find_*-Filter at first und use that
{ // WARNING: needs an action with the name "Attach", the Procedure "ATTACH" and an AbortCall of "Detached"
	SetVertex(AttachVertex(), VTX_X, GetX(target) - GetX() + GetVertex(0, VTX_X, target), this, VTX_SetPermanentUpd);
	SetVertex(AttachVertex(), VTX_Y, GetY(target) - GetY() + 5 + GetVertex(0, VTX_Y, target), this, VTX_SetPermanentUpd);

	SetAction("Attach", target);
	SetActionData(AttachVertex() << 8);
}

func UpdateTransferZone()
{
	if(Attached())
	{
		SetActionData(AttachVertex() << 8);
	}
}

func IsTubeTarget() { return true; }

func IsAnvilProduct() { return true; }

func Attached(bool containerFallback) { return (GetAction() == "Attach" && GetActionTarget()) || (containerFallback && Contained()); }

func Floating() { return GetAction() == "Float"; }

func Detached()
{
	SetVertex(AttachVertex(), VTX_X, 0, this, VTX_SetPermanentUpd);
	SetVertex(AttachVertex(), VTX_Y, 0, this, VTX_SetPermanentUpd);
}

func RejectEntrance(object container) { return Attached() && Contained(Attached()) != container || Floating(); }

func CanRetrieveObject(id type, bool allowProduction, object excludeOrigin)
{
	return FindObject2(Find_Tube(), Find_Func("CanRetrieveObject", this, type, allowProduction, excludeOrigin), excludeOrigin && Find_Not(Find_Func("HasOrigin", excludeOrigin)));
}

func RetrieveObject() { return this->~RetrieveSendObject(...) || this->~RetrieveProduceObject(...); }

func GetRetrievableIDs(bool produce)
{
	var lines = FindObjects(Find_Tube()), IDs = [], produceIDs = [];
	for(var line in lines)
	{
		var lineIDs = line->GetRetrievableObjects(this);
		if(lineIDs)
		{
			for(var id in lineIDs)
			{
				if(GetIndexOf(id, IDs) == -1)
				{
					IDs[] = id;
				}
			}
		}

		if(produce)
		{
			produceIDs[] = [line->GetLineEnd(this), line->GetRetrievableObjects(this, true)];
		}
	}
	if(produce)
	{
		if(GetLength(produceIDs) == 0)
		{
			return [];
		}
		var ret = [], parts = [], part;
		for(var i = 0; i < GetLength(produceIDs); ++i)
		{
			part = [];
			if(produceIDs[i][1])
			{
				for(var id in produceIDs[i][1])
				{
					if(GetIndexOf(id, parts) == -1 && GetIndexOf(id, IDs) == -1)
					{
						part[] = id;
						parts[] = id;
					}
				}
				if(GetLength(part) > 0)
				{
					ret[] = [produceIDs[i][0], part];
				}
			}
		}
		return ret;
	}
	return IDs;
}

func ProductionTitle() { return "$Production$"; }
func ProductionSymbol() { return GetID(Attached() || this); }

func RetrieveMenu(object caller, id selectID)
{
	var IDs = GetRetrievableIDs();
	if(caller && CreateMenu(GetID(), caller, this, C4MN_Extra_None, "$SelectRetrieveType$", 0, C4MN_Style_Context))
	{
		if(!selectID)
		{
			SetCommand(caller, "None");
		}
		var i = 0;
		for(var id in IDs)
		{
			AddMenuItem(GetName(0, id), "RetrieveMenuEntry", id, caller, 0, caller, 0, C4MN_Add_ForceNoDesc);
			if(id == selectID)
			{
				SelectMenuItem(i, caller);
			}
			++i;
		}
		IDs = GetRetrievableIDs(true);
		if(GetLength(IDs) > 0)
		{
			for(var producer in IDs)
			{
				AddMenuItem(producer[0]->ProductionTitle(), 0, producer[0]->ProductionSymbol(), caller);
				++i;
				for(var id in producer[1])
				{
					AddMenuItem(Format("    %s", GetName(0, id)), "RetrieveMenuEntry", id, caller, 0, caller, 0, C4MN_Add_ForceNoDesc);
					if(id == selectID)
					{
						SelectMenuItem(i, caller);
					}
					++i;
				}
			}
		}
		return true;
	}
}

func RetrieveMenuEntry(id type, object caller)
{
	SetRetrieveCommands(type, caller, false);
	RetrieveMenu(caller, type);
}

func SetRetrieveCommands(id type, object caller, bool indirect, object excludeOrigin)
{
	if((!indirect && ((!Attached() && !Floating()) || (GetProcedure(caller) == "PUSH" && GetActionTarget(0, caller) == this))) || !CommandRequireContainerOrPush(caller, Attached() || (Floating() && this), BindCallback((indirect && "RetrieveObjectForProduction") || "RetrieveMenuEntry", [Bind(type), 0, Bind(excludeOrigin)]), 0, indirect))
	{
		RetrieveObjectThroughTube(type);
	}
}

func RetrieveObjectForProduction(id type, object caller, object excludeOrigin)
{
	RetrieveObjectThroughTube(type, BindCallback("ObjectReceived", [Bind(caller), 0]), excludeOrigin, BindCallback("ObjectReceivingFailed", [Bind(caller)]));
	AddCommand(caller, "Wait", 0, 0, 0, 0, 0, -1);
}

func ObjectReceivingFailed(object caller)
{
	FinishCommand(caller, false);
}

func ObjectReceived(object caller, object obj)
{
	FinishCommand(caller, true);
	FinishCommand(caller, true);
	FinishCommand(caller, true);
	AddCommand(caller, "Get", obj);
}

func RetrieveObjectThroughTube(id type, arriveCallback, object excludeOrigin, failCallback)
{
	var line = FindObject2(Find_Tube(), Find_Func("CanRetrieveObject", this, type, false, excludeOrigin)) || FindObject2(Find_Tube(), Find_Func("CanRetrieveObject", this, type, true, excludeOrigin), Sort_Func("FactoryLoad", this));
	return line && line->RetrieveObject(this, type, arriveCallback, excludeOrigin, failCallback);
}

func SendableObjectConditions(object container)
{
	return Find_And(Find_OCF(OCF_Collectible), Find_OCF(OCF_Fullcon), ...);
}

func SendableConditions(object container)
{
	container = container || Attached();
	if(container)
	{
		return Find_And(Find_Container(container), SendableObjectConditions(container));
	}
	else
	{
		return Find_Or();
	}
}

func GetSendableIDs(bool production)
{
	var IDs = [];
	var attached = Attached();
	if(attached)
	{
		for(var obj in FindObjects(SendableConditions(attached)))
		{
			if(GetIndexOf(GetID(obj), IDs) == -1)
			{
				IDs[] = GetID(obj);
			}
		}
	}
	if(production)
	{
		var produceIDs = this->~GetProducableIDs();
		var ret = [];
		if(produceIDs)
		{
			for(var id in produceIDs)
			{
				if(GetIndexOf(id, IDs) == -1)
				{
					ret[] = id;
				}
			}
		}
		return ret;
	}
	return IDs;
}

func FindSendableObject(id type)
{
	return FindObject2(SendableConditions(), type && Find_ID(type));
}

func SendMenu(object caller, id selectID)
{
	if(caller && CreateMenu(GetID(), caller, this, C4MN_Extra_None, "$SelectSendType$", 0, C4MN_Style_Context))
	{
		if(!selectID)
		{
			SetCommand(caller, "None");
		}
		var i = 0;
		for(var id in GetSendableIDs())
		{
			AddMenuItem(GetName(0, id), "SendMenuEntry", id, caller, 0, caller, 0, C4MN_Add_ForceNoDesc);
			if(id == selectID)
			{
				SelectMenuItem(i, caller);
			}
			++i;
		}
		var IDs = GetSendableIDs(true);
		if(GetLength(IDs) > 0)
		{
			AddMenuItem(ProductionTitle(), 0, ProductionSymbol(), caller);
			++i;
			for(var id in IDs)
			{
				AddMenuItem(Format("    %s", GetName(0, id)), "SendMenuEntry", id, caller, 0, caller, 0, C4MN_Add_ForceNoDesc);
				if(id == selectID)
				{
					SelectMenuItem(i, caller);
				}
				++i;
			}
		}
		return true;
	}
}

func SendMenuEntry(id type, object caller)
{
	if(!Attached() || (GetProcedure(caller) == "PUSH" && GetActionTarget(0, caller) == this) || !CommandRequireContainerOrPush(caller, Attached(), BindCallback("SendMenuEntry", [Bind(type), 0])))
	{
		SendObjectThroughTube(type);
	}
	SendMenu(caller, type);
}

func SendObjectThroughTube(id type, object line, arriveCallback, object excludeOrigin, failCallback, object realTarget)
{
	var attached = Attached();
	if(attached)
	{
		var objs = FindObjects(SendableConditions(attached), type && Find_ID(type));
		if(GetLength(objs) > 0)
		{
			for(var obj in objs)
			{
				line =  line || FindObject2(Find_Tube(), Find_Func("CanSendObject", this, obj));
				if(line)
				{
					line->SendObject(this, obj, arriveCallback, false, realTarget);
					break;
				}
			}
			return true;
		}
		else if(type)
		{
			this->~RetrieveProduceObject(line, type, arriveCallback, excludeOrigin, failCallback, realTarget);
			return true;
		}
	}
}

func DenyContainedDirectCom()
{
	return Contained();
}

func TryPut(object obj, object container)
{
	var attached = container || Attached() || Contained();
	if(attached)
	{
		return Put(attached, obj);
	}
	else
	{
		return false;
	}
}

func ReceiveObject(object obj, bool noReturn)
{
	Exit(obj);
	SetPosition(GetX(), GetY(), obj);
	return TryPut(obj) || (!Attached() && !Contained());
}

func IsRemoteControllable() { return false; }

func CommandRequireContainerOrPush(object cmdTarget, object target, callback, object callbackTarget, bool add)
{
	if(add || (Contained(cmdTarget) != target && (GetProcedure(cmdTarget) != "PUSH" || GetActionTarget(0, cmdTarget) != target)))
	{
		if(add)
		{
			AddCommand(cmdTarget, "Call", this, callback, 0, callbackTarget || this, 0, "ClonkEnteredCallback");
			AddCommand(cmdTarget, "Call", this, target, 0, 0, 0, "EnterOrGrabIfNotAlready");
		}
		else
		{
			AppendCommand(cmdTarget, "Call", this, target, 0, 0, 0, "EnterOrGrabIfNotAlready");
			AppendCommand(cmdTarget, "Call", this, callback, 0, callbackTarget || this, 0, "ClonkEnteredCallback");
		}
		return true;
	}
	return false;
}

func EnterOrGrabIfNotAlready(object cmdTarget, object target)
{
	if(target && Contained(cmdTarget) != target && (GetProcedure(cmdTarget) != "PUSH" || GetActionTarget(0, cmdTarget) != target))
	{
		if(GetDefGrab(GetID(target)))
		{
			AddCommand(cmdTarget, "Grab", target);
		}
		else
		{
			AddCommand(cmdTarget, "Enter", target);
		}
		return true;
	}
}

func ClonkEnteredCallback(object cmdTarget, callback, null, object callbackTarget)
{
	return callbackTarget && callbackTarget->Call(callback, cmdTarget);
}

func ObjectOrigin()
{
	return Attached(true) || this;
}