Added license and modularized
[feedlot.git] / fakeplayer.lua
1 -- mostly taken from the digtron mod
2
3 -- Copyright (C) 2017 FaceDeer, licensed under MIT license
4
5 -- The purpose of this class is to have something that can be passed into callbacks that
6 -- demand a "Player" object as a parameter and hopefully prevent the mods that have
7 -- registered with those callbacks from crashing on a nil dereference or bad function
8 -- call. This is not supposed to be a remotely functional thing, it's just supposed
9 -- to provide dummy methods and return values of the correct data type for anything that
10 -- might ignore the false "is_player()" return and go ahead and try to use this thing
11 -- anyway.
12
13 -- I'm trying to patch holes in bad mod programming, essentially. If a mod is so badly
14 -- programmed that it crashes anyway there's not a lot else I can do on my end of things.
15
16
17
18 feedlotFakePlayer = {}
19 feedlotFakePlayer.__index = feedlotFakePlayer
20
21 local function return_value(x)
22 return (function() return x end)
23 end
24
25 local function return_nil()
26 return nil
27 end
28
29 local function return_empty_string()
30 return ""
31 end
32
33 local function return_zero()
34 return 0
35 end
36
37 local function return_empty_table()
38 return {}
39 end
40
41 function feedlotFakePlayer.update(self, pos, player_name)
42 self.is_fake_player = ":digtron " .. player_name
43 self.get_pos = return_value(pos)
44 end
45 local function get_feed (pos)
46 return function ()
47 local meta = minetest.get_meta(pos)
48 local inv = meta:get_inventory()
49 if not inv or not inv:get_list("main") then
50 return ItemStack(nil)
51 end
52 -- minetest.chat_send_all(inv:get_stack("main", 1):get_name())
53 return inv:get_stack("main", 1)
54 end
55 end
56 local function set_feed (pos)
57 return function (_, stack)
58 local meta = minetest.get_meta(pos)
59 local inv = meta:get_inventory()
60 inv:set_stack("main", 1, stack)
61 return
62 end
63 end
64
65 function feedlotFakePlayer.create(pos, player_name)
66 local self = {}
67 setmetatable(self, feedlotFakePlayer)
68
69 self.is_fake_player = ":digtron " .. player_name
70
71 -- ObjectRef
72 self.get_pos = return_value(pos)
73 self.set_pos = return_nil
74 self.move_to = return_nil
75 self.punch = return_nil
76 self.right_click = return_nil
77 self.get_hp = return_value(10)
78 self.set_hp = return_nil
79 self.get_inventory = return_nil -- returns an `InvRef`
80 self.get_wield_list = return_empty_string
81 self.get_wield_index = return_value(1)
82 -- self.get_wielded_item = return_value(ItemStack(nil))
83 self.get_wielded_item = get_feed(pos)
84 self.set_wielded_item = set_feed(pos)
85
86 self.set_armor_groups = return_nil
87 self.get_armor_groups = return_empty_table
88 self.set_animation = return_nil
89 self.get_animation = return_nil -- a set of values, maybe important?
90 self.set_attach = return_nil
91 self.get_attach = return_nil
92 self.set_detach = return_nil
93 self.set_bone_position = return_nil
94 self.get_bone_position = return_nil
95 self.set_properties = return_nil
96 self.get_properties = return_empty_table
97
98 self.is_player = return_value(false)
99
100 self.get_nametag_attributes = return_empty_table
101 self.set_nametag_attributes = return_nil
102
103 --LuaEntitySAO
104 self.set_velocity = return_nil
105 self.get_velocity = return_value({x=0,y=0,z=0})
106 self.set_acceleration = return_nil
107 self.get_acceleration = return_value({x=0,y=0,z=0})
108 self.set_yaw = return_nil
109 self.get_yaw = return_zero
110 self.set_texture_mod = return_nil
111 self.get_texture_mod = return_nil -- maybe important?
112 self.set_sprite = return_nil
113 --self.get_entity_name` (**Deprecated**: Will be removed in a future version)
114 self.get_luaentity = return_nil
115
116 -- Player object
117
118 self.get_player_name = return_empty_string
119 self.get_player_velocity = return_nil
120 self.get_look_dir = return_value({x=0,y=1,z=0})
121 self.get_look_horizontal = return_zero
122 self.set_look_horizontal = return_nil
123 self.get_look_vertical = return_zero
124 self.set_look_vertical = return_nil
125
126 --self.get_look_pitch`: pitch in radians - Deprecated as broken. Use `get_look_vertical`
127 --self.get_look_yaw`: yaw in radians - Deprecated as broken. Use `get_look_horizontal`
128 --self.set_look_pitch(radians)`: sets look pitch - Deprecated. Use `set_look_vertical`.
129 --self.set_look_yaw(radians)`: sets look yaw - Deprecated. Use `set_look_horizontal`.
130 self.get_breath = return_value(10)
131 self.set_breath = return_nil
132 self.get_attribute = return_nil
133 self.set_attribute = return_nil
134
135 self.set_inventory_formspec = return_nil
136 self.get_inventory_formspec = return_empty_string
137 self.get_player_control = return_value({jump=false, right=false, left=false, LMB=false, RMB=false, sneak=false, aux1=false, down=false, up=false} )
138 self.get_player_control_bits = return_zero
139
140 self.set_physics_override = return_nil
141 self.get_physics_override = return_value({speed = 1, jump = 1, gravity = 1, sneak = true, sneak_glitch = false, new_move = true,})
142
143
144 self.hud_add = return_nil
145 self.hud_remove = return_nil
146 self.hud_change = return_nil
147 self.hud_get = return_nil -- possibly important return value?
148 self.hud_set_flags = return_nil
149 self.hud_get_flags = return_value({ hotbar=true, healthbar=true, crosshair=true, wielditem=true, breathbar=true, minimap=true })
150 self.hud_set_hotbar_itemcount = return_nil
151 self.hud_get_hotbar_itemcount = return_zero
152 self.hud_set_hotbar_image = return_nil
153 self.hud_get_hotbar_image = return_empty_string
154 self.hud_set_hotbar_selected_image = return_nil
155 self.hud_get_hotbar_selected_image = return_empty_string
156 self.set_sky = return_nil
157 self.get_sky = return_empty_table -- may need members on this table
158
159 self.set_clouds = return_nil
160 self.get_clouds = return_value({density = 0, color = "#fff0f0e5", ambient = "#000000", height = 120, thickness = 16, speed = {x=0, y=-2}})
161
162 self.override_day_night_ratio = return_nil
163 self.get_day_night_ratio = return_nil
164
165 self.set_local_animation = return_nil
166 self.get_local_animation = return_empty_table
167
168 self.set_eye_offset = return_nil
169 self.get_eye_offset = return_value({x=0,y=0,z=0},{x=0,y=0,z=0})
170
171 return self
172 end
173