wind mill is only power producer for itself

issue1247
Maikel de Vries 2015-01-02 12:21:36 +01:00
parent 4914d73a82
commit 2c317888ca
3 changed files with 162 additions and 69 deletions

View File

@ -10,7 +10,6 @@
#include Library_Flag
#include Library_PowerProducer
local DefaultFlagRadius = 90;
/*-- Initialization --*/

View File

@ -1,66 +1,129 @@
/*--
/**
Windmill
Authors: Ringwaul, Clonkonaut
Crushes seeds into flour using power. It can either use its own power
generated by the mill or drain power from the network if the wind does
not supply enough.
Crushes seeds into flour using power - is an own power producer too
--*/
@authors Ringwaul, Clonkonaut, Maikel
*/
#include Library_Structure
#include Library_Ownable
#include Library_Producer
#include Library_PowerConsumer
#include Library_PowerProducer
#include Library_Flag
local DefaultFlagRadius = 90;
/*-- Initialization --*/
local wind_anim;
local last_wind;
local last_power;
local wheel;
func TurnAnimation(){return "Spin";}
func MinRevolutionTime(){return 18000;} // in frames
public func GetProducerPriority() { return 100; }
func TurnAnimation() { return "Spin"; }
func MinRevolutionTime() { return 18000; } // in frames
protected func Construction(object creator)
protected func Construction()
{
SetProperty("MeshTransformation", Trans_Rotate(-30,0,1,0));
SetProperty("MeshTransformation", Trans_Rotate(-30, 0, 1, 0));
SetAction("Default");
// uses functions of the wind generator
this.Wind2TurnEx = WindGenerator.Wind2Turn;
this.GetWeightedWind = WindGenerator.GetWeightedWind;
AddTimer("CollectionZone", 1);
return _inherited(creator, ...);
return _inherited(...);
}
protected func Initialize()
{
// create wheel
(this.wheel = CreateObject(WindGenerator_Wheel, 0, 0, NO_OWNER))->SetParent(this, 150);
// Set initial position
wind_anim = PlayAnimation(TurnAnimation(), 5, this.wheel->Anim_R(GetAnimationLength(TurnAnimation()), 0), Anim_Const(1000));
// Create a helper object for the wheel.
wheel = CreateObject(WindGenerator_Wheel, 0, 0, NO_OWNER);
wheel->SetParent(this, 150);
// Start the animation for the wheel.
PlayAnimation(TurnAnimation(), 5, wheel->Anim_R(GetAnimationLength(TurnAnimation()), 0), Anim_Const(1000));
// Initialize a regular check of the wheel's position and speed, also handles power updates.
last_power = 0;
AddTimer("Wind2Turn", 4);
Wind2Turn();
// Another timer for the collection zone.
AddTimer("CollectionZone", 1);
return _inherited(...);
}
func Wind2Turn()
// Timer, check for objects to collect in the designated collection zone.
public func CollectionZone()
{
// dummy, uses the function of the WindGenerator
this->Wind2TurnEx();
if (GetCon() < 100)
return;
for (var object in FindObjects(Find_InRect(-18 + 21 * GetDir(), 35, 15, 15), Find_OCF(OCF_Collectible), Find_NoContainer(), Find_Layer(GetObjectLayer())))
Collect(object);
return;
}
protected func Collection()
{
Sound("Clonk");
return;
}
/*-- Power Production --*/
// Returns the wind weighted over several points.
private func GetWeightedWind()
{
return (
(10 * GetWind(-150, -30)) +
(25 * GetWind( -75, -30)) +
(30 * GetWind( 0, -30)) +
(25 * GetWind( +75, -30)) +
(10 * GetWind(+150, -30))
) / 100;
}
// Turns wind into power and adjusts the power production accordingly.
public func Wind2Turn()
{
// Only produce power if fully constructed.
if (GetCon() < 100)
return;
// Determine the current power production.
var power = 0;
if (!wheel->Stuck() && !wheel->HasStopped())
{
power = Abs(wheel->GetRDir(MinRevolutionTime() / 90));
if (power < 5)
power = 0;
else
power = Max((power + 5) / 25, 1) * 50;
}
// Update the power consumption if the produced power has changed.
if (last_power != power)
{
last_power = power;
// If in production update the power consumption.
if (GetEffect("InProduction", this))
RegisterPowerRequest(PowerNeed());
}
// Adjust the wheel speed.
var current_wind = GetWeightedWind();
wheel->SetRDir(current_wind * 90, MinRevolutionTime());
// Make some sounds.
if (Abs(current_wind) >= 10 && Random(15 - Abs(current_wind / 10)) < 5)
Sound(["WoodCreak?","HingeCreak?"][Random(2)], false, nil, nil, nil, 75);
return;
}
// Power need for the production library.
private func PowerNeed()
{
return Max(0, 75 - last_power);
}
/*-- Production --*/
public func IsInteractable() { return true; }
private func IgnoreKnowledge() { return true; }
private func IsProduct(id product_id)
{
return product_id->~IsWindmillProduct();
}
private func ProductionTime(id toProduce) { return 290; }
private func PowerNeed() { return 75; }
public func NeedRawMaterial(id rawmat_id)
{
@ -71,6 +134,7 @@ public func NeedRawMaterial(id rawmat_id)
public func OnProductionStart(id product)
{
AddEffect("InProduction", this, 100, 10, this);
AddEffect("Crushing", this, 100, 10, this);
return;
}
@ -89,25 +153,14 @@ public func OnProductionHold(id product)
public func OnProductionFinish(id product)
{
RemoveEffect("InProduction", this);
RemoveEffect("Crushing", this);
return;
}
// Timer, check for objects to collect in the designated collection zone
func CollectionZone()
public func FxInProductionTimer(object target, proplist effect, int time)
{
if (GetCon() < 100) return;
if (!(FrameCounter() % 35)) Wind2Turn();
for (var object in FindObjects(Find_InRect(-18 + 21 * GetDir(),35,15,15), Find_OCF(OCF_Collectible), Find_NoContainer(), Find_Layer(GetObjectLayer())))
Collect(object);
}
protected func Collection()
{
Sound("Clonk");
return;
return 1;
}
public func FxCrushingTimer(object target, proplist effect, int time)
@ -136,30 +189,33 @@ public func OnProductEjection(object product)
protected func RejectCollect(id item, object collect)
{
if(collect->~IsMillIngredient()) return false;
else
return true;
if (collect->~IsMillIngredient())
return false;
return true;
}
func IsInteractable() { return true; }
/*-- Properties --*/
local ActMap = {
Default = {
Prototype = Action,
Name = "Default",
Procedure = DFA_NONE,
Directions = 2,
FlipDir = 1,
Length = 1,
Delay = 0,
FacetBase = 1,
NextAction = "Default",
},
Default = {
Prototype = Action,
Name = "Default",
Procedure = DFA_NONE,
Directions = 2,
FlipDir = 1,
Length = 1,
Delay = 0,
FacetBase = 1,
NextAction = "Default",
},
};
func Definition(def) {
SetProperty("PictureTransformation", Trans_Mul(Trans_Translate(2000,0,7000),Trans_Rotate(-20,1,0,0),Trans_Rotate(30,0,1,0)), def);
protected func Definition(def)
{
SetProperty("PictureTransformation", Trans_Mul(Trans_Translate(2000, 0, 7000), Trans_Rotate(-20, 1, 0, 0), Trans_Rotate(30, 0, 1, 0)), def);
}
local ContainBlast = true;
local BlastIncinerate = 100;
local HitPoints = 70;

View File

@ -424,11 +424,49 @@ global func Test7_OnFinished()
return;
}
// Test double network and power producing pumps.
// Test the reduced on-demand consumer (wind mill) with an on-demand producer to always have power.
global func Test8_OnStart(int plr)
{
// Power source (network 1): one wind generator.
// Power source: one steam engine.
var engine = CreateObject(SteamEngine, 40, 160, plr);
engine->CreateContents(Coal, 1);
// Change the windlevels so that the engine is needed from time to time.
SetWindFixed(50);
Schedule(nil, "SetWindFixed(25)", 5 * 36);
Schedule(nil, "SetWindFixed(0)", 10 * 36);
Schedule(nil, "SetWindFixed(50)", 15 * 36);
Schedule(nil, "SetWindFixed(0)", 20 * 36);
// Power consumer: one wind mill.
var windmill = CreateObject(Windmill, 116, 160, plr);
windmill->CreateContents(Seeds, 3);
windmill->AddToQueue(Flour, 3);
// Log what the test is about.
Log("An on-demand producer (steam engine) always provides power to an reduced on-demand consumer (wind mill).");
return true;
}
global func Test8_Completed()
{
if (ObjectCount(Find_ID(Flour)) >= 3)
return true;
return false;
}
global func Test8_OnFinished()
{
// Remove steam engine, wind mill and flour.
RemoveAll(Find_Or(Find_ID(SteamEngine), Find_ID(Windmill), Find_ID(Flour)));
return;
}
// Test a double separated network and power producing pumps.
global func Test9_OnStart(int plr)
{
// Power source (network 1): one wind generator.
SetWindFixed(100);
CreateObject(WindGenerator, 40, 160, plr);
// Power consumer (network 1): five pumps.
@ -476,14 +514,14 @@ global func Test8_OnStart(int plr)
return true;
}
global func Test8_Completed()
global func Test9_Completed()
{
if (ObjectCount(Find_ID(Wood)) >= 5)
return true;
return false;
}
global func Test8_OnFinished()
global func Test9_OnFinished()
{
// Restore water levels.
DrawMaterialQuad("Water", 144, 168, 208 + 1, 168, 208 + 1, 304, 144, 304, true);