• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Grid System

This bundle is marked as pending. It has not been reviewed by a staff member yet.
Due to the popularity of this resource, I have decided to update it to a Lua version.

Note: The Lua upload utilizes both Bribe's Total Initialization and Eikonium's Debug Utils. Both of these are optional and not required to work.

Information

Code - Jass

Code - Lua

Changelog

A simple grid system that allows for easier building/tower placement.

How to use:
Press G to show/hide. Once you have pressed to show you have to click somewhere and it will appear at that location.

-gc RRGGBB //set the color of the grid, based on inputed hex value.
-gs XX //sets the grids size based on inputed value. Note that it has to be between defined limits.


unknown.png


JASS:
library GridSystem initializer init

//Grid System By CanFight

    globals
        private image array gridEffect
        private boolean showHide
        private boolean waitForClick
        private integer gridRed = 200
        private integer gridGreen = 200
        private integer gridBlue = 200
        private integer gridAlpha  = 255
        private integer gridSize = 8
        private integer gridSizeMax = 32
        private integer gridSizeMin = 4
    endglobals
 
    private function ShowHideGrid takes nothing returns nothing
        local integer i = 0
        if GetLocalPlayer() == GetTriggerPlayer() then
            if showHide then
                set showHide = false      
                loop
                    exitwhen i >= gridSizeMax * gridSizeMax
                    call SetImageColor(gridEffect[i],255,255,255,0)
                    set i = i + 1
                endloop
            else
                set waitForClick = true
            endif
        endif
    endfunction

    private function MouseMove takes nothing returns nothing endfunction
        local real x = I2R(R2I(BlzGetTriggerPlayerMouseX()) / 128) * 128
        local real y = I2R(R2I(BlzGetTriggerPlayerMouseY()) / 128) * 128
        local real tx
        local real ty
        local integer i = 0
        local integer ix = 0
        local integer iy = 0
        if GetLocalPlayer() == GetTriggerPlayer() then
            if waitForClick then
                set waitForClick = false
                set showHide = true
                loop
                    exitwhen i >= gridSize * gridSize
                    set tx = x + ix * 128 - gridSize / 2 * 128
                    set ty = y + iy * 128 - gridSize / 2 * 128
                    call SetImageColor(gridEffect[i],gridRed,gridGreen,gridBlue,gridAlpha)
                    call SetImagePosition(gridEffect[i],tx,ty,0)
                    set ix = ix + 1
                    if ix >= gridSize then
                        set ix = 0
                        set iy = iy + 1
                    endif
                    set i = i + 1
                endloop
            endif
        endif
    endfunction
 
    private function GetHexSignValue takes string sign returns integer
        if sign == "a" or sign == "A" then
            return 10
        endif
        if sign == "b" or sign == "B" then
            return 11
        endif
        if sign == "c" or sign == "C" then
            return 12
        endif
        if sign == "d" or sign == "D" then
            return 13
        endif
        if sign == "e" or sign == "E" then
            return 14
        endif
        if sign == "f" or sign == "F" then
            return 15
        endif
        return S2I(sign)
    endfunction

    private function SetGridProp takes nothing returns nothing
        local string s = GetEventPlayerChatString()
        local integer i = 0
        if SubString(s, 0, 4) == "-gc " and GetLocalPlayer() == GetTriggerPlayer() then
            set gridRed = GetHexSignValue(SubString(s, 4, 5)) * 16 + GetHexSignValue(SubString(s, 5, 6))
            set gridGreen = GetHexSignValue(SubString(s, 6, 7)) * 16 + GetHexSignValue(SubString(s, 7, 8))
            set gridBlue = GetHexSignValue(SubString(s, 8, 9)) * 16 + GetHexSignValue(SubString(s, 9, 10))
            if showHide then
                loop
                    exitwhen i >= gridSize * gridSize
                    call SetImageColor(gridEffect[i],gridRed,gridGreen,gridBlue,gridAlpha)
                    set i = i + 1
                endloop
            endif
            return
        endif
 
        if SubString(s, 0, 4) == "-gs " and GetLocalPlayer() == GetTriggerPlayer() then
            set gridSize = S2I(SubString(s, 4, 6))
            if gridSize < gridSizeMin then
                set gridSize = gridSizeMin
            endif
            if gridSize > gridSizeMax then
                set gridSize = gridSizeMax
            endif
            set showHide = true
            call ShowHideGrid()
        endif
    endfunction
 

    private function init takes nothing returns nothing
        local integer i = 0
        local trigger t = CreateTrigger()
        local trigger t2 = CreateTrigger()
        local trigger t3 = CreateTrigger()
        call TriggerAddAction(t, function ShowHideGrid)
        call TriggerAddAction(t2, function MouseMove)
        call TriggerAddAction(t3, function SetGridProp)
        loop
            exitwhen i > 23
            call BlzTriggerRegisterPlayerKeyEvent(t, Player(i),OSKEY_G,0,true)
            call TriggerRegisterPlayerEvent(t2, Player(i), EVENT_PLAYER_MOUSE_DOWN)
            call TriggerRegisterPlayerChatEvent( t3, Player(i), "", false )
            set i = i + 1
        endloop
        set showHide = false
        set waitForClick = false
        set i = 0
        loop
            exitwhen i >= gridSizeMax * gridSizeMax
            set gridEffect[i] = CreateImage("Gridplane.dds",128,128,0,0,0,0, 1,1,1, 1)
            call SetImageRenderAlways( gridEffect[i], true )
            call SetImageColor( gridEffect[i], 255, 255, 255, 0)
            set i = i + 1
        endloop
    endfunction

