Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

'Infinite mining' and preventing overlapping?

Asked by
Dummiez 360 Moderation Voter
10 years ago

Alright, so I decided to work on a pickaxe that basically generates new blocks when the previous block is destroyed, creating an 'infinite mine' effect. If you ever heard of, or played the game Epic Mining 2 by Piedude777, his is able to detect whether that area has already been mined.

-- This is a portion of the script that handles generation of dirt.

function onMouseButton1Down(mouse)

-- Target, Surface selection and storage (shortened).

local target = mouse.Target
local surface = mouse.TargetSurface
local dirt = game.ReplicatedStorage.Dirt

local a,b,c,d,e = dirt:Clone(), dirt:Clone(), dirt:Clone(), dirt:Clone(), dirt:Clone() -- such mess
a.Parent, b.Parent, c.Parent, d.Parent, e.Parent = Workspace, Workspace, Workspace, Workspace, Workspace

-- Checking mouse target and generate according to where they clicked.

if surface == Enum.NormalId.Left then print("Left")
    a.CFrame = CFrame.new(target.CFrame.x + 8,target.CFrame.y,target.CFrame.z)
    b.CFrame = CFrame.new(target.CFrame.x,target.CFrame.y + 8,target.CFrame.z)
    c.CFrame = CFrame.new(target.CFrame.x,target.CFrame.y - 8,target.CFrame.z)
    d.CFrame = CFrame.new(target.CFrame.x,target.CFrame.y,target.CFrame.z + 8)
    e.CFrame = CFrame.new(target.CFrame.x,target.CFrame.y,target.CFrame.z - 8)
elseif surface == Enum.NormalId.Top then print("Top")
    a.CFrame = CFrame.new(target.CFrame.x + 8,target.CFrame.y,target.CFrame.z)
    b.CFrame = CFrame.new(target.CFrame.x - 8,target.CFrame.y,target.CFrame.z)
    c.CFrame = CFrame.new(target.CFrame.x,target.CFrame.y - 8,target.CFrame.z)
    d.CFrame = CFrame.new(target.CFrame.x,target.CFrame.y,target.CFrame.z + 8)
    e.CFrame = CFrame.new(target.CFrame.x,target.CFrame.y,target.CFrame.z - 8)      
elseif surface == Enum.NormalId.Bottom then print("Bottom")

--- And you get the rest.. (Bottom,  Right,  Back,  Front)

end
game:GetService("Debris"):AddItem(target, 0)

end

I'm trying to get that same effect in this, any ideas on how I could do that? (Basically not generating new blocks over areas where the block has been mined.)

1 answer

Log in to vote
2
Answered by 10 years ago

in Workspace you need a RBXAPI/RBXAPIServer/OnMined

ServerScriptService you need:

