File: Script.c

package info (click to toggle)
openclonk 8.1-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 169,520 kB
  • sloc: cpp: 180,479; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 145; sh: 101; javascript: 34
file content (102 lines) | stat: -rw-r--r-- 2,256 bytes parent folder | download | duplicates (5)
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
/*
	Zaphive

	Author: Clonkonaut
*/

/* Placement */

public func Place(int amount, proplist rectangle)
{
	// No calls to objects, only definitions
	if (GetType(this) == C4V_C4Object) return;
	// Default parameters
	if (!amount) amount = 1;
	if (!rectangle) rectangle = Rectangle(0,0, LandscapeWidth(), LandscapeHeight());

	var trees = FindObjects(Find_InRect(rectangle.x, rectangle.y, rectangle.wdt, rectangle.hgt), Find_OCF(OCF_Fullcon), Find_Func("IsTree"), Find_Func("IsStanding"));
	var hives = CreateArray(), hive;
	while (amount)
	{
		hive = nil;
		ShuffleArray(trees);
		for (var tree in trees)
		{
			hive = tree->CreateObjectInTreetop(this);
			if (hive) break;
		}
		if (hive)
			hives[GetLength(hives)] = hive;
		amount--;
	}
	return hives;
}

local tree;

// Called by trees
public func AttachToTree(object to_attach)
{
	tree = to_attach;
	// Constant offset. The old comment didn't say why this was necessary.
	var additional_y_offset = 4;
	// Calculate the offset from the tree, taking into account the target vertex' offset.
	var offset_x = GetX() - to_attach->GetX() - to_attach->GetVertex(0, VTX_X);
	var offset_y = GetY() - to_attach->GetY() - to_attach->GetVertex(0, VTX_Y) + additional_y_offset;
	SetAction("Attach", to_attach);
	// And specify/set the vertex to attach.
	SetActionData(4 << 8);
	SetVertexXY(4, -offset_x, -offset_y);
}

// Called by trees
public func DetachFromTree()
{
	AttachTargetLost();
}

public func AttachTargetLost()
{
	// Fall down
	SetAction("Idle");
	SetVertexXY(4, 0, 0);
	SetRDir(RandomX(-3, 3));
}

/* Creation */

private func Initialize()
{
	SetProperty("MeshTransformation", Trans_Rotate(RandomX(-50,50),0,1,0));
	// Create a little swarm
	var zap = CreateObject(Zap);
	// Could be instantly dead
	if (zap)
		zap->SetHome(this);
}

/* Destruction */

private func Hit2()
{
	// Create an enraged swarm
	var zap = CreateObject(Zap,0, -3);
	// Could be instantly dead
	if (zap)
		zap->SetEnraged();
	Sound("Hits::OrganicHit?");
	CreateParticle("WoodChip", 0,0, PV_Random(-2,2), -4, PV_Random(36 * 3, 36 * 10), Particles_Straw(), 5);
	RemoveObject();
}

local ActMap = {
	Attach = {
		Prototype = Action,
		Procedure = DFA_ATTACH,
		FacetBase = 1,
		Length = 1,
		Delay = 0
	},
};

local Name = "$Name$";