forked from Ascerr/Lua-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnergy Wave Stand.lua
More file actions
133 lines (117 loc) · 4.33 KB
/
Energy Wave Stand.lua
File metadata and controls
133 lines (117 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
--[[
Script Name: Energy Wave Stand
Description: Reach best position to shoot wave in blocker with monsters
Author: Ascer - example
]]
local BLOCKER = "Player nick" -- nick of blocker.
local ALLOW_WALK_IDS = {123, 2118, 2119, 2123, 2125, 2124} -- enter here id such as parcels, boxes, fields etc we check for it.
----------------------------------------------------------------------------------------------------------------------------------------------------------
--> Function: tileIsWalkable(x, y, z)
--> Description: Check is tile is walkable // Medivia only
--> Params:
--> @x coordinate in the map on the x-axis
--> @y coordinate in the map on the y-axis
--> @z coordinate in the map on the z-axis
-->
--> Return: boolean true or false
----------------------------------------------------------------------------------------------------------------------------------------------------------
function tileIsWalkable(x, y, z)
local path = 0
local items = Map.GetItems(x, y, z)
for i, item in ipairs(items) do
if item.id == 99 then return false end
if Item.hasAttribute(item.id, ITEM_FLAG_NOT_WALKABLE) then return false end
if Item.hasAttribute(item.id, ITEM_FLAG_NOT_PATHABLE) then
if table.find(ALLOW_WALK_IDS, item.id) then
path = path + 1
if path >= 2 then return false end
else
return false
end
end
end
return true
end
----------------------------------------------------------------------------------------------------------------------------------------------------------
--> Function: getBlocker()
--> Description: Get your blocker
--> Params: None
-->
--> Return: -1 if no creature else table with creature
----------------------------------------------------------------------------------------------------------------------------------------------------------
function getAttackedCreature()
local c = Creature.getCreatures(BLOCKER)
if table.count(c) < 2 then return -1 end
return c
end
----------------------------------------------------------------------------------------------------------------------------------------------------------
--> Function: reachBlocker()
--> Description: Get possible best way to reach blocker
--> Params: None
-->
--> Return: -1 if no creature else table with creature
----------------------------------------------------------------------------------------------------------------------------------------------------------
function reachBlocker()
local t = getAttackedCreature()
if t ~= -1 then
-- load self pos
local selfpos = Self.Position()
-- load positions available positions
local positions = {{x=t.x-4, y=t.y, z=t.z, walkable=false, dir=1}, {x=t.x+4, y=t.y, z=t.z, walkable=false, dir=3}, {x=t.x, y=t.y+4, z=t.z, walkable=false, dir=0}, {x=t.x, y=t.y-4, z=t.z, walkable=false, dir=2}}
-- check if we are on pos.
for i, pos in ipairs(positions) do
if (pos.x == selfpos.x and pos.y == selfpos.y and pos.z == selfpos.z) then
if Self.Direction() ~= pos.dir then
Self.Turn(pos.dir)
end
return true
end
end
-- check if pos is walkable.
for i, pos in ipairs(positions) do
if tileIsWalkable(pos.x, pos.y, pos.z) then
positions[i].walkable = true
end
end
-- set params for calc
local far, lastpos = 12, -1
-- get the possible close way:
for i, pos in ipairs(positions) do
if pos.walkable then
local dist = Self.DistanceFromPosition(pos.x, pos.y, pos.z)
if dist < far then
far = dist
lastpos = pos
end
end
end
-- when no pos.
if lastpos == -1 then
local near = {}
for x = -1, 1 do
for y = -1, 1 do
if tileIsWalkable(t.x+x, t.y+y, t.z) then
table.insert(near, {x=t.x+x, y=t.y+y, z=t.z})
end
end
end
for i, pos in ipairs(near) do
local dist = Self.DistanceFromPosition(pos.x, pos.y, pos.z)
if dist < far then
far = dist
lastpos = pos
end
end
end
-- when no last post after calculation break
if lastpos == -1 then return false end
-- walkto pos
Self.WalkTo(lastpos.x, lastpos.y, lastpos.z)
end
end
-- module run function in loop 200ms
Module.New("Energy Wave Stand", function()
if Self.isConnected() then
reachBlocker()
end
end)