local rbxapi    = require(workspace.RBXAPI)
local storage   = game:GetService("ServerStorage")
local minerals  = {}
local mineral_count = 0
local mineral_probability = {
    {"Grass"},
    {"Dirt"},
    {"Dirt", "Dirt", "Dirt", "Coal", "Dirt", "Sand", "Dirt", "Dirt", "Dirt", "Stone", "Stone"},
    {"Dirt", "Coal", "Dirt", "Dirt", "Dirt", "Stone", "Stone"},
    {"Coal", "Dirt", "Dirt", "Copper", "Dirt", "Stone", "Stone", "Stone"},
    {"Gold", "Iron", "Iron", "Stone", "Stone", "Coal", "Coal", "Dirt", "Dirt", "Sand", "Copper", "Stone", "Stone"},
    {"Gold", "Iron", "Iron", "Stone", "Stone", "Coal", "Coal", "Dirt", "Dirt", "Sand", "Copper", "Stone", "Stone", "Gold", "Ruby"},
    {"Gold", "Iron", "Iron", "Stone", "Stone", "Coal", "Coal", "Dirt", "Dirt", "Sand", "Copper", "Stone", "Stone", "Emerald", "Gold", "Gold", "Ruby"},
    {"Gold", "Iron", "Gold", "Stone", "Coal", "Coal", "Stone", "Copper", "Copper", "Emerald", "Gold", "Ruby", "Ruby", "Ruby", "Emerald", "Stone", "Stone", "Stone", "Stone"},
    {"Gold", "Iron", "Gold", "Stone", "Coal", "Coal", "Stone", "Copper", "Copper", "Emerald", "Gold", "Ruby", "Ruby", "Ruby", "Emerald", "Stone", "Stone", "Stone", "Stone"},
    {"Gold", "Iron", "Gold", "Stone", "Coal", "Coal", "Stone", "Copper", "Copper", "Emerald", "Gold", "Ruby", "Ruby", "Ruby", "Emerald", "Stone", "Stone", "Stone", "Stone"},
    {"Gold", "Gold", "Iron", "Iron", "Robloxium", "Copper", "Copper", "Emerald", "Emerald", "Emerald", "Ruby", "Ruby", "Stone", "Stone", "Stone", "Stone", "Dirt", "Stone", "Stone"},
    {"Gold", "Gold", "Iron", "Iron", "Robloxium", "Copper", "Copper", "Emerald", "Emerald", "Emerald", "Ruby", "Ruby", "Stone", "Stone", "Stone", "Stone", "Dirt", "Stone", "Stone"},
    {"Gold", "Gold", "Iron", "Iron", "Robloxium", "Copper", "Copper", "Emerald", "Emerald", "Emerald", "Ruby", "Ruby", "Stone", "Stone", "Stone", "Stone", "Dirt", "Stone", "Stone"},
    {"Gold", "Gold", "Iron", "Iron", "Robloxium", "Copper", "Copper", "Emerald", "Emerald", "Emerald", "Ruby", "Ruby", "Stone", "Stone", "Stone", "Stone", "Dirt", "Stone", "Stone"},
    {"Gold", "Gold", "Iron", "Iron", "Robloxium", "Copper", "Copper", "Emerald", "Emerald", "Emerald", "Ruby", "Ruby", "Stone", "Stone", "Stone", "Stone", "Dirt", "Stone", "Stone"},
    {"Gold", "Gold", "Iron", "Iron", "Robloxium", "Copper", "Copper", "Emerald", "Emerald", "Emerald", "Ruby", "Ruby", "Stone", "Stone", "Stone", "Stone", "Dirt", "Stone", "Stone"},
    {"Gold", "Gold", "Iron", "Iron", "Robloxium", "Copper", "Copper", "Emerald", "Emerald", "Emerald", "Ruby", "Ruby", "Stone", "Stone", "Stone", "Stone", "Dirt", "Stone", "Stone"},
    {"Gold", "Gold", "Iron", "Iron", "Robloxium", "Copper", "Copper", "Emerald", "Emerald", "Emerald", "Ruby", "Ruby", "Stone", "Stone", "Stone", "Stone", "Dirt", "Stone", "Stone"},
    {"Robloxium", "Robloxium", "Robloxium", "Copper", "Gold", "Gold", "Iron", "Iron", "Iron", "Ruby", "Ruby", "Emerald"},
    {"Robloxium", "Robloxium", "Robloxium", "Copper", "Gold", "Gold", "Iron", "Iron", "Iron", "Ruby", "Ruby", "Emerald"},
    {"Robloxium", "Robloxium", "Robloxium", "Copper", "Gold", "Gold", "Iron", "Iron", "Iron", "Ruby", "Ruby", "Emerald"},
    {"Robloxium", "Robloxium", "Robloxium", "Copper", "Gold", "Gold", "Iron", "Iron", "Iron", "Ruby", "Ruby", "Emerald"},
    {"Robloxium", "Robloxium", "Robloxium", "Copper", "Gold", "Gold", "Iron", "Iron", "Iron", "Ruby", "Ruby", "Emerald"},
    {"Robloxium", "Robloxium", "Robloxium", "Copper", "Gold", "Gold", "Iron", "Iron", "Iron", "Ruby", "Ruby", "Emerald"},
    {"Robloxium", "Robloxium", "Robloxium", "Copper", "Gold", "Gold", "Iron", "Iron", "Iron", "Ruby", "Ruby", "Emerald"},
    {"Robloxium", "Robloxium", "Robloxium", "Copper", "Gold", "Gold", "Iron", "Iron", "Iron", "Ruby", "Ruby", "Emerald"},
    {"Robloxium", "Robloxium", "Robloxium", "Copper", "Gold", "Gold", "Iron", "Iron", "Iron", "Ruby", "Ruby", "Emerald"},
    {"Robloxium", "Robloxium", "Robloxium", "Copper", "Gold", "Gold", "Iron", "Iron", "Iron", "Ruby", "Ruby", "Emerald"},
    {"Robloxium", "Robloxium", "Robloxium", "Copper", "Gold", "Gold", "Iron", "Iron", "Iron", "Ruby", "Ruby", "Emerald"},
    {"Marble"}
}
local world = {}
local height
local size = {64, 64}
local blockSize = 8
local app = rbxapi.new("037af93efe04aaf6287fea99ae1c8d5ee0ca1b0c7292641445cd1a4360d49cc9")
local OnMinedFunction = workspace.OnMined
local RBXAPIService = workspace.RBXAPIServer

