You might have to invert the vector in the code below... I always mix up the operands of the subtraction.
This goes to after you spawned the new NPC. Replace invalid variable names with your proper names:
--------------------------------------------------------
local speed = 5
local distanceBetweenPlayerAndNPC = vector(player.x - npc.x, player.y - npc.y)
local distNormal = distanceBetweenPlayerAndNPC:normalize()
--can also derive speed from distance
--speed = distanceBetweenPlayerAndNPC.length * 0.01
newNPC.speedX = distNormal.x * speed
newNPC.speedY = distNormal.y * speed
--------------------------------------------------------
Documentation is here:
https://wohlsoft.ru/pgewiki/VectR.lua
In the PAL version, the vector class is automatically loaded like in the example above. the vector(a,b) constructor automatically interprets the vector's dimension. Like you can see on the documentation page, it's also possible to rotate vectors, which could be useful if you want to make some sort of spread shot (projectile 1 misses by -5 degrees, projectile 3 misses by +5 degrees)
P/s:
A tad tricky, considering the ability for blocks to be nonsolid or invisible. You basically have to run a getIntersecting check on the desired coordinates and then filter the results by anything the NPC might be able to pass through regardless of its status as a block. The two checks you're asking for is just the same check again then, but offset by a certain amount:
This goes to where you need to do your check:
--------------------------------------------------------
local hasHitValidBlock = false
for k,v in ipairs(Block.getIntersecting(leftEdge, topEdge, rightEdge, bottomEdge) do
if (not v.isHidden) and v:mem(0x5C, FIELD_WORD) == 0 and (not Block.NONSOLID_MAP[v.id]) then
hasHitValidBlock = true
break
end
end
if (hasHitValidBlock) then
--execute code that depends on the npc finding a solid block. invert the conditional if you require the npc to find a lack of blocks for this code to run
end
--------------------------------------------------------
Docs:
https://wohlsoft.ru/pgewiki/Block_(class)
The snippit assumes the PAL version, too. If you're on Beta 3, you'll need to load expandedDefines [[ local expandedDefines = API.load("expandedDefines") ]] and use expandedDefines.BLOCK_NONSOLID_MAP instead. The expandedDefines library is also where you're able to take a look at any block and npc lists I haven't listed in this post.
To get the block the NPC is standing on, you can store "v" in hasHitValidBlock, rather than true. That'll get you the first valid block you found. One thing to keep in mind: If the object you're checking on is standing on a top-solid NPC you might have to do the same checks for a NPC.getIntersecting check, checking for NPC.config[npc.id].playerblocktop and NPC.config[npc.id].npcblocktop.
Here's the docs for those:
https://docs.google.com/spreadsheets/d/ ... edit#gid=0
https://wohlsoft.ru/pgewiki/NPC_(class)
If you need to account for blocks that the NPC might be in FRONT of, the check gets a bit more complicated. the SEMISOLID_MAP includes top-solid and sizeable blocks and blocks you find from it should only be valid in your check if [[ block.y >= npc.y + npc.height ]] to ensure that the block's top bound is below the NPC's feet.
Hope this helps.