System Ammo System

Tukki

is Skeleton Pirate.
Reaction score
29
Ammo System
v1.0

A simple ammo system with the main purpose to be as easy to use as possible. Can use food, gold/lumber or other ways of ammo info.

Requirements:
  • A vJass compiler (such as JNGP);
  • Small knowledge of calling functions;
  • PUI v5.1;
  • A Reload ability;

JASS:
//===========================================================================
// Ammo System -- v1.0
//===========================================================================
//
//  A simple yet useful ammo system which can use food, gold&lumber or other 
//  ways to display ammo and clips.
//
//  Coder: Tukki
//  Requirements:
//                  - a reload ability
//                  - this trigger
//                  - PUI by Cohadar [v5.1]
//                  - a vJass compiler such as JassNewGenPack 1.5b+
//
//  Importing:
//                  -(1) Left-click on 'Ammo System', press Ctrl+C
//                  -(2) Open your map and open Trigger Editor, press Ctrl+V
//                  - Repeat the process(1 and 2) with PUI if you don't have it
//                  - Create or copy the 'Reload' ability found in this map
//                  - Change the AID_RELOAD to the rawcode of the ability 
//                    press Ctrl+D in Object Editor to view rawcodes
//
//  Functions Provided:
//               
//      Ammo_Add(whichUnit, amount, whichType) -> nothing
//      Ammo_Get(whichUnit, whichType) -> integer (-1 if invaild)
//      Ammo_Create(whichUnit, startingAmmo, startingClips, maxAmmo, maxClips)
//
//      Example: call Ammo_Create(GetTriggerUnit(), 30, 5, 30, 10)
//
//               will: make the unit have 5 initial clips +  the one active
//                     restrict clip maximum to 10 and ammo maximum to 30
//
//  Pros:
//      
//      * Supports several types of ammo/clip display
//      * Easy-to-use, even for GUI:ers
//      * Uses PUI for unit-struct attaching
//      
//  Cons:
//
//      * Does not recycle/destroy structs 
//      * 8190 unit-limit
//      * Unit-stopping is not 100% accurate, depending on the attack rate 
//
//===========================================================================
library Ammo initializer init uses PUI

    globals
        private constant boolean USE_FOOD_ALL        = true  // Use the food counter to display
        private constant boolean USE_GOLD_AND_LUMBER = false // current ammo/current clips or
                                                             // gold&lumber resources.
                                                             // Set both to false if you use a
                                                             // multiboard.
        constant integer AMMO_TYPE_CLIP     = 0 // Use this "names" as whichType in the Get&Add
        constant integer AMMO_TYPE_AMMO     = 1 // functions. 
        constant integer AMMO_TYPE_MAX_CLIP = 2
        constant integer AMMO_TYPE_MAX_AMMO = 3
        
        private constant integer AID_RELOAD  = 'A000'   // Rawcode of the Reload ability
        private constant string  OID_RELOAD  = "absorb" // Order string of formentioned ability
        private constant boolean AUTO_RELOAD = true     // Auto-reload when out of ammo?
        
        private constant string  NO_AMMO  = "|CFFEE0000Out of ammo!|r"  // no-ammo error text
        private constant string  NO_CLIP  = "|CFFEE0000Out of clips!|r" // no-clips error text
        private constant string  RELOAD   = "|CFF00EE00Reloading!|r"    // reloading text
        private constant real    REMINDER = 2.0 // how often error triggered texts may appear
        
        private constant real TT_SIZE     = 0.024            // Error text size
        private constant real TT_VELOCITY = 0.02             // Error text velocity
        private constant real TT_LIFETIME = 3.0              // Error text lifespan
        private constant real TT_FADE     = TT_LIFETIME*0.66 // Error text fadepoint
    endglobals
    
    //---------------------------------------------------------------------------
    private function TextTag takes unit u, player p, string s returns nothing
        local texttag t=CreateTextTag()
            
        call SetTextTagText(t, s, TT_SIZE)
        call SetTextTagPos(t, GetUnitX(u), GetUnitY(u), 0)
        call SetTextTagPermanent(t, false)
        call SetTextTagVisibility(t, GetLocalPlayer()==p)
        call SetTextTagFadepoint(t, TT_FADE)
        call SetTextTagLifespan(t, TT_LIFETIME)
        call SetTextTagColor(t, 255, 255, 255, 255)
        call SetTextTagVelocity(t, 0, TT_VELOCITY)
        
        set t=null
    endfunction
    
    //---------------------------------------------------------------------------
    private struct unitData
        unit whichUnit
        integer currentAmmo=0
        integer currentClips=0
        
        integer maxAmmo=0
        integer maxClips=0
        real last=0
        
        //! runtextmacro PUI()
        
        static group Pivot=null
        static timer Timer=null
        
        //---------------------------------------------------------------------------
        static method create takes unit u, integer sa, integer sc, integer ma, integer mc returns unitData
            local unitData this = unitData.allocate()
            
            set .whichUnit=u
            set .currentAmmo=sa
            set .currentClips=sc
            set .maxAmmo=ma
            set .maxClips=mc
            
            call .update()
            
            call GroupAddUnit(unitData.Pivot, u)
            set unitData<u>=this
            
            return this
        endmethod
        
        //---------------------------------------------------------------------------
        method update takes nothing returns nothing
            if(USE_GOLD_AND_LUMBER)then
                call SetPlayerState(GetOwningPlayer(.whichUnit), PLAYER_STATE_RESOURCE_GOLD, .currentAmmo)
                call SetPlayerState(GetOwningPlayer(.whichUnit), PLAYER_STATE_RESOURCE_LUMBER, .currentClips)
            elseif(USE_FOOD_ALL)then
                call SetPlayerState(GetOwningPlayer(.whichUnit), PLAYER_STATE_RESOURCE_FOOD_USED, .currentAmmo)
                call SetPlayerState(GetOwningPlayer(.whichUnit), PLAYER_STATE_RESOURCE_FOOD_CAP, .currentClips)
            endif
        endmethod
        
        //---------------------------------------------------------------------------
        static method onAttackEvent takes nothing returns nothing
            local unit u=GetAttacker()
            local unitData this=unitData<u>

                if(.currentAmmo&gt;0)then
                    set .currentAmmo=.currentAmmo-1
                    call .update()
                else
                    call PauseUnit(u, true)
                    call IssueImmediateOrder(u, &quot;stop&quot;)
                    call PauseUnit(u, false)
                    if(.currentClips&gt;0)then
                        if(AUTO_RELOAD)then
                            call IssueImmediateOrder(u, OID_RELOAD)
                            call TextTag(u, GetOwningPlayer(u), RELOAD)
                        else
                            call PauseUnit(u, true)
                            call IssueImmediateOrder(u, &quot;stop&quot;)
                            call PauseUnit(u, false)
                            if (TimerGetElapsed(unitData.Timer)-.last&gt;REMINDER)then
                                call TextTag(u, GetOwningPlayer(u), NO_AMMO)
                                set .last=TimerGetElapsed(unitData.Timer)
                            endif
                        endif
                    endif
                endif

            set u=null
        endmethod
        
        //---------------------------------------------------------------------------
        static method onSpellEvent takes nothing returns nothing
            local unit u=GetSpellAbilityUnit()
            local unitData this=unitData<u>
            
                if(.currentClips&gt;0)then
                    set .currentAmmo=.maxAmmo
                    set .currentClips=.currentClips-1
                    call .update()
                else
                    call TextTag(u, GetOwningPlayer(u), NO_CLIP)
                endif
                
            set u=null
        endmethod
        
        //---------------------------------------------------------------------------
        static method checkAttack takes nothing returns boolean
            return IsUnitInGroup(GetAttacker(), unitData.Pivot)
        endmethod
        
        //---------------------------------------------------------------------------
        static method checkSpell takes nothing returns boolean
            return IsUnitInGroup(GetSpellAbilityUnit(), unitData.Pivot) and GetSpellAbilityId()==AID_RELOAD
        endmethod
        
    endstruct
    
    //---------------------------------------------------------------------------
    public function Add takes unit whichUnit, integer amount, integer state returns nothing
        local unitData d=unitData[whichUnit]
            
            if(d==0)then
                debug call BJDebugMsg(&quot;|CFFEE0000&quot;+SCOPE_NAME+&quot; error: &quot;+GetUnitName(whichUnit)+&quot; is not indexed!|r&quot;)
                return
            endif
            if(state==AMMO_TYPE_CLIP)then
                if(d.currentClips+amount&gt;d.maxClips)then
                    set d.currentClips=d.maxClips
                else
                    set d.currentClips=d.currentClips+amount
                endif
            elseif(state==AMMO_TYPE_AMMO)then
                if(d.currentAmmo+amount&gt;d.maxAmmo)then
                    set d.currentAmmo=d.maxAmmo
                else
                    set d.currentAmmo=d.currentAmmo+amount
                endif
            elseif(state==AMMO_TYPE_MAX_AMMO)then
                set d.maxAmmo=amount
            elseif(state==AMMO_TYPE_MAX_CLIP)then
                set d.maxClips=amount
            endif
            
            call d.update()
    endfunction 
    
    //---------------------------------------------------------------------------
    public function Get takes unit whichUnit, integer state returns integer
        local unitData d=unitData[whichUnit]
            
            if(state==AMMO_TYPE_AMMO)then
                return d.currentAmmo
            elseif(state==AMMO_TYPE_CLIP)then
                return d.currentClips
            elseif(state==AMMO_TYPE_MAX_CLIP)then
                return d.maxClips
            elseif(state==AMMO_TYPE_MAX_AMMO)then
                return d.maxAmmo
            endif
            return -1
    endfunction
    
    //---------------------------------------------------------------------------
    public function Create takes unit whichUnit, integer startAmmo, integer startClips, integer maxAmmo, integer maxClips returns nothing
        call unitData.create(whichUnit, startAmmo, startClips, maxAmmo, maxClips)
    endfunction
    
    //---------------------------------------------------------------------------
    private function restart takes nothing returns nothing
        call TimerStart(unitData.Timer, 3600, false, function restart)
    endfunction
    
    //---------------------------------------------------------------------------
    private function init takes nothing returns nothing
        local trigger t=CreateTrigger()
            
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_ATTACKED)
        call TriggerAddCondition(t, Condition(function unitData.checkAttack))
        call TriggerAddAction(t, function unitData.onAttackEvent)
        
        set t=CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
        call TriggerAddCondition(t, Condition(function unitData.checkSpell))
        call TriggerAddAction(t, function unitData.onSpellEvent)        
        
        set unitData.Pivot=CreateGroup()
        set unitData.Timer=CreateTimer() // I hope this doesn&#039;t kill the world..
        
        call TimerStart(unitData.Timer, 3600, false, function restart)
    endfunction
