Creating Assembly machine with integrated Electric pole

Place to get help with not working mods / modding interface.
Alex33
Burner Inserter
Burner Inserter
Posts: 17
Joined: Thu Oct 24, 2019 11:56 am
Contact:

Creating Assembly machine with integrated Electric pole

Post by Alex33 »

Hello community,
I'm new on Factorio modding, and I wish you can help me to validate/correct this approach for my first mod:

I want to create a new assembly-machine item, that is also a small-electric-pole (with energy distribution and connection).
(ie: I want my custom-assembly-machine to share electricity 1 tile arround each side (so supply_area_distance=3 (from the middle)), and that connect with other electric-poles and custom-assembly-machines arround (maximum_wire_distance=xx). The top of this pylon (with its connections) must be visible
How would you achieve that?

I read couple guides and topics on this forum, and found the mod "Bio Industries" as example (more specificaly, the "bio_farm"). viewtopic.php?f=25&t=55518&p=326305&hil ... le#p326305
Regarding what I understood, I was thinking my needs:

Create prototype "assembly-machine-electric"
Copy the initial "assembly-machine-3",
change the recepe to add 1 small-electric-pole
Create prototype "assembly-machine-pole",
Copy of "small-electric-pole",
change values of its properties supply_area_distance and maximum_wire_distance
collision_box = {{-0, -0}, {0, 0}}

in control.lua:
when the assembling-machine-electric is built, automatically creates a assembling-machine-pole at the same position, and save it as a group_entities.
when the assembling-machine-electric is removed/destroyed, get the grouped assembling-machine-pole to destroy both.

Code: Select all

-- example with the on_built_entity
script.on_event(defines.events.on_built_entity)
	local entity = event.created_entity
	if entity.valid and entity.name == "assembling-machine-electric" then	   
		local assembling_machine_electric = entity
		local create_pole = surface.create_entity({name = "assembling-machine-pole" , position = entity.position, force = entity.force})		  
		create_pole.minable = false
		create_pole.destructible = false		
		group_entities(cantor(position.x,position.y), {assembling_machine_electric, create_pole})	  
	end
end
then:
* Prevent players to create the assembly-machine-pole directly
* Manage technology so assembly-machine-electric is allowed with the technology for classic assembly machine (in data-updates.lua or data-final-fixes.lua?)
* Manage the image(s?) of the assembly-machine-pole so the bottom on the pylon is transparent/removed (the part of the pylon that is supposed to be "in the assembly machine") (how can I manage which image is on top of the other one?)

Do you feel this is the correct approach ?
How would you do?
Did I miss something important?

Thank you for reading,

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3699
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by DaveMcW »

Yes, that is the correct approach.

Note that there are 5 build events (with 3 different entities!) and 4 destroy events that you should handle. viewtopic.php?f=25&t=74316

The easy way to control graphics order is to position the "top" entity slightly to the south. The hard way involves creating special entities that have direct control over the render_layer.

Alex33
Burner Inserter
Burner Inserter
Posts: 17
Joined: Thu Oct 24, 2019 11:56 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by Alex33 »

All right,
Thank you very much

Alex33
Burner Inserter
Burner Inserter
Posts: 17
Joined: Thu Oct 24, 2019 11:56 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by Alex33 »

Hi,
My mod development is going well, even if I have hard time discovering basic stuff on the fly

I currently have issues with positionning, and some additional questions for the next use cases. I wish you can kindly help me, please?

Q1) how can I manage not integer numbers for positionning?

Code: Select all

local entity = event.created_entity
local surface = entity.surface
local position = entity.position
local polePosition={position.x, position.y}
local create_pole = surface.create_entity({name = "assembling-machine-pole" , position = polePosition, force = force})


