-
Notifications
You must be signed in to change notification settings - Fork 18
/
Helpers.c
182 lines (163 loc) · 5.37 KB
/
Helpers.c
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
/* Some useful helper functions */
#strict
global func MessageWindow(string pMsg, int iForPlr, id idIcon, string pCaption)
{
// get icon
if (!idIcon) idIcon=GetID();
// get caption
if (!pCaption) pCaption=GetName();
// create msg window (menu)
var pCursor = GetCursor(iForPlr);
if (!CreateMenu(idIcon, pCursor, pCursor, 0, pCaption, 0, 2)) return();
AddMenuItem(pCaption, "", TIM1, pCursor,0,0,pMsg);
return(1);
}
global func RemoveAll(id idDef, int dwOCF)
{
var Cnt, obj = FindObject(idDef, 0,0,0,0, dwOCF), next;
while (obj)
{
// Get the next object in case obj->Destruction does funny things
next = FindObject(idDef, 0,0,0,0, dwOCF, 0,0,0, obj);
if(RemoveObject(obj))
++Cnt;
obj = next;
}
return(Cnt);
}
global func CastlePanic()
{
return(ResortObjects("CastlePanicResort", 1));
}
global func CastlePanicResort(object pObj1, object pObj2)
{
return(GetDefBottom(pObj1)-GetDefBottom(pObj2));
}
global func SetBit(int iOldVal, int iBitNr, bool iBit)
{
if(GetBit(iOldVal, iBitNr) != (iBit != 0))
return(ToggleBit(iOldVal, iBitNr));
return(iOldVal);
}
global func GetBit(int iValue, int iBitNr)
{
return((iValue & (1 << iBitNr)) != 0);
}
global func ToggleBit(int iOldVal, int iBitNr)
{
return(iOldVal ^ (1 << iBitNr));
}
global func RGB(int r, int g, int b) { return((r & 255)<<16 | (g & 255)<<8 | (b & 255)); }
global func RGBa (int r, int g, int b, int a) { return((a & 255)<<24 | (r & 255)<<16 | (g & 255)<<8 | (b & 255)); }
global func DrawParticleLine (szKind, x0, y0, x1, y1, prtdist, a, b0, b1, ydir)
{
// Parameter gültig?
if (!prtdist) return(0);
// Anzahl der benötigten Partikel berechnen
var prtnum = Max(Distance(x0, y0, x1, y1) / prtdist, 2);
var i=prtnum;
// Partikel erzeugen!
while (i>-1)
{
var i1,i2,b; i2 = i*256/prtnum; i1 = 256-i2;
b = ((b0&16711935)*i1 + (b1&16711935)*i2)>>8 & 16711935
| ((b0>>8&16711935)*i1 + (b1>>8&16711935)*i2) & -16711936;
if (!b && (b0 | b1)) ++b;
CreateParticle(szKind, x0+(x1-x0)*i/prtnum, y0+(y1-y0)*i--/prtnum, 0,ydir, a, b);
}
// Erfolg; Anzahl erzeugter Partikel zurückgeben
return (prtnum);
}
//Such nach einem Objekt, dass ein Acquire-Command holen könnte
global func GetAvailableObject (def, xobj)
{
var obj;
// Next closest
while (obj = FindObject (def, 0, 0, -1, -1, OCF_Available (), 0, 0, 0, obj))
{
if (ObjectDistance (obj) > 559) return ();
// Object is near enough
if (!Inside (GetX () - GetX (obj), -500, +500)) continue;
if (!Inside (GetY () - GetY (obj), -250, +250)) continue;
// Object is not connected to a pipe (for line construction kits)
if (FindObject (SPIP, 0, 0, 0, 0, 0, "Connect", obj)) continue;
if (FindObject (DPIP, 0, 0, 0, 0, 0, "Connect", obj)) continue;
// Must be complete
if (~GetOCF (obj) & OCF_Fullcon ()) continue;
// Doesn't burn
if (OnFire( obj)) continue;
// Not contained in xobj
if (!(Contained (obj) == xobj) || !xobj)
// We found one
break;
}
return (obj);
}
// Einen Script zeitverzögert und ggf. wiederholt ausführen
global func Schedule(string strScript, int iInterval, int iRepeat, object pObj)
{
// Default
if(!iRepeat) iRepeat = 1;
if(!pObj) pObj = this();
// Effekt erzeugen
var iEffect = AddEffect("IntSchedule", pObj, 1, iInterval, pObj);
if(iEffect <= 0) return(false);
// Variablen setzen
EffectVar(0, pObj, iEffect) = strScript;
EffectVar(1, pObj, iEffect) = iRepeat;
return(true);
}
global func FxIntScheduleTimer(object pObj, int iEffect)
{
// Nur eine bestimmte Anzahl Ausführungen
var fDone = (--EffectVar(1, pObj, iEffect) <= 0);
// Ausführen
eval(EffectVar(0, pObj, iEffect));
return(-fDone);
}
// Eine Funktion zeitverzögert und ggf. wiederholt aufrufen
global func ScheduleCall(object pObj, string strFunction, int iInterval, int iRepeat, par0, par1, par2, par3, par4)
{
// Default
if(!iRepeat) iRepeat = 1;
if(!pObj) pObj = this();
// Effekt erzeugen
var iEffect = AddEffect("IntScheduleCall", pObj, 1, iInterval, pObj);
if(iEffect <= 0) return(false);
// Variablen setzen
EffectVar(0, pObj, iEffect) = strFunction;
EffectVar(1, pObj, iEffect) = iRepeat;
// EffectVar(2): Nur zur Abwärtskompatibilität reserviert
EffectVar(2, pObj, iEffect) = pObj;
for(var i = 0; i < 5; i++)
EffectVar(i + 3, pObj, iEffect) = Par(i + 4);
return(true);
}
global func FxIntScheduleCallTimer(object pObj, int iEffect)
{
// Nur eine bestimmte Anzahl Ausführungen
var fDone = (--EffectVar(1, pObj, iEffect) <= 0);
// Ausführen
Call(EffectVar(0, pObj, iEffect), EffectVar(3, pObj, iEffect), EffectVar(4, pObj, iEffect), EffectVar(5, pObj, iEffect), EffectVar(6, pObj, iEffect), EffectVar(7, pObj, iEffect));
// Nur eine bestimmte Anzahl Ausführungen
return (-fDone);
}
global func ClearScheduleCall(object pObj, string strFunction)
{
var i, iEffect;
// Von Effektzahl abwärts zählen, da Effekte entfernt werden
i = GetEffectCount("IntScheduleCall", pObj);
while (i--)
// Alle ScheduleCall-Effekte prüfen
if (iEffect = GetEffect("IntScheduleCall", pObj, i))
// Gesuchte Zielfunktion
if (EffectVar(0, pObj, iEffect) S= strFunction)
// Effekt löschen
RemoveEffect(0, pObj, iEffect);
}
// parameters same as for SetAction
global func SetActionKeepPhase(...)
{
var phase = GetPhase();
return SetAction(...) && SetPhase(phase);
}