System Material system

Lobster

Old Fogey ofthe site
Reaction score
90
ixh2te.jpg

(I need a better screenshot, if its really necessary, ill make a different one)
A material selling system based off final fantasy 12s. Sell X item A's, and Y item B's, and a reward item becomes availible at a shop. The counter then resets.

Its simple to add items, but impossible to remove items, unfortunately.
To add items call:
JASS:
function AddMaterial takes integer matA, integer matB, integer rwrd , integer NeededA, integer NeededB, unit shop returns nothing


Made in Vjass



JASS:
library materials initializer onInit





library materials initializer onInit

    globals
        private integer totalMaterials = 0
        private materials array matlist
        private integer array solditems
    endglobals
    
    function AddMaterial takes integer matA, integer matB, integer rwrd , integer NeededA, integer NeededB, unit shop returns nothing
        set matlist[totalMaterials] = materials.create()
        set matlist[totalMaterials].shopunit = shop

        set matlist[totalMaterials].materialA = matA
        set matlist[totalMaterials].materialB = matB

        set matlist[totalMaterials].reward = rwrd

        set matlist[totalMaterials].NeededA = NeededA
        set matlist[totalMaterials].NeededB = NeededB

        set matlist[totalMaterials].totalA = 0
        set matlist[totalMaterials].totalB = 0
        
        set totalMaterials = totalMaterials + 1
    endfunction

    struct materials
        integer materialA = 0
        integer materialB = 0
        integer reward = 0
        integer totalA = 0
        integer totalB = 0 
        integer NeededA = 0
        integer NeededB = 0
        
        unit shopunit = null
    endstruct

    private function onSell takes nothing returns boolean
        local integer i = 0
        local integer imax = totalMaterials
        local unit u = GetTriggerUnit()
        local player p = GetOwningPlayer( u )
        
        call DisplayTextToPlayer( p, 0., 0., "Item sold !" )
        
        loop
            exitwhen i == imax
            
            if GetItemTypeId( GetSoldItem() ) == matlist<i>.materialA then
                set matlist<i>.totalA = matlist<i>.totalA + 1
            endif
            
            if GetItemTypeId( GetSoldItem() ) == matlist<i>.materialB then
                set matlist<i>.totalB = matlist<i>.totalB + 1
            endif
            
            if matlist<i>.totalA &gt;= matlist<i>.NeededA and matlist<i>.totalB &gt;= matlist<i>.NeededB then
                call AddItemToStock( matlist<i>.shopunit ,matlist<i>.reward, 1, 1 )
                
                set matlist<i>.totalA = 0
                set matlist<i>.totalB = 0
            endif
            
            set i = i+1
        endloop
        
        set p = null
        set u = null

        return false
    endfunction



    private function onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        
        call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_PAWN_ITEM)
        call TriggerAddCondition( t, Condition( function onSell ) )
    endfunction
    
endlibrary</i></i></i></i></i></i></i></i></i></i></i></i></i></i>

Link:
http://www.epicwar.com/maps/127999/

Now uses komaqtion's code
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Ok, I'm sorry to say this but you really need to clean up that code a bit !
Maybe not the actual functionality (Haven't checked yet :p) but the intendation, and lineup...

Why use a global trigger ?!

JASS:
library materials initializer onInit

    globals
        private integer totalMaterials = 0
        private materials array matlist
        private integer array solditems
    endglobals
    
    function AddMaterial takes integer matA, integer matB, integer rwrd , integer NeededA, integer NeededB, unit shop returns nothing
        set matlist[totalMaterials] = materials.create()
        set matlist[totalMaterials].shopunit = shop

        set matlist[totalMaterials].materialA = matA
        set matlist[totalMaterials].materialB = matB

        set matlist[totalMaterials].reward = rwrd

        set matlist[totalMaterials].NeededA = NeededA
        set matlist[totalMaterials].NeededB = NeededB

        set matlist[totalMaterials].totalA = 0
        set matlist[totalMaterials].totalB = 0
        
        set totalMaterials = totalMaterials + 1
    endfunction

    struct materials
        integer materialA = 0
        integer materialB = 0
        integer reward = 0
        integer totalA = 0
        integer totalB = 0 
        integer NeededA = 0
        integer NeededB = 0
        
        unit shopunit = null
    endstruct

    private function onSell takes nothing returns boolean
        local integer i = 0
        local integer imax = totalMaterials
        local unit u = GetTriggerUnit()
        local player p = GetOwningPlayer( u )
        
        call DisplayTextToPlayer( p, 0., 0., &quot;Item sold !&quot; )
        
        loop
            exitwhen i == imax
            
            if GetItemTypeId( GetSoldItem() ) == matlist<i>.materialA then
                set matlist<i>.totalA = matlist<i>.totalA + 1
            endif
            
            if GetItemTypeId( GetSoldItem() ) == matlist<i>.materialB then
                set matlist<i>.totalB = matlist<i>.totalB + 1
            endif
            
            if matlist<i>.totalA &gt;= matlist<i>.NeededA and matlist<i>.totalB &gt;= matlist<i>.NeededB then
                call AddItemToStock( matlist<i>.shopunit ,matlist<i>.reward, 1, 1 )
                
                set matlist<i>.totalA = 0
                set matlist<i>.totalB = 0
            endif
            
            set i = i+1
        endloop
        
        set p = null
        set u = null

        return false
    endfunction



    private function onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        
        call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_PAWN_ITEM)
        call TriggerAddCondition( t, Condition( function onSell ) )
    endfunction
    
endlibrary</i></i></i></i></i></i></i></i></i></i></i></i></i></i>
 

Lobster

Old Fogey ofthe site
Reaction score
90
Ok, I'm sorry to say this but you really need to clean up that code a bit !
Maybe not the actual functionality (Haven't checked yet :p) but the intendation, and lineup...

Why use a global trigger ?!

Well it works, though I admit Im not the best coder.
BTW, why does the code lineup? matter, as long as its functional and efficient. I mainly state this because the user doesnt have to look at that part of the code, but instead should create a separate trigger for adding the recipes.
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Yeah, but making most of a public resource private is a very good idea...

And, to make it easier for moderators, and other people who want to help improve the code, it should be easy to read ! :D

You also need a better destription of the system, and also a guide to how to use it...
Maybe a Demo Map ? :eek:
 

Zwiebelchen

You can change this now in User CP.
Reaction score
60
This fails, as it is not multi-player instancable.

When player A sells stuff to a shop and is able to buy the reward item, player B can also buy it.
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
wat is dis i dont even no

Seriously, no documentation, nothing.
 

Lobster

Old Fogey ofthe site
Reaction score
90
This fails, as it is not multi-player instancable.

When player A sells stuff to a shop and is able to buy the reward item, player B can also buy it.

Hmm, Any suggestions on how to fix that? Can you locally add items?

The link has a demo map,

And, to make it easier for moderators, and other people who want to help improve the code, it should be easy to read !

Ill see what i can do :)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top