- Joined
- Mar 10, 2009
- Messages
- 5,016
Here's my progress: FINAL
To generate metal, you have to research and purchase it at the Castle...
I still have problems on how to display those metals coz leaderboard and multiboard wont do any good,
somehow I've been thinking to do it via texttag...
JASS:
/*
===Metal Resource v1.0
===Created by Mckill2009
Uses metal as additional requirement to train units and construct structures.
HOW TO USE:
- Copy ALL the Required Libraries and this code to your map.
- Copy the "Buy Metal" and "Metal Generation" to your map, but be sure to input the correct RAW CODES.
- You can see the raw code by pressing CTRL+D in the object editor.
- Register your unitID type that requires metal as seen in the "MetalResource DEMO".
API:
static method regTrained takes integer unitID, integer cost returns nothing
static method regBuilding takes integer unitID, integer cost returns nothing
CREDITS:
- Table by Bribe
- RegisterPlayerUnitEvent by Magtheridon96
- SimError by Vexorian
- SpellEffectEvent by Bribe
- CRAZYRUSSIAN for his icons
*/
library MetalResource uses RegisterPlayerUnitEvent, SpellEffectEvent, Table, SimError, BoardL
globals
private constant integer METAL_PURCHASE_ID = 'A001' //Raw Code, abilityID to purchase metal
private constant integer METAL_TECH_ID = 'R000' //Raw Code, research
private constant integer METAL_STORED = 100 //configurable
private constant real METAL_GENERATION = 3.0 //recommended
private integer array MetalRes
private integer array GoldStored
endglobals
native GetUnitGoldCost takes integer unitid returns integer
native GetUnitWoodCost takes integer unitid returns integer
struct MetalResource extends array
private static timer tm = CreateTimer()
private static integer buildingCounter = 0
private static integer trainedCounter = 0
private static TableArray bldg
private static TableArray unt
private static method metalGeneration takes nothing returns nothing
local integer i = 0
local integer gold
local integer tech
loop
set tech = GetPlayerTechCount(Player(i),METAL_TECH_ID,true)
if GoldStored[i] > 0 then
set GoldStored[i] = GoldStored[i]-1
set MetalRes[i] = MetalRes[i]+tech
call BoardL.boardSetValue(i,0,MetalRes[i])
endif
set i = i + 1
exitwhen i==12
endloop
endmethod
private static method cancelEvent takes nothing returns nothing
call ForceUICancel()
call PauseTimer(GetExpiredTimer())
call DestroyTimer(GetExpiredTimer())
endmethod
private static method resourceReset takes player p, integer g, integer w returns nothing
call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD)+g)
call SetPlayerState(p,PLAYER_STATE_RESOURCE_LUMBER,GetPlayerState(p,PLAYER_STATE_RESOURCE_LUMBER)+w)
endmethod
private static method checkConstruct takes nothing returns nothing
local unit building = GetTriggerUnit()
local player pl = GetTriggerPlayer()
local integer unitID = GetUnitTypeId(building)
local integer pID = GetPlayerId(pl)
local integer gold = GetUnitGoldCost(unitID)
local integer wood = GetUnitWoodCost(unitID)
local integer costMetal
local integer i = 0
loop
if bldg[1][i]==unitID then
exitwhen true
endif
set i = i + 1
exitwhen i==buildingCounter
endloop
set costMetal = bldg[2][i]
if costMetal > MetalRes[pID] then
call SimError(pl,"You need at least "+I2S(costMetal)+" metal to build a "+"|cdfff1230"+GetUnitName(building)+"|r")
call thistype.resourceReset(pl,gold,wood)
call RemoveUnit(building)
else
set MetalRes[pID] = MetalRes[pID]-costMetal
call BoardL.boardSetValue(pID,0,MetalRes[pID])
endif
set building = null
set pl = null
endmethod
private static method checkTrained takes nothing returns nothing
local unit trainer = GetTriggerUnit()
local player pl = GetTriggerPlayer()
local integer orderID = GetIssuedOrderId()
local integer pID = GetPlayerId(pl)
local integer costMetal
local integer gold
local integer wood
local integer i = 0
if orderID > 0 then
loop
if orderID==unt[1][i] then
set gold = GetUnitGoldCost(orderID)
set wood = GetUnitWoodCost(orderID)
set costMetal = unt[2][i]
exitwhen true
endif
set i = i + 1
exitwhen i==trainedCounter
endloop
if costMetal > MetalRes[pID] then
call SimError(pl,"You need at least "+I2S(costMetal)+" metal to train this unit.")
call thistype.resourceReset(pl,gold,wood)
call TimerStart(CreateTimer(),0.05,false,function thistype.cancelEvent)
else
set MetalRes[pID] = MetalRes[pID]-costMetal
call BoardL.boardSetValue(pID,0,MetalRes[pID])
endif
endif
set trainer = null
endmethod
private static method buyMetal takes nothing returns nothing
local integer pID = GetPlayerId(GetTriggerPlayer())
local integer gold = GetPlayerState(Player(pID),PLAYER_STATE_RESOURCE_GOLD)
if gold > METAL_STORED then
set GoldStored[pID] = GoldStored[pID] + (METAL_STORED/2)
call SetPlayerState(Player(pID),PLAYER_STATE_RESOURCE_GOLD,gold-METAL_STORED)
else
call IssueImmediateOrder(GetTriggerUnit(),"stop")
call SimError(GetTriggerPlayer(),"You need at least "+I2S(METAL_STORED)+" gold to purchase metals.")
endif
endmethod
private static method onInit takes nothing returns nothing
local integer i = 0
call RegisterSpellEffectEvent(METAL_PURCHASE_ID,function thistype.buyMetal)
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_CONSTRUCT_START,function thistype.checkConstruct)
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_ISSUED_ORDER,function thistype.checkTrained)
call TimerStart(tm,METAL_GENERATION,true,function thistype.metalGeneration)
set bldg = TableArray[0x3000]
set unt = TableArray[0x3000]
loop
set MetalRes[i] = 0
set i = i + 1
exitwhen i==12
endloop
endmethod
//API:
static method regTrained takes integer unitID, integer cost returns nothing
local integer i = -1
loop
if unt[1][i]==unitID then
debug call BJDebugMsg("regTrained ERROR: "+UnitId2String(unitID)+" can only be registered once!")
return
endif
set i = i + 1
exitwhen i==trainedCounter
endloop
set unt[1][trainedCounter] = unitID
set unt[2][trainedCounter] = cost
set trainedCounter = trainedCounter + 1
endmethod
static method regBuilding takes integer unitID, integer cost returns nothing
local integer i = -1
loop
if bldg[1][i]==unitID then
debug call BJDebugMsg("regBuilding ERROR: "+UnitId2String(unitID)+" can only be registered once!")
return
endif
set i = i + 1
exitwhen i==buildingCounter
endloop
set bldg[1][buildingCounter] = unitID
set bldg[2][buildingCounter] = cost
set bldg[3][unitID] = 0
set buildingCounter = buildingCounter + 1
endmethod
endstruct
endlibrary
-
MetalResource DEMO
-
Events
-
Time - Elapsed game time is 0.00 seconds
-
-
Conditions
-
Actions
-
-------- First you need to register a building or unit --------
-
-------- farm requires 20 metal --------
-
Custom script: call MetalResource.regBuilding('hhou',20)
-
-------- barracks requires 150 metal --------
-
Custom script: call MetalResource.regBuilding('hbar',150)
-
-------- arcane sanctum requires 120 metal --------
-
Custom script: call MetalResource.regBuilding('hars',120)
-
-------- peasant requires 10 metal --------
-
Custom script: call MetalResource.regTrained('hpea',10)
-
-------- footman requires 35 metal --------
-
Custom script: call MetalResource.regTrained('hfoo',35)
-
-------- priest requires 15 metal --------
-
Custom script: call MetalResource.regTrained('hmpr',50)
-
-
To generate metal, you have to research and purchase it at the Castle...
I still have problems on how to display those metals coz leaderboard and multiboard wont do any good,
somehow I've been thinking to do it via texttag...
Attachments
Last edited: