File: Script.c

package info (click to toggle)
openclonk 8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 169,516 kB
  • sloc: cpp: 180,479; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 145; sh: 101; javascript: 34
file content (379 lines) | stat: -rw-r--r-- 10,690 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
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
/**
	Tutorial 07: Airborne Mining
	Author: Maikel
	
	Mine valuable gems from a ruby stalactite.
	
	Following controls and concepts are explained:
	 * Airship controls
	 * Rope ladders
	 * Gem materials
	 * Selling at the flagpole
*/

static guide; // guide object

protected func Initialize()
{
	// Tutorial goal.
	var goal = CreateObject(Goal_Tutorial);
	goal.Name = "$MsgGoalName$";
	goal.Description = "$MsgGoalDescription$";
	CreateObject(Rule_NoPowerNeed);
	
	// Place objects in different sections.
	InitMountain();
	InitRubyIsland();
	InitIslands();
	InitVegetation();
	InitAnimals();
	InitAI();
	
	// Environment.
	Time->Init();
	Time->SetTime(22 * 60 + 30);
	Time->SetCycleSpeed(0);
	
	// Wealth shown at all time
	GUI_Controller->ShowWealth();

	// Dialogue options -> repeat round.
	SetNextMission("Tutorials.ocf\\Tutorial07.ocs", "$MsgRepeatRound$", "$MsgRepeatRoundDesc$");
	return;
}

// Gamecall from goals, set next mission.
protected func OnGoalsFulfilled()
{
	// Achievement: Tutorial completed.
	GainScenarioAchievement("TutorialCompleted", 3);
	// Dialogue options -> next round.
	SetNextMission("Tutorials.ocf\\Tutorial08.ocs", "$MsgNextTutorial$", "$MsgNextTutorialDesc$");
	// Normal scenario ending by goal library.
	return false;
}

private func InitMountain()
{
	// Shipyard with lantern and flagpole.
	CreateObjectAbove(Flagpole, 55, 504)->MakeInvincible();
	var shipyard = CreateObjectAbove(Shipyard, 90, 504);
	shipyard->MakeInvincible();
	var lamp = shipyard->CreateContents(Lantern);
	lamp->TurnOn();
	// Indestructible airship.
	var airship = CreateObjectAbove(Airship, 120, 504);
	airship->MakeInvincible();
	// Lorry with dynamite and tools.
	var lorry = CreateObjectAbove(Lorry, 120, 498);
	for (var cnt = 0; cnt < 4; cnt ++)
	{
		var dynamite_box = lorry->CreateContents(DynamiteBox);
		dynamite_box->SetDynamiteCount(3);
	}
	lorry->CreateContents(Pickaxe);
	lorry->CreateContents(TeleGlove);
	lorry->MakeInvincible();
	return;
}

private func InitRubyIsland()
{
	// Two rope ladders give access to the ruby stalactite.
	CreateObjectAbove(Ropeladder, 740, 160)->Unroll(-1, COMD_Up);
	CreateObjectAbove(Ropeladder, 780, 160)->Unroll(-1, COMD_Up);
	return;
}

private func InitIslands()
{
	// Armory on the lower island with weaponry to kill bats.
	CreateObjectAbove(WindGenerator, 700, 648)->MakeInvincible();
	var armory = CreateObjectAbove(Armory, 654, 648);
	armory->CreateContents(Wood, 10);
	armory->CreateContents(Metal, 5);
	armory->MakeInvincible();
	// Flowers on two of the islands.
	Flower->Place(12, Rectangle(200, 100, 300, 200));
	Flower->Place(8, Rectangle(300, 300, 200, 200));
	return;
}

// Vegetation throughout the scenario.
private func InitVegetation()
{
	PlaceGrass(85);
	PlaceObjects(Firestone, 25 + Random(5), "Earth");
	PlaceObjects(Loam, 15 + Random(5), "Earth");
	Mushroom->Place(10);
	Branch->Place(40);
	Trunk->Place(10);
	Tree_Deciduous->Place(40);
	return;
}

private func InitAnimals()
{
	// The sky islands are a natural place for bats to hide. 		
	var bats = Bat->Place(6);
	// Make the bats a bit weaker so that they are killed with a single arrow.
	for (var bat in bats)
	{
		bat.MaxEnergy = 7000;
		bat->DoEnergy(bat.MaxEnergy - bat->GetEnergy());
	}
	// Some fireflies attracted to trees on two islands.
	Firefly->Place(2, nil, Rectangle(200, 100, 300, 200));
	Firefly->Place(2, nil, Rectangle(300, 300, 200, 200));
	return;
}

