Snippet PrintOrders

Azlier

Old World Ghost
Reaction score
461
A simple debugging snippet that I get tired of rewriting for every map.

When a unit is issued an order, it prints some order data. It will print the order ID in hex if it's a normal order, in ASCII if it's a rawcode. It also displays the type of order, the unit that was issued the order, and the name of the order (if any). Optionally, it can also display data on the target of an order if DISPLAY_TARGET_INFO is flipped to true.

Only works in debug mode.

Requires ASCII.

JASS:
library PrintOrders initializer Init requires Ascii

//Config
globals
    private constant boolean CLEAR_BEFORE_PRINTING = false
    //Setting this to true will clear the screen of text messages when printing order data.
    
    private constant string ACTIVATE = "-printon"
    //Typing this in-game will activate PrintOrder (it is activated by default).
    private constant string DEACTIVATE = "-printoff"
    //Typing this in-game will deactivate PrintOrder.
    
    private constant boolean DISPLAY_TARGET_INFO = false
    //When flipped to true, the system will display information on targets of orders.
endglobals
//End config

globals
    private string array Hex_Values
    private trigger t1 = CreateTrigger()
    private trigger t2 = CreateTrigger()
    private trigger t3 = CreateTrigger()
    private constant integer OFFSET = 0xD0000
endglobals
 
private function Int2HexString takes integer i returns string
    local string hex = ""
    local integer r = 0
    local string neg = ""
    if (i<0) then
        set neg = "-"
        set i = -i
    endif
    loop
        set r = i - (i / 16) * 16
        set i = i/16
        set hex = Hex_Values[r] + hex
        exitwhen (i==0)
    endloop
    return neg+hex
endfunction

private function Rawcode2String takes integer i returns string
    local string c = ""
    loop
        exitwhen i == 0
        set c = Ascii2Char(i - (i / 256) * 256)+c
        set i = i / 256
    endloop
    return c
endfunction

private function Target takes nothing returns boolean
    local integer order = GetIssuedOrderId()
    local string s
    static if DISPLAY_TARGET_INFO then
        local string n
    endif
    if order - OFFSET < 8191 then
        set s = "0x" + Int2HexString(order)
    else
        set s = "'" + Rawcode2String(order) + "'"
    endif
    static if CLEAR_BEFORE_PRINTING then
        call ClearTextMessages()
    endif
    
    static if DISPLAY_TARGET_INFO then
        if GetOrderTargetUnit() != null then
            set n = GetUnitName(GetOrderTargetUnit())
        elseif GetOrderTargetDestructable() != null then
            set n = GetDestructableName(GetOrderTargetDestructable())
        else
            set n = GetItemName(GetOrderTargetItem())
        endif
        
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, GetUnitName(GetTriggerUnit()) +/*
        */" issued target object order.\n  OrderId: " + s + "\n  order name: " +/*
        */OrderId2String(GetIssuedOrderId()) + "\n\nTarget name: " + n + "\nTarget handle id: " + /*
        */I2S(GetHandleId(GetOrderTarget())) + "\n\n")
    else
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, GetUnitName(GetTriggerUnit()) +/*
        */" issued target object order.\n  OrderId: " + s + "\n  order name: " +/*
        */OrderId2String(GetIssuedOrderId()) + "\n\n")
    endif
    
    return false
endfunction

private function Point takes nothing returns boolean
    local integer order = GetIssuedOrderId()
    local string s
    if order - OFFSET < 8191 then
        set s = "0x" + Int2HexString(order)
    else
        set s = "'" + Rawcode2String(order) + "'"
    endif
    static if CLEAR_BEFORE_PRINTING then
        call ClearTextMessages()
    endif
    
    static if DISPLAY_TARGET_INFO then
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, GetUnitName(GetTriggerUnit()) +/*
        */" issued target point order at (" + R2S(GetOrderPointX()) + "," + R2S(GetOrderPointY()) + ")"/*
        */ + ".\n  OrderId: " + s + "\n  order name: " +/*
        */OrderId2String(GetIssuedOrderId()) + "\n\n")
    else
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, GetUnitName(GetTriggerUnit()) +/*
        */" issued target point order.\n  OrderId: " + s + "\n  order name: " +/*
        */OrderId2String(GetIssuedOrderId()) + "\n\n")
    endif
    
    return false
endfunction

private function Order takes nothing returns boolean
    local integer order = GetIssuedOrderId()
    local string s
    if order - OFFSET < 8191 then
        set s = "0x" + Int2HexString(order)
    else
        set s = "'" + Rawcode2String(order) + "'"
    endif
    static if CLEAR_BEFORE_PRINTING then
        call ClearTextMessages()
    endif
    call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, GetUnitName(GetTriggerUnit()) +/*
    */" issued order with no target.\n  OrderId: " + s + "\n  order name: " +/*
    */OrderId2String(GetIssuedOrderId()) + "\n\n")
    return false
endfunction

private function Activate takes nothing returns boolean
    call EnableTrigger(t1)
    call EnableTrigger(t2)
    call EnableTrigger(t3)
    call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "PrintOrder activated.")
    return false
endfunction

private function Deactivate takes nothing returns boolean
    call DisableTrigger(t1)
    call DisableTrigger(t2)
    call DisableTrigger(t3)
    call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "PrintOrder deactivated.")
    return false
endfunction

