From 9cec9b507106d8a47ab19a9a270670d6362ed60c Mon Sep 17 00:00:00 2001 From: Sven Eberhardt Date: Thu, 1 Sep 2016 02:23:42 -0400 Subject: [PATCH] Make animal library properties editor-adjustable Allows e.g. placement of animals that do not reproduce. --- .../Objects.ocd/Animals.ocd/Bat.ocd/Script.c | 7 ++- .../Libraries.ocd/Animal.ocd/Script.c | 58 +++++++++++++++---- .../Libraries.ocd/Animal.ocd/StringTblDE.txt | 6 ++ .../Libraries.ocd/Animal.ocd/StringTblUS.txt | 6 ++ 4 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 planet/Objects.ocd/Libraries.ocd/Animal.ocd/StringTblDE.txt create mode 100644 planet/Objects.ocd/Libraries.ocd/Animal.ocd/StringTblUS.txt diff --git a/planet/Objects.ocd/Animals.ocd/Bat.ocd/Script.c b/planet/Objects.ocd/Animals.ocd/Bat.ocd/Script.c index 9dd946b0f..12cf62bdd 100644 --- a/planet/Objects.ocd/Animals.ocd/Bat.ocd/Script.c +++ b/planet/Objects.ocd/Animals.ocd/Bat.ocd/Script.c @@ -486,9 +486,10 @@ private func Death() /*-- Reproduction --*/ -private func ReproductionAreaSize() { return 1200; } -private func ReproductionRate() { return 200; } -private func MaxAnimalCount() { return 10; } +// Overloading animal library default values +local animal_reproduction_area_size = 1200; +local animal_reproduction_rate = 50; +local animal_max_count = 10; // Only bats with full health reproduce. private func SpecialReproductionCondition() diff --git a/planet/Objects.ocd/Libraries.ocd/Animal.ocd/Script.c b/planet/Objects.ocd/Libraries.ocd/Animal.ocd/Script.c index 7fbd26b7d..327decd01 100644 --- a/planet/Objects.ocd/Libraries.ocd/Animal.ocd/Script.c +++ b/planet/Objects.ocd/Libraries.ocd/Animal.ocd/Script.c @@ -10,15 +10,26 @@ // This object is an animal. public func IsAnimal() { return true; } -protected func Construction() +protected func Construction(...) { // Add a reproduction timer. - AddEffect("IntReproduction", this, 100, 72, this); + AddReproductionEffect(); // Add a growth effect. StartGrowth(GrowthSpeed()); _inherited(...); } +private func AddReproductionEffect() +{ + if (!GetEffect("IntReproduction", this)) + return AddEffect("IntReproduction", this, 100, 72, this); +} + +private func RemoveReproductionEffect() +{ + return RemoveEffect("IntReproduction"); +} + /*-- Growth --*/ @@ -28,14 +39,26 @@ private func GrowthSpeed() { return 5; } /*-- Reproduction --*/ +local animal_reproduction_area_size = 800; +local animal_reproduction_rate = 67; +local animal_max_count = 10; + // Population control is handled through these variables. // The area, in which new animals of this kind can appear. -private func ReproductionAreaSize() { return 800; } -// The chance that reproduction takes place in one timer interval. -// The higher this value the less likely it is to reproduce. -private func ReproductionRate() { return 150; } +public func ReproductionAreaSize() { return animal_reproduction_area_size; } +public func SetReproductionAreaSize(int v) { animal_reproduction_area_size = v; return true; } +// The chance that reproduction takes place in one timer interval. From 0 to 10000. +// The higher this value the more likely it is to reproduce. Special: If it is zero, reproduction is off. +public func ReproductionRate() { return animal_reproduction_rate; } +public func SetReproductionRate(int v) +{ + animal_reproduction_rate = v; + if (v) AddReproductionEffect(); else RemoveReproductionEffect(); + return true; +} // The maximal animal count in the area. -private func MaxAnimalCount() { return 10; } +public func MaxAnimalCount() { return animal_max_count; } +public func SetMaxAnimalCount(int v) { animal_max_count = v; return true; } // Special reproduction method (e.g. with egg). private func SpecialReproduction() @@ -66,14 +89,14 @@ public func FxIntReproductionTimer(object target, proplist effect, int time) // Already dead or not full grown? Don't do anything. if (!GetAlive() || GetCon() < 100) return FX_OK; + // Apply the reproduction rate. + if (Random(10000) >= ReproductionRate()) + return FX_OK; // Special conditions not fulfilled? Don't do anything either. if (!SpecialReproductionCondition()) return FX_OK; // Check whether there are already enough animals of this kind. - if (CountAnimalsInArea() > MaxAnimalCount()) - return FX_OK; - // Then apply the reproduction rate. - if (Random(ReproductionRate())) + if (CountAnimalsInArea() >= MaxAnimalCount()) return FX_OK; // Reproduction: first try special reproduction, otherwise normal. if (!SpecialReproduction()) @@ -110,3 +133,16 @@ protected func RejectEntrance(object container) // For all other cases the entrance is blocked. return true; } + + + +/* Editor */ + +public func Definition(def, ...) +{ + if (!def.EditorProps) def.EditorProps = {}; + def.EditorProps.animal_reproduction_area_size = { Name="$ReproductionAreaSize$", EditorHelp="$ReproductionAreaSizeHelp$", Type="int", Min=0, AsyncGet="ReproductionAreaSize", Set="SetReproductionAreaSize" }; + def.EditorProps.animal_reproduction_rate = { Name="$ReproductionRate$", EditorHelp="$ReproductionRateHelp$", Type="int", Min=0, Max=10000, AsyncGet="ReproductionRate", Set="SetReproductionRate" }; + def.EditorProps.animal_max_count = { Name="$MaxCount$", EditorHelp="$MaxCountHelp$", Type="int", Min=1, AsyncGet="MaxAnimalCount", Set="SetMaxAnimalCount" }; + return _inherited(def, ...); +} diff --git a/planet/Objects.ocd/Libraries.ocd/Animal.ocd/StringTblDE.txt b/planet/Objects.ocd/Libraries.ocd/Animal.ocd/StringTblDE.txt new file mode 100644 index 000000000..4ab65a3b1 --- /dev/null +++ b/planet/Objects.ocd/Libraries.ocd/Animal.ocd/StringTblDE.txt @@ -0,0 +1,6 @@ +ReproductionAreaSize=Reproduktionsgebiet +ReproductionAreaSizeHelp=Groesse des quadratischen Gebietes in Pixeln in dem Tiere der gleichen Art gezaehlt werden, um eine Reproduktion zu verhindern. +ReproductionRate=Reproduktionsrate +ReproductionRateHelp=Reproduktionsrate von 0 bis 10000. 0 ist keine Reproduktion; 10000 ist maximale Rate (etwa alle zwei Sekunden). +MaxCount=Maximale Anzahl +MaxCountHelp=Maximale Anzahl an Tieren im Reproduktionsgebiet. Wenn diese Zahl ueberschritten wird, vermehren sich die Tiere nichtmehr. diff --git a/planet/Objects.ocd/Libraries.ocd/Animal.ocd/StringTblUS.txt b/planet/Objects.ocd/Libraries.ocd/Animal.ocd/StringTblUS.txt new file mode 100644 index 000000000..b6171422a --- /dev/null +++ b/planet/Objects.ocd/Libraries.ocd/Animal.ocd/StringTblUS.txt @@ -0,0 +1,6 @@ +ReproductionAreaSize=Reproduction area +ReproductionAreaSizeHelp=Size of the square area in pixels in which animals of the same kind are counted in order to prevent over-population. +ReproductionRate=Reproduction rate +ReproductionRateHelp=Reproduction rate from 0 to 10,000. 0 is no reproduktion; 10000 ist maximum rate (approximately every two seconds). +MaxCount=Maximum count +MaxCountHelp=Maximale number of animals in reproduction area. If this number is exceeded, animals stop reproducing.