2019-02-09 11:42:02 +01:00
local event = require ' utils.event '
local math_random = math.random
local valid_entities = {
[ " rock-big " ] = true ,
[ " rock-huge " ] = true ,
2019-11-07 16:00:36 +01:00
[ " sand-rock-big " ] = true ,
[ " mineable-wreckage " ] = true
2019-02-09 11:42:02 +01:00
}
local rock_mining_chance_weights = {
{ " iron-ore " , 25 } ,
{ " copper-ore " , 18 } ,
2019-10-19 09:20:40 +02:00
{ " mixed " , 15 } ,
2019-02-09 11:42:02 +01:00
{ " coal " , 14 } ,
2019-10-18 07:01:14 +02:00
{ " stone " , 8 } ,
2019-02-09 11:42:02 +01:00
{ " uranium-ore " , 3 }
}
local ore_raffle = { }
for _ , t in pairs ( rock_mining_chance_weights ) do
for x = 1 , t [ 2 ] , 1 do
table.insert ( ore_raffle , t [ 1 ] )
end
end
2019-10-19 09:20:40 +02:00
local mixed_ores = { " iron-ore " , " copper-ore " , " stone " , " coal " }
2019-10-18 07:01:14 +02:00
2019-02-09 11:42:02 +01:00
local size_raffle = {
2019-10-20 06:07:07 +02:00
{ " giant " , 65 , 96 } ,
2019-10-19 09:20:40 +02:00
{ " huge " , 33 , 64 } ,
2019-02-09 11:42:02 +01:00
{ " big " , 17 , 32 } ,
2019-10-19 09:20:40 +02:00
{ " smol " , 9 , 16 } ,
{ " tiny " , 4 , 8 } ,
2019-02-09 11:42:02 +01:00
}
local ore_prints = {
2019-10-30 17:29:15 +01:00
[ " coal " ] = { " dark " , " coal " , " [img=entity/coal] " } ,
[ " iron-ore " ] = { " shiny " , " iron " , " [img=entity/iron-ore] " } ,
[ " copper-ore " ] = { " glimmering " , " copper " , " [img=entity/copper-ore] " } ,
[ " uranium-ore " ] = { " glowing " , " uranium " , " [img=entity/uranium-ore] " } ,
[ " stone " ] = { " solid " , " stone " , " [img=entity/stone] " } ,
[ " mixed " ] = { " glitter " , " mixed ore " , " " } ,
2019-02-09 11:42:02 +01:00
}
2019-10-18 07:01:14 +02:00
local function get_amount ( position )
local distance_to_center = math.sqrt ( position.x ^ 2 + position.y ^ 2 ) * 4 + 1500
local m = ( 75 + math_random ( 0 , 50 ) ) * 0.01
return distance_to_center * m
end
local function draw_chain ( surface , count , ore , ore_entities , ore_positions )
local vectors = { { 0 , - 1 } , { - 1 , 0 } , { 1 , 0 } , { 0 , 1 } }
local r = math_random ( 1 , # ore_entities )
local position = { x = ore_entities [ r ] . position.x , y = ore_entities [ r ] . position.y }
for _ = 1 , count , 1 do
table.shuffle_table ( vectors )
for i = 1 , 4 , 1 do
local p = { x = position.x + vectors [ i ] [ 1 ] , y = position.y + vectors [ i ] [ 2 ] }
if surface.can_place_entity ( { name = " coal " , position = p , amount = 1 } ) then
if not ore_positions [ p.x .. " _ " .. p.y ] then
position.x = p.x
position.y = p.y
ore_positions [ p.x .. " _ " .. p.y ] = true
local name = ore
2019-10-19 09:20:40 +02:00
if ore == " mixed " then name = mixed_ores [ math_random ( 1 , # mixed_ores ) ] end
2019-10-18 07:01:14 +02:00
ore_entities [ # ore_entities + 1 ] = { name = name , position = p , amount = get_amount ( position ) }
break
2019-02-09 11:42:02 +01:00
end
end
2019-10-18 07:01:14 +02:00
end
2019-02-09 11:42:02 +01:00
end
end
2019-10-18 07:01:14 +02:00
local function ore_vein ( event )
local surface = event.entity . surface
local size = size_raffle [ math_random ( 1 , # size_raffle ) ]
local ore = ore_raffle [ math_random ( 1 , # ore_raffle ) ]
local player = game.players [ event.player_index ]
for _ , p in pairs ( game.connected_players ) do
if p.index == player.index then
2019-10-30 17:29:15 +01:00
p.print ( " You notice something " .. ore_prints [ ore ] [ 1 ] .. " underneath the rubble. It's a " .. size [ 1 ] .. " vein of " .. ore_prints [ ore ] [ 2 ] .. " !! " .. ore_prints [ ore ] [ 3 ] , { r = 0.80 , g = 0.80 , b = 0.80 } )
2019-10-18 07:01:14 +02:00
else
2019-10-30 17:29:15 +01:00
game.print (
" [color= " .. player.chat_color . r .. " , " .. player.chat_color . g .. " , " .. player.chat_color . b .. " ] " .. player.name
.. " [/color] found a " .. size [ 1 ] .. " vein of " .. ore_prints [ ore ] [ 2 ] .. " ! " .. ore_prints [ ore ] [ 3 ] , { r = 0.80 , g = 0.80 , b = 0.80 } )
2019-10-18 07:01:14 +02:00
end
end
local ore_entities = { { name = ore , position = { x = event.entity . position.x , y = event.entity . position.y } , amount = get_amount ( event.entity . position ) } }
2019-10-19 09:20:40 +02:00
if ore == " mixed " then
ore_entities = { { name = mixed_ores [ math_random ( 1 , # mixed_ores ) ] , position = { x = event.entity . position.x , y = event.entity . position.y } , amount = get_amount ( event.entity . position ) } }
2019-10-18 07:01:14 +02:00
end
local ore_positions = { [ event.entity . position.x .. " _ " .. event.entity . position.y ] = true }
local count = math_random ( size [ 2 ] , size [ 3 ] )
for _ = 1 , 128 , 1 do
local c = math_random ( math.floor ( size [ 2 ] * 0.25 ) + 1 , size [ 2 ] )
if count < c then c = count end
local placed_ore_count = # ore_entities
draw_chain ( surface , c , ore , ore_entities , ore_positions )
count = count - ( # ore_entities - placed_ore_count )
if count <= 0 then break end
end
for _ , e in pairs ( ore_entities ) do surface.create_entity ( e ) end
end
local function on_player_mined_entity ( event )
if not event.entity . valid then return end
if not valid_entities [ event.entity . name ] then return end
2019-10-20 06:07:07 +02:00
if math_random ( 1 , 768 ) ~= 1 then return end
2019-10-18 07:01:14 +02:00
ore_vein ( event )
end
2019-02-09 11:42:02 +01:00
event.add ( defines.events . on_player_mined_entity , on_player_mined_entity )