endlibrary
Lua:
if Debug then Debug.beginFile "PlayerGrid" end
OnInit.global("PlayerGrid", function(require)
    local BUTTON_KEY = OSKEY_G
    GridSystem = {}
    function GridSystem:init()
        self.gridEffect = {}
        self.showHide = false
        self.waitForClick = false
        self.gridRed = 200
        self.gridGreen = 200
        self.gridBlue = 200
        self.gridAlpha = 255
        self.gridSize = 8
        self.gridSizeMax = 32
        self.gridSizeMin = 4
        for i = 1, self.gridSizeMax * self.gridSizeMax do
            self.gridEffect[i] = CreateImage("Gridplane.dds", 128, 128, 0, 0, 0, 0, 1, 1, 1, 1)
            SetImageRenderAlways(self.gridEffect[i], true)
            SetImageColor(self.gridEffect[i], 255, 255, 255, 0)
        end
        local t = CreateTrigger()
        local t2 = CreateTrigger()
        local t3 = CreateTrigger()
        for i = 0, bj_MAX_PLAYER_SLOTS do
            BlzTriggerRegisterPlayerKeyEvent(t, Player(i), BUTTON_KEY, 0, true)
            TriggerRegisterPlayerEvent(t2, Player(i), EVENT_PLAYER_MOUSE_DOWN)
            TriggerRegisterPlayerChatEvent(t3, Player(i), "", false)
        end
        TriggerAddAction(t, function() self:ShowHideGrid() end)
        TriggerAddAction(t2, function() self:MouseMove() end)
        TriggerAddAction(t3, function() self:SetGridProp() end)
    end

    function GridSystem:ShowHideGrid()
        if GetLocalPlayer() ~= GetTriggerPlayer() then
            return
        end
        if self.showHide then
            self.showHide = false
            for i, image in ipairs(self.gridEffect) do
                SetImageColor(image, 255, 255, 255, 0)
            end
        else
            self.waitForClick = true
        end
    end

    function GridSystem:MouseMove()
        local x = math.floor(BlzGetTriggerPlayerMouseX() / 128) * 128
        local y = math.floor(BlzGetTriggerPlayerMouseY() / 128) * 128
        if GetLocalPlayer() ~= GetTriggerPlayer() or not self.waitForClick then return end
        self.waitForClick = false
        self.showHide = true
        local ix, iy = 0, 0
        for i, image in ipairs(self.gridEffect) do
            if i > self.gridSize * self.gridSize then break end
            local tx = x + ix * 128 - self.gridSize / 2 * 128
            local ty = y + iy * 128 - self.gridSize / 2 * 128
            SetImageColor(image, self.gridRed, self.gridGreen, self.gridBlue, self.gridAlpha)
            SetImagePosition(image, tx, ty, 0)
            ix = ix + 1
            if ix >= self.gridSize then
                ix = 0
                iy = iy + 1
            end
        end
    end

    function GridSystem:GetHexSignValue(sign)
        local hex = { ["a"] = 10, ["b"] = 11, ["c"] = 12, ["d"] = 13, ["e"] = 14, ["f"] = 15 }
        return hex[sign:lower()] or tonumber(sign)
    end

    function GridSystem:SetGridProp()
        local s = GetEventPlayerChatString()
        if GetLocalPlayer() ~= GetTriggerPlayer() then
            return
        end
        if s:sub(1, 4) == "-gc " then
            self.gridRed = self:GetHexSignValue(s:sub(5, 5)) * 16 + self:GetHexSignValue(s:sub(6, 6))
            self.gridGreen = self:GetHexSignValue(s:sub(7, 7)) * 16 + self:GetHexSignValue(s:sub(8, 8))
            self.gridBlue = self:GetHexSignValue(s:sub(9, 9)) * 16 + self:GetHexSignValue(s:sub(10, 10))
            if self.showHide then
                for i, image in ipairs(self.gridEffect) do
                    if i > self.gridSize * self.gridSize then break end
                    SetImageColor(image, self.gridRed, self.gridGreen, self.gridBlue, self.gridAlpha)
                end
            end
        elseif s:sub(1, 4) == "-gs " then
            self.gridSize = tonumber(s:sub(5, 7))
            self.gridSize = math.max(self.gridSizeMin, math.min(self.gridSize, self.gridSizeMax))
            self.showHide = true
            self:ShowHideGrid()
        end
    end

    GridSystem:init()
end)
if Debug then Debug.endFile() end
March 16, 2020 v1.00 Release - Jass Version.
March 11, 2024 v2.00 Update - Added Lua Version.
Contents

