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 (161 lines) | stat: -rw-r--r-- 2,898 bytes parent folder | download | duplicates (6)
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
/*
Location for King of the Hill
Author: Zapper

This object manages the logic behind the goal.
It creates the marker and a star circle one frame after it is created.
*/

local marker;
local stars;
local color;
local king;
local timer;
local koth_goal;

func Initialize() {
	
	ScheduleCall(this, "PostInitialize", 1, 0);
	timer=0;
	return(1);
}

func SetKotH(object koth)
{
	koth_goal = koth;
}

func GetKing()
{
	return king;
}

func Destruction()
{
	for(var s in stars)
	{
		if(s)
			s->RemoveObject();
	}
	if(marker)
		marker->RemoveObject();
}

func PostInitialize()
{
	marker=CreateObjectAbove(KingOfTheHill_Marker, 0, -5, NO_OWNER);
	marker->SetOrigin(this);
	CreateStarCircle();
	AddEffect("Timer", this, 10, 10, this);
}

func NewPosition()
{
	if(marker) marker->SetPosition(this->GetX(), this->GetY());
	CreateStarCircle();
}

func FxTimerTimer(target, effect, effect_time)
{
	this->AdjustStarColor();
	this->CheckNewKing();
}

func CheckNewKing()
{
	if(king)
	if(!king->GetAlive())
		king=nil;
		
	if(king) return;
	
	var new=FindObject(Find_Distance(koth_goal->GetRadius()), Find_NoContainer(), Find_OCF(OCF_CrewMember));
	if(new)
	{
		king=new;
	}
}

func SetKing(object to)
{
	
	if(king)
		if(GetEffect("KOTHKing", king))
			RemoveEffect("KOTHKing", king);
	king=to;
	
	if(king != nil)
		AddEffect("KOTHKing", king, 10, 35, this);
}

func FxKOTHKingTimer(target, effect)
{
	//target->DoEnergy(1);
}

func FxKOTHKingStop(target, effect, reason, temp)
{
	if(temp) return;
	if(!target) return;
	if(!this) return;
	
	var killer=target->GetKiller();
	if(killer == NO_OWNER || killer == target->GetOwner())
		this->SetKing(nil);
	else
	{
		var crew=GetCursor(killer);
		if(!(crew->GetOCF() & OCF_CrewMember))
			crew=GetCrew(killer);
		if(!crew) this->SetKing(nil);
		else
			this->SetKing(crew);
	}
}

func GetStarColor(int which)
{
	if(king) return color;
	return RGB(200 + Cos(timer + which, 50), 200 + Sin(timer * 2 + which, 50), 0);
}

func AdjustStarColor()
{
	++timer;
	if(king)
	{
		var percent=(king->GetEnergy() * 100) / (king->GetMaxEnergy());
		var red=255; if(percent > 50) red=(255*(100-2*(percent-50))) / 100;
		var green=255; if(percent < 50) green=(255*(2*percent)) / 100;
		color=RGB(red/2, green/2, 0);
	}
	else
	{
		color=RGB(200 + Cos(timer, 50), 200 + Sin(timer * 2, 50), 0);
	}
}

func CreateStarCircle()
{
	var radius=koth_goal->GetRadius();
	if(radius == nil) return FatalError("Goal_KingOfTheHill: radius has to be set before use!");
	
	if(GetType(stars) != C4V_Array)
		stars=[];
	for(var star in stars) star->RemoveObject();
	stars=[];
	var amount=radius / 15;
	
	var cnt=0;
	for(var i=0;i<360;i+=360/amount)
	{
		var star=CreateObjectAbove(KingOfTheHill_Star, Sin(i, radius), -Cos(i, radius) + 15);
		star->Init(this, cnt++);
		stars[GetLength(stars)]=star;
	}
}

// stored by main goal
public func SaveScenarioObject() { return false; }

local Name = "$Name$";