• 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.

[System] Scrolling Message

Level 7
Joined
Apr 30, 2011
Messages
359
JASS:
//========================================================================================
//      
//      Scrolling Message
//      -*- overcold_ice -*-
//      
//     -[*] Requirements:
//          - JNGP
//          - latest version of JassHelper
//          - Ascii
//              [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-ascii-190746/[/url]
//          - /*New*/Table
//              [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url]
//      
//          Provides functions to make scrolling game messages, as seen on most Credits
//      
//     -[*] API:
//      
//      struct ScrollingMsg extends array
//      
//          static method create takes nothing returns ScrollingMsg
//          method destroy takes nothing returns nothing
//              allocator and deallocator for the struct
//      
//          method add takes string Message returns nothing
//              add string Message to the struct's string
//              located after the last line of the struct's string
//      
//          method display takes real XOffset, real YOffset, real Velocity, force ToForce returns boolean
//              display scrolling message at XOffset and YOffset,
//              with the speed of Velocity (line per second),
//              shown to ToForce
//              if the struct is destroyed when it's still displaying messages,
//              it will automatically destroyed when it finishes displaying
//      
//========================================================================================
library ScrollingMessage requires /*New*/Table
    
    //====================================================================================
    //  C A L I B R A T I O N  S E C T I O N
    //====================================================================================
    globals
        private constant real UNKNOWN_CHAR_SIZE = 0.010966167
        private constant real MSG_LINE_HEIGHT   = 0.0675
        private constant real MSG_UPDATE_TIME   = 0.0325
        private constant real MSG_FADING_TIME   = 0.0325
    endglobals
    //====================================================================================
    
    globals
        private real array CharSize
        private TableArray Table
    endglobals
    
    private module Init
        private static method onInit takes nothing returns nothing
            set CharSize ['A'] = 0.015288462
            set CharSize ['B'] = 0.012268519
            set CharSize ['C'] = 0.013802083
            set CharSize ['D'] = 0.015288462
            set CharSize ['E'] = 0.012268519
            set CharSize ['F'] = 0.009287383
            set CharSize ['G'] = 0.015288462
            set CharSize ['H'] = 0.015288462
            set CharSize ['I'] = 0.006172360
            set CharSize ['J'] = 0.006172360
            set CharSize ['K'] = 0.013802083
            set CharSize ['L'] = 0.010801630
            set CharSize ['M'] = 0.019875000
            set CharSize ['N'] = 0.015288462
            set CharSize ['O'] = 0.016843220
            set CharSize ['P'] = 0.012268519
            set CharSize ['Q'] = 0.018402778
            set CharSize ['R'] = 0.013802083
            set CharSize ['S'] = 0.012268519
            set CharSize ['T'] = 0.010801630
            set CharSize ['U'] = 0.015288462
            set CharSize ['V'] = 0.013802083
            set CharSize ['W'] = 0.021603261
            set CharSize ['X'] = 0.015288462
            set CharSize ['Y'] = 0.013802083
            set CharSize ['Z'] = 0.013802083
            
            set CharSize ['a'] = 0.010801630
            set CharSize ['b'] = 0.012268519
            set CharSize ['c'] = 0.010801630
            set CharSize ['d'] = 0.012268519
            set CharSize ['e'] = 0.012268519
            set CharSize ['f'] = 0.007703488
            set CharSize ['g'] = 0.013802083
            set CharSize ['h'] = 0.012268519
            set CharSize ['i'] = 0.004643692
            set CharSize ['j'] = 0.006172360
            set CharSize ['k'] = 0.010801630
            set CharSize ['l'] = 0.004643692
            set CharSize ['m'] = 0.018402778
            set CharSize ['n'] = 0.012268519
            set CharSize ['o'] = 0.012268519
            set CharSize ['p'] = 0.012268519
            set CharSize ['q'] = 0.012268519
            set CharSize ['r'] = 0.007703488
            set CharSize ['s'] = 0.009287383
            set CharSize ['t'] = 0.007703488
            set CharSize ['u'] = 0.012268519
            set CharSize ['v'] = 0.012268519
            set CharSize ['w'] = 0.016843220
            set CharSize ['x'] = 0.012268519
            set CharSize ['y'] = 0.012268519
            set CharSize ['z'] = 0.010801630
            
            set CharSize [' '] = 0.006172360
            set CharSize ['!'] = 0.004643692
            set CharSize ['"'] = 0.006172360
            set CharSize ['#'] = 0.013802083
            set CharSize ['$'] = 0.010801630
            set CharSize ['%'] = 0.015288462
            set CharSize ['&'] = 0.016843220
            set CharSize [ 39] = 0.003095794
            set CharSize ['('] = 0.006172360
            set CharSize [')'] = 0.006172360
            set CharSize ['*'] = 0.009287383
            set CharSize ['+'] = 0.010801630
            set CharSize [','] = 0.004643692
            set CharSize ['-'] = 0.007703488
            set CharSize ['.'] = 0.004643692
            set CharSize ['/'] = 0.009287383
            set CharSize ['0'] = 0.012268519
            set CharSize ['1'] = 0.004643692
            set CharSize ['2'] = 0.012268519
            set CharSize ['3'] = 0.012268519
            set CharSize ['4'] = 0.013802083
            set CharSize ['5'] = 0.012268519
            set CharSize ['6'] = 0.012268519
            set CharSize ['7'] = 0.010801630
            set CharSize ['8'] = 0.012268519
            set CharSize ['9'] = 0.012268519
            set CharSize [':'] = 0.004643692
            set CharSize [';'] = 0.004643692
            set CharSize ['<'] = 0.010801630
            set CharSize ['='] = 0.010801630
            set CharSize ['>'] = 0.010801630
            set CharSize ['?'] = 0.010801630
            set CharSize ['@'] = 0.015288462
            set CharSize ['['] = 0.006172360
            set CharSize [ 92] = 0.007703488
            set CharSize [']'] = 0.006172360
            set CharSize ['^'] = 0.010801630
            set CharSize ['_'] = 0.010801630
            set CharSize ['`'] = 0.007703488
            set CharSize ['{'] = 0.006172360
            set CharSize ['|'] = 0.004643692
            set CharSize ['}'] = 0.006172360
            set CharSize ['~'] = 0.010801630
            
            set Table = TableArray [8192]
        endmethod
    endmodule
    
    private struct Implementer
        implement Init
    endstruct
    
    private function IsColorCode takes string s returns boolean
        local integer l = 1
        local integer i = Char2Ascii(StringCase(SubString(s, 0, 1), false))
        
        if i != 99 then
            return false
        endif
        
        loop
            exitwhen l == 9
            
            set i = Char2Ascii(StringCase(SubString(s, l, l + 1), false))
            
            if (i < 48 or i > 57) and (i < 97 or i > 102) then
                return false
            endif
            
            set l = l + 1
        endloop
        
        return true
    endfunction
    
    struct ScrollingMsg extends array
        private static integer  c
        private        thistype r
        
        private real x
        private real y
        private real v
        private real l
        private boolean s
        private boolean d
        private boolean ad
        private integer lc
        private integer cl
        
        private static timer          t
        private static thistype array i
        private static integer        ic
        
        private method storeMsg takes string s returns nothing
            local integer sl = StringLength(s)
            local integer ls = 0
            local string  ss = ""
            local real    r  = 0
            local integer l  = 0
            
            loop
                if l == sl then
                    set this.lc = this.lc + 1
                    set Table [this].string [this.lc] = SubString(s, ls, l)
                    
                    exitwhen true
                endif
                
                set ss = SubString(s, l, l + 1)
                
                if ss == "|" then
                    set ss = SubString(s, l + 1, l + 2)
                    
                    if IsColorCode(SubString(s, l + 1, l + 10)) then
                        set l = l + 9
                    elseif StringCase(ss, false) == "r" then
                        set l = l + 1
                    elseif ss == "|" then
                        set l = l + 1
                        set r = r + CharSize ['|']
                    else
                        set r = r + CharSize ['|']
                    endif
                elseif StringCase(SubString(s, l, l + 2), false) == "\n" then
                    set this.lc = this.lc + 1
                    set Table [this].string [this.lc] = SubString(s, ls, l)
                    set l  = l + 2
                    set ls = l
                    set r  = 0
                elseif CharSize [Char2Ascii(ss)] != 0 then
                    set r = r + CharSize [Char2Ascii(ss)]
                else
                    set r = r + UNKNOWN_CHAR_SIZE
                endif
                
                if r > 1 then
                    set this.lc = this.lc + 1
                    set Table [this].string [this.lc] = SubString(s, ls, l)
                    set ls = l
                    set r  = 0
                endif
                
                set l = l + 1
            endloop
        endmethod
        
        static method create takes nothing returns thistype
            local thistype this = thistype(0).r
            
            if this == 0 then
                set .c   = .c + 1
                set this = .c
            else
                set thistype(0).r = this.r
            endif
            
            return this
        endmethod
        
        method add takes string s returns nothing
            call this.storeMsg(s)
        endmethod
        
        private static method onExpire takes nothing returns nothing
            local thistype this = thistype(0)
            local integer  l
            local integer  ll
            
            loop
                set this = .i [this + 1]
                
                set this.l  = this.l + this.v
                set l  = R2I(this.l / MSG_LINE_HEIGHT)
                set ll = this.cl
                set this.cl = l
                
                if l > this.lc then
                    set l = this.lc
                    
                    if this.cl == l + 16 and .s then
                        call ClearTextMessages()
                        
                        set this.x  = 0
                        set this.y  = 0
                        set this.v  = 0
                        set this.s  = false
                        set this.d  = false
                        set this.l  = 0
                        set this.cl = 0
                        
                        if this.ad then
                            call this.destroy()
                        endif
                        
                        set .i [this] = .i [.ic]
                        set .i [.ic] = 0
                        set .ic = .ic - 1
                        set this = this - 1
                    elseif ll != this.cl and .s then
                        call DisplayTimedTextToPlayer(GetLocalPlayer(), this.x, this.y + this.l - l, 16 * this.v / (MSG_LINE_HEIGHT * MSG_UPDATE_TIME) - MSG_FADING_TIME, " ")
                    endif
                elseif ll != this.cl and .s then
                    call DisplayTimedTextToPlayer(GetLocalPlayer(), this.x, this.y + this.l - l, 16 * this.v / (MSG_LINE_HEIGHT * MSG_UPDATE_TIME) - MSG_FADING_TIME, Table [this].string [this.cl])
                endif
                
                exitwhen this == .ic
            endloop
        endmethod
        
        method display takes real x, real y, real v, force f returns boolean
            if this.d then
                return false
            endif
            
            set this.x  = x
            set this.y  = y
            set this.v  = v * MSG_LINE_HEIGHT * MSG_UPDATE_TIME
            set this.s  = IsPlayerInForce(GetLocalPlayer(), f) or f == null
            set this.d  = true
            
            set this.l  = 0
            set this.cl = 1
            
            if .s then
                call DisplayTimedTextToPlayer(GetLocalPlayer(), this.x, this.y, 16 * this.v / (MSG_LINE_HEIGHT * MSG_UPDATE_TIME) - MSG_FADING_TIME, Table [this].string [this.cl])
            endif
            
            if .ic == 0 then
                call TimerStart(.t, MSG_UPDATE_TIME, true, function thistype.onExpire)
            endif
            
            set     .ic  = .ic + 1
            set .i [.ic] = this
            
            return true
        endmethod
        
        method destroy takes nothing returns nothing
            if this.d then
                set this.ad = true
            else
                call Table [this].flush()
                set this.lc = 0
                set this.ad = false
            endif
            
            set this.r        = thistype(0).r
            set thistype(0).r = this
        endmethod
    endstruct
