File: Script.c

package info (click to toggle)
openclonk 7.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 145,828 kB
  • ctags: 33,094
  • sloc: cpp: 163,891; ansic: 82,846; xml: 29,876; python: 1,203; php: 767; makefile: 138; sh: 77
file content (358 lines) | stat: -rwxr-xr-x 8,828 bytes parent folder | download
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
/**
	ConstructionSite
	Needs material put into it, then constructs the set building.

	@author boni
*/

local definition;
local direction;
local stick_to;
local full_material; // true when all needed material is in the site
local no_cancel; // if true, site cannot be cancelled
local is_constructing;

// This should be recongnized as a container by the interaction menu independent of its category.
public func IsContainer() { return !full_material; }
// disallow taking stuff out
public func RefuseTransfer(object toMove) { return true; }
// disallow site cancellation. Useful e.g. for sites that are pre-placed for a game goal
public func MakeUncancellable() { no_cancel = true; return true; }

/*-- Interaction --*/

public func HasInteractionMenu() { return true; }

public func GetInteractionMenuEntries(object clonk)
{
	// default design of a control menu item
	var custom_entry = 
	{
		Right = "100%", Bottom = "2em",
		BackgroundColor = {Std = 0, OnHover = 0x50ff0000},
		image = {Right = "2em"},
		text = {Left = "2em"}
	};
	
	return [{symbol = Icon_Cancel, extra_data = "abort", 
			custom =
			{
				Prototype = custom_entry,
				Priority = 1,
				text = {Prototype = custom_entry.text, Text = "$TxtAbort$"},
				image = {Prototype = custom_entry.image, Symbol = Icon_Cancel}
			}}];
}

public func GetMissingMaterialMenuEntries(object clonk)
{
	var material = GetMissingComponents();
	if (!material) return [];
	
	var entries = [];
	for (var mat in material)
	{
		var text = nil;
		if (mat.count > 1) text = Format("x%d", mat.count);
		PushBack(entries, {symbol = mat.id, text = text});
	}
	return entries;
}

public func GetInteractionMenus(object clonk)
{
	var menus = _inherited() ?? [];		
	var comp_menu =
	{
		title = "$TxtMissingMaterial$",
		entries_callback = this.GetMissingMaterialMenuEntries,
		BackgroundColor = RGB(50, 0, 0),
		Priority = 15
	};
	PushBack(menus, comp_menu);
	var prod_menu =
	{
		title = "$TxtAbort$",
		entries_callback = this.GetInteractionMenuEntries,
		callback = "OnInteractionControl",
		callback_hover = "OnInteractionControlHover",
		callback_target = this,
		BackgroundColor = RGB(0, 50, 50),
		Priority = 20
	};
	PushBack(menus, prod_menu);
	return menus;
}

public func OnInteractionControlHover(id symbol, string action, desc_menu_target, menu_id)
{
	var text = "";
	if (action == "abort")
	{
		if (no_cancel)
			text = "$TxtNoAbortDesc$";
		else
			text = "$TxtAbortDesc$";
	}
	GuiUpdateText(text, menu_id, 1, desc_menu_target);
}

public func OnInteractionControl(id symbol, string action, object clonk)
{
	if (action == "abort")
	{
		if (!Deconstruct())
			Sound("UI::Click*", false, nil, clonk->GetOwner());
	}
}

public func Deconstruct()
{
	// Remove Site
	if (!no_cancel)
	{
		for(var obj in FindObjects(Find_Container(this)))
			obj->Exit();
		RemoveObject();
		return true;
	}
	return false;
}

public func Construction()
{
	this.visibility = VIS_None;
	definition = nil;
	full_material = false;
	
	return true;
}

public func Set(id def, int dir, object stick)
{
	definition = def;
	direction = dir;
	stick_to = stick;

	var xw = (1 - dir * 2) * 1000;
	var w, h;
	w = def->GetDefWidth();
	h = def->GetDefHeight();
	// Draw the building with a wired frame and large alpha unless site graphics is overloaded by definition
	if (!def->~SetConstructionSiteOverlay(this, direction, stick_to))
	{
		SetGraphics(nil, nil, 0);
		SetGraphics(nil, def, 1, GFXOV_MODE_Base);
		SetClrModulation(RGBa(255, 255, 255, 128), 1);
		// If the structure is a mesh, use wire frame mode to show the site.
		// TODO: use def->IsMesh() once this becomes available.
		if (def->GetMeshMaterial())
		{
			SetClrModulation(RGBa(255, 255, 255, 50), 1);
			SetGraphics(nil, def, 2, GFXOV_MODE_Base, nil, GFX_BLIT_Wireframe);
		}
		SetGraphics("", GetID(), 3, GFXOV_MODE_ExtraGraphics);
	}
	SetObjDrawTransform(xw,0,0,0,1000, -h*500,1);
	SetObjDrawTransform(xw,0,0,0,1000, -h*500,2);
	// Height of construction site needs to exceed 12 pixels for the clonk to be able to add materials.
	h = Max(12, h);
	SetShape(-w/2, -h, w, h);
	// Increase shape for below surface constructions to allow for adding materials.
	if (definition->~IsBelowSurfaceConstruction())
		SetShape(-w/2, -2 * h, w, 2 * h);
	
	SetName(Format(Translate("TxtConstruction"),def->GetName()));
	
	this.visibility = VIS_Owner | VIS_Allies;
	
	ShowMissingComponents();
}