Grid System Template (Map)

AGD

AGD

Level 16
Joined
Mar 29, 2016
Messages
688
Cool. Maybe allow more than just showing/hiding the grid. Ex: a mode wherein single grids are shown on mouse hover and hidden again when not hovered.


exitwhen i > 23 -> exitwhen i == bj_MAX_PLAYER_SLOTS
Avoid inlining constants if possible. Think about all older resources that uses 12 as the player count. Guess what? They now need to edited everytime they are imported to newer maps. Not saying this will happen again, but it should be a lesson and its better to just observe the best practice in all areas.

You should move the show/hide hotkeys and chat commands into a configuration section at the top. They should not be hardcoded. I see a lot of hardcoding in fact - the default grid color, the individual grid size (128), etc.

Maybe you could also allow to change the size of the individual grid, in multiples of 128.

You could store the repeatedly used function such as GetTriggerPlayer() into local variables, while you could also set GetLocalPlayer() into a global variable at map init.
 

Wrda

Spell Reviewer
Level 26
Joined
Nov 18, 2012
Messages
1,887
Quick Lua review:
Lua:
function GridSystem:GetHexSignValue(sign)
Get rid of that and use ->
Lua:
---@param sign string|integer
---@return integer
local function hex2Dec(sign)
    return tonumber(tostring(sign), 16)
end

Would be nice to be able to configure the grid to 32, 64, 128, and even 256 size.
I agree that API which takes player for grid, to either show, hide, get position, change colour & alpha is better than having these events.
 
Top