with polePosition = event.created_entity.position, there is a shift of 0.5 (both x and y) compared to standard electric pole, as show the picture bellow:
Image
local polePosition={position.x-0.5, position.y-0.5} is interpreted like {position.x-1, position.y-1} (same with -0.2 or whatever -0.### value)

But I have read some positions = {-#.#, -#.#} (for pipe_connections for example)... I don't understand why it doesn't work for me? (because the main error source is between the keyboard and the chair? ^^)


Q2) I want to display the all group assembling-machine-electric+assembling-electric-pole (including the assembling-electric-pole's area of effect and wire connections).
Is it manageable the same way as previously (on_built)?
What is the event for "on select item for construction"? on_put_item ->Called when players uses an item to build something.
What is the event for "on cancel item for construction"? = "on_selected_entity_changed" ?
How to know if there is more item "assembling-machine-electric" ready to be constructed in the inventory, after one has be built?

Should I use those ones ?

Code: Select all

"on_trigger_created_entity"	Called when an entity with a trigger prototype (such as capsules) create an entity AND that trigger prototype defined trigger_created_entity="true".
"script_raised_built"	A static event mods can use to tell other mods they built something with a script.
"script_raised_destroy"	A static event mods can use to tell other mods they destroyed something with a script.
"script_raised_revive"	A static event mods can use to tell other mods they revived something with a script
Q3) I want to allow the player to build a standard "small-electric-pole" on a regular "assembling-machine-1", so it becomes an "assembling-machine-electric + its assembling-machine-pole".
How to allow such construction? (only on assembling machines) = with event "on_put_item" with surface.find_entities_filtered, I can check if there is an assembling machine on it, but then, service can_place_entity() is only a getter, not a setter... or is it the "forced" parameter that I can define it as ok to do?
How to detect the type of building? (assembling-machine-1, -2, -3, or a different entity that doesn't allow such construction on top). "on_put_item" with surface.find_entities_filtered?

To actually perform the building upgrade, is it with : surface.upgrade_area{area=…, force=…, player=…, skip_fog_of_war=…, item=…} ?

Thank you for reading

julius1701
Inserter
Inserter
Posts: 33
Joined: Sun Dec 17, 2017 8:33 pm
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by julius1701 »

Q3: i would look at the recipe of the electric-engine since it can only be built in an assembly machine. maybe you could copy and edit that code

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by eradicator »

q1) https://wiki.factorio.com/Types/EntityP ... ff-grid.22
q2) You have to use a third "proxy pole" entity that is an electric pole but has the collision box of the assembler. Then in on_built remove the proxy and place the two real entities. (Nobody said this is easy)
q3) Impossible. on_built does not fire if collision prevents the pole from being built in the first place. You'd have to change the collision mask of the pole so that it becomes placeable *everywhere* and then offensively script to remove it everywhere except your machine. It's as ugly as it sounds.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Cribbit
Fast Inserter
Fast Inserter
Posts: 199
Joined: Mon Oct 09, 2017 9:35 pm
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by Cribbit »

julius1701 wrote:
Fri Oct 25, 2019 12:44 pm
Q3: i would look at the recipe of the electric-engine since it can only be built in an assembly machine. maybe you could copy and edit that code
They're asking about being able to "upgrade" an assembler to one that includes the pole, not having poles only buildable in assemblers.

Cribbit
Fast Inserter
Fast Inserter
Posts: 199
Joined: Mon Oct 09, 2017 9:35 pm
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by Cribbit »

eradicator wrote:
Fri Oct 25, 2019 1:11 pm
q3) Impossible. on_built does not fire if collision prevents the pole from being built in the first place. You'd have to change the collision mask of the pole so that it becomes placeable *everywhere* and then offensively script to remove it everywhere except your machine. It's as ugly as it sounds.
How does the upgrade in place mechanic work though? I feel like there should be a way to do it through that.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by eradicator »

q3) I guess if you just want "clicks with a pole" you could use a linked_game_control then check selected and cursor_stack. Then create_entity{fast_replace=true} the thing (which you need to do anyway). That actually not ugly at all anymore.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Alex33
Burner Inserter
Burner Inserter
Posts: 17
Joined: Thu Oct 24, 2019 11:56 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by Alex33 »

Q1) I have added the "placeable-off-grid" flag to my electric pole, but it still behave the same:

Code: Select all

local polePosition={position.x-0.5, position.y-0.5}
local create_pole = surface.create_entity({name = "assembling-machine-pole" , position = polePosition, force = force})	

is the same as

Code: Select all

local polePosition={position.x-1, position.y-1}
local create_pole = surface.create_entity({name = "assembling-machine-pole" , position = polePosition, force = force})	
and different from

Code: Select all

local polePosition={position.x, position.y}
local create_pole = surface.create_entity({name = "assembling-machine-pole" , position = polePosition, force = force})	
in all 3 cases, there is a 0.5 tile shift compared to regular small-electric-pole.


Q2) So this proxy pole is actually the item that comes out of the recipe, and it should be an electric pole instead of a assembling-machine.
Then, both my "initial entities" will be created with surface.create_entity, instead of just one, and remove the proxy as you said
That should not be such a big change...

Q3) There is for sure an event fired when trying to build something that is not allowed. When I do it, there is a message: "Unknown key: "entity-name.assembling-machine-electric" is on the path" displayed.
Regarding the default upgrade mechanism, it seams there is a "fast_replaceable_group" parameter on entities (ex: fast_replaceable_group = "furnace"), but one of my previous error (when game loaded my mods) mentioned that it must have the same size to be replaceable. I don't want to change the original small-electric-pole...
q3) I guess if you just want "clicks with a pole" you could use a linked_game_control then check selected and cursor_stack. Then create_entity{fast_replace=true} the thing (which you need to do anyway). That actually not ugly at all anymore.
linked_game_control is only available with type: "Prototype/CustomInput". correct ? (to be implemented as an entity)
there is no cursor-selected nor custor-stack as possible values.. https://wiki.factorio.com/Prototype/Cus ... me_control


