Vehicle Corpses


Adds lootable corpses to vehicles, like when a player dies.

Tweaks
a day ago
1.1 - 2.0
4.05K
Transportation

g Corpse persists if built over

11 months ago
(updated 11 months ago)

Hi! Just an observation, not sure if this is intended behaviour:

If a vehicle is destroyed, and something is built over the corpse, the corpse will become invisible but its collision box is still there. For example, let a locomotive crash into a tank parked on the rails, so that the locomotive gets destroyed. Put a new locomotive in its place. If you try to put fuel into the locomotive directly (not via the train GUI), you may end up putting the fuel into the (now invisible) corpse below the locomotive.

Perhaps you could also listen to filtered built events, and if a vehicle is placed, remove any vehicle corpses located at that position? (Regarding filters, I'm not sure whether filter = "vehicle" will be a catch-all that includes filter = "rolling-stock". Perhaps both are needed. Then again, you may want to use more fine-grained filters as not every "car" or "spider-vehicle" prototype is meant to be used as a real vehicle: Industrial Revolution will use invisible cars to hide players, SE has asteroids and debris that are cars in disguise,
King Jo's Warhammer 40K Bunker is a car that can't move, …)

11 months ago

So I know why this is happening, and it's mainly because of the way the mod was implemented.

Most things in the game have a "corpse" entity that spawns when it gets destroyed, which is just for the graphics of the thing (you can see them in the editor, they're all "remnants")
They don't have inventory, but character corpses are special, since they do have inventory slots, and thus are a different prototype. However, character corpses can only have one sprite, not a 4 way directional sprite like most vehicles.
So, I ended up leaving the regular corpse entity intact, and I spawn an invisible character corpse on top of it for the inventory.
Then, when the character corpse is destroyed, I destroy the regular corpse as well, if it's still there.
The downside to this approach is that the regular corpse can be destroyed before the character corpse is, making it effectively invisible, as you have noticed.

I'm not really sure what behavior is intended in that scenario, whether it's not being allowed to build on top of a corpse or replacing the visible corpse entity if it gets destroyed before it's supposed to, but I'll tinker around with it and see what fits. I don't think destroying it when built over is a good idea, because it would be very easy to accidentally destroy the items inside, and that kind of goes against the point of the mod :P

11 months ago

Thank you for the detailed reply!

I'm not really sure what behavior is intended in that scenario, whether it's not being allowed to build on top of a corpse or replacing the visible corpse entity if it gets destroyed before it's supposed to, but I'll tinker around with it and see what fits. I don't think destroying it when built over is a good idea, because it would be very easy to accidentally destroy the items inside, and that kind of goes against the point of the mod :P

I haven't looked at your code yet, but using the Lua API global Variable Viewer (gvv), I noticed that you don't save as many data as possible. For instance, after killing a crawler vehicle, I found this in global.corpse_data:

{
  names = {
    ["medium-remnants"] = LuaEntityPrototype,
  },
  position = {
    x = -20.328125,
    y = -167.79296875,
  },
  surface = LuaSurface,
}

Why would you care only about the LuaEntityPrototype of the default remnants? Hovering over the remnants and pressing CTRL+SHIFT+F, I see something that seems much more useful: it's a CharacterCorpsePrototype named "crawler-corpse-dummy"! I would add at least the vehicle name derived from CharacterCorpsePrototype by stripping off the suffix "-corpse-dummy", i.e. "crawler". This way, if an entity is built on top of the corpse, you could check (using surface and position) if the name of the new entity matches that of the destroyed vehicle. If so -- why not let the new vehicle inherit the loot left behind by the old one? It would save players from having to pick up the corpse and manually adding fuel and ammo to the new vehicle, inserting whatever was in the vehicle's equipment grid, and collecting the stuff from the trunk.

In your handler for on_entity_died, you have

    store_inventory(entity, temp, di.fuel)
    store_inventory(entity, temp, di.burnt_result)
    store_inventory(entity, temp, di.car_ammo) -- covers spider_ammo
    store_inventory(entity, temp, di.car_trunk) -- covers spider_trunk
    store_inventory(entity, temp, di.spider_trash)
    store_grid(entity, temp)

This puts everything in the inventory of the dummy corpse, after sorting/merging. If you were to restore that to the newly built vehicle, you couldn't tell anymore what fuel was in the fuel inventory and what was just inside the trunk. Therefore, I'd modify store_inventory and store_grid in such a way that they also call game.create_inventory(#source_inventory) and copy each stack from the real to the script inventory (script inventories are just virtual, so they don't interfere with the actual corpse inventory), to be stored with the corpse data. This way, the dummy corpse could be mined as usual by the player, but if a new vehicle of the same type as the destroyed one is placed on that position, you could move the contents of each script inventory back to the ammo, fuel, trunk, trash, or grid inventory of the new vehicle. As far as I can tell, this should also raise on_equipment_inserted, which my mod Autodrive depends on to keep track of its active sensors. :-D

11 months ago

"crawler-corpse-dummy" is the name of the entity that my mod auto generates, not the original corpse entity. That would be "crawler-corpse", which is the entity underneath the invisible corpse dummy, which comes from the "crawler" entity's corpse list. The reason it's storing LuaEntityPrototype in names is because that's what LuaEntity::corpses is, which I need in order to tell which entity to find and delete when the corpse dummy gets removed. If I wanted to save the original vehicle name, I would do so in the on_entity_destroyed event where this data is generated in the first place.

I'm not sure I like the idea of being able to instantly restore vehicles from their corpses. I'm imagining someone's using a vehicle to take out some biters, it gets destroyed, and they just instantly plonk down another vehicle and get in like nothing happened. It feels a little unbalanced, but I'm not completely against it either. Maybe at some point I will add it, perhaps with some "transfer time" or something. I think for now I'm just going to make it so the real corpse doesn't get deleted when you build something on top of it.

11 months ago

"crawler-corpse-dummy" is the name of the entity that my mod auto generates, not the original corpse entity. That would be "crawler-corpse", which is the entity underneath the invisible corpse dummy, which comes from the "crawler" entity's corpse list.

I realize that, what I meant is this would allow to extract the name of the original prototype.

If I wanted to save the original vehicle name, I would do so in the on_entity_destroyed event where this data is generated in the first place.

Makes sense. Sometimes I tend to make things unnecessarily complicated. :-D

I'm not sure I like the idea of being able to instantly restore vehicles from their corpses. I'm imagining someone's using a vehicle to take out some biters, it gets destroyed, and they just instantly plonk down another vehicle and get in like nothing happened.

Yes, makes sense as well!

Maybe at some point I will add it, perhaps with some "transfer time" or something.

OK. I also wouldn't mind if that was an optional feature. (I guess a map/global setting would be most fitting, so that it applies to all players and nobody gets an advantage over the others.)

Anyway, thanks for taking the time to discuss your mod with me! I'll probably get back to you later on about your "Simple Vehicle Physics" mod. Still must investigate a bit, but it seems that your mod causes problems with vehicle bouncing in Autodrive …

New response