This commit is contained in:
Paul 2021-07-25 17:33:55 +02:00
commit 43086ea1eb
553 changed files with 20250 additions and 0 deletions

View file

@ -0,0 +1,364 @@
--local gr_gui = require('scripts/gravitation/gui')
--===================================================================================================================
--TODO
--===================================================================================================================
--init global
--when builded register
--when destroyed unregister
--build gui when base opend
--start button
--spawn, register energy
--check nth tick for stabilizer and other
--calc output, stable
--write in global
--output power, clear inv
--===================================================================================================================
--init
--===================================================================================================================
function gr_black_hole_init()
global.black_hole = {}
global.black_hole.base = {}
global.black_hole.energy = {}
global.black_hole.dirty = false
global.black_hole.counter = 0
global.black_hole.gui = {}
global.black_hole.gui.frame = nil
global.black_hole.gui.id = nil
end
--===================================================================================================================
--on built
--===================================================================================================================
function gr_black_hole_on_built(e)
if e['entity'] then
if e['entity'].name == "gr_black_hole_base_entity" then
make_black_hole(e['entity'])
--game.print("make")
end
end
if e['created_entity'] then
if e['created_entity'].name == "gr_black_hole_base_entity" then
make_black_hole(e['created_entity'])
end
end
end
--===================================================================================================================
--on remove
--===================================================================================================================
function gr_black_hole_on_remove(e)
if e["entity"] then
if e["entity"].name == "gr_black_hole_base_entity" then
if e["player_index"] then
destroy_black_hole(e["entity"],e["player_index"],nil)
elseif e["robot"] then
destroy_black_hole(e["entity"],nil,e["robot"])
else
destroy_black_hole(e["entity"],nil,nil)
end
end
end
end
--===================================================================================================================
--make
--===================================================================================================================
function make_black_hole(entity)
register_black_hole_base(entity)
global.black_hole.dirty = true
--gr_gui.update_main()
end
function make_black_hole_energy(base_entity)
local slave = create_black_hole_energy(base_entity)
register_black_hole_energy(slave)
link_base_energy(base_entity,slave)
--game.print("from make: "..slave.unit_number)
global.black_hole.dirty = true
--gr_gui.update_main()
end
--===================================================================================================================
--destoy
--===================================================================================================================
function destroy_black_hole(entity,player,robot)
local slave = nil
if global.black_hole.base then
if global.black_hole.base[entity.unit_number] then
if global.black_hole.base[entity.unit_number].energy then
if global.black_hole.energy[global.black_hole.base[entity.unit_number].energy] then
if global.black_hole.energy[global.black_hole.base[entity.unit_number].energy].entity then
slave = global.black_hole.energy[global.black_hole.base[entity.unit_number].energy].entity
end
end
end
unregister_black_hole(entity)
if slave then
destroy_energy(slave)
end
end
end
global.black_hole.dirty = true
--gr_gui.update_main()
end
--===================================================================================================================
--register
--===================================================================================================================
function register_black_hole_base(entity)
local unit = entity.unit_number
global.black_hole.base[unit] = {}
global.black_hole.base[unit].entity = entity
global.black_hole.base[unit].energy = nil
global.black_hole.base[unit].active = false
--game.print("register base: "..unit)
end
function register_black_hole_energy(entity)
local unit = entity.unit_number
global.black_hole.energy[unit] = {}
global.black_hole.energy[unit].entity = entity
global.black_hole.energy[unit].base = nil
global.black_hole.energy[unit].total = 0
global.black_hole.energy[unit].stabilizers = 100
global.black_hole.energy[unit].power_gen = 0
global.black_hole.energy[unit].matter_consumption = 0
global.black_hole.energy[unit].stable = 0
--game.print("from erngy make: "..unit)
end
function link_base_energy(base_entity,energy)
local unit_base = base_entity.unit_number
local unit_energy = energy.unit_number
if global.black_hole.base[unit_base] then
if global.black_hole.energy[unit_energy] then
global.black_hole.base[unit_base].energy = unit_energy
global.black_hole.energy[unit_energy].base = unit_base
global.black_hole.base[unit_base].active = true
end
end
end
--===================================================================================================================
--unregister
--===================================================================================================================
function unregister_black_hole(entity)
local unit = entity.unit_number
if global.black_hole.base[unit].energy then
--game.print("unregister base.energy: "..global.black_hole.base[unit].energy)
unregister_black_hole_energy(nil,global.black_hole.base[unit].energy)
unregister_black_hole_base(entity,nil)
else
--game.print("2")
unregister_black_hole_base(entity,nil)
end
end
function unregister_black_hole_base(entity,unitin)
local unit = nil
if entity then
unit = entity.unit_number
else
unit = unitin
end
global.black_hole.base[unit] = nil
--game.print(unit)
if global.black_hole.energy then
for i,v in pairs(global.black_hole.energy) do
if global.black_hole.energy[i].base == unit then
global.black_hole.energy[i].base = nil
end
end
end
end
function unregister_black_hole_energy(entity,unitin)
local unit = nil
if entity then
unit = entity.unit_number
--game.print("entity")
else
unit = unitin
--game.print("unitin")
end
global.black_hole.energy[unit] = nil
if global.black_hole.base then
for i,v in pairs(global.black_hole.base) do
if global.black_hole.base[i].energy == unit then
global.black_hole.base[i].energy = nil
global.black_hole.base[i].active = false
end
end
end
end
--===================================================================================================================
--slaves
--===================================================================================================================
function create_black_hole_energy(base_entity)
local pos = base_entity.position
local slave = base_entity.surface.create_entity{
name = 'gr_black_hole_energy_entity',
position = pos,
force = base_entity.force
}
slave.destructible = false
return slave
end
function destroy_energy(entity)
entity.destroy()
end
--===================================================================================================================
--update
--===================================================================================================================
function black_hole_base_update()
if global.black_hole then
if global.black_hole.base then
for i,v in pairs(global.black_hole.base) do
if global.black_hole.base[i].entity then
if global.black_hole.base[i].entity.valid then
if global.black_hole.base[i].energy then
local energy = global.black_hole.base[i].energy
local entity = global.black_hole.base[i].entity
local inv = entity.get_inventory(defines.inventory.chest)
local total = global.black_hole.energy[energy].total
local stable = global.black_hole.energy[energy].stable
local power_gen, matter_consumption = calc_black_hole_stats(stable)
local new_matter = inv.get_item_count() - inv.get_item_count("gr_materials_stabilizer_item")
local new_stabilizer = inv.get_item_count("gr_materials_stabilizer_item")
stable = stable - 1
if total then
if stabilizer then
stable = stable + new_stabilizer
end
end
if stable >= 100 then
stable = 100
end
total = total - matter_consumption + new_matter
global.black_hole.energy[energy].power_gen = power_gen
global.black_hole.energy[energy].matter_consumption = matter_consumption
global.black_hole.energy[energy].stable = stable
global.black_hole.energy[energy].total = total
--game.print(stable.." "..power_gen.." "..total)
black_hole_make_energy(energy, power_gen)
inv.clear()
if (total <= 0) or (stable <= 0) then
global.black_hole.counter = global.black_hole.counter + 1
if global.black_hole.counter == 10 then
local energy_entity = global.black_hole.energy[energy].entity
unregister_black_hole_energy(energy_entity)
energy_entity.destroy()
global.black_hole.counter = 0
end
end
global.black_hole.dirty = true
end
end
end
end
end
end
end
--===================================================================================================================
--util
--===================================================================================================================
function can_make_black_hole_energy(entity)
if entity.valid then
inv = entity.get_inventory(defines.inventory.chest)
total = inv.get_item_count() - inv.get_item_count("gr_materials_stabilizer_item")
stabilizer = inv.get_item_count("gr_materials_stabilizer_item")
if (stabilizer >= 100) and (total >= 1000) then
return true
else
return false
end
end
end
function calc_black_hole_stats(stable)
-- power_gen: Power gen/s in MW
-- matter_consumption: matter consumed/s
-- stable: max 100, each stabilizer put in will add 1 stable regardless
-- every 10 ticks - 1
local matter_consumption_rate = black_hole_stable_function(stable)
local power_gen = black_hole_energy_function(matter_consumption_rate)
return power_gen, matter_consumption_rate
end
function black_hole_stable_function(stable)
--stable from 0 to 100
if stable <= 0 then
return 0
end
return stable
end
function black_hole_energy_function(matter_consumption_rate)
if matter_consumption_rate <= 0 then
return 0
end
return (matter_consumption_rate * matter_consumption_rate * matter_consumption_rate) * 64 * 1000 * 0.3
end
function black_hole_make_energy(energy, power_gen)
if global.black_hole.energy[energy].entity then
if global.black_hole.energy[energy].entity.valid then
--game.print("spawn energy: "..power_gen.."W")
local entity = global.black_hole.energy[energy].entity
entity.power_production = power_gen
end
end
end
--TODO
--k2 fix
--KI tech description

