Custom images on maps
#1
I noticed that you can change individual pixels on maps using SetPixel in cMap. I had an idea of creating a plugin (similar to Bukkit plugins) that downloads an image from the internet, resizes it to the size of a map, fetches the colors from the pixels in the image and places them on the map with SetPixel. However, I have no idea how to manipulate the image easily with Lua. Does anyone have any ideas?

(Map in this case is the map item that you can hold in your hand and place)

The code I have written so far. It currently only downloads the image:

Code:
Connect = {}

function Initialize(Plugin)
    Plugin:SetName(g_PluginInfo.Name)

    dofile(cPluginManager:GetPluginsPath() .. "/InfoReg.lua")
    RegisterPluginInfoCommands()

    LOG("Initialised " .. Plugin:GetName())
    return true
end

function HandleMapImageCommand(Split, Player)
    if Split[2] == nil then
        Player:SendMessageInfo("Usage: " .. Split[1] .. " <imageurl>")
    else
        local URL = Split[2]
        local FileName = URL:match("([^/]+)$")
        cUrlClient:Get(Split[2],
            function(Body, Data)
                local Image = io.open(FileName, "wb")
                Image:write(Body)
                Image:close()
                local Image = nil
            end
        )
    end
    return true
end

function OnDisable()
    LOG("Disabled " .. cPluginManager:GetCurrentPlugin():GetName() .. "!")
end
Reply
Thanks given by:
#2
You'll have to decode the image so you can read the individual image. There is a pure-Lua one for PNG's: https://github.com/DelusionalLogic/pngLua
Reply
Thanks given by:
#3
The code does this:
  • Add map to player inventory with damage value 1, if not existent
  • Call callback on the map
  • For loop with x,y = 1-  100, the size is by default 128 x 128
  • Set the pixel to cMap.E_BASE_COLOR_TRANSPARENT
Don't see any change, maybe a missing packet update to the player.

Add the code in a plugin and register the command:
cPluginManager:BindCommand("/testmap", "testmap", CommandTestMap, "testmap")

function CommandTestMap(a_Split, a_Player)
	if not(a_Player:GetInventory():HasItems(cItem(E_ITEM_MAP,1, 1))) then
	-- Add map
		a_Player:GetInventory():AddItem(cItem(E_ITEM_MAP, 1, 1))
	end

	a_Player:GetWorld():GetMapManager():DoWithMap(1, -- damage value = item id of map
	function(a_Map)
		a_Player:SendMessage("Callback called")
		for x = 1, 100 do
			for y = 1, 100 do
				a_Map:SetPixel(x, y, cMap.E_BASE_COLOR_TRANSPARENT)
			end
		end
	end)
	a_Player:SendMessage("Done!")
	return true
end
Reply
Thanks given by:
#4
@Mathias Does it work for you setting the pixel and you see it then?
Reply
Thanks given by: ErnestoSiz
#5
(05-18-2017, 11:32 PM)Seadragon91 Wrote: @Mathias Does it work for you setting the pixel and you see it then?

It seems like maps are not generated unless right-clicking an empty map. I can then modify the pixels, but the custom pixels are overwritten if you don't change the map scale.

This code works after the map has been generated by right-clicking an empty map:
Code:
    Player:GetWorld():GetMapManager():DoWithMap(0,
    function(Map)
        Map:SetPosition(99999999, 99999999)
        Map:SetScale(10)
        for x = 0, 128 do
            for y = 0, 128 do
                Map:SetPixel(x, y, cMap.E_BASE_COLOR_BLUE)
            end
        end
    end)
    return true
Reply
Thanks given by:
#6
It also seems to only work if a player is currently holding a map, im guessing map updates ore only sent to players who are holding a map.
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)