-
Notifications
You must be signed in to change notification settings - Fork 3
/
commands.lua
137 lines (120 loc) · 3.85 KB
/
commands.lua
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
134
135
136
137
local _ENV=mkmodule('hack.scripts.http.commands')
local core=require 'hack.scripts.http.core'
local jobs=require 'hack.scripts.http.jobs'
function respond_json_equip(server,cmd,cookies,user,unit)
if not cmd.id or not tonumber(cmd.id) then return nil,"invalid_item_id" end
local item=df.item.find(tonumber(cmd.id))
if not item then return nil,"item_not_found" end
local ret=jobs.pickup_equipment(unit,item)
if not ret then return nil,"failed" end
return "{}"
end
function respond_json_haul(server,cmd,cookies,user,unit)
if not cmd.id or not tonumber(cmd.id) then return nil,"invalid_item_id" end
local item=df.item.find(tonumber(cmd.id))
if not item then return nil,"item_not_found" end
local dx=item.pos.x-unit.pos.x
local dy=item.pos.y-unit.pos.y
local dz=item.pos.z-unit.pos.z
if dx ~=0 or dy~=0 or dz~=0 then return nil,"item_too_far" end
local ret=dfhack.items.moveToInventory(item,unit,0,0)
if not ret then return nil,"failed" end
return "{}"
end
function respond_json_drop_haul(server,cmd,cookies,user,unit)
local item
for i,v in ipairs(unit.inventory) do
if v.mode==0 then
item=v
break
end
end
if not item then return nil,"no_item_hauled" end
local ret=dfhack.items.moveToGround(item.item,copyall(unit.pos))
if not ret then return nil,"failed_to_drop" end
return "{}"
end
function dir_signs( dx,dy )
local sx,sy
sx=0
sy=0
if dx>0 then sx=1 end
if dy>0 then sy=1 end
if dx<0 then sx=-1 end
if dy<0 then sy=-1 end
return sx,sy
end
function move_unit( unit,dx,dy,dz )
local tx=unit.pos.x+dx
local ty=unit.pos.y+dy
unit.idle_area.x=tx
unit.idle_area.y=ty
--unit.idle_area_type=df.unit_station_type.Guard
--unit.idle_area_type=df.unit_station_type.DungeonCommander
unit.idle_area_type=df.unit_station_type.SquadMove
unit.idle_area_threshold=0
--ramp fixup TODO: could be plugin setting
if dfhack.maps.isValidTilePos(tx,ty,unit.pos.z) and dz==0 then
local attrs = df.tiletype.attrs
local tt=dfhack.maps.getTileType(tx,ty,unit.pos.z)
local td,to=dfhack.maps.getTileFlags(tx,ty,unit.pos.z)
if attrs[tt].shape==df.tiletype_shape.RAMP_TOP then --down is easy, just move down
unit.idle_area.z=unit.pos.z-1
elseif attrs[tt].shape==df.tiletype_shape.RAMP then --up is harder. Try stepping in same general direction...
local sx,sy=dir_signs(dx,dy)
unit.idle_area.x=unit.idle_area.x+sx
unit.idle_area.y=unit.idle_area.y+sy
unit.idle_area.z=unit.pos.z+1
end
else
unit.idle_area.z=unit.pos.z+dz
end
--invalidate old path
unit.path.dest={x=unit.idle_area.x,y=unit.idle_area.y,z=unit.idle_area.z}
unit.path.goal=88 --SQUAD STATION
unit.path.path.x:resize(0)
unit.path.path.y:resize(0)
unit.path.path.z:resize(0)
return true
end
function respond_json_move( server,cmd,cookies,user,unit )
if unit.flags1.dead then return nil,"dead" end
if not cmd.dx or not tonumber(cmd.dx) then return nil,'invalid_dx' end
if not cmd.dy or not tonumber(cmd.dy) then return nil,'invalid_dy' end
local dz=0
if cmd.dz and tonumber(cmd.dz) then dz=tonumber(cmd.dz) end
local dx=tonumber(cmd.dx)
local dy=tonumber(cmd.dy)
move_unit(unit,dx,dy,dz)
return "{}"
end
function respond_gamestate(server, plug, req, state, hidden)
local unit=hidden.unit
if unit==nil then
return false
end
local m=req.move
if m==nil then
return true
end
if m.dx==0 and m.dy==0 and m.dz==0 then
return true
end
state.move=move_unit(unit,m.dx,m.dy,m.dz)
return true
end
serv_commands=defclass(serv_commands,core.serv_plugin)
serv_commands.ATTRS={
expose_json={
--items
haul_item={data=respond_json_haul,needs_user=true,needs_unit=true},
drop_hauled_item={data=respond_json_drop_haul,needs_user=true,needs_unit=true},
equip_item={data=respond_json_equip,needs_user=true,needs_unit=true},
--movement
move_unit={data=respond_json_move,needs_user=true,needs_unit=true},
},
gamestate_hook=respond_gamestate,
name="commands",
}
plug=serv_commands
return _ENV