Page 1 of 1
Events
Posted: Sun Oct 27, 2013 10:14 pm
by Dysoch
Is it possible to add events to the power grid?
Like when close or at max capacity, that rolling blackouts start? Or power line malfunctions?
Re: Events
Posted: Sun Oct 27, 2013 10:49 pm
by ficolas
You can check for entities energy in the ontick event.
And if you want custom events, I think you can add them and send them from other events.
The only events in the game are ontick oninit onload onsave onguitick onchunckgenerated onsectorscanned onplayerdeath onshiplanded and im not sure if there are more.
Re: Events
Posted: Sun Oct 27, 2013 11:13 pm
by Dysoch
Ok, but is it possible to have a chance that something happens? Like 1% chance that a power line fails?
Re: Events
Posted: Mon Oct 28, 2013 2:42 am
by FreeER
Dysoch wrote:[...]rolling blackouts start? Or power line malfunctions?
Ok, but is it possible to have a chance that something happens? Like 1% chance that a power line fails?
Interesting
Hm...in control.lua you could easily use if math.random(100) == 1 for a 1% chance or math.random(1000/entity.energy)==1 so that the more energy the more likely it is. However electric poles do not have energy so you'd have to measure the entities around them or the energy of generators/solar panels. I think you could take down the power line by changing active to false, but as for a rolling blackout I don't think there is (currently) a way to tell what entities a pole is connected to, so you'd have to use findentities or findfilteredentities and start small, increasing the area when the table is empty. Though even then it's not guaranteed that the poles are connected (since you can manually wire them), just more likely.
Re: Events
Posted: Mon Oct 28, 2013 8:12 am
by Dysoch
Ill see what i can create, thx for the help
Re: Events
Posted: Mon Oct 28, 2013 8:58 am
by slpwnd
ficolas wrote:You can check for entities energy in the ontick event.
And if you want custom events, I think you can add them and send them from other events.
The only events in the game are ontick oninit onload onsave onguitick onchunckgenerated onsectorscanned onplayerdeath onshiplanded and im not sure if there are more.
There is couple of more (onbuiltentity, onputitem, onplayermineditem, etc.) and we will be extending this interface as well (there is onplayercrafted coming soon for instance).
Re: Events
Posted: Mon Oct 28, 2013 2:24 pm
by Dysoch
slpwnd wrote:ficolas wrote:You can check for entities energy in the ontick event.
And if you want custom events, I think you can add them and send them from other events.
The only events in the game are ontick oninit onload onsave onguitick onchunckgenerated onsectorscanned onplayerdeath onshiplanded and im not sure if there are more.
There is couple of more (onbuiltentity, onputitem, onplayermineditem, etc.) and we will be extending this interface as well (there is onplayercrafted coming soon for instance).
Playercrafted is good, need that for my dynamic research system i want to put in place in my mod.
Re: Events
Posted: Sat Nov 02, 2013 11:58 am
by Dysoch
this is my control.lua:
Code: Select all
require "defines"
glob.specieOfTreeTable={"rubber"}
if remote.interfaces["F-mod"] then
remote.call("F-mod", "addboiler", "boiler-mk2")
remote.call("F-mod", "addboiler", "boiler-mk3")
remote.call("F-mod", "addboiler", "boiler-mk4")
remote.call("F-mod", "addboiler", "boiler-mk5")
end
game.onevent(defines.events.ontick, function(event)
if event.tick%3600==0 then
for _,specie in pairs (glob.specieOfTreeTable) do
if glob[specie.."trees"]~=nil then
for i,d in pairs(glob[specie.."trees"]) do
if d.entity.valid then
if math.random(5)==1 and d.entity.name==specie.."-tree" then
d.entity.insert({name=specie.."-tree-resin",count=1})
end
else
table.remove(glob[specie.."trees"],i)
end
end
end
end
end
end)
but the event isnt working (the one with the rubber tree)
what is wrong or missing?
is it because the rubber trees are added by the autoplace?
code is from f-mod (thx ficolas for your amazing codes!) (credits will be given at modpage when released!)
Re: Events
Posted: Sat Nov 02, 2013 1:31 pm
by FreeER
From what you posted I can not see if you have a glob.rubbertrees to look through, with autoplace you'd need to use onchunkgenerated and add any of your trees to the table, if you didn't then the table is going to be empty when the code runs through it and so it won't do anything. Also you could always throw a game.player.print(serpent.block(glob[specie.."trees"])) into the for loop to see it.
Re: Events
Posted: Sun Nov 03, 2013 1:28 am
by Dysoch
FreeER wrote:From what you posted I can not see if you have a glob.rubbertrees to look through, with autoplace you'd need to use onchunkgenerated and add any of your trees to the table, if you didn't then the table is going to be empty when the code runs through it and so it won't do anything. Also you could always throw a game.player.print(serpent.block(glob[specie.."trees"])) into the for loop to see it.
I could be wrong, but if i use autoplace in the entity file, isnt that enough? The event i use should search for rubber-tree (is the entity name) and add the resin.
Thats what i think after looking thru all the files. But, still new to modding, so i could be wrong.
Re: Events
Posted: Sun Nov 03, 2013 2:29 am
by FreeER
Dysoch wrote:if i use autoplace in the entity file, isnt that enough?
For this, no. You used
Code: Select all
if glob[specie.."trees"]~=nil then
for i,d in pairs(glob[specie.."trees"]) do
[...]
but, as far as I know, autoplace does not create a table for the entities. So the specie.."trees" (equating to glob["rubbertrees"]) will always be nil unless you create it yourself (and add the trees to it at some point, with autoplace the best way is with onchunkgenerated). If you check the f-mod's control.lua you'll see that Ficolas has done this as well (though he is using the onchunkgenerated to randomly create his trees instead of autoplace he still has to create the table and add the entity to it).
Re: Events
Posted: Sun Nov 03, 2013 8:50 am
by drs9999
FreeER wrote:F-code snippet
Another advantage using something similar instead of the autoplacer is that you can specify the force, so you probably can get rid off the red dots on map.
Re: Events
Posted: Sun Nov 03, 2013 8:50 am
by ficolas
What you can do to use autoplace is use onchunkgenerated to add the trees to a table, by looking for them in the chunk that has been generated, with findentitiesfiltered.
Re: Events
Posted: Sun Nov 03, 2013 9:32 am
by Dysoch
Code: Select all
game.onevent(defines.events.onchunkgenerated, function(event)
if math.random(5)==1 then
local specieOfTree=glob.specieOfTreeTable[math.random(#glob.specieOfTreeTable)]
local treex = event.area.lefttop.x+math.random(32)
local treey = event.area.lefttop.y+math.random(32)
local typeoftree = math.random(3)
if glob[specieOfTree.."trees"]==nil then
glob[specieOfTree.."trees"]={}
end
local createdentity=game.createentity( {name=(specieOfTree.."-tree"..typeoftree), position={treex,treey}} )
table.insert(glob[specieOfTree.."trees"], {entity=createdentity} )
end
end)
why is it when i use this, it looks for rubber-tree2? only rubber-tree exists, not 1,2 or 3.
Re: Events
Posted: Sun Nov 03, 2013 9:39 am
by drs9999
Well because you programmed it like that
You used:
Code: Select all
game.createentity( {name=(specieOfTree.."-tree"..typeoftree), position={treex,treey}} )
where typeoftree is a random number between 1-3
Re: Events
Posted: Sun Nov 03, 2013 10:05 am
by Dysoch
Ok they generate now, resin is produced every once in a while, but still the red dots. How can i change or imply the force of the entity?
using the berry's build by f-mod to grow more trees, as people like to automate the process of rubber making (i think
)
Re: Events
Posted: Sun Nov 03, 2013 10:39 am
by Dysoch
Code: Select all
game.onevent(defines.events.ontick, function(event)
if event.tick%3600==0 then
for _,specie in pairs (glob.specieOfTreeTable) do
if glob[specie.."trees"]~=nil then
for i,d in pairs(glob[specie.."trees"]) do
if d.entity.valid then
if math.random(5)==1 and d.entity.name==specie.."-tree" then
d.entity.insert({name=specie.."-tree-resin",count=1})
end
else
table.remove(glob[specie.."trees"],i)
end
end
end
end
end
if string.find(event.createdentity.name,"-resin")~=nil then --[[Berries build]]--
specie = string.gsub(event.createdentity.name, '%-resin', '') (THINK ERROR IS HERE!)
if glob[specie.."trees"]==nil then
glob[specie.."trees"]={}
end
table.insert(glob[specie.."trees"], {entity=game.createentity({name=specie.."-tree", position=event.createdentity.position}), time=0})
event.createdentity.destroy()
end
end)
if you take a look at the berries build. it gives an error when starting a new game: attempted to index field 'createdentity' (a nil value)
all i did was remove the small tree builds, changed berry to resin and added resin as an entity. think it should work.
but anyway, already thanks all, still learning events, but im slowly getting there
EDIT:
Question 2: this should work, but from what i see, works with the more power, the more chance. but is that full network power usage, or at or near max power production per entity?
Code: Select all
if glob.steamengines~=nil and math.random(1000/entity.energy)==1 then --[[Power failure steam engines]]--
for i,_ in pairs(glob.steamengines) do
if foundsteamengines==true then
if glob.steamengines[i].entity.valid then
glob.steamengines[i].entity.die()
game.getplayer().print("Your steam engine was destroyed because it was working at or near max capacity. Get more power generation to lessen the chance of breakdown!")
end
end
end
end
if glob.solarpanels~=nil and math.random(1000/entity.energy)==1 then --[[Power failure solar panels]]--
for i,_ in pairs(glob.solarpanels) do
if foundsolarpanels==true then
if glob.solarpanels[i].entity.valid then
glob.solarpanels[i].entity.die()
game.getplayer().print("Your solar panel was destroyed because it was working at or near max capacity. Get more power generation to lessen the chance of breakdown!")
end
end
end
end
Re: Events
Posted: Sun Nov 03, 2013 11:43 pm
by Dysoch
after a few tries, and looking throu the wiki, i got the red dots to dissapear. used force=game.forces.neutral to correct it.
but still need help with the resin builds and the events with the power. (post above!)
Re: Events
Posted: Mon Nov 04, 2013 2:14 am
by FreeER
First the d.entity.name is not needed here
Code: Select all
--ontick code
if math.random(5)==1 and d.entity.name==specie.."-tree" then
because each table is only for that specific tree, if you were using a single table for all of your trees then you'd want to check the name...
as for
Code: Select all
d.entity.insert({name=specie.."-tree-resin",count=1})
Unless you have a different resin for each tree then the name should probably be "tree-resin", but since I can't see the full mod I don't know for sure
Now the "attempted to index field 'createdentity' (a nil value)" is because createdentity is not a table that is provided by ontick, it's provided by onbuiltentity. From the looks of it that's where this code was meant to be so all you need to do is end ontick and start onbuiltentity with
Code: Select all
game.onevent(defines.events.onbuiltentity, function(event)
Looking at the code for it though I'm confused as to why you are looking for a -resin entity being built instead of a tree...I'd have expected to see you finding built trees and adding them to the proper table here, not resin trees that you immediately replace with trees. From my understanding the resin is an item that is being inserted into the tree (container) so it will never be 'built'. With that you'd want either
Code: Select all
for _, specie in pairs(glob.specieOfTreeTable) do
if event.name==specie then
if glob[specie.."trees"]==nil then
glob[specie.."trees"]={}
end
table.insert(glob[specie.."trees"], {entity=createdentity})
break
end
end--note I used entity= in the table.insert because in the for loop in ontick you used d.entity (if you used just d then you could just insert the createdentity)
or
Code: Select all
if event.name:sub(-5)=="-tree" then --sub(-5) simply takes the last five characters of the string, so this assumes all your trees end with "-tree"
if glob[specie.."trees"]==nil then
glob[specie.."trees"]={}
end
table.insert(glob[specie.."trees"], {entity=createdentity})
end
These should both do the same, just slightly different ways of seeing if the entity is one of your trees. To be honest, the second may be easier since I'm not sure if the specieOfTreeTable index names will be the same as that of the trees. Again, these assume that you are adding the built tree entities to their tables rather than the original code that seems to be immediately replacing any -resin entities with tree entities and adding those to the tables
edit: as to the power solarpanel.energy seems to be the energy given per tick = 0 when night (no output), however the steam engines seem to give 9.0666666666667 when they are not doing anything (perhaps that would be the available peformace??). I'll try to research it a bit more, but I'm not certain I'll be able to understand what I find lol
edit2: steamengine.energy seems to be about 9 when connected but not used, 0 when not connected to anything and 0 when at max output??
solarpanel also seems to be 0 when in full use and goes to 1.06666666666667 when connected but nothing is drawing power. So I'm going to guess that highest use is as energy approaches 0...Thus I think the formula you'd want to use is more along the lines of math.floor(math.random(10*entity.energy+1))==2 (should give 10% chance of failure at nearly full usage, unfortunately if i checked things right then full use would appear the same as disconnected. just change the 10 at the front to make it higher or lower chance) the plus one is so that you don't pass a range of 0 to math.random and you need to check for 2 because of that addition (since 0 could be a disconnected entity, you don't want to get math.random(0+1)==1)
Re: Events
Posted: Mon Nov 04, 2013 1:40 pm
by Dysoch
Code: Select all
if glob.steamengine~=nil and math.floor(math.random(5*entity.energy+1))==2 then --[[Power failure steam engines]]--
for i,_ in pairs(glob.steamengine) do
if foundsteamengine==true then
if glob.steamengine[i].entity.valid then
glob.steamengine[i].entity.die()
game.getplayer().print("Your steam engine was destroyed because it was working at or near max capacity. Get more power generation to lessen the chance of breakdown!")
end
end
end
end
if glob.solarpanel~=nil and math.floor(math.random(5*entity.energy+1))==2 then --[[Power failure solar panels]]--
for i,_ in pairs(glob.solarpanel) do
if foundsolarpanel==true then
if glob.solarpanel[i].entity.valid then
glob.solarpanel[i].entity.die()
game.getplayer().print("Your solar panel was destroyed because it was working at or near max capacity. Get more power generation to lessen the chance of breakdown!")
end
end
end
end
Already changed to math, but they code itself doesnt appear to work, the engine or panels wont get destroyed when running near max capacity!