endlibrary
 
Wow, you've actually made something cool and useful O-o.

Fix your var names and add some comments. Give credits where due (unless you came up with all of those values on your own).

-> CharSize is supposed to be camelcase -> charSize

You should be creating local credits given a boolean condition...

this is not necessary
IsPlayerInForce(GetLocalPlayer(), f) or f == null

Just check the condition that's passed in... by taking a condition, the user can pass in whatever they want to check for.

I'll check this more after you make the code readable ;D.
 
Because I have no life:

JASS:
/********************************
*
*   CharSize
*   v1.0.0.0
*   By overcold_ice
*
*   - Stores character sizes on the screen
*     for usage by multiple resources.
*
*   API:
*   ----
*
*       - function GetCharSize takes integer character returns real
*           - Gives the value of the size of a character.
*             The integer that should be passed is the Ascii index
*             of that character. Examples: '8', 'w', '>', etc...
*
********************************/
library CharSize
    
    globals
        private constant real UNKNOWN = 0.010966167
        private real array size
    endglobals
    
    private module Init
        private static method onInit takes nothing returns nothing
        
            /*
            *   The logic behind subtracting the unknown character size from all the character sizes
            *   is quite interesting. It allows me to add 0 safey to the function and let it inline
            *   and it will still pass UNKNOWN to bad input. (Invalid characters)
            */
            set size[32] = 0.006172360 - UNKNOWN
            set size[33] = 0.004643692 - UNKNOWN
            set size[34] = 0.006172360 - UNKNOWN
            set size[35] = 0.013802083 - UNKNOWN
            set size[36] = 0.010801630 - UNKNOWN
            set size[37] = 0.015288462 - UNKNOWN
            set size[38] = 0.016843220 - UNKNOWN
            set size[39] = 0.003095794 - UNKNOWN
            set size[40] = 0.006172360 - UNKNOWN
            set size[41] = 0.006172360 - UNKNOWN
            set size[42] = 0.009287383 - UNKNOWN
            set size[43] = 0.010801630 - UNKNOWN
            set size[44] = 0.004643692 - UNKNOWN
            set size[45] = 0.007703488 - UNKNOWN
            set size[46] = 0.004643692 - UNKNOWN
            set size[47] = 0.009287383 - UNKNOWN
            set size[48] = 0.012268519 - UNKNOWN
            set size[49] = 0.004643692 - UNKNOWN
            set size[50] = 0.012268519 - UNKNOWN
            set size[51] = 0.012268519 - UNKNOWN
            set size[52] = 0.013802083 - UNKNOWN
            set size[53] = 0.012268519 - UNKNOWN
            set size[54] = 0.012268519 - UNKNOWN
            set size[55] = 0.010801630 - UNKNOWN
            set size[56] = 0.012268519 - UNKNOWN
            set size[57] = 0.012268519 - UNKNOWN
            set size[58] = 0.004643692 - UNKNOWN
            set size[59] = 0.004643692 - UNKNOWN
            set size[60] = 0.010801630 - UNKNOWN
            set size[61] = 0.010801630 - UNKNOWN
            set size[62] = 0.010801630 - UNKNOWN
            set size[63] = 0.010801630 - UNKNOWN
            set size[64] = 0.015288462 - UNKNOWN
            set size[65] = 0.015288462 - UNKNOWN
            set size[66] = 0.012268519 - UNKNOWN
            set size[67] = 0.013802083 - UNKNOWN
            set size[68] = 0.015288462 - UNKNOWN
            set size[69] = 0.012268519 - UNKNOWN
            set size[70] = 0.009287383 - UNKNOWN
            set size[71] = 0.015288462 - UNKNOWN
            set size[72] = 0.015288462 - UNKNOWN
            set size[73] = 0.006172360 - UNKNOWN
            set size[74] = 0.006172360 - UNKNOWN
            set size[75] = 0.013802083 - UNKNOWN
            set size[76] = 0.010801630 - UNKNOWN
            set size[77] = 0.019875000 - UNKNOWN
            set size[78] = 0.015288462 - UNKNOWN
            set size[79] = 0.016843220 - UNKNOWN
            set size[80] = 0.012268519 - UNKNOWN
            set size[81] = 0.018402778 - UNKNOWN
            set size[82] = 0.013802083 - UNKNOWN
            set size[83] = 0.012268519 - UNKNOWN
            set size[84] = 0.010801630 - UNKNOWN
            set size[85] = 0.015288462 - UNKNOWN
            set size[86] = 0.013802083 - UNKNOWN
            set size[87] = 0.021603261 - UNKNOWN
            set size[88] = 0.015288462 - UNKNOWN
            set size[89] = 0.013802083 - UNKNOWN
            set size[90] = 0.013802083 - UNKNOWN
            set size[91] = 0.006172360 - UNKNOWN
            set size[92] = 0.007703488 - UNKNOWN
            set size[93] = 0.006172360 - UNKNOWN
            set size[94] = 0.010801630 - UNKNOWN
            set size[95] = 0.010801630 - UNKNOWN
            set size[96] = 0.007703488 - UNKNOWN
            set size[97] = 0.010801630 - UNKNOWN
            set size[98] = 0.012268519 - UNKNOWN
            set size[99] = 0.010801630 - UNKNOWN
            set size[100] = 0.012268519 - UNKNOWN
            set size[101] = 0.012268519 - UNKNOWN
            set size[102] = 0.007703488 - UNKNOWN
            set size[103] = 0.013802083 - UNKNOWN
            set size[104] = 0.012268519 - UNKNOWN
            set size[105] = 0.004643692 - UNKNOWN
            set size[106] = 0.006172360 - UNKNOWN
            set size[107] = 0.010801630 - UNKNOWN
            set size[108] = 0.004643692 - UNKNOWN
            set size[109] = 0.018402778 - UNKNOWN
            set size[110] = 0.012268519 - UNKNOWN
            set size[111] = 0.012268519 - UNKNOWN
            set size[112] = 0.012268519 - UNKNOWN
            set size[113] = 0.012268519 - UNKNOWN
            set size[114] = 0.007703488 - UNKNOWN
            set size[115] = 0.009287383 - UNKNOWN
            set size[116] = 0.007703488 - UNKNOWN
            set size[117] = 0.012268519 - UNKNOWN
            set size[118] = 0.012268519 - UNKNOWN
            set size[119] = 0.016843220 - UNKNOWN
            set size[120] = 0.012268519 - UNKNOWN
            set size[121] = 0.012268519 - UNKNOWN
            set size[122] = 0.010801630 - UNKNOWN
            set size[123] = 0.006172360 - UNKNOWN
            set size[124] = 0.004643692 - UNKNOWN
            set size[125] = 0.006172360 - UNKNOWN
            set size[126] = 0.010801630 - UNKNOWN
        endmethod
    endmodule
    
    function GetCharSize takes integer char returns real
        return size[char] + UNKNOWN
    endfunction
    
    private struct Inits extends array
        implement Init
    endstruct

