• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] System Problem(Part 2)

Status
Not open for further replies.
Level 8
Joined
Jul 28, 2008
Messages
211
So i tried fixing my system but it isn't going well xD

JASS:
//Made by Destroyer95
//
//
//              
//               
//                
//               
//              


library Resource

globals

    integer array BUILDING
    integer array RESOURCE1_COST
    integer array RESOURCE2_COST
    integer array RESOURCE3_COST
    integer array RESOURCE_ITEMS
    
endglobals

function SetBuildingCost takes integer buildingNumber, integer buildingRawcode, integer res1Cost, integer res2Cost, integer res3Cost returns nothing
    set BUILDING[buildingNumber] = buildingRawcode
    set RESOURCE1_COST[buildingNumber] = res1Cost
    set RESOURCE2_COST[buildingNumber] = res2Cost
    set RESOURCE3_COST[buildingNumber] = res3Cost
endfunction

function SetupItemRawcodes takes integer item1Rawcode, integer item2Rawcode, integer item3Rawcode returns nothing
    set RESOURCE_ITEMS[1] = item1Rawcode
    set RESOURCE_ITEMS[2] = item2Rawcode
    set RESOURCE_ITEMS[3] = item3Rawcode
endfunction

function UnitStartsBuilding takes nothing returns nothing
    local unit structure = GetConstructingStructure()
    local unit builder = GetOrderedUnit() 
    local boolean array bo
    local integer id = UnitId( OrderId2StringBJ( GetIssuedOrderId() ) ) 
    local integer count = 0
    local integer index = 0
    local item array indexItem

    if id != null then
        loop 
            exitwhen id == BUILDING[count] or count == 8191
            if id != BUILDING[count] then
                set count = count + 1
            endif
        endloop
    endif
    if RESOURCE1_COST[count] != 0 then
        loop
            set indexItem[1] = UnitItemInSlot(builder, index)
            if (indexItem[1] != null) and (GetItemTypeId(indexItem[1]) == RESOURCE_ITEMS[1]) then
                if GetItemCharges(indexItem[1]) >= RESOURCE1_COST[count] then
                    set bo[1] = true
                endif
            endif

            set index = index + 1
            exitwhen index >= bj_MAX_INVENTORY or bo[1] == true
        endloop
        else
            set bo[1] = true
    endif
        
        
    if RESOURCE2_COST[count] != 0 then
        loop
            set indexItem[2] = UnitItemInSlot(builder, index)
            if (indexItem[2] != null) and (GetItemTypeId(indexItem[2]) == RESOURCE_ITEMS[2]) then
                if GetItemCharges(indexItem[2]) >= RESOURCE2_COST[count] then
                    set bo[2] = true
                endif
            endif

            set index = index + 1
            exitwhen index >= bj_MAX_INVENTORY or bo[2] == true
       endloop
       else
           set bo[2] = true
    endif
    
        
    if RESOURCE3_COST[count] != 0 then
        loop
            set indexItem[3] = UnitItemInSlot(builder, index)
            if (indexItem[3] != null) and (GetItemTypeId(indexItem[3]) == RESOURCE_ITEMS[3]) then
                if GetItemCharges(indexItem[3]) >= RESOURCE3_COST[count] then
                    set bo[3] = true
                endif
            endif

            set index = index + 1
            exitwhen index >= bj_MAX_INVENTORY or bo[3] == true
        endloop
        else
            set bo[3] = true
    endif
        
    if bo[1] == true and bo[2] == true and bo[3] == true then
        call SetItemCharges(indexItem[1], GetItemCharges(indexItem[1]) - RESOURCE1_COST[count])
        call SetItemCharges(indexItem[2], GetItemCharges(indexItem[2]) - RESOURCE2_COST[count])
        call SetItemCharges(indexItem[3], GetItemCharges(indexItem[3]) - RESOURCE3_COST[count])
        else
            call KillUnit(structure)
    endif
        
    set structure = null
    set builder = null
    set indexItem[1] = null
    set indexItem[2] = null
    set indexItem[3] = null
    
endfunction
    
function Trig_ResourceSystem_Conditions takes nothing returns boolean
    local integer i = 0
    loop
        exitwhen i == 8190 or GetUnitTypeId(GetConstructingStructure()) == BUILDING[i]
        set i = i + 1
    endloop
    return GetUnitTypeId(GetConstructingStructure()) == BUILDING[i]
endfunction
    
function InitTrig_Resource_System takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerAddCondition(t, Condition( function Trig_ResourceSystem_Conditions ) )
    call TriggerAddAction(t, function UnitStartsBuilding)
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_CONSTRUCT_START )
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER )
endfunction

endlibrary

i tried putting
JASS:
local unit structure = GetTriggerUnit()
but then it would KILL the builder even if he had those resources. But when i set it to GetConstructingStructure(or whatever), it doesn't kill the building even if the builder doesn't have the resources. What's the problem here?
 
Level 10
Joined
May 19, 2008
Messages
176
Try to make two Triggers, BECAUSE: The Order Event returns the Builder and the Build Event the structure. So you should make to triggers. The first is the one with the order because it triggers before the Build Event. There you need to set a variable ( the Builder ).
BUT there is a problem, they can overwrithe so its a bit more complicate. My idea is: Take the Issued Order and change it to a string when the building is a custom then the string looks like this: custom_IDOFTHEBUILDING. Yet you can take the id with substring and convert it to an integer. Yet you have the ID of the Building. Now safe it in some way. ( May Hashtable ) Then in the second trigger look through the variable for the ID. When you found this you now the the builder. WHEN you want to be really sure check the distance between Builder and Building, should it be more then 250 or something the unit can't be the builder. So the trigger should search through the other places in the array...

cedi
 
Status
Not open for further replies.
Top