// Initializes the AI: which is all defined in System.ocg
private func InitAI()
{
	// A pilot npc for explaining the sky islands.
	var npc_pilot = CreateObjectAbove(Clonk, 108, 496);
	npc_pilot->SetColor(0xffff00);
	npc_pilot->SetName("Chris");
	npc_pilot->SetObjectLayer(npc_pilot);
	npc_pilot->SetSkin(2);
	npc_pilot->SetDir(DIR_Right);
	npc_pilot->SetDialogue("Pilot", true);
	return;
}

/*-- Player Handling --*/

protected func InitializePlayer(int plr)
{
	// Position player's clonk.
	var clonk = GetCrew(plr, 0);
	clonk->SetPosition(60, 494);
	var effect = AddEffect("ClonkRestore", clonk, 100, 10);
	effect.to_x = 60;
	effect.to_y = 494;
	
	// Items for the clonk.
	clonk->CreateContents(Shovel);
	var dynamite_box = clonk->CreateContents(DynamiteBox);
	dynamite_box->SetDynamiteCount(3);
	
	// Take ownership of the flags.
	for (var flag in FindObjects(Find_Or(Find_Func("IsFlagpole"), Find_ID(WindGenerator))))
		flag->SetOwner(plr);
	
	// Knowledge to construct bow and arrow.
	SetPlrKnowledge(plr, Bow);
	SetPlrKnowledge(plr, Arrow);
	
	// Add an interaction to call the airship.
	Helper_CallAirship->Create(clonk, Dialogue->FindByName("Pilot")->GetDialogueTarget(), FindObject(Find_ID(Airship)));
	
	// Add an effect to the clonk to track the goal.
	var track_goal = AddEffect("TrackGoal", nil, 100, 2);
	track_goal.plr = plr;

	// Standard player zoom for tutorials, player is not allowed to zoom in/out.
	SetPlayerViewLock(plr, true);
	SetPlayerZoomByViewRange(plr, 400, nil, PLRZOOM_Direct | PLRZOOM_LimitMax);
	
	// Determine player movement keys.
	var interact_prev = GetPlayerControlAssignment(plr, CON_InteractNext_Right, true, true);
	var interact_next = GetPlayerControlAssignment(plr, CON_InteractNext_Left, true, true);
	var interact_cycle = GetPlayerControlAssignment(plr, CON_InteractNext_CycleObject, true, true);
	var interact_cancel = GetPlayerControlAssignment(plr, CON_InteractNext_Stop, true, true);
	
	// Create tutorial guide, add messages, show first.
	guide = CreateObjectAbove(TutorialGuide, 0, 0, plr);
	guide->AddGuideMessage(Format("$MsgTutorialTalkToPilot$", interact_prev, interact_next, interact_cycle, interact_cancel));
	guide->ShowGuideMessage();
	var effect = AddEffect("TutorialTalkedToPilot", nil, 100, 2);
	effect.plr = plr;
	return;
}


/*-- Intro, Tutorial Goal & Outro --*/

global func FxTrackGoalTimer(object target, proplist effect, int time)
{
	if (GetWealth(effect.plr) >= 250)
	{
		var outro = AddEffect("GoalOutro", target, 100, 5);
		outro.plr = effect.plr;
		return FX_Execute_Kill;
	}
	return FX_OK;
}

global func FxGoalOutroStart(object target, proplist effect, int temp)
{
	if (temp)
		return FX_OK;	
	// Show guide message congratulating.
	guide->AddGuideMessage("$MsgTutorialCompleted$");
	guide->ShowGuideMessage();	
	return FX_OK;
}

global func FxGoalOutroTimer(object target, proplist effect, int time)
{
	if (time >= 60)
	{
		var goal = FindObject(Find_ID(Goal_Tutorial));
		goal->Fulfill();
		return FX_Execute_Kill;
	}
	return FX_OK;
}

global func FxGoalOutroStop(object target, proplist effect, int reason, bool temp)
{
	if (temp) 
		return FX_OK;
	return FX_OK;
}


/*-- Guide Messages --*/

public func OnHasTalkedToPilot()
{
	RemoveEffect("TutorialTalkedToPilot");
	return;
}

global func FxTutorialTalkedToPilotTimer()
{
	return FX_OK;
}