private function Init takes nothing returns nothing
    static if DEBUG_MODE then
    local integer i = 15
    
    local trigger t = CreateTrigger()
    call TriggerRegisterPlayerChatEvent(t, GetLocalPlayer(), ACTIVATE, true)
    call TriggerAddCondition(t, Condition(function Activate))
    
    set t = CreateTrigger()
    call TriggerRegisterPlayerChatEvent(t, GetLocalPlayer(), DEACTIVATE, true)
    call TriggerAddCondition(t, Condition(function Deactivate))

    loop
        call TriggerRegisterPlayerUnitEvent(t1, Player(i), EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER, null)
        call TriggerRegisterPlayerUnitEvent(t2, Player(i), EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER, null)
        call TriggerRegisterPlayerUnitEvent(t3, Player(i), EVENT_PLAYER_UNIT_ISSUED_ORDER, null)
        exitwhen i == 0
        set i = i - 1
    endloop
    call TriggerAddCondition(t1, Filter(function Target))
    call TriggerAddCondition(t2, Filter(function Point))
    call TriggerAddCondition(t3, Filter(function Order))
    set Hex_Values[0] = "0"
    set Hex_Values[1] = "1"
    set Hex_Values[2] = "2"
    set Hex_Values[3] = "3"
    set Hex_Values[4] = "4"
    set Hex_Values[5] = "5"
    set Hex_Values[6] = "6"
    set Hex_Values[7] = "7"
    set Hex_Values[8] = "8"
    set Hex_Values[9] = "9"
    set Hex_Values[10] = "A"
    set Hex_Values[11] = "B"
    set Hex_Values[12] = "C"
    set Hex_Values[13] = "D"
    set Hex_Values[14] = "E"
    set Hex_Values[15] = "F"
    
    endif
endfunction

endlibrary
 

quraji

zap
Reaction score
144
You could make the order display in hex also, should be shorter to copy down... ;)

JASS:
library Int2HexString initializer init
    // converts an integer into a hex string (255 -> "FF")

globals
    private string array Hex_Values
endglobals

// see any decimal -> hex conversion guide online to see what's done here
function Int2HexString takes integer i returns string
    local string hex = ""
    local integer r = 0
    local string neg = ""
    if (i<0) then
        set neg = "-"
        set i = -i
    endif
    loop
        set r = ModuloInteger(i,16)
        set i = i/16
        set hex = Hex_Values[r] + hex
        exitwhen (i==0)
    endloop
    return neg+hex
endfunction

private function init takes nothing returns nothing
    // hex values
    set Hex_Values[0] = "0"
    set Hex_Values[1] = "1"
    set Hex_Values[2] = "2"
    set Hex_Values[3] = "3"
    set Hex_Values[4] = "4"
    set Hex_Values[5] = "5"
    set Hex_Values[6] = "6"
    set Hex_Values[7] = "7"
    set Hex_Values[8] = "8"
    set Hex_Values[9] = "9"
    set Hex_Values[10] = "A"
    set Hex_Values[11] = "B"
    set Hex_Values[12] = "C"
    set Hex_Values[13] = "D"
    set Hex_Values[14] = "E"
    set Hex_Values[15] = "F"
endfunction

endlibrary


From my widely used "More String Functions" library of awesome string functions!!
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Static ifs outside functions/methods aren't supported, they are just ignored.
Also there is already a library for this :

http://www.wc3c.net/showthread.php?t=102883

Even if you need to delete the H2I function and change the calling of it by GetHandleId.
And instead of activate/deactivate the library yes it needs static ifs.
 

Azlier

Old World Ghost
Reaction score
461
>Static ifs outside functions/methods aren't supported, they are just ignored.

Whose idea was that? How sad.

>You could make the order display in hex also

I actually thought of that, but thought it would be overkill. Maybe I should actually add it :p.

>Also there is already a library for this

Noooooooo.

Well, I personally won't be using that one because that one is indeed overkill.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Well, I personally won't be using that one because that one is indeed overkill.
Diod's one ?

EDIT :
I've just realized theses constants are totally useless :

JASS:
    private integer TYPE_WHOKNOWS = 0
    
    private integer TYPE_NOTHING  = 1
    private integer TYPE_LOCATION = 2
    private integer TYPE_WIDGET   = 3
    
    private integer TYPE_UNIT         = 4
    private integer TYPE_ITEM         = 5
    private integer TYPE_DESTRUCTABLE = 6


Also there are indeed useless functions like Echo,DEBUG and such.

With your's the BJs inline makes me lol ^^
In fact you still even use a pretty useless one ModuloInteger
 

Azlier

Old World Ghost
Reaction score
461
Yes. /shrug

Usually I just use this to find a specific order id.
 

Azlier

Old World Ghost
Reaction score
461
Well, I've already inlined stuff. Might as well not change it back. But I don't quite feel like inlining ModuloInteger at the moment :eek:.
 

quraji

zap
Reaction score
144
What's wrong with not inlining ModuloInteger? Just makes your code harder to read.

What's wrong with using any BJ that is a decent function (and not just "WhateverSwapped"...those are just stupid)?

I do think you should add the display in hex. It makes it easier to read/write/type. Just make sure you add a note that it does indeed display in hex (and one will need to add the 0x to make it work correctly if they copy one).

EDIT: Oh hey, you did, woops. Hey it's my snippet too! :thup:
 

Azlier

Old World Ghost
Reaction score
461
I already did. I just didn't inline ModuleInteger because I am excessively lazy.
 

quraji

zap
Reaction score
144
I already did. I just didn't inline ModuleInteger because I am excessively lazy.

No reason to do it anyways :p
You should add a note that it displays in hex though. Or just prefix the output with "0x".

EDIT: You did that too....maybe I should look at things sometimes..
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top