endlibrary</u></u></u>


How to use:
JASS:
call Ammo_Create(&lt;someUnit&gt;, &lt;startAmmo&gt;, &lt;startClips&gt;, &lt;maxAmmo&gt;, &lt;maxClips&gt;)


Screenshots: I like systems that doesn't benefit from these things :D

Change log:
  • 30.01.2009 - v1.0 released

Upcoming Features:
  • Directly using Gold/Lumber/Food for ammo usage (currently it's only changing them)
 

Attachments

  • AmmoSystem.w3x
    54.9 KB · Views: 287

duderock101

Check out my 2 Player Co-op RPG!
Reaction score
71
hmm, a jass version of ammo system, looks good ( i dunno anything bout jass hehhe), if you don't know jass use my system (its simpler and in GUI) hehe well +rep for you
 

Tukki

is Skeleton Pirate.
Reaction score
29
hmm, a jass version of ammo system, looks good ( i dunno anything bout jass hehhe), if you don't know jass use my system (its simpler and in GUI) hehe well
Maybe you should learn vJass/JASS :) I'll check your system out.
 

psychophat

New Member
Reaction score
0
Nice, could you add an update that it cannot shoot anything else if there is no ammo.

Clip still shootable.
 

Tukki

is Skeleton Pirate.
Reaction score
29
I'm not 100% sure of what you mean with "clip still shootable", are you saying that:

a - the system malfunction and the user may attack, discardig how many bullets he got?
b - that you shouldn't be able to target some things when out of ammo?
c - that it malfunctions and doesn't reduce the number of clips, a.k.a you have unlimited clips

Glad you liked it, though :)
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +1
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top