Skip to content

Commit

Permalink
Add computer entity
Browse files Browse the repository at this point in the history
Add lan & wlan APIs
  • Loading branch information
Relik77 committed Nov 27, 2017
1 parent 499c0c6 commit 9203401
Show file tree
Hide file tree
Showing 16 changed files with 613 additions and 9 deletions.
154 changes: 146 additions & 8 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ require("logic.gui.computerWaypointGui")

require("logic.apis.term")
require("logic.apis.os")
require("logic.apis.lan")
require("logic.apis.wlan")

baseEnv = {
ipairs = ipairs,
Expand Down Expand Up @@ -67,7 +69,9 @@ local function OnGuiClick(event)
local name = event.element.name

if name:match("^computer_") then
if not global.computerGuis then global.computerGuis = {} end
if not global.computerGuis then
global.computerGuis = {}
end
local player = game.players[event.player_index]

if name == "computer_gauntlet_btn" then
Expand Down Expand Up @@ -133,6 +137,119 @@ local function OnGuiElemChanged(event)
end
end

local function struct_create_or_revive(entity_type, surface, area, position, force)
local found_ghost = false
local ghosts = surface.find_entities_filtered {
area = area,
name = "entity-ghost",
force = force }
for _, each_ghost in pairs(ghosts) do
if each_ghost.valid and each_ghost.ghost_name == entity_type then
if found_ghost then
each_ghost.destroy()
else
each_ghost.revive()
if not each_ghost.valid then
found_ghost = true
else
each_ghost.destroy()
end
end
end
end

if found_ghost then
local entity = surface.find_entities_filtered {
area = area,
name = entity_type,
force = force,
limit = 1
}[1]
if entity then
entity.direction = defines.direction.south
entity.teleport(position)
return entity
end
else
local reals = surface.find_entities_filtered {
area = area,
name = entity_type,
limit = 1
}
if #reals == 1 then
return reals[1]
end

return surface.create_entity {
name = entity_type,
position = position,
force = force,
fast_replace = true
}
end
end

local function OnBuiltEntity(event)
local entity = event.created_entity

if not (entity and entity.valid) then
return
end
if not global.structures then
global.structures = {}
end

if entity.name == "computer-entity" then
local struct = {
type = "computer",
entity = entity,
sub = {}
}

struct.sub.left_combinator = struct_create_or_revive(
"computer-combinator",
entity.surface, -- surface
{ { entity.position.x - 1.5, entity.position.y - 1 }, { entity.position.x + 0, entity.position.y + 1 } }, -- ghost search area
{ x = entity.position.x - 0.83, y = entity.position.y + 0.51 }, -- position
entity.force
)
struct.sub.left_combinator.destructible = false

struct.sub.right_combinator = struct_create_or_revive(
"computer-combinator",
entity.surface, -- surface
{ { entity.position.x + 0, entity.position.y - 1 }, { entity.position.x + 1.5, entity.position.y + 1 } }, -- ghost search area
{ x = entity.position.x + 0.76, y = entity.position.y + 0.51 }, -- position
entity.force
)
struct.sub.right_combinator.destructible = false

table.insert(global.structures, struct)
end
end

local function OnEntityDied(event)
local entity = event.entity

if not entity.valid then return end
if global.structures then
local index = searchIndexInTable(global.structures, entity, 'entity')
local struct = global.structures[index]

if not struct then return end

if struct.sub then
for key, subentity in pairs(struct.sub) do
if subentity.valid then
subentity.destroy()
end
struct.sub[key] = nil
end
end
global.structures[index] = nil
end
end

local function OnPlayerJoinedGame(event)
local player = game.players[event.player_index]
local technology = player.force.technologies["computer-gauntlet-technology"]
Expand Down Expand Up @@ -172,6 +289,9 @@ local function supportedEntity(entity)
if not entity then
return false
end
if entity.name == "computer-entity" then
return true
end
for index, api in pairs(computer.apis) do
if type(api.entities) == "function" and api.entities(entity) then
return true
Expand All @@ -191,22 +311,34 @@ local function supportedEntity(entity)
return false
end

script.on_event("open-computer", function (event)
if not global.computerGuis then global.computerGuis = {} end
local player = game.players[event.player_index]
local function OpenComputer(player, entity)
if entity.electric_buffer_size and entity.energy == 0 then
return
end
if not global.computerGuis then
global.computerGuis = {}
end
local technology = player.force.technologies["computer-gauntlet-technology"]

if technology and technology.valid and technology.researched and player.selected and player.selected.type ~= "player" then
local distance = getDistance(player.position, player.selected.position)
if distance <= 10 and supportedEntity(player.selected) then
if technology and technology.valid and technology.researched and entity and entity.type ~= "player" then
local distance = getDistance(player.position, entity.position)
if distance <= 10 and supportedEntity(entity) then
if not global.computerGuis[player.index] then
computer.new(player.selected):openGui("console", player)
computer.new(entity):openGui("console", player)
else
global.computerGuis[player.index]:destroy()
global.computerGuis[player.index] = nil
end
end
end
end

script.on_event("open-computer", function(event)
local player = game.players[event.player_index]

if player.selected then
OpenComputer(player, player.selected)
end
end)

script.on_event(defines.events.on_tick, OnTick)
Expand All @@ -217,6 +349,12 @@ script.on_event(defines.events.on_gui_selection_state_changed, OnGuiSelectionSta
script.on_event(defines.events.on_gui_checked_state_changed, OnGuiCheckedStateChanged)
script.on_event(defines.events.on_gui_elem_changed, OnGuiElemChanged)

script.on_event(defines.events.on_built_entity, OnBuiltEntity)
script.on_event(defines.events.on_robot_built_entity, OnBuiltEntity)
script.on_event(defines.events.on_entity_died, OnEntityDied)
script.on_event(defines.events.on_preplayer_mined_item, OnEntityDied)
script.on_event(defines.events.on_robot_pre_mined, OnEntityDied)

script.on_event(defines.events.on_player_joined_game, OnPlayerJoinedGame)
script.on_event(defines.events.on_player_left_game, OnPlayerLeft)

Expand Down
6 changes: 6 additions & 0 deletions data.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
-- Add items
require ("prototypes.items.computer-gauntlet-item")
require ("prototypes.items.computer-item")

-- Add entities
require ("prototypes.entities.computer")

-- Add recipes
require ("prototypes.recipes.computer_recipe")

-- Add technology research
require ("prototypes.technologies.computer_gauntlet_technology")
require ("prototypes.technologies.computer_technology")

-- Add gui styles
require ("prototypes.style.style")
Expand Down
Binary file added graphics/blank.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added graphics/entities/computer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added graphics/entities/computer_hr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added graphics/icons/computer-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added graphics/icons/computer-technology.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions locale/en/locale.cfg
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
[controls]
open-computer=Open computer console

[entity-name]
computer-entity=Computer
computer-combinator=Circuit network connector

[entity-description]
computer-entity=Allows you to run lua code and interact with the circuit network
computer-combinator=Read and send information on the circuit network

[item-name]
computer-item=Computer

[item-description]
computer-item=Allows you to run lua code and interact with the circuit network

[recipe-name]
computer-recipe=Computer

[recipe-description]
computer-recipe=Allows you to run lua code and interact with the circuit network

[technology-name]
computer-gauntlet-technology=Personal computer
computer-technology=Computer

[technology-description]
computer-gauntlet-technology=You have a laptop built into your gauntlet, giving you the ability to write Lua programs on any item with a computer. Files can also be store on yourself.
computer-technology=Allows you to run lua code and interact with the circuit network
128 changes: 128 additions & 0 deletions logic/apis/lan.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
require("logic.computer")

table.insert(computer.apis,{
name = "lan",
description = "The LAN API provides functions to communicate using circuit network",
entities = function(entity)
if entity.name == "computer-entity" then
return true
end
return false
end,
prototype = {
_readCombinatorSignal = {
"private function lan:_readCombinatorSignal(combinator, wire)",
function(self, combinator, wire)
local signals = {}
local network

if wire then
network = combinator.get_circuit_network(wire)
end

if network then
for key, value in pairs(network.signals) do
if value.signal and value.signal.name and value.count > 0 then
if not signals[value.signal.name] then
signals[value.signal.name] = {
signal = value.signal,
count = value.count
}
else
signals[value.signal.name].count = signals[value.signal.name].count + value.count
end
end
end
else
for key, value in pairs(combinator.get_or_create_control_behavior().parameters.parameters) do
if value.signal and value.signal.name and value.count > 0 then
if not signals[value.signal.name] then
signals[value.signal.name] = {
signal = value.signal,
count = value.count
}
else
signals[value.signal.name].count = signals[value.signal.name].count + value.count
end
end
end
end

return signals
end
},
_writeCombinatorSignal = {
"private function lan:_readCombinatorSignal(combinator, signals)",
function(self, combinator, signals)
local parameters = {}
local index = 1;

if not combinator then return end

for _, signal_count in pairs(signals) do
table.insert(parameters, {
index=index,
signal=signal_count.signal,
count= math.floor(signal_count.count)
})
index = index + 1
end
combinator.get_or_create_control_behavior().parameters = {parameters = parameters}
end
},
readLeftSignals = {
"lan.readLeftSignals(wire) - Returns left signals",
function(self, wire)
local struct = self.__entityStructure
local signals = {}

local combinator = struct.sub.left_combinator
if not combinator then return end

for key, signal in pairs(self:_readCombinatorSignal(combinator, wire)) do
table.insert(signals, signal)
end

return signals
end
},
readRightSignals = {
"lan.readRightSignals(wire) - Returns right signals",
function(self, wire)
local struct = self.__entityStructure
local signals = {}

local combinator = struct.sub.right_combinator
if not combinator then return end

for key, signal in pairs(self:_readCombinatorSignal(combinator, wire)) do
table.insert(signals, signal)
end

return signals
end
},
writeLeftSignals = {
"lan.writeLeftSignal(signals) - Write signals on left combinator",
function(self, signals)
local struct = self.__entityStructure

local combinator = struct.sub.left_combinator
if not combinator then return end

self:_writeCombinatorSignal(combinator, signals)
end
},
writeRightSignals = {
"lan.writeRightSignals(signals) - Write signals on right combinator",
function(self, signals)
local struct = self.__entityStructure

local combinator = struct.sub.right_combinator
if not combinator then return end

self:_writeCombinatorSignal(combinator, signals)
end
}
}
})
Loading

0 comments on commit 9203401

Please sign in to comment.