function OnMinedFunction.OnServerInvoke(player, layerData)
    local x = layerData.x
    local y = layerData.y
    local layerID = layerData.z

    Instantiate(world[layerID + 1], y, x, layerID + 1)
    Instantiate(world[layerID - 1], y, x, layerID - 1)

    Instantiate(world[layerID + 1], y + 1, x, layerID + 1)
    Instantiate(world[layerID + 1], y, x + 1, layerID + 1)
    Instantiate(world[layerID + 1], y - 1, x, layerID + 1)
    Instantiate(world[layerID + 1], y, x - 1, layerID + 1)

    Instantiate(world[layerID], y + 1, x, layerID)
    Instantiate(world[layerID], y, x + 1, layerID)
    Instantiate(world[layerID], y - 1, x, layerID)
    Instantiate(world[layerID], y, x - 1, layerID)

    Instantiate(world[layerID - 1], y + 1, x, layerID - 1)
    Instantiate(world[layerID - 1], y, x + 1, layerID - 1)
    Instantiate(world[layerID - 1], y - 1, x, layerID - 1)
    Instantiate(world[layerID - 1], y, x - 1, layerID - 1)
end

function RBXAPIService.OnServerInvoke(player, method, key, value)
    if method == "set" then
        app:Set(player.Name .. " " .. key, value)
    elseif method == "get" then
        RBXAPIService:InvokeClient(player, key, app:Get(player.Name .. " " .. key).output)
    end
end

function GetMineral(name)
    return minerals[name] ~= nil and minerals[name] or nil
end

function GenerateLayer(y, width, depth)
    local prob  

    print(y)

    if y > #mineral_probability then 
        prob = mineral_probability[#mineral_probability]
    elseif y < 1 then
        return nil
    else
        prob = mineral_probability[y]
    end

    local layer = {}

    for y = 1, depth do
        layer[y] = {}
        for x = 1, width do
            -- type, HasMined, HasInstanced
            layer[y][x] = {prob[math.random(1, #prob)], false, false}
        end
    end

    return layer
end

function Instantiate(layer, y, x, i)
    if layer == nil then
        layer = GenerateLayer(i, size[1], size[2])

        if layer == nil then return end     
        table.insert(world, layer)
    end

    if layer[y] == nil then layer[y] = {} end

    if layer[y][x] == nil then
        local prob

        if i > #mineral_probability then
            prob = mineral_probability[#mineral_probability]
        else
            prob = mineral_probability[i]
        end     

        layer[y][x] = {prob[math.random(1, #prob)], false, false}
    end

    if layer[y][x][3] then return end

    local p = GetMineral(layer[y][x][1]):clone()
    p.Parent = workspace.Minespace
    p.Size = Vector3.new(blockSize, blockSize, blockSize)
    p.CFrame = CFrame.new(Vector3.new(x * blockSize, height - (i * blockSize), y * blockSize))

    local props = Instance.new("Vector3Value")
    props.Value = Vector3.new(x, y, i)
    props.Name  = "Layer Data"
    props.Parent = p

    layer[y][x][3] = true
end

function Load()
    game:GetService("StarterGui").ResetPlayerGuiOnSpawn = false 

    local objects = storage:GetChildren()

    for i, v in ipairs(objects) do
        if v.Name == "Mineral" and v:IsA("Part") and v:FindFirstChild("Type") ~= nil then
            minerals[v.Type.Value] = v
            mineral_count = mineral_count + 1
        end
    end

    height = #mineral_probability * blockSize

    local spawn = Instance.new("SpawnLocation", workspace)
    spawn.CanCollide = false
    spawn.Anchored = true
    spawn.Transparency = 1
    spawn.Position = Vector3.new(size[1] * blockSize / 2, height + 20, size[2] * blockSize / 2)

    for i = 1, #mineral_probability do
        local layer = GenerateLayer(i, size[1], size[2])

        if i == 1 then
            for y = 1, #layer do
                for x = 1, #layer[y] do
                    Instantiate(layer, y, x, i)
                end
            end
        end     

        table.insert(world, layer)
    end

    print("Loaded")
end

Load()

In ServerStorage you need:

12 Bricks with a Type value in them. Named Mineral

In sound Service you need A bunch of sounds revlant to the script.

For screen GUi there is the model in my models I will have.

And that is the whole thing for a mining game.

Ad

Answer this question