Adventure-Terraria-Server-Project / AdvancedCircuits-Plugin

Advanced Circuits Plugin for Terraria Server API and TShock.
GNU General Public License v3.0
8 stars 8 forks source link

Motion Sensor Component #22

Open CoderCow opened 11 years ago

CoderCow commented 11 years ago

If a player, npc, mob comes into range, the component sends 1, if there are no more players closeby it sends 0. I think the Crystal Ball would be a good sprite to represent this component.

Suggested Modifiers: 1 - The component will only react on players comming closeby. 2 - The component will only react on NPCs comming closeby. 3 - The component will only react on mobs comming closeby.

The range of the sensor should be configurable. Also, since this component can take up quite some performance for the checking, a limit per player should be configuable.

Though, I'm not sure on how useful this component will be in the end. Especially because this is quite a lot of work to implement.

Ijwu commented 11 years ago

Wouldn't this cause a large amount of work on the server? The motion sensor would have to constantly check each npc/player/mob to tell if they're within its range. The amount of entities to check could get ridiculously high.

CoderCow commented 11 years ago

That depends on how tricky the implementation is.

Players, due to their "small" amount are not much to worry about in general. For the mobs/npcs (I like to differ them usually but they are technically all just NPCs - same array), their array is limited to 200 at most, so each Motion Sensor would actually have to iterate 200 NPCs which is not much at all.

Proably the best fitting hook here is GameUpdate with a frame counter, let's say we want Motion Sensors to update on every second. I could easy peasy just let all Motion Sensors check the NPCs in range on every second, though if we had 10 Motion Sensors that would technically end up in 2000 iterations - that's still not much to worry about but it could become ugly with let's say 30 Sensors since they're all processed within one game frame - especially if we consider that there might be other plugins that might do some things on that frame.

So better to spread the performance over more than one frame, like checking NPCs 0-50 after the first 10 frames passed, 50-100 after the next 10 frames etc. If even this really leads to performance lacks though, one could also pimp up the in-range check itself in several ways, like simply forming a rectangle on Motion Sensor placement, covering the ranges of ALL Motion Sensors (that do NPC checks at all) which are currently placed on the map, if an NPC is not even in this rectangle, it would already be unnecessary to iterate through the Sensors for that NPC at all. And there are more ways to boost performance, if necessary.

zaqen commented 11 years ago

Would it be possible to arrange it so that these sensors could be turned off and on by a timer next to it? So that it check every time the timer send a pulse and the pulse will pass once the mobs are cleared.

CoderCow commented 11 years ago

Good idea, but this would indeed cause performance trouble.

zaqen commented 11 years ago

I might be wrong but i assumed that the sensor would sense NPCs in a larger area, lets say 50x10. Would tat cause a lot of performance issues? Because with this it's possible to make sort of a "tower defense" mode. I'm really exited about this working or not.

CoderCow commented 11 years ago

The size of the area to check makes pretty much no difference in matter of performance.