endlibrary

Just copy and paste this to a new thread.
 
Level 7
Joined
Apr 30, 2011
Messages
359
i have think that before . . .
and it gets toooooo complicated,
and this is what will i do:
1. Make a library that do the size thing
2. Another one for the Line Breaker thing
and they won't be special to only game message,
but also multiboard, dialog text, etc (put to constant)

@nes: it's my own things :p
 
Level 7
Joined
Apr 30, 2011
Messages
359
sorry . . .
i found it by experimenting how many characters in a line (it's in percent)
and i take the lowest value available (since higher value will be buggy to the lowest value, but not in reverse)
lowest value/highest value is derived from the screen resolution
i'm going to fix all those values + more values :)
 
Level 7
Joined
Apr 30, 2011
Messages
359
well . . .
the only thing that differs is that, and if someone going to use this width thing,
he should use my other extension (the line breaker based on those values)
yeah, it's a 'bit' inaccurate . . .
the differences is around 1 - 5 letters per line (no more than 5 i think)
 
Here's a better idea, make a CharSize library that returns values by taking the original values
(meaning you should go for a very small resolution and expirement with that), and multiplies
by a certain factor to get accurate values for any resolution.

You'd also need another script for storing resolution values (for re-usability) and just to make
it even more useful, you could provide functions that return scaling factors for images and
such based on current resolution.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
well . . .
the only thing that differs is that, and if someone going to use this width thing,
he should use my other extension (the line breaker based on those values)
yeah, it's a 'bit' inaccurate . . .
the differences is around 1 - 5 letters per line (no more than 5 i think)

Damn, i so hate "..." abuses, especially when the guy is totally wrong, like here.

With the resolution 640*480*32 i'm able to display 81 'a' in a single line, whereas with 1600*900*32 i can display 112.

The bad new, there is no native function to get the resolution being used.
I suppose you could try something with a camera and trackable, but even if it works it won't be instant and it will be easy to break.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Stupid users deserve stupid results :ogre_hurrhurr:

To be honest i'm not sure i always know which resolution i'm using when i play wc3 :eek:

edit
A better idea would be to use the values you get for the lowest resolution.

It will
always
work for
sure but
it could be
annoying
on display.
Just to
ensure
some
unlikely
poor
resolutions
to work.
Right ?
 
Level 7
Joined
Apr 30, 2011
Messages
359
my max resolution is 1024x600 ~.~
and that resource is going to get more delays
(i'm going into a contest :D)
(many things to experiment)

i doubt the scaling thing mag,
the values are random ~.~
 
Assume a resolution LxV

- See how the values change for a constant L
- See how the values change for a constant V
- Do the above two steps for every L and V

You can find a relation using the above method and try to come up with a way to return correct values for any resolution.

OR, you could just find values for each resolution and cache them (But I wouldn't recommend that, it'll make your code too bulky and messy :p)
 
Top