Thank you all for you help and contributions :)

Alex33
Burner Inserter
Burner Inserter
Posts: 17
Joined: Thu Oct 24, 2019 11:56 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by Alex33 »

q2) You have to use a third "proxy pole" entity that is an electric pole but has the collision box of the assembler. Then in on_built remove the proxy and place the two real entities. (Nobody said this is easy)
Eradicator, your proxy pole solution actually solved my Q1 :D (and also Q2, despite of the "unfortunatly part" of my answer, bellow)
I still don't know why I had this shift, but I take it as a win :D

Unfortunatly, it appears that electric poles have a "pictures" parameter, of type "RotatedSprite", when assembly machines have an "animation" parameter of type "Animation4Way". I guess I will have to create the RotatedSprite for the all stuff... it doesn't look easy at all. :s

moreover, I have no idea how to manage the green and red wires... both the assembling machine and the electric pole are suppose to use them... I have not even used them as a player... any idea ?

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by eradicator »

q1) dunno

q3) pseudocode:

Code: Select all

--data oO
data:extend{{
	type = 'custom-input',
	linked_game_control = 'build'
	name='bla'
	}}
--control o_O
script.on_event('bla',function(e) 
local p = game.players[e.player_index]
if p.selected and p.selected.name == 'superwhateverassemblyfurnace' and
 p.cursor_stack.valid_for_read and p.cursor_stack.name == 'whateverpole' then
 --replace furnace
 --consume cursor stack
 --etcpp
 end
end)
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by eradicator »

Alex33 wrote:
Fri Oct 25, 2019 3:53 pm
moreover, I have no idea how to manage the green and red wires...
What is there even to manage? As long as the pole is selectable it should work.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

mrvn
Smart Inserter
Smart Inserter
Posts: 5682
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by mrvn »

eradicator wrote:
Fri Oct 25, 2019 4:01 pm
Alex33 wrote:
Fri Oct 25, 2019 3:53 pm
moreover, I have no idea how to manage the green and red wires...
What is there even to manage? As long as the pole is selectable it should work.
The pole has to be included in blueprints (should happen on it's own if you select a large enough area). And when the assembler entity is build you have to check for a ghost pole and revive that instead of building your own. Building over the ghost destroys the wires iirc.

Alex33
Burner Inserter
Burner Inserter
Posts: 17
Joined: Thu Oct 24, 2019 11:56 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by Alex33 »

I will check this blueprints at one point, thanks to rising it up mrvn.
For the wires, I was thinking that maybe someone want to pass a cable through the electric pole, but not use it on the assemble that holds it.. not sure if I really need to do something about it.

Anyway, the mod globally works. :) thank you mostly Eradicator!
My last hard time was with the inventory and cursor_stack, but it's finally solved.

I have to ask.. debugging is really painfull: potentially modifying 1 ligne, zip folder, set name with _version.zip, copy to %appdata%/Factorio/mod folder, start game (from steam), and depending on the thing to test start a game and do the stuff, so you can have 1 tiny feedback when it crashes...
Please tell me there is a better way?
I was thinking making a .bat file to not do the foldering part manually anymore... How are you managing the process?
Thanks


My TODO list is still big, I'm writting it here to keep track, and so you can insert the knowledge/advices you are thinking about when reading ;)
  • Correct an issue: when building the "proxy" directly, both entities are created as expected, but on remove, only the assembly is removed, not the electric pole.
    I don't have this unwanted behaviour with a standard assembly that received an electric pole later.
  • The mod is not compliant with the mod "bottleneck": The lights from Bottleneck are removed when I add an electric pole, and they are not here when I create the proxy.
  • Complete displayed text and my 2 languages
  • Make the feature available for all assembling-machines, not just for the assembling-machine-1
  • Ensure the electric pole are keeped when upgrading an assembling machine/that removing an upgraded assembling machine removes the electric pole as well
  • Change proxy image to show both assembling machine and electric pole when placing the object
  • Include pole in blueprints being careful with ghost
  • Improve graphics for pole/wire connection points to make the all stuff visually appealing
  • Ensure the "pole" follows the assembling-machine rotation, when they can have fluid connection
  • ...
  • publish

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3699
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by DaveMcW »

