Remove out of map projectiles! Or how to clean the beach

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: Remove out of map projectiles! Or how to clean the beach

Post by bongobeat »

thanks for both scripts. Don't know which I will use. The idea of destroying the projectile after it leaves the map suits me, because I like the nice reflecting colors of any attack spell on the water :lol:
Well, have to test it with a long play, to see if it slow down the fps.
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
User avatar
AndakRainor
Posts: 674
Joined: Thu Nov 20, 2014 5:18 pm

Re: Remove out of map projectiles! Or how to clean the beach

Post by AndakRainor »

Hey ! To slow down the fps with my last script you would need over 9000 entities in only one tile AND on a map border ! Don't tell me you want to do that :twisted:
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Remove out of map projectiles! Or how to clean the beach

Post by Azel »

AndakRainor wrote:Hey ! To slow down the fps with my last script you would need over 9000 entities in only one tile AND on a map border ! Don't tell me you want to do that :twisted:
Haste + Flame Wave spamfest? Could be fun :P
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: Remove out of map projectiles! Or how to clean the beach

Post by bongobeat »

who knows?
suddenly an army of spiting frog land on the beach, that could be fun
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Remove out of map projectiles! Or how to clean the beach

Post by Azel »

AndakRainor wrote:Here is a reworked version of the script. You can safely use it in only one script entity anywhere in your dungeon:

Code: Select all

performanceHit = 0.001 -- must be in [0.000001, 0.01]
vanishDistance = 300 -- in meters = tiles * 3

level = 1
tile = -1

--entities = 0
--duration = 0
--maps = 1

function check()
  local begin = Time.systemTime()
  local levels = Dungeon.getMaxLevels()
  local pHit = 0
  repeat 
    local map = Dungeon.getMap(level)
    local X, Y = map:getWidth()-1, map:getHeight()-1
    tile = tile+1
    local x, y
    if tile<X then x=tile y=0
    elseif tile<X+Y then x=X y=tile-X
    elseif tile<X+X+Y then x=X+X+Y-tile y=Y
    elseif tile<X+X+Y+Y then x=0 y=X+X+Y+Y-tile
    else
      tile=0 x=0 y=0 level = level % levels + 1
      map = Dungeon.getMap(level)
      X, Y = map:getWidth()-1, map:getHeight()-1
      --maps = maps+1
    end
    for e in map:entitiesAt(x,y) do
      if e.projectile then
        local w = e:getWorldPosition()
        --hudPrint(e.name.." at map("..e.x..", "..e.y..") world("..w.x..", "..w.y..", "..w.z..")")
        if w.x < -vanishDistance or w.y < -vanishDistance-21 or w.z < -vanishDistance
        or w.x > X*3+3+vanishDistance or w.y > vanishDistance+21 or w.z > Y*3+3+vanishDistance
        then e:destroy() end
      end
      --entities = entities + 1
    end
    pHit = Time.systemTime() - begin
  until pHit >= performanceHit
  local delay = pHit/performanceHit - pHit
  delayedCall(self.go.id, delay, "check")
  --duration = duration + pHit * 1000000000
  --print("time/map = "..math.floor(duration/maps).."ns, entities/map = "..math.floor(entities/maps)..", time/entity = "..math.floor(duration/entities).."ns, frame = 1/60s, entity/frame = "..math.floor(entities/duration * 1000000000/60)..", delay = "..math.floor(delay*1000).."ms")
end

check()
As the previous ones, it auto starts and is called periodically.
You can set the performanceHit and vanishDistance values to your liking. The first one is a ratio of time used to run the script, so it should be a small value, near 0, but never 0 or it won't work. The second is in meters, as it is used with tests on the projectiles world positions instead of time spend out of maps. It does not test all entities in a map anymore but only those on borders. If you want to see what it does, you can uncomment the commented lines (times are shown in ns = nanoseconds).

Here is the data I got when testing performance hits with my previous scripts (hours of data);

Code: Select all

+-----------------+----------------+------------------+-----------------+---------------------+
|    loop type    |  time per map  | entities per map | time per entity | entities per frame* |
|                 | (milliseconds) |                  | (milliseconds)  |  (perf hit limit)   |
+-----------------+----------------+------------------+-----------------+---------------------+
|  all entities   | 1.724          | 1350             | 0.001275        | 13063               |
+-----------------+----------------+------------------+-----------------+---------------------+
| entities at x,y | 0.267          | 132              | 0.002016        | 8264                |
+-----------------+----------------+------------------+-----------------+---------------------+
(*): frame = 1/60 s = 16.667 ms
I'm adding this to my Mod today and testing it out!
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Remove out of map projectiles! Or how to clean the beach

Post by Azel »

Yessir, it's working fabulous. There are very few locations that allow players to sneak a fireball out over open water (usually because I place the Party at a high Elevation in an Open Area); but your script cleans it up very nicely. Players are reporting a good gaming experience thus far in my Mod, which contains this very script. Thanks for creating this, AndakRainor, you saved my bootay! :mrgreen:
Post Reply