// Scenario saving
func SaveScenarioObject(props)
{
	if (!inherited(props, ...)) return false;
	props->Remove("Name");
	if (definition) props->AddCall("Definition", this, "Set", definition, direction, stick_to);
	if (no_cancel) props->AddCall("NoCancel", this, "MakeUncancellable");
	return true;
}

// only allow collection if needed
public func RejectCollect(id def, object obj)
{
	var max = GetComponent(def, nil, nil, definition);
	
	// not a component?
	if(max == 0)
		return true;
	if(ContentsCount(def) < max)
		return false;
	
	return true;
}

// check if full
public func Collection2(object obj)
{
	// Ignore any activity during construction
	if (is_constructing) return;
	
	// update message
	ShowMissingComponents();
	
	// Update possibly open menus.
	UpdateInteractionMenus(this.GetMissingMaterialMenuEntries);
	
	// Update preview image
	if (definition) definition->~SetConstructionSiteOverlay(this, direction, stick_to, obj);
	
	// check if we're done?
	if(full_material)
		StartConstructing();
}

// component removed (e.g.: Contained wood burned down or some externel scripts went havoc)
// Make sure lists are updated
public func ContentsDestruction(object obj) { return Collection2(nil); }
public func Ejection(object obj) { return Collection2(nil); }

private func ShowMissingComponents()
{
	if (definition == nil)
	{
		Message("");
		return;
	}
		
	var stuff = GetMissingComponents();
	var msg = "@";
	for (var s in stuff)
		if (s.count > 0)
			msg = Format("%s %dx{{%i}}", msg, s.count, s.id);
	// Ensure that the message is not below the bottom of the map.
	var dy = 23 - Max(23 + GetY() - LandscapeHeight(), 0) / 2;
	CustomMessage(msg, this, NO_OWNER, 0, dy);
}

private func GetMissingComponents()
{
	if(definition == nil)
		return;
	
	if(full_material == true)
		return nil;
	
	// set false again as soon as we find a missing component
	full_material = true;
	
	// check for material
	var comp, index = 0;
	var missing_material = CreateArray();
	while (comp = GetComponent(nil, index, nil, definition))
	{
		// find material
		var max_amount = GetComponent(comp, nil, nil, definition);
		var c = ContentsCount(comp);
		var dif = max_amount-c;
		
		if(dif > 0)
		{
			PushBack(missing_material, {id=comp, count=dif});
			full_material = false;
		}		
		
		index++;
	}
	
	return missing_material;
}

private func StartConstructing()
{
	if(!definition)
		return;
	if(!full_material)
		return;
	
	is_constructing = true;
	
	// find all objects on the bottom of the area that are not stuck
	var wdt = GetObjWidth();
	var hgt = GetObjHeight();
	var lying_around = FindObjects(Find_Category(C4D_Vehicle | C4D_Object | C4D_Living), Find_AtRect(-wdt/2 - 2, -hgt, wdt + 2, hgt + 12), Find_OCF(OCF_InFree), Find_NoContainer());
	
	// create the construction, below surface constructions don't perform any checks.
	// uncancellable sites (for special game goals) are forced and don't do checks either
	var site;
	var checks = !definition->~IsBelowSurfaceConstruction() && !no_cancel;
	if(!(site = CreateConstruction(definition, 0, 0, GetOwner(), 1, checks, checks)))
	{
		// spit out error message. This could happen if the landscape changed in the meantime
		// a little hack: the message would immediately vanish because this object is deleted. So, instead display the
		// message on one of the contents.
		if(Contents(0))
			CustomMessage("$TxtNoConstructionHere$", Contents(0), GetOwner(), nil,nil, RGB(255,0,0));
		Deconstruct();
		return;
	}
	
	if(direction)
		site->SetDir(direction);
	// Inform about sticky building
	if (stick_to)
		site->CombineWith(stick_to);
	
	// Object provides custom construction effects?
	if (!site->~DoConstructionEffects(this))
	{
		// If not: Autoconstruct 2.0!
		Schedule(site, "DoCon(2)",1,50);
		Schedule(this,"RemoveObject()",1);
		site->Sound("Structures::FinishBuilding");
	}
	
	// clean up stuck objects
	for(var o in lying_around)
	{
		if (!o) continue;
		
		var x, y;
		var dif = 0;
		
		x = o->GetX();
		y = o->GetY();
		
		// move living creatures upwards till they stand on top.
		if(o->GetOCF() & OCF_Alive)
		{
			while(o->GetContact(-1, CNAT_Bottom))
			{
				// only up to 20 pixel
				if(dif > 20)
				{
					o->SetPosition(x,y);
					break;
				}
				
				dif++;
				o->SetPosition(x, y-dif);
			}
		}
		else {
			while(o->Stuck())
			{
				// only up to 20 pixel
				if(dif > 20)
				{
					o->SetPosition(x,y);
					break;
				}
				
				dif++;
				o->SetPosition(x, y-dif);
			}
		}
	}
}