//TESH.scrollpos=0
//TESH.alwaysfold=0
Name | Type | is_array | initial_value |
u | unit | No | |
UDex | integer | No | |
UDexEnabled | boolean | No | |
UDexEvent | real | No | |
UDexGen | integer | No | |
UDexNext | integer | Yes | |
UDexPrev | integer | Yes | |
UDexRecycle | integer | No | |
UDexUnits | unit | Yes | |
UDexWasted | integer | No |
//TESH.scrollpos=112
//TESH.alwaysfold=0
________________________________________________________________________________________
Farm Land Generator 1.5.3
This spell Produces Farm Land around a finished building of your choice. The Yield produced
is customizable depending on the terrain under the Farm Land. The production value is stored
and the purpose is to use it in another trigger to have more flexible farm output - be it for
income, food or anything else really.
Features: Support multiple farm-lands and farm-tiles.
Customizeable color gradient, and display toggle.
Calculation of expected yield before production
Test-map containing income examples as well as customization of yield.
Link: http://www.hiveworkshop.com/forums/spells-569/farm-land-1-5-a-265277/
________________________________________________________________________________________
Appendix
1. Requirments
2. Implementation
3. API
4. Known Issues
5. Credits
6. Change Log
________________________________________________________________________________________
1. Requirments
1) UnitIndexer(*) http://www.hiveworkshop.com/forums/spells-569/gui-unit-indexer-1-2-0-2-a-197329/?prev=search%3DUNIT%2520INDEXER%26d%3Dlist%26r%3D20
2) PathingLibrary http://www.hiveworkshop.com/forums/spells-569/pathing-type-v1-2-a-263230/
3) Table http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
4) WorldBounds http://www.hiveworkshop.com/forums/graveyard-418/snippet-worldbounds-180494/
5) Native Declaration (See Trigger in the Required Category)
________________________________________________________________________________________
2. Implementation
a) Make sure your map contains all the required resources (See folder Required).
*Note that The used UnitIndexer is optional in the sense that you can use any
availible unit indexer based on unit-custom value.
b) Copy the trigger: FarmLandLib into your map.
c) Copy FarmMain or make a similar trigger using those functions.
d) Change the configuration to suit your needs.
e) Change FarmMain or what ever trigger you are using to match your need. When in doubt
take a look at the API and the provided exmples.
________________________________________________________________________________________
3. Application Programming Interface
struct FarmLand
1. static method create takes nothing returns FarmLand
2. method addGrid takes real x, real y returns nothing
3. method setTerrainYield takes integer terrain, real yield, integer farmTile returns nothing
4. method addFarmType takes integer unitTypeId returns nothing
5. method setupRadius takes nothing returns nothing
6. method addColor takes string colorcode returns nothing
7. method getTerrainYield takes real x, real y returns real
8. method isGridFarmable takes real x, real y returns boolean
Functions
1. function IsUnitFarm takes unit u returns boolean
2. function GetUnitFarmLand takes unit u returns FarmLand
3. function AddFarm takes FarmLand fl, unit u returns nothing
4. function RemoveFarm takes FarmLand fl, unit u returns nothing
5. function UpdateRadiusAtUnitLoc takes unit u returns nothing
6. function DisplayUnitYield takes FarmLand fl, unit u, real yield returns nothing
7. function CalcExpectedYieldXY takes FarmLand fl, real x, real y, integer unitTypeId returns real
8. function GetFarmGroup takes FarmLand fl returns group
9. function GetUnitYield takes unit u returns real
10. function GetLastChangedFarm takes nothing returns unit
________________________________________________________________________________________
4. Known Issues
a) Some times the update of farmland isnt 100% correct.
________________________________________________________________________________________
5. Credits
a) Dalvengyr for making Pathing Library
b) Bribe for making GUI-UnitIndexer and Table library used.
c) Dr Super Good, PurgeandFire and others for teaching me.
d) rulerofiron99 for giving me suggestions.
e) BPower for Native Declaration
f) edo494 and Moderators for giving feedback
g) SAUS for suggestions on how to solve the unit pathing check.
________________________________________________________________________________________
6. Change Log
v. 1.5
- Rewrote it.
- Uses WorldBounds
- Now supports multiple types of FarmLand (Testmap-contains 3 races)
- Support farm-tile variation, so you can have rough farm tile on low yield and lush farm tile on high yield.
- Now Uses Table http
- Yield Display can now exceed 100%
- New functionality: Can change unit yield without any events and also reset it to the state it should be in.
- Can toggle a display variable on and off.
- Added Example Triggers to show how to use the custom yield change.
- improved income example delta somewhat in function OnYieldChange
v. 1.5 .1
- fixed texttag bug
- fixed income bug
- fixed issue with the order of the function calls
- fixed documentation error
- removed customizeation of farm yield outside of event
- Grid and Color struct now uses Table instead of an array
v. 1.5 .2
- Units no longer block added farmland.
- Two new functions: AddFarm and RemoveFarm
- Made RefreshAreaAroundUnit privet.
- Minor bug-fixes
v. 1.5 .3
- Made the methods getTerrainYield and isGridFarmable 'public'
- Changed getTerrainYield paremeter to only take real x, y and made the previous integer i into a private global
________________________________________________________________________________________
//TESH.scrollpos=110
//TESH.alwaysfold=0
library PathingLib initializer onInit
// Configuration
globals
private constant integer PATH_CHECKER = 'h000'
private constant integer FLY_CHECKER = 'h001'
private constant integer WALK_CHECKER = 'h002'
private constant integer BUILD_CHECKER = 'h003'
private constant player DUMMY_PLAYER = Player(15)
endglobals
/*
Pathing Library v1.4
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
by: Quilnez aka Dalvengyr
Description
¯¯¯¯¯¯¯¯¯¯¯
This library allows you to detect all pathability types:
walkablility, flyability, and buildability. As well as allowing
you to detect the pathing color of given coordinates.
Warning!
Please keep informed that this system is more sensitive than
any other walkability checker systems out there, as it also
detects pathing map generated by normal units as well.
Here is the pathing color specifications:
Color Buildable Walkable Flyable Red Green Blue
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
White x x x 255 255 255
Magenta x x o 255 0 255
Cyan x o x 0 255 255
Blue x o o 0 0 255
Yellow o x x 255 255 0
Red o x o 255 0 0
Green o o x 0 255 0
Black o o o 0 0 0
(You can check your pathing map by pressing 'p' at terrain editor.)
API
¯¯¯
1. Used for checking certain point's flyability
| function IsTerrainFlyable takes real x, real y returns boolean
2. Used for checking certain point's walkability.
| function IsTerrainWalkable takes real x, real y returns boolean
3. Used for checking certain point's buildability.
| function IsTerrainBuildable takes real x, real y returns boolean
4. (Bonus) Used for checking certain point's pathability color.
| function GetTerrainPathingColor takes real x, real y returns integer
Returned values (pathing colors):
| PATHING_COLOR_WHITE
| PATHING_COLOR_MAGENTA
| PATHING_COLOR_CYAN
| PATHING_COLOR_BLUE
| PATHING_COLOR_YELLOW
| PATHING_COLOR_RED
| PATHING_COLOR_GREEN
| PATHING_COLOR_BLACK
How to import
¯¯¯¯¯¯¯¯¯¯¯¯¯
- Copy Fly, Walk, Build, and Path Checker (units) at object editor.
- Make sure Path Checker is able to build Fly, Walk, and Build Checker
(at object editor>unit>Path Checker>"Techtree - Structures built")
- Configure this system (available above).
Credits
¯¯¯¯¯¯¯
- PurgeandFire for pathability checking method.
- TheHelper.net for complete pathing types tutorial.
Link
¯¯¯¯
hiveworkshop.com/forums/spells-569/pathing-type-v1-2-a-263230/
*/
globals
private unit PathChecker
constant integer PATHING_COLOR_WHITE = 0
constant integer PATHING_COLOR_MAGENTA = 1
constant integer PATHING_COLOR_CYAN = 2
constant integer PATHING_COLOR_BLUE = 3
constant integer PATHING_COLOR_YELLOW = 4
constant integer PATHING_COLOR_RED = 5
constant integer PATHING_COLOR_GREEN = 6
constant integer PATHING_COLOR_BLACK = 7
endglobals
function IsTerrainFlyable takes real x, real y returns boolean
return IssueBuildOrderById(PathChecker, FLY_CHECKER, x, y)
endfunction
function IsTerrainWalkable takes real x, real y returns boolean
return IssueBuildOrderById(PathChecker, WALK_CHECKER, x, y)
endfunction
function IsTerrainBuildable takes real x, real y returns boolean
return IssueBuildOrderById(PathChecker, BUILD_CHECKER, x, y)
endfunction
function GetTerrainPathingColor takes real x, real y returns integer
local integer color = 0
if IsTerrainFlyable(x, y) then
set color = color + 1
endif
if IsTerrainWalkable(x, y) then
set color = color + 2
endif
if IsTerrainBuildable(x, y) then
set color = color + 4
endif
return color
endfunction
private function onInit takes nothing returns nothing
set PathChecker = CreateUnit(DUMMY_PLAYER, PATH_CHECKER, 0, 0, 0)
call UnitRemoveAbility(PathChecker, 'Amov')
call ShowUnit(PathChecker, false)
if GetLocalPlayer() == DUMMY_PLAYER then
call FogEnable(false)
endif
endfunction
endlibrary
//TESH.scrollpos=78
//TESH.alwaysfold=0
library Table /* made by Bribe, special thanks to Vexorian & Nestharus, version 3.1.1.0
One map, one hashtable. Welcome to NewTable 3.1
This library was originally called NewTable so it didn't conflict with
the API of Table by Vexorian. However, the damage is done and it's too
late to change the library name now. To help with damage control, I
have provided an extension library called TableBC, which bridges all
the functionality of Vexorian's Table except for 2-D string arrays &
the ".flush(integer)" method. I use ".flush()" to flush a child hash-
table, because I wanted the API in NewTable to reflect the API of real
hashtables (I thought this would be more intuitive).
API
------------
struct Table
| static method create takes nothing returns Table
| create a new Table
|
| method destroy takes nothing returns nothing
| destroy it
|
| method flush takes nothing returns nothing
| flush all stored values inside of it
|
| method remove takes integer key returns nothing
| remove the value at index "key"
|
| method operator []= takes integer key, $TYPE$ value returns nothing
| assign "value" to index "key"
|
| method operator [] takes integer key returns $TYPE$
| load the value at index "key"
|
| method has takes integer key returns boolean
| whether or not the key was assigned
|
----------------
struct TableArray
| static method operator [] takes integer array_size returns TableArray
| create a new array of Tables of size "array_size"
|
| method destroy takes nothing returns nothing
| destroy it
|
| method flush takes nothing returns nothing
| flush and destroy it
|
| method operator size takes nothing returns integer
| returns the size of the TableArray
|
| method operator [] takes integer key returns Table
| returns a Table accessible exclusively to index "key"
*/
globals
private integer less = 0 //Index generation for TableArrays (below 0).
private integer more = 8190 //Index generation for Tables.
//Configure it if you use more than 8190 "key" variables in your map (this will never happen though).
private hashtable ht = InitHashtable()
private key sizeK
private key listK
endglobals
private struct dex extends array
static method operator size takes nothing returns Table
return sizeK
endmethod
static method operator list takes nothing returns Table
return listK
endmethod
endstruct
private struct handles extends array
method has takes integer key returns boolean
return HaveSavedHandle(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSavedHandle(ht, this, key)
endmethod
endstruct
private struct agents extends array
method operator []= takes integer key, agent value returns nothing
call SaveAgentHandle(ht, this, key, value)
endmethod
endstruct
//! textmacro NEW_ARRAY_BASIC takes SUPER, FUNC, TYPE
private struct $TYPE$s extends array
method operator [] takes integer key returns $TYPE$
return Load$FUNC$(ht, this, key)
endmethod
method operator []= takes integer key, $TYPE$ value returns nothing
call Save$FUNC$(ht, this, key, value)
endmethod
method has takes integer key returns boolean
return HaveSaved$SUPER$(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSaved$SUPER$(ht, this, key)
endmethod
endstruct
private module $TYPE$m
method operator $TYPE$ takes nothing returns $TYPE$s
return this
endmethod
endmodule
//! endtextmacro
//! textmacro NEW_ARRAY takes FUNC, TYPE
private struct $TYPE$s extends array
method operator [] takes integer key returns $TYPE$
return Load$FUNC$Handle(ht, this, key)
endmethod
method operator []= takes integer key, $TYPE$ value returns nothing
call Save$FUNC$Handle(ht, this, key, value)
endmethod
method has takes integer key returns boolean
return HaveSavedHandle(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSavedHandle(ht, this, key)
endmethod
endstruct
private module $TYPE$m
method operator $TYPE$ takes nothing returns $TYPE$s
return this
endmethod
endmodule
//! endtextmacro
//Run these textmacros to include the entire hashtable API as wrappers.
//Don't be intimidated by the number of macros - Vexorian's map optimizer is
//supposed to kill functions which inline (all of these functions inline).
//! runtextmacro NEW_ARRAY_BASIC("Real", "Real", "real")
//! runtextmacro NEW_ARRAY_BASIC("Boolean", "Boolean", "boolean")
//! runtextmacro NEW_ARRAY_BASIC("String", "Str", "string")
//New textmacro to allow table.integer[] syntax for compatibility with textmacros that might desire it.
//! runtextmacro NEW_ARRAY_BASIC("Integer", "Integer", "integer")
//! runtextmacro NEW_ARRAY("Player", "player")
//! runtextmacro NEW_ARRAY("Widget", "widget")
//! runtextmacro NEW_ARRAY("Destructable", "destructable")
//! runtextmacro NEW_ARRAY("Item", "item")
//! runtextmacro NEW_ARRAY("Unit", "unit")
//! runtextmacro NEW_ARRAY("Ability", "ability")
//! runtextmacro NEW_ARRAY("Timer", "timer")
//! runtextmacro NEW_ARRAY("Trigger", "trigger")
//! runtextmacro NEW_ARRAY("TriggerCondition", "triggercondition")
//! runtextmacro NEW_ARRAY("TriggerAction", "triggeraction")
//! runtextmacro NEW_ARRAY("TriggerEvent", "event")
//! runtextmacro NEW_ARRAY("Force", "force")
//! runtextmacro NEW_ARRAY("Group", "group")
//! runtextmacro NEW_ARRAY("Location", "location")
//! runtextmacro NEW_ARRAY("Rect", "rect")
//! runtextmacro NEW_ARRAY("BooleanExpr", "boolexpr")
//! runtextmacro NEW_ARRAY("Sound", "sound")
//! runtextmacro NEW_ARRAY("Effect", "effect")
//! runtextmacro NEW_ARRAY("UnitPool", "unitpool")
//! runtextmacro NEW_ARRAY("ItemPool", "itempool")
//! runtextmacro NEW_ARRAY("Quest", "quest")
//! runtextmacro NEW_ARRAY("QuestItem", "questitem")
//! runtextmacro NEW_ARRAY("DefeatCondition", "defeatcondition")
//! runtextmacro NEW_ARRAY("TimerDialog", "timerdialog")
//! runtextmacro NEW_ARRAY("Leaderboard", "leaderboard")
//! runtextmacro NEW_ARRAY("Multiboard", "multiboard")
//! runtextmacro NEW_ARRAY("MultiboardItem", "multiboarditem")
//! runtextmacro NEW_ARRAY("Trackable", "trackable")
//! runtextmacro NEW_ARRAY("Dialog", "dialog")
//! runtextmacro NEW_ARRAY("Button", "button")
//! runtextmacro NEW_ARRAY("TextTag", "texttag")
//! runtextmacro NEW_ARRAY("Lightning", "lightning")
//! runtextmacro NEW_ARRAY("Image", "image")
//! runtextmacro NEW_ARRAY("Ubersplat", "ubersplat")
//! runtextmacro NEW_ARRAY("Region", "region")
//! runtextmacro NEW_ARRAY("FogState", "fogstate")
//! runtextmacro NEW_ARRAY("FogModifier", "fogmodifier")
//! runtextmacro NEW_ARRAY("Hashtable", "hashtable")
struct Table extends array
// Implement modules for intuitive syntax (tb.handle; tb.unit; etc.)
implement realm
implement integerm
implement booleanm
implement stringm
implement playerm
implement widgetm
implement destructablem
implement itemm
implement unitm
implement abilitym
implement timerm
implement triggerm
implement triggerconditionm
implement triggeractionm
implement eventm
implement forcem
implement groupm
implement locationm
implement rectm
implement boolexprm
implement soundm
implement effectm
implement unitpoolm
implement itempoolm
implement questm
implement questitemm
implement defeatconditionm
implement timerdialogm
implement leaderboardm
implement multiboardm
implement multiboarditemm
implement trackablem
implement dialogm
implement buttonm
implement texttagm
implement lightningm
implement imagem
implement ubersplatm
implement regionm
implement fogstatem
implement fogmodifierm
implement hashtablem
method operator handle takes nothing returns handles
return this
endmethod
method operator agent takes nothing returns agents
return this
endmethod
//set this = tb[GetSpellAbilityId()]
method operator [] takes integer key returns Table
return LoadInteger(ht, this, key) //return this.integer[key]
endmethod
//set tb[389034] = 8192
method operator []= takes integer key, Table tb returns nothing
call SaveInteger(ht, this, key, tb) //set this.integer[key] = tb
endmethod
//set b = tb.has(2493223)
method has takes integer key returns boolean
return HaveSavedInteger(ht, this, key) //return this.integer.has(key)
endmethod
//call tb.remove(294080)
method remove takes integer key returns nothing
call RemoveSavedInteger(ht, this, key) //call this.integer.remove(key)
endmethod
//Remove all data from a Table instance
method flush takes nothing returns nothing
call FlushChildHashtable(ht, this)
endmethod
//local Table tb = Table.create()
static method create takes nothing returns Table
local Table this = dex.list[0]
if this == 0 then
set this = more + 1
set more = this
else
set dex.list[0] = dex.list[this]
call dex.list.remove(this) //Clear hashed memory
endif
debug set dex.list[this] = -1
return this
endmethod
// Removes all data from a Table instance and recycles its index.
//
// call tb.destroy()
//
method destroy takes nothing returns nothing
debug if dex.list[this] != -1 then
debug call BJDebugMsg("Table Error: Tried to double-free instance: " + I2S(this))
debug return
debug endif
call this.flush()
set dex.list[this] = dex.list[0]
set dex.list[0] = this
endmethod
//! runtextmacro optional TABLE_BC_METHODS()
endstruct
//! runtextmacro optional TABLE_BC_STRUCTS()
struct TableArray extends array
//Returns a new TableArray to do your bidding. Simply use:
//
// local TableArray ta = TableArray[array_size]
//
static method operator [] takes integer array_size returns TableArray
local Table tb = dex.size[array_size] //Get the unique recycle list for this array size
local TableArray this = tb[0] //The last-destroyed TableArray that had this array size
debug if array_size <= 0 then
debug call BJDebugMsg("TypeError: Invalid specified TableArray size: " + I2S(array_size))
debug return 0
debug endif
if this == 0 then
set this = less - array_size
set less = this
else
set tb[0] = tb[this] //Set the last destroyed to the last-last destroyed
call tb.remove(this) //Clear hashed memory
endif
set dex.size[this] = array_size //This remembers the array size
return this
endmethod
//Returns the size of the TableArray
method operator size takes nothing returns integer
return dex.size[this]
endmethod
//This magic method enables two-dimensional[array][syntax] for Tables,
//similar to the two-dimensional utility provided by hashtables them-
//selves.
//
//ta[integer a].unit[integer b] = unit u
//ta[integer a][integer c] = integer d
//
//Inline-friendly when not running in debug mode
//
method operator [] takes integer key returns Table
static if DEBUG_MODE then
local integer i = this.size
if i == 0 then
call BJDebugMsg("IndexError: Tried to get key from invalid TableArray instance: " + I2S(this))
return 0
elseif key < 0 or key >= i then
call BJDebugMsg("IndexError: Tried to get key [" + I2S(key) + "] from outside TableArray bounds: " + I2S(i))
return 0
endif
endif
return this + key
endmethod
//Destroys a TableArray without flushing it; I assume you call .flush()
//if you want it flushed too. This is a public method so that you don't
//have to loop through all TableArray indices to flush them if you don't
//need to (ie. if you were flushing all child-keys as you used them).
//
method destroy takes nothing returns nothing
local Table tb = dex.size[this.size]
debug if this.size == 0 then
debug call BJDebugMsg("TypeError: Tried to destroy an invalid TableArray: " + I2S(this))
debug return
debug endif
if tb == 0 then
//Create a Table to index recycled instances with their array size
set tb = Table.create()
set dex.size[this.size] = tb
endif
call dex.size.remove(this) //Clear the array size from hash memory
set tb[this] = tb[0]
set tb[0] = this
endmethod
private static Table tempTable
private static integer tempEnd
//Avoids hitting the op limit
private static method clean takes nothing returns nothing
local Table tb = .tempTable
local integer end = tb + 0x1000
if end < .tempEnd then
set .tempTable = end
call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
else
set end = .tempEnd
endif
loop
call tb.flush()
set tb = tb + 1
exitwhen tb == end
endloop
endmethod
//Flushes the TableArray and also destroys it. Doesn't get any more
//similar to the FlushParentHashtable native than this.
//
method flush takes nothing returns nothing
debug if this.size == 0 then
debug call BJDebugMsg("TypeError: Tried to flush an invalid TableArray instance: " + I2S(this))
debug return
debug endif
set .tempTable = this
set .tempEnd = this + this.size
call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
call this.destroy()
endmethod
endstruct
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
library WorldBounds
//struct WorldBounds extends array
//static readonly rect world
// same as GetWorldBounds()
//static readonly region worldRegion
// contains world for triggers
//static readonly real maxX
//static readonly real maxY
//static readonly real minX
//static readonly real minY
//static readonly real centerX
//static readonly real centerY
struct WorldBounds extends array
readonly static real maxX = 0.
readonly static real maxY = 0.
readonly static real minX = 0.
readonly static real minY = 0.
readonly static real centerX = 0.
readonly static real centerY = 0.
readonly static rect world = null
readonly static region worldRegion = null
private static method onInit takes nothing returns nothing
set world = GetWorldBounds()
set maxX = GetRectMaxX(world)
set maxY = GetRectMaxY(world)
set minX = GetRectMinX(world)
set maxY = GetRectMaxY(world)
set centerX = (maxX+minX)/2
set centerY = (minY+maxY)/2
set worldRegion = CreateRegion()
call RegionAddRect(worldRegion, world)
endmethod
endstruct
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
// Native Declaration
// This line of code must exist somewhere in your map
native UnitAlive takes unit u returns boolean
//TESH.scrollpos=237
//TESH.alwaysfold=0
library FarmLand initializer Init uses PathingLib, WorldBounds, Table
globals
private hashtable udg_hash = InitHashtable()
private group search = CreateGroup()
private group g1 = CreateGroup()
private group g2 = CreateGroup()
private real array sum
private real array yield
private integer i
boolean fland_show = true
real fland_event
unit fland_unit
real fland_yield_old
real fland_yield_new
endglobals
private struct Color
Table s
integer count = 0
real max = 0.
static method create takes nothing returns Color
local thistype this = .allocate()
set this.s = Table.create()
return this
endmethod
method getColor takes real yield returns string
set i = 0
if not (.count == 0) then
if yield <= .max then
loop
exitwhen i == .count - 1
if yield <= (.max / R2I(.count - 2))*R2I(i) then
return .s.string[i]
endif
set i = i +1
endloop
endif
return .s.string[.count - 1]
endif
return ""
endmethod
endstruct
private struct Grid
Table x
Table y
integer n = 0
static method create takes nothing returns Grid
local thistype this = .allocate()
set this.x = Table.create()
set this.y = Table.create()
return this
endmethod
endstruct
function IsUnitFarm takes unit u returns boolean
return LoadBoolean(udg_hash, GetUnitTypeId(u), 0)
endfunction
function GetUnitFarmLand takes unit u returns FarmLand
return LoadInteger(udg_hash, GetUnitTypeId(u), 1)
endfunction
private function IsUnitGridOwner takes unit u, real x, real y returns boolean
return u == LoadUnitHandle(udg_hash, R2I(x), R2I(y))
endfunction
private function ClearFarmLand takes real x, real y returns nothing
call SetTerrainType(x,y, LoadInteger(udg_hash, R2I(x), R2I(y)), -1, 1, 1)
call RemoveSavedInteger(udg_hash, R2I(x),R2I(y))
call RemoveSavedHandle(udg_hash, R2I(x), R2I(y))
endfunction
private function SpawnFarmLand takes FarmLand fl, unit u, real x, real y returns nothing
local integer terrain = fl.t2[GetTerrainType(x, y)]
call SaveInteger(udg_hash, R2I(x), R2I(y), GetTerrainType(x,y))
call SaveUnitHandle(udg_hash, R2I(x), R2I(y), u)
call SetTerrainType(x, y, terrain, -1, 1, 1)
endfunction
struct FarmLand
Grid grid
Color color
Table t1
Table t2
group g = CreateGroup()
static real radius = 0.
static method create takes nothing returns FarmLand
local thistype this = allocate()
set this.color = Color.create()
set this.grid = Grid.create()
set this.t1 = Table.create()
set this.t2 = Table.create()
return this
endmethod
method addGrid takes real x, real y returns nothing
set .grid.x.real[.grid.n] = x
set .grid.y.real[.grid.n] = y
set .grid.n = .grid.n + 1
endmethod
method setTerrainYield takes integer terrain, real yield, integer farmTile returns nothing
set .t1.real[terrain] = yield
set .t2[terrain] = farmTile
if yield > color.max then
set color.max = yield
endif
endmethod
method getTerrainYield takes real x, real y returns real
return .t1.real[GetTerrainType(x, y)]
endmethod
method addFarmType takes integer unitTypeId returns nothing
call SaveBoolean(udg_hash, unitTypeId, 0, true)
call SaveInteger(udg_hash, unitTypeId, 1, this)
endmethod
method setupRadius takes nothing returns nothing
local real d
set i = 0
loop
exitwhen i == .grid.n
set d = SquareRoot(.grid.x.real[i]*.grid.x.real[i] + .grid.y.real[i]*.grid.y.real[i])
if d > FarmLand.radius then
set FarmLand.radius = d + 246.
endif
set i = i + 1
endloop
endmethod
method addColor takes string colorcode returns nothing
set color.s.string[color.count] = colorcode
set color.count = color.count + 1
endmethod
method isGridFarmable takes real x, real y returns boolean
return (IsTerrainWalkable(x, y) or i == 0) and .getTerrainYield(x, y) > 0.
endmethod
endstruct
private function RefreshAreaAroundUnit takes FarmLand fl, unit u, unit enum returns nothing
local integer uid = GetUnitUserData(u)
local real change = 0.
local real array x
local real array y
set x[uid] = GetUnitX(u)
set y[uid] = GetUnitY(u)
call GroupEnumUnitsInRange(g1, x[uid], y[uid], FarmLand.radius, null)
loop
set enum = FirstOfGroup(g1)
exitwhen enum == null
call GroupRemoveUnit(g1, enum)
if not IsUnitType(enum, UNIT_TYPE_STRUCTURE) then
call GroupAddUnit(g2, enum)
set i = GetUnitUserData(enum)
set x[i] = GetUnitX(enum)
set y[i] = GetUnitY(enum)
call SetUnitX(enum, x[uid])
call SetUnitY(enum, y[uid])
endif
endloop
set i = 0
loop
exitwhen i == fl.grid.n
set x[0] = x[uid] + fl.grid.x.real[i]
set y[0] = y[uid] + fl.grid.y.real[i]
if IsUnitGridOwner(u, x[0], y[0]) and (not IsTerrainWalkable(x[0], y[0]) or not IsUnitInGroup(u, fl.g)) then
call ClearFarmLand(x[0], y[0])
set change = change - fl.getTerrainYield(x[0], y[0])
elseif IsUnitInGroup(u, fl.g) and fl.isGridFarmable(x[0], y[0]) then
set change = change + fl.getTerrainYield(x[0], y[0])
call SpawnFarmLand(fl, u, x[0], y[0])
endif
set i = i + 1
endloop
loop
set enum = FirstOfGroup(g2)
exitwhen enum == null
call GroupRemoveUnit(g2, enum)
set i = GetUnitUserData(enum)
call SetUnitX(enum, x[i])
call SetUnitY(enum, y[i])
endloop
if change != 0. then
set sum[uid] = sum[uid] + change
set fland_yield_old = yield[uid]
set yield[uid] = sum[uid]/fl.grid.n
set fland_yield_new = yield[uid]
set fland_unit = u
set fland_event = 0
set fland_event = 1
endif
endfunction
function UpdateRadiusAtUnitLoc takes unit u returns nothing
call GroupEnumUnitsInRange(search, GetUnitX(u), GetUnitY(u), FarmLand.radius, null)
loop
set u = FirstOfGroup(search)
exitwhen u == null
call GroupRemoveUnit(search, u)
if IsUnitFarm(u) and UnitAlive(u) then
call RefreshAreaAroundUnit(GetUnitFarmLand(u), u, null)
endif
endloop
endfunction
function AddFarm takes FarmLand fl, unit u returns nothing
call GroupAddUnit(fl.g, u)
call RefreshAreaAroundUnit(fl, u, null)
endfunction
function RemoveFarm takes FarmLand fl, unit u returns nothing
call GroupRemoveUnit(fl.g, u)
call RefreshAreaAroundUnit(fl, u, null)
endfunction
function DisplayUnitYield takes FarmLand fl, unit u, real yield returns nothing
local texttag tt = CreateTextTag()
if GetLocalPlayer() == GetOwningPlayer(u) then
call SetTextTagPermanent(tt, false)
call SetTextTagPosUnit(tt, u, 50)
call SetTextTagVelocity(tt, 30*0.071/128*Cos(90*bj_DEGTORAD), 30*0.071/128*Sin(90*bj_DEGTORAD))
call SetTextTagFadepoint(tt, 3.5)
call SetTextTagLifespan(tt, 4)
call SetTextTagText(tt, fl.color.getColor(yield) + I2S(R2I(yield*100)) + "%|r", 12*0.023/10)
endif
endfunction
function CalcExpectedYieldXY takes FarmLand fl, real x0, real y0, integer unitTypeId returns real
local real x
local real y
local real tempSum = 0
set i = 0
loop
exitwhen i == fl.grid.n
set x = x0 + fl.grid.x.real[i]
set y = y0 + fl.grid.y.real[i]
if fl.isGridFarmable(x, y) then
set tempSum = tempSum + fl.getTerrainYield(x,y)
endif
set i = i + 1
endloop
return tempSum/i
endfunction
function GetFarmGroup takes FarmLand fl returns group
return fl.g
endfunction
function GetLastChangedFarm takes nothing returns unit
return fland_unit
endfunction
function GetUnitYield takes unit u returns real
return yield[GetUnitUserData(u)]
endfunction
private function AddPreplacedFarms takes nothing returns nothing
local timer t = GetExpiredTimer()
local unit u
local FarmLand fl
call GroupEnumUnitsInRect(search, WorldBounds.world, null)
loop
set u = FirstOfGroup(search)
exitwhen u == null
call GroupRemoveUnit(search, u)
if IsUnitFarm(u) and UnitAlive(u) then
set fl = GetUnitFarmLand(u)
call GroupAddUnit(fl.g, u)
call RefreshAreaAroundUnit(fl, u, null)
endif
endloop
call DestroyTimer(t)
set t = null
endfunction
private function Init takes nothing returns nothing
local timer t = CreateTimer()
call TimerStart(t, 0.1, false, function AddPreplacedFarms)
set t = null
endfunction
endlibrary
//TESH.scrollpos=131
//TESH.alwaysfold=0
scope Test initializer Init
globals
FarmLand farmland_hu
FarmLand farmland_orc
FarmLand farmland_elf
endglobals
private function ConfigurationHuman takes nothing returns nothing
set farmland_hu = FarmLand.create()
call farmland_hu.addGrid(0,0)
call farmland_hu.addGrid(128,0)
call farmland_hu.addGrid(-128,0)
call farmland_hu.addGrid(0,-128)
call farmland_hu.addGrid(0,128)
call farmland_hu.addGrid(128,128)
call farmland_hu.addGrid(-128,128)
call farmland_hu.addGrid(128,-128)
call farmland_hu.addGrid(-128,-128)
call farmland_hu.addGrid(256,0)
call farmland_hu.addGrid(-256,0)
call farmland_hu.addGrid(0,256)
call farmland_hu.addGrid(0,-256)
call farmland_hu.setTerrainYield('Agrs', 1.0, 'Vcrp') // Base Tile, yield, Farm Tile Produced
call farmland_hu.setTerrainYield('Adrg', 0.75, 'Vcrp')
call farmland_hu.setTerrainYield('Adrd', 0.40, 'Vcrp')
call farmland_hu.addFarmType('hhou')
call farmland_hu.setupRadius()
call farmland_hu.addColor("|c00ff0000") // Lowest Yield Gradient
call farmland_hu.addColor("|c00bf3f00")
call farmland_hu.addColor("|c007f7f00")
call farmland_hu.addColor("|c003fbf00")
call farmland_hu.addColor("|c0000ff00") // Highest Yield Gradient
call farmland_hu.addColor("|c00540081") // This Color will display when above the maxium yield
endfunction
private function ConfigurationOrc takes nothing returns nothing
set farmland_orc = FarmLand.create()
call farmland_orc.addGrid(0,0)
call farmland_orc.addGrid(128,0)
call farmland_orc.addGrid(-128,0)
call farmland_orc.addGrid(0,-128)
call farmland_orc.addGrid(0,128)
call farmland_orc.addGrid(128,128)
call farmland_orc.addGrid(-128,128)
call farmland_orc.addGrid(128,-128)
call farmland_orc.addGrid(-128,-128)
call farmland_orc.setTerrainYield('Agrs', 1.00, 'Ofst')
call farmland_orc.setTerrainYield('Adrg', 0.75, 'Ofst')
call farmland_orc.setTerrainYield('Adrd', 0.40, 'Ofst')
call farmland_orc.addFarmType('otrb')
call farmland_orc.setupRadius()
call farmland_orc.addColor("|c00ff0000")
call farmland_orc.addColor("|c00bf3f00")
call farmland_orc.addColor("|c007f7f00")
call farmland_orc.addColor("|c003fbf00")
call farmland_orc.addColor("|c0000ff00")
call farmland_orc.addColor("|c00540081")
endfunction
private function ConfigurationElf takes nothing returns nothing
set farmland_elf = FarmLand.create()
call farmland_elf.addGrid(0,0)
call farmland_elf.addGrid(128,0)
call farmland_elf.addGrid(-128,0)
call farmland_elf.addGrid(0,-128)
call farmland_elf.addGrid(0,128)
call farmland_elf.addGrid(128,128)
call farmland_elf.addGrid(-128,128)
call farmland_elf.addGrid(128,-128)
call farmland_elf.addGrid(-128,-128)
call farmland_elf.setTerrainYield('Agrs', 1.20, 'Clvg')
call farmland_elf.setTerrainYield('Adrg', 1.00, 'Cgrs')
call farmland_elf.setTerrainYield('Adrd', 0.24, 'Cvin')
call farmland_elf.addFarmType('emow')
call farmland_elf.setupRadius()
endfunction
private function ConfigurationUndead takes nothing returns nothing
endfunction
// Note: If you only have one element of FarmLand, you can replace GetUnitFarmLand(u) with that variable.
private function OnFinishConstruction takes nothing returns boolean
local unit u = GetFilterUnit()
if IsUnitFarm(u) then
call AddFarm(GetUnitFarmLand(u), u)
endif
set u = null
return false
endfunction
private function OnStartConstruction takes nothing returns boolean
local unit u = GetFilterUnit()
if not IsUnitFarm(u) then
call UpdateRadiusAtUnitLoc(u)
endif
// Optional Example Usage - Detecting when a farm "should" be canceled.
if IsUnitFarm(u) then
if CalcExpectedYieldXY(GetUnitFarmLand(u), GetUnitX(u), GetUnitY(u), GetUnitTypeId(u)) <= 0.25 then
call BJDebugMsg("Yield is below 25%! - CANCEL IT!")
endif
endif
set u = null
return false
endfunction
private function OnBuildingDeath takes nothing returns boolean
local unit u = GetTriggerUnit()
if IsUnitType(u, UNIT_TYPE_STRUCTURE) then
if IsUnitFarm(u) then
call RemoveFarm(GetUnitFarmLand(u), u)
endif
call UpdateRadiusAtUnitLoc(u)
endif
set u = null
return false
endfunction
private function OnYieldChange takes nothing returns boolean
local integer pid = GetPlayerId(GetOwningPlayer(fland_unit))
local integer uid = GetUnitUserData(fland_unit)
// Example of how to change income variable
set incPlayerTotIncome[pid] = incPlayerTotIncome[pid] - R2I(fland_yield_old*MAX_FARM_INCOME) + R2I(fland_yield_new*MAX_FARM_INCOME)
set incFarmIncome[uid] = R2I(MAX_FARM_INCOME*fland_yield_new)
if UnitAlive(fland_unit) and fland_show then
call DisplayUnitYield(GetUnitFarmLand(fland_unit), fland_unit, GetUnitYield(fland_unit))
endif
return false
endfunction
private function Welcome takes nothing returns nothing
call BJDebugMsg("|cffffcc00Farmland Generator 1.5.3 - By Pinzu|r")
call BJDebugMsg("Build Farms to gain more income.You can destroy your farms by pressing 'esc'. \nHuman Farm Commands: 'display'")
endfunction
private function Init takes nothing returns nothing
local trigger t0 = CreateTrigger()
local trigger t1 = CreateTrigger()
local trigger t2 = CreateTrigger()
local integer i = 0
local player p
loop
set p = Player(i)
call TriggerRegisterPlayerUnitEvent(t1, p, EVENT_PLAYER_UNIT_CONSTRUCT_FINISH, function OnFinishConstruction)
call TriggerRegisterPlayerUnitEvent(t1, p, EVENT_PLAYER_UNIT_DEATH, null)
call TriggerRegisterPlayerUnitEvent(t1, p, EVENT_PLAYER_UNIT_CONSTRUCT_START, function OnStartConstruction)
set i = i + 1
exitwhen i == 12
endloop
call TriggerRegisterTimerEvent(t0, 0.1, false)
call TriggerRegisterVariableEvent(t2, "fland_event", EQUAL, 1)
call TriggerAddAction(t0, function Welcome)
call TriggerAddCondition(t1, Condition(function OnBuildingDeath))
call TriggerAddCondition(t2, Condition(function OnYieldChange))
call ConfigurationHuman()
call ConfigurationOrc()
call ConfigurationElf()
endfunction
endscope
//TESH.scrollpos=0
//TESH.alwaysfold=0
scope Display initializer Init
private function EnumDisplay takes nothing returns nothing
local unit u = GetEnumUnit()
call DisplayUnitYield(farmland_hu, u, GetUnitYield(u))
set u = null
endfunction
private function Display takes nothing returns nothing
set bj_wantDestroyGroup = true
call ForGroup(GetFarmGroup(farmland_hu), function EnumDisplay)
endfunction
private function Init takes nothing returns nothing
local trigger t3 = CreateTrigger()
call TriggerRegisterPlayerChatEvent(t3, Player(0), "display", true )
call TriggerAddAction(t3, function Display)
endfunction
endscope
//TESH.scrollpos=2
//TESH.alwaysfold=0
scope Income initializer Init
globals
constant real INCOME_INTERVAL = 10
constant real MAX_FARM_INCOME = 100
integer array incFarmIncome
integer array incPlayerTotIncome
endglobals
private function Income takes nothing returns nothing
local integer i = 0
local player p
loop
set p = Player(i)
if incPlayerTotIncome[i] > 0 then
call BJDebugMsg(GetPlayerName(p) + " was granted " + I2S(incPlayerTotIncome[i]) + " income.")
call SetPlayerState(p, PLAYER_STATE_RESOURCE_GOLD, GetPlayerState(p, PLAYER_STATE_RESOURCE_GOLD) + incPlayerTotIncome[i])
call SetPlayerState(p, PLAYER_STATE_RESOURCE_LUMBER, GetPlayerState(p, PLAYER_STATE_RESOURCE_LUMBER) + incPlayerTotIncome[i])
endif
set i = i + 1
exitwhen i == 12
endloop
endfunction
private function Init takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterTimerEvent(t, INCOME_INTERVAL, true)
call TriggerAddAction(t, function Income)
endfunction
endscope