Alex33 wrote:
Sat Oct 26, 2019 1:10 am
I have to ask.. debugging is really painfull: potentially modifying 1 ligne, zip folder, set name with _version.zip, copy to %appdata%/Factorio/mod folder, start game (from steam), and depending on the thing to test start a game and do the stuff, so you can have 1 tiny feedback when it crashes...
Please tell me there is a better way?
Factorio does not require mods to be zipped, so skip that step.

You can edit the mod directly from your mods folder, or add a symlink. https://docs.microsoft.com/en-us/window ... nds/mklink

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by eradicator »

DaveMcW wrote:
Sat Oct 26, 2019 1:56 am
Alex33 wrote:
Sat Oct 26, 2019 1:10 am
I have to ask.. debugging is really painfull: potentially modifying 1 ligne, zip folder, set name with _version.zip, copy to %appdata%/Factorio/mod folder, start game (from steam), and depending on the thing to test start a game and do the stuff, so you can have 1 tiny feedback when it crashes...
Please tell me there is a better way?
Factorio does not require mods to be zipped, so skip that step.

You can edit the mod directly from your mods folder, or add a symlink. https://docs.microsoft.com/en-us/window ... nds/mklink
Further more changes to control.lua do not require restarting the whole game. It's sufficient to load an existing savegame/create a new map.

For blueprints you can simply keep both assembler+pole in the blueprint (though the pole must not collide with the assembler for this to work). Then either make the pole unbuildable by bots or wait for bots to build both, or..whatever you think how it should work. For bottleneck you probably need to create_entity{raise_built=true}. To dynamically supporting every modded machine you'll have to a) cope with different size machines (1x1, 15x15, etc, anything's possible) and b) it'll never look nice because they're not all "flat" as the vanilla ones, so if you really want to add that feature i recommend doing it after publishing a working version for standard assemblers.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Alex33
Burner Inserter
Burner Inserter
Posts: 17
Joined: Thu Oct 24, 2019 11:56 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by Alex33 »

Hello fellow Modders,
Thank you very very much for your valuable feedbacks!

Bottleneck is now working fine with my mod, and much more has be done. I will release the electric-assembling-machine mod soon :).
There are still couple things that I wish to correct:

Player is able to put an electric-pole on an assembling machine, but this feature is not reflected:
When I mouse over the assembling-machine with the electric pole in "hand_stack": .
Q1.a) The electric-pole is displayed in "red", like when it's not authorized: How to make it green?
Q2.b) Assembling machine is 3x3, the electric pole is 1x1: Can I force "construction position" to be on the middle of the assembling machine, whatever the on-mouse_over position is on the assembling-machine 3x3 grid ? (on_build, it is at the right place, I'm just talking about the electric pole image before the player clicks to validate construction)
Q1.c) Is it possible to temporary change the image of the entity to be built so the picture of another entity is displayed when on-mouse-over an assembling-machine (to show the specific pole design to be constructed)? (or probably temporary modify the entity and not just the picture)

Maybe the "fast_replaceable_group" mechanism could help, but the called function would have to be override because the electric-pole (and my proxy) are not crafting machines, so all content/recipe from assembling machine will be lost if I can't manage copy settings before fast replacement.
Indeed, in order to have the electric pole image on top of the assembling machine image, I must first invoke surface.create_entity for "electric-pole" and only after invoking surface.create_entity for "assembling machine". It is problematic because that means the initial assembling machine has to be destroy() and recreated later. As a consequence, I managed content transfer manually (done for setting, input-stack, output-stack, modules).

Because of this manual content transfer management, I will sometime have to drop item on the floor: (ex: it's a downgrade of the assembling machine, I need to get the modules back, but there is no more place in the player's inventory.
Q2) How to drop a stack on the floor, at player's position?


I didn't checked blueprints yet

About Wire connection:
As the electric-pole on top of the assembling machine is not selectable, there is no way to remove a copper cable connection with another electric pole.
I don't know if a green/red wire will keep all their possible connections just from the assembling machine (but Eradicator said there should be nothing to manage). I have not tested anything about green/red wires, as it's low priority

Then, I wish couple nice people will agree to beta test :)
Regards,

Alex33
Burner Inserter
Burner Inserter
Posts: 17
Joined: Thu Oct 24, 2019 11:56 am
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by Alex33 »

Hello all,
I'm glad to announce that first version of my "Electric Assembling Machines" mod (version 0.1.4) is now released. :)
Please do not hesitate to try it, your feedback (both functional and technical) are welcome.
Thank you, all participants

julius1701
Inserter
Inserter
Posts: 33
Joined: Sun Dec 17, 2017 8:33 pm
Contact:

Re: Creating Assembly machine with integrated Electric pole

Post by julius1701 »

Hi Alex!
I just wrote a german locale for your mod.
Attachments
settings_german.cfg
(1.78 KiB) Downloaded 65 times

Post Reply

Return to “Modding help”