global func FxTutorialTalkedToPilotStop(object target, proplist effect, int reason, bool temp)
{
	if (temp)
		return FX_OK;

	// Determine player movement keys.
	var left = GetPlayerControlAssignment(effect.plr, CON_Left, true, true);
	var right = GetPlayerControlAssignment(effect.plr, CON_Right, true, true);
	var up = GetPlayerControlAssignment(effect.plr, CON_Up, true, true);
	var down = GetPlayerControlAssignment(effect.plr, CON_Down, true, true);
	var interact = GetPlayerControlAssignment(effect.plr, CON_Interact, true, true);
	var control_keys = Format("[%s] [%s] [%s] [%s]", up, left, down, right);
	
	guide->AddGuideMessage(Format("$MsgTutorialFindRubies$", interact, control_keys));
	guide->ShowGuideMessage();
	var new_effect = AddEffect("TutorialFoundStalactite", nil, 100, 2);
	new_effect.plr = effect.plr;
	return FX_OK;
}

global func FxTutorialFoundStalactiteTimer(object target, proplist effect, int timer)
{
	var clonk = FindObject(Find_ID(Clonk), Find_Distance(150, 780, 200));
	if (clonk)
	{
		guide->AddGuideMessage(Format("$MsgTutorialParkAirship$"));
		guide->ShowGuideMessage();
		AddEffect("TutorialAirshipParked", nil, 100, 2);
		return FX_Execute_Kill;
	}
	return FX_OK;
}

global func FxTutorialAirshipParkedTimer(object target, proplist effect, int timer)
{
	var clonk = FindObject(Find_ID(Clonk), Find_Distance(30, 688, 200));
	var airship = FindObject(Find_ID(Airship), Find_Distance(30, 688, 200));
	if (clonk && airship)
	{
		var plr = clonk->GetOwner();
		var left = GetPlayerControlAssignment(plr, CON_Left, true, true);
		var right = GetPlayerControlAssignment(plr, CON_Right, true, true);
		guide->AddGuideMessage(Format("$MsgTutorialLadderJump$", left, right));
		guide->ShowGuideMessage();
		AddEffect("TutorialOnStalactite", nil, 100, 2);
		return FX_Execute_Kill;
	}
	return FX_OK;
}

global func FxTutorialOnStalactiteTimer(object target, proplist effect, int timer)
{
	if (FindObject(Find_ID(Clonk), Find_InRect(810, 150, 24, 72)))
	{
		guide->AddGuideMessage("$MsgTutorialBlastGems$");
		guide->ShowGuideMessage();
		AddEffect("TutorialCollectGems", nil, 100, 2);
		return FX_Execute_Kill;
	}
	return FX_OK;
}

global func FxTutorialCollectGemsTimer(object target, proplist effect, int timer)
{
	if (FindObject(Find_ID(Ruby)))
	{
		guide->AddGuideMessage("$MsgTutorialCollectGems$");
		guide->ShowGuideMessage();
		return FX_Execute_Kill;
	}
	return FX_OK;
}

protected func OnGuideMessageShown(int plr, int index)
{
	// Show airship parking space.
	if (index == 2)
		TutArrowShowPos(688, 220, 135);
	// Show dynamite placement location.
	if (index == 4)
		TutArrowShowPos(800, 200, 60);	
	return;
}

protected func OnGuideMessageRemoved(int plr, int index)
{
	TutArrowClear();
	return;
}


/*-- Clonk restoring --*/

global func FxClonkRestoreTimer(object target, proplist effect, int time)
{
	// Respawn clonk to new location if reached certain position.
	return FX_OK;
}

// Relaunches the clonk, from death or removal.
global func FxClonkRestoreStop(object target, effect, int reason, bool  temporary)
{
	if (reason == 3 || reason == 4)
	{
		var restorer = CreateObject(ObjectRestorer, 0, 0, NO_OWNER);
		var x = BoundBy(target->GetX(), 0, LandscapeWidth());
		var y = BoundBy(target->GetY(), 0, LandscapeHeight());
		restorer->SetPosition(x, y);
		var to_x = effect.to_x;
		var to_y = effect.to_y;
		var airship = FindObject(Find_ID(Airship));
		if (airship)
		{
			to_x = airship->GetX();
			to_y = airship->GetY();
		}
		// Respawn new clonk.
		var plr = target->GetOwner();
		var clonk = CreateObject(Clonk, 0, 0, plr);
		clonk->GrabObjectInfo(target);
		Rule_Relaunch->TransferInventory(target, clonk);
		SetCursor(plr, clonk);
		clonk->DoEnergy(100000);
		// Add an interaction to call the airship.
		Helper_CallAirship->Create(clonk, Dialogue->FindByName("Pilot")->GetDialogueTarget(), FindObject(Find_ID(Airship)));
		restorer->SetRestoreObject(clonk, nil, to_x, to_y, 0, "ClonkRestore");
	}
	return FX_OK;
}