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 (254 lines) | stat: -rw-r--r-- 5,937 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
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
/*
	Mosquito
	Author: Clonkonaut

	A small annoying being.
*/

#include Library_InsectSwarm

local lib_swarm_standard = 20;
local lib_insect_max_dist = 50;
local lib_insect_nocturnal = true;

private func Initialize()
{
	SetAction("Fly");
	SetPhase(Random(3));

	_inherited(...);
}

private func Death()
{
	_inherited(...);
	RemoveObject();
}

private func MissionComplete()
{
	if (Time->IsDay()) return;

	// Mosquitos are very active
	MoveToTarget();
	// Little dipshits
}

private func Sleep()
{
	if (lib_insect_sleeping) return;

	if (lib_insect_going2sleep)
	{
		SetAction("Sleep");
		lib_insect_sleeping = true;
		return;
	}
	// One last trip, then become invisible
	MoveToTarget();
	lib_insect_going2sleep = true;
}

private func SleepComplete()
{
	SetAction("Sleep");
	_inherited(...);
}

private func WakeUp()
{
	SetAction("Fly");
	_inherited(...);
}

// Array: [object lovely_object, int x, int y]
local lovely_place;
// How many times I tried to get to the lovely place
local lovely_tries = 0;

// Mosquitos always look for a lovely place to hang out
private func GetAttraction(proplist coordinates)
{
	// GetAttraction will only be called for the swarm master, perfect to have just one being make sound
	if(!Random(30))
		Sound("Animals::MosquitoBuzz", nil,nil,nil,nil, 200);

	if (!lovely_place) lovely_place = CreateArray(3);
	if (!lovely_place[1])
	{
		if (lovely_tries < 35) lovely_tries++;
		if (lovely_tries > 15 + Random(30))
			// Should not be called too often.
			LookForLovelyPlace();
	}
	if (!lovely_place[1])
		return false; // No lovely place found. Default behaviour (just fly around)

	// Occasionally check if lovely place still qualifies
	if (!Random(100) && !CheckLovelyPlace())
		return false;

	// Lovely is an object (plant or corpse): Stay close
	if (lovely_place[0])
	{
		// Too far away from lovely place. Maybe I can't reach it?
		if (Distance(GetX(), GetY(), lovely_place[1], lovely_place[2]) > 50)
			lovely_tries++;
		coordinates.x = lovely_place[1] + Random(60) - 30;
		// This might fail with *very* high plants in respect of the next distance check. That's okay, I guess.
		coordinates.y = lovely_place[2] + Random(50) - 25;
	} else { // Lovely place is water: Fly around a bit
		// Too far away from lovely place. Maybe I can't reach it?
		if (Distance(GetX(), GetY(), lovely_place[1], lovely_place[2]) > 250)
			lovely_tries++;
		coordinates.x = lovely_place[1] + Random(300) - 150;
		coordinates.y = GetHorizonHeight(coordinates.x);
		if (!GBackLiquid(AbsX(coordinates.x), AbsY(coordinates.y))) // Stay on water
		{
			coordinates.x = lovely_place[1];
			coordinates.y = lovely_place[2];
		}
		coordinates.y -= 25 - Random(20);
	}

	// Tried to reach that place for far too long
	// Or maybe I've got enough of that place!
	if (lovely_tries >= 10 || !Random(1000))
		return RemoveLovelyPlace();

	return true;
}

private func LookForLovelyPlace()
{
	lovely_tries = 0;
	// Corpses are first priority places
	var corpse = FindObject(Find_Distance(200), Find_NoContainer(), Find_Category(C4D_Living), Find_Not(Find_OCF(OCF_Alive)), Find_Exclude(lovely_place[0]));
	if (corpse && Random(15))
	{
		lovely_place[0] = corpse;
		lovely_place[1] = corpse->GetX();
		lovely_place[2] = GetHorizonHeight(corpse->GetY()) - 30;
		return;
	}
	// Water is second best place
	var water = FindLocation(Loc_InRect(-100,-50,200,100), Loc_Material("Water"), Loc_MaxTries(80));
	if (water)
	{
		// Try to find the surface
		while (!GBackSky(water.x, water.y) && AbsY(water.y) > 0)
			water.y--;
		if (water.y > 0)
		{
			lovely_place[0] = nil;
			lovely_place[1] = water.x;
			lovely_place[2] = water.y;
			return;
		}
	}
	// Dense vegetation is third best place
	var plants = FindObjects(Find_Distance(200), Find_NoContainer(), Find_Func("IsPlant"), Find_Exclude(lovely_place[0]));
	if (GetLength(plants) >= 10)
	{
		var plant = plants[Random(GetLength(plants))];
		lovely_place[0] = plant;
		lovely_place[1] = plant->GetX();
		lovely_place[2] = GetHorizonHeight(plant->GetY()) - 30;
		return;
	}
}

private func CheckLovelyPlace()
{
	if (lovely_place[0]) // Corpse or plant
	{
		if (lovely_place[0]->~IsPlant())
			// Vegetation is not so dense anymore?
			if (lovely_place[0]->ObjectCount(Find_Distance(200), Find_NoContainer(), Find_Func("IsPlant"), Find_Exclude(lovely_place[0])) < 5)
				return RemoveLovelyPlace();
		// Place has moved?
		if (Distance(lovely_place[0]->GetX(), lovely_place[0]->GetY(), lovely_place[1], lovely_place[2]) > 100)
			return RemoveLovelyPlace();
	} else {
		// Water is gone?
		if (!GBackLiquid(AbsX(lovely_place[1]), AbsY(lovely_place[2])))
			return RemoveLovelyPlace();
	}
	return true;
}

private func RemoveLovelyPlace()
{
	lovely_tries = 0;
	// Don't delete lovely_place[0]
	// It will be ignored in future searches
	lovely_place[1] = nil;
	lovely_place[2] = nil;
	return false;
}

private func CheckTurn()
{
	if (GetXDir() < 0)
		SetDir(DIR_Left);
	if (GetXDir() > 0)
		SetDir(DIR_Right);
}

/*-- Saving --*/

public func SaveScenarioObject(proplist props)
{
	if (!inherited(props, ...)) return false;
	// Ignore some fast-changing stuff
	props->Remove("XDir");
	props->Remove("YDir");
	props->Remove("Command");
	return true;
}

/* Definition */

local ActMap = {
	Fly = {
		Prototype = Action,
		Name = "Fly",
		Procedure = DFA_FLOAT,
		Speed = 200,
		Accel = 32,
		Decel = 32,
		Directions = 2,
		FlipDir = 1,
		Length = 3,
		Delay = 2,
		X = 0,
		Y = 0,
		Wdt = 2,
		Hgt = 2,
		NextAction = "Fly",
		EndCall = "CheckTurn",
	},
	Sleep = {
		Prototype = Action,
		Name = "Sleep",
		Procedure = DFA_FLOAT,
		Speed = 0,
		Directions = 2,
		FlipDir = 1,
		Length = 1,
		Delay = 1,
		X = 6,
		Y = 0,
		Wdt = 2,
		Hgt = 2,
		NextAction = "Hold",
	},
};

local Name = "$Name$";
local MaxEnergy = 20000;
local MaxBreath = 250;
local Placement = 2;
local NoBurnDecay = true;
local BorderBound = C4D_Border_Sides | C4D_Border_Top | C4D_Border_Bottom;
local ContactCalls = true;