View file

@ -0,0 +1,36 @@
function gr_make_white_hole_recipe(item)
local blank = {
name = 'blank',
type = 'recipe',
enabled = 'true',
hidden = 'true',
allow_as_intermediate = 'false',
category = 'gr_white_hole_category',
ingredients = {
{'blank',1}
},
result = 'blank',
result_count = 2,
energy_required = 10,
}
blank["name"] = "gr_white_hole_cycle_"..item.."_recipe"
blank["ingredients"] = {
{item,1}
}
blank["result"] = item
--table.insert(data.raw.recipe, base)
data:extend({blank})
end
function gr_white_hole_recipe_generator()
local items = data.raw.item
for i,v in pairs(items) do
if data.raw.item[i].stack_size > 1 then
gr_make_white_hole_recipe(data.raw.item[i].name)
end
end
end
gr_white_hole_recipe_generator()

305
scripts/gravitation/gui.lua Normal file
View file

@ -0,0 +1,305 @@
require('scripts/gravitation/gr_black_hole_script')
local mod_gui = require("mod-gui")
local gr_gui = {}
function gr_gui.add_black_hole_gui(e,update)
local player = nil
local id = nil
local active = true
local total = nil
local stabilizer = nil
local power_gen = 0
local matter_consumption = 0
local stable = 0
local totalmatter = 0
if update then
player = e
id = global.black_hole.gui.id
if global.black_hole.base[id] then
active = global.black_hole.base[id].active
local entity = global.black_hole.base[id].entity
local inv = entity.get_inventory(defines.inventory.chest)
total = inv.get_item_count() - inv.get_item_count("gr_materials_stabilizer_item")
stabilizer = inv.get_item_count("gr_materials_stabilizer_item")
if global.black_hole.base[id].energy then
local energy = global.black_hole.base[id].energy
power_gen = global.black_hole.energy[energy].power_gen
matter_consumption = global.black_hole.energy[energy].matter_consumption
stable = global.black_hole.energy[energy].stable
totalmatter = global.black_hole.energy[energy].total
end
end
else
player = game.get_player(e["player_index"])
id = e["entity"].unit_number
if global.black_hole.base[id] then
active = global.black_hole.base[id].active
local entity = global.black_hole.base[id].entity
local inv = entity.get_inventory(defines.inventory.chest)
total = inv.get_item_count() - inv.get_item_count("gr_materials_stabilizer_item")
stabilizer = inv.get_item_count("gr_materials_stabilizer_item")
if global.black_hole.base[id].energy then
local energy = global.black_hole.base[id].energy
power_gen = global.black_hole.energy[energy].power_gen
matter_consumption = global.black_hole.energy[energy].matter_consumption
stable = global.black_hole.energy[energy].stable
totalmatter = global.black_hole.energy[energy].total
end
end
end
--====================================
--destroy and clear
--====================================
if player.gui.left["main248kblackholeframe"] then
player.gui.left["main248kblackholeframe"].destroy()
global.black_hole.gui = {}
return
end
if global.black_hole then
if global.black_hole.base then
if not global.black_hole.base[id] then
if player.gui.left["main248kblackholeframe"] then
player.gui.left["main248kblackholeframe"].destroy()
global.black_hole.gui = {}
return
else
return
end
end
end
end
local main248kblackholeframe = player.gui.left.add({
type = "frame",
name = "main248kblackholeframe",
caption = "Black hole generator: "
})
main248kblackholeframe.style.minimal_height = 10
main248kblackholeframe.style.minimal_width = 10
main248kblackholeframe.style.maximal_width = 320
local main248kblackholecontentframe = main248kblackholeframe.add({
type = "frame",
name = "main248kblackholecontentframe",
direction = "vertical",
style = "inside_shallow_frame_with_padding"
})
local main248kblackholeonframe = main248kblackholecontentframe.add({
type = "frame",
name = "main248kblackholeonframe",
caption = "Status ",
direction = "horizontal",
style = "bordered_frame"
})
--local main248kblackholeontextfield = main248kblackholeonframe.add({
-- type = "textfield",
-- name = "main248kblackholeontextfield",
-- numeric = "true",
-- text = channel
--})
--main248kblackholeontextfield.style.maximal_width = 30
local main248kblackholeonbutton = nil
if active == false then
main248kblackholeonbutton = main248kblackholeonframe.add({
type = "button",
name = "main248kblackholeonbutton",
caption = "Start",
style = "confirm_button"
})
else
main248kblackholeonbutton = main248kblackholeonframe.add({
type = "button",
name = "main248kblackholeonbutton",
caption = "Stop",
style = "red_confirm_button"
})
end
local main248kblackholestabilizerframe = main248kblackholecontentframe.add({
type = "frame",
name = "main248kblackholestabilizerframe",
caption = "Core stabilizers: ",
direction = "horizontal",
style = "bordered_frame"
})
local main248kblackholestabilizerbar = main248kblackholestabilizerframe.add({
type = "progressbar",
name = "main248kblackholestatstabilizerbar",
--caption = "Core stabilizers: ",
value = stabilizer/100,
direction = "horizontal",
style = "electric_satisfaction_in_description_progressbar"
})
local main248kblackholetotalframe = main248kblackholecontentframe.add({
type = "frame",
name = "main248kblackholetotalframe",
caption = "Consumable matter: ",
direction = "horizontal",
style = "bordered_frame"
})
local main248kblackholetotalbar = nil
if active == false then
main248kblackholetotalbar = main248kblackholetotalframe.add({
type = "progressbar",
name = "main248kblackholetotalbar",
--caption = "Consumable matter: ",
value = total/1000,
direction = "horizontal",
style = "electric_satisfaction_in_description_progressbar"
})
else
main248kblackholetotalbar = main248kblackholetotalframe.add({
type = "progressbar",
name = "main248kblackholetotalbar",
--caption = "Consumable matter: ",
value = totalmatter/10000,
direction = "horizontal",
style = "electric_satisfaction_in_description_progressbar"
})
end
local main248kblackholestableframe = main248kblackholecontentframe.add({
type = "frame",
name = "main248kblackholestableframe",
caption = "Reactor stability: ",
direction = "horizontal",
style = "bordered_frame"
})
local main248kblackholestablebar = main248kblackholestableframe.add({
type = "progressbar",
name = "main248kblackholestablebar",
--caption = "Consumable matter: ",
value = stable/100,
direction = "horizontal",
style = "electric_satisfaction_statistics_progressbar"
})
local main248kblackholeconsumptionframe = main248kblackholecontentframe.add({
type = "frame",
name = "main248kblackholeconsumptionframe",
caption = "Matter consumption: ",
direction = "horizontal",
style = "bordered_frame"
})
local main248kblackholeconsumptionbar = main248kblackholeconsumptionframe.add({
type = "progressbar",
name = "main248kblackholeconsumptionbar",
--caption = "Consumable matter: ",
value = matter_consumption/100,
direction = "horizontal",
style = "production_progressbar"
})
local main248kblackholepowerframe = main248kblackholecontentframe.add({
type = "frame",
name = "main248kblackholepowerframe",
caption = "Power gen. rate: ",
direction = "horizontal",
style = "bordered_frame"
})
local main248kblackholepowerbar = main248kblackholepowerframe.add({
type = "progressbar",
name = "main248kblackholepowerbar",
--caption = "Consumable matter: ",
value = power_gen/19000000000,
direction = "horizontal",
style = "production_progressbar"
})
global.black_hole.gui.frame = main248kblackholeframe
global.black_hole.gui.id = id
global.black_hole.gui.active = active
end
--=================================================================================
--on click
--=================================================================================
function gr_gui.on_change(e)
local player = game.get_player(e["player_index"])
if e["element"] then
if e["element"].valid then
local element = e["element"].name
if element == "main248kblackholeonbutton" then
if global.black_hole then
if global.black_hole.gui then
if not global.black_hole.gui.active then
if global.black_hole.gui.id then
local id = global.black_hole.gui.id
if global.black_hole.base then
if global.black_hole.base[id] then
if global.black_hole.base[id].entity then
if can_make_black_hole_energy(global.black_hole.base[id].entity) then
make_black_hole_energy(global.black_hole.base[id].entity)
gr_gui.update_main()
return
end
end
end
end
end
end
if global.black_hole.gui.active then
if global.black_hole.gui.id then
local id = global.black_hole.gui.id
if global.black_hole.base then
if global.black_hole.base[id] then
if global.black_hole.base[id].entity then
if global.black_hole.base[id].active then
if global.black_hole.base[id].energy then
local energy_unit = global.black_hole.base[id].energy
local energy_entity = global.black_hole.energy[energy_unit].entity
unregister_black_hole_energy(energy_entity)
energy_entity.destroy()
global.black_hole.counter = 0
gr_gui.update_main()
return
end
end
end
end
end
end
end
end
end
end
end
end
end
--=================================================================================
--update
--=================================================================================
function gr_gui.update_main()
for i,v in pairs(game.players) do
if game.players[i].gui.left["main248kblackholeframe"] then
game.players[i].gui.left["main248kblackholeframe"].destroy()
gr_gui.add_black_hole_gui(game.players[i],true)
end
end
end
return gr_gui