Refactoring: Construction site takes materials, not the constructor

The functionality of taking construction materials from a clonk and lorries was extracted to a separate function and moved from the constructor to the construction site. This is a little bit of an esthetic decision, but it is also useful for my project that has a spacebar-interaction which takes construction materials from the clonk without the need to open the inventory menu.
liquid_container
Mark 2016-03-29 17:38:43 +02:00
parent fb761fbd58
commit 7e74baac55
2 changed files with 40 additions and 35 deletions

View File

@ -353,4 +353,42 @@ private func StartConstructing()
}
}
}
}
}
func TakeConstructionMaterials(object from_clonk)
{
// check for material
var comp, index = 0;
var mat;
var w = definition->GetDefWidth() + 10;
var h = definition->GetDefHeight() + 10;
while (comp = GetComponent(nil, index, nil, definition))
{
// find material
var count_needed = GetComponent(comp, nil, nil, definition);
index++;
mat = CreateArray();
// 1. look for stuff in the clonk
mat[0] = FindObjects(Find_ID(comp), Find_Container(from_clonk));
// 2. look for stuff lying around
mat[1] = from_clonk->FindObjects(Find_ID(comp), Find_NoContainer(), Find_InRect(-w/2, -h/2, w,h));
// 3. look for stuff in nearby lorries/containers
var i = 2;
for(var cont in from_clonk->FindObjects(Find_Or(Find_Func("IsLorry"), Find_Func("IsContainer")), Find_InRect(-w/2, -h/2, w,h)))
mat[i] = FindObjects(Find_ID(comp), Find_Container(cont));
// move it
for(var mat2 in mat)
{
for(var o in mat2)
{
if(count_needed <= 0)
break;
o->Exit();
o->Enter(this);
count_needed--;
}
}
}
}

View File

@ -162,41 +162,8 @@ public func CreateConstructionSite(object clonk, id structure_id, int x, int y,
site->Set(structure_id, dir, stick_to);
//if(!(site = CreateConstruction(structure_id, x, y, Contained()->GetOwner(), 1, 1, 1)))
//return false;
// check for material
var comp, index = 0;
var mat;
var w = structure_id->GetDefWidth() + 10;
var h = structure_id->GetDefHeight() + 10;
while (comp = GetComponent(nil, index, nil, structure_id))
{
// find material
var count_needed = GetComponent(comp, nil, nil, structure_id);
index++;
mat = CreateArray();
// 1. look for stuff in the clonk
mat[0] = FindObjects(Find_ID(comp), Find_Container(clonk));
// 2. look for stuff lying around
mat[1] = clonk->FindObjects(Find_ID(comp), Find_NoContainer(), Find_InRect(-w/2, -h/2, w,h));
// 3. look for stuff in nearby lorries/containers
var i = 2;
for(var cont in clonk->FindObjects(Find_Or(Find_Func("IsLorry"), Find_Func("IsContainer")), Find_InRect(-w/2, -h/2, w,h)))
mat[i] = FindObjects(Find_ID(comp), Find_Container(cont));
// move it
for(var mat2 in mat)
{
for(var o in mat2)
{
if(count_needed <= 0)
break;
o->Exit();
o->Enter(site);
count_needed--;
}
}
}
site->TakeConstructionMaterials(clonk);
// Message
clonk->Message("$TxtConstructions$", structure_id->GetName());