clean up gold seller library

issue1247
Maikel de Vries 2015-02-09 13:16:45 +01:00
parent 81f9ee1c85
commit b7b6b187f7
1 changed files with 68 additions and 72 deletions

View File

@ -1,70 +1,33 @@
/* --- GoldSeller --- */
/*
automatically sells gold and other valuables nearby when in a base area
/**
Gold Seller
Automatically sells gold and other valuables nearby when in a base area.
Include this library in the object you wan to make an automatic gold
seller and don't forget to return _inherited(...) in Initialize().
The radius for selling gold can be set by overloading the function
AutoSellValuablesRadius() and returning the new radius.
@author Maikel
*/
func AutoSellValuablesRadius(){return 50;}
static const LIBRARY_GOLDSELLER_MinTimer = 10;
static const LIBRARY_GOLDSELLER_MaxTimer = 30;
static const LIBRARY_GOLDSELLER_TimerStep = 10;
// Standard the object has an auto sell radius of 50 pixels.
private func AutoSellValuablesRadius() { return 50; }
func Initialize()
protected func Initialize()
{
AddEffect("AutoSellValuables", this, 1, LIBRARY_GOLDSELLER_MaxTimer, this);
AddEffect("AutoSellValuables", this, 1, 4, this);
return _inherited(...);
}
func FxAutoSellValuablesStart(t, effect, temp)
protected func FxAutoSellValuablesStart(object target, proplist effect, int temp)
{
if(temp) return;
effect.target = t;
}
func FxAutoSellValuablesTimer(_, effect, time)
{
var owner = GetOwner();
if(!GetPlayerName(owner))
{
effect.Interval = LIBRARY_GOLDSELLER_MaxTimer;
return 1;
}
var objs = [];
for(var obj in FindObjects(Find_Distance(AutoSellValuablesRadius()), Find_NoContainer(), Find_Func("IsValuable")))
{
if(obj->Stuck()) continue;
if(!IsAllied(owner, obj->GetController())) continue;
objs[GetLength(objs)] = obj;
}
if(!GetLength(objs))
{
effect.Interval = LIBRARY_GOLDSELLER_MaxTimer;
return 1;
}
var comp = objs[0];
var to_remove = [];
for(var obj in objs)
{
var d = ObjectDistance(obj, comp);
if(d > 50) continue;
to_remove[GetLength(to_remove)] = obj;
}
// assert: at least one object in to_remove
var value = 0;
var fm = CreateObjectAbove(FloatingMessage, comp->GetX() - GetX(), comp->GetY() - GetY(), NO_OWNER);
fm->SetColor(250, 200, 50);
fm->FadeOut(2, 10);
fm->SetSpeed(0, -5);
var dust_particles =
if (temp)
return FX_OK;
// Ensure a rather short interval to not miss any fast objects.
effect.Interval = 4;
// Define particles.
effect.dust_particles =
{
Prototype = Particles_Dust(),
Size = PV_KeyFrames(0, 0, 0, 100, 10, 1000, 0),
@ -73,23 +36,56 @@ func FxAutoSellValuablesTimer(_, effect, time)
G = 125,
B = 125,
};
for(var valuable in to_remove)
effect.flash_particles =
{
Prototype = Particles_Flash(),
Size = 12,
};
return FX_OK;
}
protected func FxAutoSellValuablesTimer(object target, proplist effect, int time)
{
var owner = GetOwner();
var sell_objects = [];
// Find objects which can be sold.
for (var obj in FindObjects(Find_Distance(AutoSellValuablesRadius()), Find_NoContainer(), Find_Func("IsValuable")))
{
if(valuable->~QueryOnSell(valuable->GetController())) continue;
if (obj->Stuck())
continue;
if (!IsAllied(owner, obj->GetController()))
continue;
PushBack(sell_objects, obj);
}
// Don't do anything if there are no objects to sell.
if (GetLength(sell_objects) <= 0)
return FX_OK;
DoWealth(valuable->GetController(), valuable->GetValue());
value += valuable->GetValue();
CreateParticle("Flash", valuable->GetX() - GetX(), valuable->GetY() - GetY(), 0, 0, 8, { Prototype = Particles_Flash(), Size = 2 * Max(5, Max(valuable->GetDefWidth(), valuable->GetDefHeight())) });
CreateParticle("Dust", valuable->GetX() - GetX(), valuable->GetY() - GetY(), PV_Random(-10, 10), PV_Random(-10, 10), PV_Random(18, 36), dust_particles, 10);
valuable->RemoveObject();
// Loop over all objects which are to be sold.
var has_sold = false;
for (var to_sell in sell_objects)
{
// Check if object may be sold and add wealth.
if (to_sell->~QueryOnSell(to_sell->GetController()))
continue;
DoWealth(to_sell->GetController(), to_sell->GetValue());
// Set has sold variable to true.
has_sold = true;
// For each of the objects to sell create a floating message indicating the value.
var floating_message = CreateObjectAbove(FloatingMessage, to_sell->GetX() - GetX(), to_sell->GetY() - GetY(), NO_OWNER);
floating_message->SetColor(250, 200, 50);
floating_message->FadeOut(2, 10);
floating_message->SetSpeed(0, -5);
floating_message->SetMessage(Format("%d</c>{{Icon_Coins}}", to_sell->GetValue()));
// Create some particles and remove object.
CreateParticle("Flash", to_sell->GetX() - GetX(), to_sell->GetY() - GetY(), 0, 0, 8, effect.flash_particles);
CreateParticle("Dust", to_sell->GetX() - GetX(), to_sell->GetY() - GetY(), PV_Random(-10, 10), PV_Random(-10, 10), PV_Random(18, 36), effect.dust_particles, 10);
to_sell->RemoveObject();
}
fm->SetMessage(Format("%d</c>{{Icon_Coins}}", value));
Sound("Cash");
// Play a sound if a valuable has been sold.
if (has_sold)
Sound("Cash");
effect.Interval = BoundBy(effect.Interval - Random(LIBRARY_GOLDSELLER_TimerStep), LIBRARY_GOLDSELLER_MinTimer, LIBRARY_GOLDSELLER_MaxTimer);
return 1;
return FX_OK;
}