Page 1 of 1
Spawn Biter When Item Placed
Posted: Sun Dec 21, 2014 1:38 pm
by FishSandwich
So, I'm trying to mod a way to spawn a biter when you place an item. I've already created the recipe, entity etc, and I can craft the item needed and research it.
When trying to place the item, I want to run this script:
Code: Select all
spawncoords = game.findnoncollidingposition("small-biter", game.player.character.position, 10, 1)
game.createentity{name = "small-biter", position = spawncoords, force = game.player.character.force}
Is this possible? Thanks!
Re: Spawn Biter When Item Placed
Posted: Sun Dec 21, 2014 2:15 pm
by L0771
FishSandwich wrote:So, I'm trying to mod a way to spawn a biter when you place an item. I've already created the recipe, entity etc, and I can craft the item needed and research it.
When trying to place the item, I want to run this script:
Code: Select all
spawncoords = game.findnoncollidingposition("small-biter", game.player.character.position, 10, 1)
game.createentity{name = "small-biter", position = spawncoords, force = game.player.character.force}
Is this possible? Thanks!
i don't know if character has property force (must be the same like player.force), but it must work.
With little changes...
Code: Select all
game.onevent(defines.events.onbuiltentity, function(event)
if event.createdentity.name == "item" then
local spawncoords = game.findnoncollidingposition("small-biter", event.createdentity.position, 10, 1)
if spawncoords ~= nil then
game.createentity{name = "small-biter", position = spawncoords, force = game.getplayer(event.playerindex).force}
end
end
end)
Re: Spawn Biter When Item Placed
Posted: Sun Dec 21, 2014 4:32 pm
by Rahjital
There is a much easier way: You can clone defender capsules and make them spawn biters instead of robots. No scripting required.
Re: Spawn Biter When Item Placed
Posted: Sun Dec 21, 2014 4:41 pm
by L0771
Rahjital wrote:There is a much easier way: You can clone defender capsules and make them spawn biters instead of robots. No scripting required.
Can create an unit type? because combat-robot type needs time_to_live.
Re: Spawn Biter When Item Placed
Posted: Sun Dec 21, 2014 6:52 pm
by Rahjital
Yes, you can create any entity type with it. Robots, units, assemblers, as long as it's an entity, it can be created.
Re: Spawn Biter When Item Placed
Posted: Sun Dec 21, 2014 7:54 pm
by L0771
in that case it is better with capsules
Re: Spawn Biter When Item Placed
Posted: Sun Dec 21, 2014 10:04 pm
by FishSandwich
Rahjital wrote:There is a much easier way: You can clone defender capsules and make them spawn biters instead of robots. No scripting required.
Ah cool, I didn't know that, I'll give this a try.
L0771 wrote:Code: Select all
game.onevent(defines.events.onbuiltentity, function(event)
if event.createdentity.name == "item" then
local spawncoords = game.findnoncollidingposition("small-biter", event.createdentity.position, 10, 1)
if spawncoords ~= nil then
game.createentity{name = "small-biter", position = spawncoords, force = game.getplayer(event.playerindex).force}
end
end
end)
I can still use this to spawn biter spawners, thanks!
Re: Spawn Biter When Item Placed
Posted: Sun Dec 21, 2014 11:08 pm
by FishSandwich
Rahjital wrote:Yes, you can create any entity type with it. Robots, units, assemblers, as long as it's an entity, it can be created.
Hmm, I'm not having much luck with this. My modding skills are limited, could you tell me how to do this exactly?
Re: Spawn Biter When Item Placed
Posted: Mon Dec 22, 2014 2:08 am
by L0771
FishSandwich wrote:Rahjital wrote:Yes, you can create any entity type with it. Robots, units, assemblers, as long as it's an entity, it can be created.
Hmm, I'm not having much luck with this. My modding skills are limited, could you tell me how to do this exactly?
Some like this.
__base__/prototypes/item/capsule.lua > line 108, and change the proyectile
Code: Select all
{
type = "capsule",
name = "put-unit-capsule",
icon = "__base__/graphics/icons/defender-capsule.png",
flags = {"goes-to-quickbar"},
capsule_action =
{
type = "throw",
attack_parameters =
{
ammo_category = "capsule",
cooldown = 15,
projectile_creation_distance = 0.6,
range = 20,
ammo_type =
{
category = "capsule",
target_type = "position",
action =
{
type = "direct",
action_delivery =
{
type = "projectile",
projectile = "put-unit-projectile", -- i only change it
starting_speed = 0.3
}
}
}
}
},
subgroup = "capsule",
order = "d[defender-capsule]",
stack_size = 100
},
and add the proyectile
__base__/prototypes/entity/projectiles.lua > line 402, and change the entity created
Code: Select all
{
type = "projectile",
name = "put-unit-projectile",
flags = {"not-on-map"},
acceleration = 0.005,
action =
{
type = "direct",
action_delivery =
{
type = "instant",
target_effects =
{
{
type = "create-entity",
entity_name = "your-unit", -- i only change it
},
}
}
},
light = {intensity = 0.5, size = 4},
animation =
{
filename = "__base__/graphics/entity/combat-robot-capsule/defender-capsule.png",
frame_count = 1,
width = 32,
height = 32,
priority = "high"
},
shadow =
{
filename = "__base__/graphics/entity/combat-robot-capsule/combat-robot-capsule-shadow.png",
frame_count = 1,
width = 32,
height = 32,
priority = "high"
},
smoke = capsule_smoke,
},
And need a recipe if you want craft it.
Re: Spawn Biter When Item Placed
Posted: Tue Dec 23, 2014 2:42 pm
by FishSandwich
Okay, I'm having problems here.
Edit: Never mind, it works, it was my mistake! Thanks!
Re: Spawn Biter When Item Placed
Posted: Tue Dec 23, 2014 3:37 pm
by L0771
FishSandwich wrote:Okay, I'm having problems here.
Edit: Never mind, it works, it was my mistake! Thanks!
Only the subgroup?
I just got the speed x20, spawned 50 small biters and game has crashed
No one born knowing
Re: Spawn Biter When Item Placed
Posted: Tue Dec 23, 2014 4:38 pm
by FishSandwich
Okay, I have this working quite well, I have a problem though. When throwing capsules, I'll sometimes get biters/worms/spawners spawning on top of each other or on water, which is annoying.
Is there any way to get capsules to check collision and spawn in a free spot?
Re: Spawn Biter When Item Placed
Posted: Tue Dec 23, 2014 5:31 pm
by L0771
FishSandwich wrote:Is there any way to get capsules to check collision and spawn in a free spot?
no, you can't, this action haven't a defined event.
This don't happen with robots because they fly and they haven't collusion_box
collision_box = {{0, 0}, {0, 0}}
Can create a unit for this without collusion_box.
Some like this in data.lua, and need change entity created in projectile.
Code: Select all
function newUnit()
newunit = data.raw["unit"]["small-biter"]
newunit.name = "new-small-biter"
newunit.collision_box = {{0, 0}, {0, 0}}
return newunit
end
data:extend(
{
newUnit()
}
)
Re: Spawn Biter When Item Placed
Posted: Wed Dec 24, 2014 12:26 am
by FishSandwich
Hmm, when I use that code the way you say I get error message
Code: Select all
Error while loading prototype "new-small-biter" (unit): Prototype "new-small-biter" registered twice
I'm doing something wrong I'm sure.
Sorry for my noobness
Re: Spawn Biter When Item Placed
Posted: Wed Dec 24, 2014 1:00 am
by L0771
FishSandwich wrote:Hmm, when I use that code the way you say I get error message
Code: Select all
Error while loading prototype "new-small-biter" (unit): Prototype "new-small-biter" registered twice
I'm doing something wrong I'm sure.
Sorry for my noobness
I'm sorry, my bad, i don't tested it (cost only 10 sec, but i'm lazy)
Code: Select all
local newunit = util.table.deepcopy(data.raw["unit"]["small-biter"])
newunit.name = "new-small-biter"
newunit.collision_box = {{0, -0.01}, {0, 0.01}}
data:extend( { newunit } )
... with low collision_box they don't run on water and can't bug one over other... (i was searched this for long time, now, with it, i can improve an entity )
Re: Spawn Biter When Item Placed
Posted: Wed Dec 24, 2014 9:45 am
by FishSandwich
L0771 wrote:Code: Select all
local newunit = util.table.deepcopy(data.raw["unit"]["small-biter"])
newunit.name = "new-small-biter"
newunit.collision_box = {{0, -0.01}, {0, 0.01}}
data:extend( { newunit } )
... with low collision_box they don't run on water and can't bug one over other...
You are champion.
This works nicely. Can still throw capsule on water to spawn biter, but, that is maybe unsolvable? It's no problem anyway.
L0771 wrote:(i was searched this for long time, now, with it, i can improve an entity )
I really appreciate your efforts to help me.
Re: Spawn Biter When Item Placed
Posted: Wed Dec 24, 2014 1:58 pm
by L0771
FishSandwich wrote:Can still throw capsule on water to spawn biter, but, that is maybe unsolvable? It's no problem anyway.
i think no, it's a problem, because this don't cause a event, the only way is scan the nearest map every second... it's hard and use a lot of resources, i think resources bad used.
i'm glad for help
Re: Spawn Biter When Item Placed
Posted: Wed Dec 24, 2014 10:37 pm
by LordFedora
Does is spawn a onUnitCreated? or similar?
Re: Spawn Biter When Item Placed
Posted: Wed Dec 24, 2014 11:20 pm
by L0771
LordFedora wrote:Does is spawn a onUnitCreated? or similar?
i use test mode for this, and use a capsule don't have a event. I think this must have
Re: Spawn Biter When Item Placed
Posted: Wed Dec 24, 2014 11:28 pm
by Rahjital
Yeah, unfortunately there is no generic onEntityCreated event. I think finding non-colliding positions is only possible through Lua code, and that can only be accessed through the onEntityPlaced event which doesn't work with capsules.