Formatted code

master
Mark 2018-12-21 14:26:24 +01:00
parent c6254c2512
commit 36dbbd27e1
1 changed files with 56 additions and 30 deletions

View File

@ -4,10 +4,10 @@
Components are a property of the form Components are a property of the form
local Components = {Def1 = cnt1, Def2 = cnt2, ...}; local Components = {Def1 = cnt1, Def2 = cnt2, ...};
which can be modified directly or via the script functions in this file: which can be modified directly or via the script functions in this file:
SetComponent2(id component, int count) SetComponent(id component, int count)
GetComponent(id component, int index) GetComponent(id component, int index)
Split2Components() Split2Components()
@author Maikel @author Maikel
*/ */
@ -18,22 +18,30 @@ global func SetComponent(id component, int count)
{ {
// Safety: can only be called from object. // Safety: can only be called from object.
if (!this || GetType(this) != C4V_C4Object) if (!this || GetType(this) != C4V_C4Object)
{
return FatalError(Format("SetComponent must be called from object context and not from %v", this)); return FatalError(Format("SetComponent must be called from object context and not from %v", this));
}
// Safety: component must be specified. // Safety: component must be specified.
if (!component || GetType(component) != C4V_Def) if (!component || GetType(component) != C4V_Def)
{
return FatalError(Format("First parameter (id component) of SetComponent must be a definition but was %v.", component)); return FatalError(Format("First parameter (id component) of SetComponent must be a definition but was %v.", component));
}
// Ensure count is non-negative. // Ensure count is non-negative.
count = Max(count, 0); count = Max(count, 0);
// Initialize Components if it does not exist yet. // Initialize Components if it does not exist yet.
if (!this.Components || GetType(this.Components) != C4V_PropList) if (!this.Components || GetType(this.Components) != C4V_PropList)
{
this.Components = {}; this.Components = {};
}
// Ensure object property is different from definition property. // Ensure object property is different from definition property.
if (this.Components == GetID().Components) if (this.Components == GetID().Components)
{
MakePropertyWritable("Components"); MakePropertyWritable("Components");
}
// Write the new count to the components properties. // Write the new count to the components properties.
this.Components[Format("%i", component)] = count; this.Components[Format("%i", component)] = count;
return; return;
@ -46,30 +54,40 @@ global func GetComponent(id component, int index)
{ {
// Safety: can only be called from object or definition context. // Safety: can only be called from object or definition context.
if (!this || (GetType(this) != C4V_C4Object && GetType(this) != C4V_Def)) if (!this || (GetType(this) != C4V_C4Object && GetType(this) != C4V_Def))
{
return FatalError(Format("GetComponent must be called from object or definition context and not from %v", this)); return FatalError(Format("GetComponent must be called from object or definition context and not from %v", this));
}
// Safety: return nil if Components could not be found or are not a proplist. // Safety: return nil if Components could not be found or are not a proplist.
if (!this.Components || GetType(this.Components) != C4V_PropList) if (!this.Components || GetType(this.Components) != C4V_PropList)
{
return; return;
}
// If component is specified return the count for that definition. // If component is specified return the count for that definition.
if (GetType(component) == C4V_Def) if (GetType(component) == C4V_Def)
{
return this.Components[Format("%i", component)]; return this.Components[Format("%i", component)];
}
// Ensure the index is valid. // Ensure the index is valid.
index = Max(index, 0); index = Max(index, 0);
// If component is not specified return the definition of the component at the index. // If component is not specified return the definition of the component at the index.
var cnt = 0; var iteration_index = 0;
for (var entry in GetProperties(this.Components)) for (var entry in GetProperties(this.Components))
{ {
// Check if the entry is an actual valid definition. // Check if the entry is an actual valid definition.
var entry_def = GetDefinition(entry); var entry_def = GetDefinition(entry);
if (!entry_def) if (!entry_def)
{
continue; continue;
if (index == cnt) }
if (index == iteration_index)
{
return entry_def; return entry_def;
cnt++; }
iteration_index += 1;
} }
return; return;
} }
@ -78,21 +96,25 @@ global func GetComponent(id component, int index)
global func OnCompletionChange(int old_con, int new_con) global func OnCompletionChange(int old_con, int new_con)
{ {
if (!this || GetType(this) != C4V_C4Object) if (!this || GetType(this) != C4V_C4Object)
{
return _inherited(old_con, new_con, ...); return _inherited(old_con, new_con, ...);
}
// Determine whether the object allows Oversize. // Determine whether the object allows Oversize.
var oversize = GetDefCoreVal("Oversize", "DefCore"); var oversize = GetDefCoreVal("Oversize", "DefCore");
// Loop over all components and set their new count. // Loop over all components and set their new count.
var index = 0, comp; var index = 0, component;
while (comp = GetID()->GetComponent(nil, index)) while (component = GetID()->GetComponent(nil, index))
{ {
var def_cnt = GetID()->GetComponent(comp); var def_amount = GetID()->GetComponent(component);
var new_cnt = Max(def_cnt * new_con / 100000, 0); var new_amount = Max(def_amount * new_con / 100000, 0);
// If the object has no Oversize then constrain the maximum number of components. // If the object has no Oversize then constrain the maximum number of components.
if (!oversize) if (!oversize)
new_cnt = Min(new_cnt, def_cnt); {
SetComponent(comp, new_cnt); new_amount = Min(new_amount, def_amount);
}
SetComponent(component, new_amount);
index++; index++;
} }
return _inherited(old_con, new_con, ...); return _inherited(old_con, new_con, ...);
@ -104,26 +126,30 @@ global func Split2Components()
{ {
// Safety: can only be called from object context. // Safety: can only be called from object context.
if (!this || GetType(this) != C4V_C4Object) if (!this || GetType(this) != C4V_C4Object)
{
return FatalError(Format("Split2Components must be called from object context and not from %v", this)); return FatalError(Format("Split2Components must be called from object context and not from %v", this));
}
// Transfer all contents to container. // Transfer all contents to container.
var ctr = Contained(); var container = Contained();
while (Contents()) while (Contents())
if (!ctr || !Contents()->Enter(ctr)) {
if (!container || !Contents()->Enter(container))
Contents()->Exit(); Contents()->Exit();
}
// Split components. // Split components.
for (var i = 0, compid; compid = GetComponent(nil, i); ++i) for (var i = 0, component_id; component_id = GetComponent(nil, i); ++i)
for (var j = 0; j < GetComponent(compid); ++j) for (var j = 0; j < GetComponent(component_id); ++j)
{ {
var comp = CreateObjectAbove(compid, nil, nil, GetOwner()); var component = CreateObjectAbove(component_id, nil, nil, GetOwner());
if (OnFire()) comp->Incinerate(); if (OnFire()) component->Incinerate();
if (!ctr || !comp->Enter(ctr)) if (!container || !component->Enter(container))
{ {
comp->SetR(Random(360)); component->SetR(Random(360));
comp->SetXDir(Random(3) - 1); component->SetXDir(Random(3) - 1);
comp->SetYDir(Random(3) - 1); component->SetYDir(Random(3) - 1);
comp->SetRDir(Random(3) - 1); component->SetRDir(Random(3) - 1);
} }
} }
return RemoveObject(); return RemoveObject();