Advertisement
rater193_james

NPCAPI

Aug 27th, 2015
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.86 KB | None | 0 0
  1.  
  2. --the table for storing all the functions
  3. NPCEngine = {}
  4.  
  5. --for storing all the data for the NPCs
  6. NPCEngine.Data = {}
  7.  
  8. --the list of stored entities
  9. NPCEngine.Data.entities = {}
  10. --the list of stored objects
  11. NPCEngine.Data.objects = {}
  12.  
  13. --for creating a basic fireable event
  14. local function createEvent()
  15.     --the returning table
  16.     local ret = {}
  17.    
  18.     --for storeing the list of instances that we create for bindable functions
  19.     ret.instances = {}
  20.    
  21.     --for fireing the event, with varied arguments
  22.     function ret:fire(...)
  23.         for i, v in pairs(ret.instances) do
  24.             v(...)
  25.         end
  26.     end
  27.    
  28.     --for registering new events
  29.     function ret:connect(_f)
  30.         table.insert(ret.instances, _f)
  31.     end
  32.    
  33.     --then returning the new bindable event
  34.     return ret
  35. end
  36.  
  37. --for registering new NPCs
  38. function NPCEngine:new(npcname, model, texture)
  39.     local npc = {}
  40.    
  41.     local entname = ""
  42.         default.player_register_model(model, {
  43.         animation_speed = 30,
  44.         textures = {texture, },
  45.         animations = {
  46.             -- Standard animations.
  47.             stand     = { x=  0, y= 79, },
  48.             lay       = { x=162, y=166, },
  49.             walk      = { x=168, y=187, },
  50.             mine      = { x=189, y=198, },
  51.             walk_mine = { x=200, y=219, },
  52.             -- Extra animations (not currently used by the game).
  53.             sit       = { x= 81, y=160, },
  54.         },
  55.     })
  56.  
  57.    
  58.     minetest.register_entity(npcname,{
  59.         hp_max = 1,
  60.         physical = true,
  61.         weight = 5,
  62.         collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
  63.         visual = "mesh",
  64.         visual_size = {x=1, y=1},
  65.         mesh = "NPCBasic.b3d",
  66.         textures = {"battletowers_npcs_skin_trader.png"}, -- number of required textures depends on visual
  67.         colors = {}, -- number of required colors depends on visual
  68.         spritediv = {x=1, y=1},
  69.         initial_sprite_basepos = {x=0, y=0},
  70.         is_visible = true,
  71.         makes_footstep_sound = false,
  72.         automatic_rotate = false,
  73.         entidtest=123,
  74.         loaded=false,
  75.         data = {},
  76.         on_rightclick = function(self, clicker)
  77.             print("click?")
  78.             --self.entidtest = math.random(20000)
  79.             print("entidtest:", self.entidtest)
  80.         end,
  81.         get_staticdata = function(self)
  82.             print("Get static data?")
  83.            
  84.             npc_data = {}
  85.             npc_data.data = {}
  86.            
  87.             for i, v in pairs(self.data) do
  88.                 --if type(i) ~= "function" then
  89.                     npc_data.data[i] = v
  90.                 --end
  91.             end
  92.            
  93.             npc.Events.OnSave:fire(self, npc_data)
  94.            
  95.             self.loaded = true
  96.             npc_data.loaded = true
  97.             return minetest.serialize(npc_data)
  98.         end,
  99.         on_activate = function(self, staticdata)
  100.             if self.loaded then
  101.                 --if staticdata then
  102.                     print("staticdata: ", staticdata)
  103.                     local data = minetest.deserialize(staticdata)
  104.                     print("data: ", data)
  105.                    
  106.                     for i, v in pairs(data) do
  107.                         --if type(i) ~= "function" then
  108.                             self.data[i] = v
  109.                         --end
  110.                     end
  111.                     print("[info] LOADING NPC DATA")
  112.                     npc.Events.OnLoad:fire(self, data)
  113.                 --end
  114.             else
  115.                 --print("[info] DATA:", staticdata)
  116.             end
  117.         end
  118.        
  119.     }
  120.     )
  121.    
  122.    
  123.  
  124.     function npc:spawn(...)
  125.         local args = {...}
  126.         local wantedpos = {x=0, y=0, z=0}
  127.         if #args == 1 then
  128.             --using pos
  129.             pos = args[1]
  130.             wantedpos = pos
  131.             npc.Events.OnSpawn:fire(pos)
  132.         elseif #args >= 3 then
  133.             --use x, y, z
  134.             local x, y, z = args[1], args[2], args[3]
  135.             wantedpos = {x = x, y = y, z = z}
  136.             --npc.Events.OnSpawn:fire(pos)
  137.         else
  138.             error("Invalid arguments, npc:spawn(pos) or npc:spawn(x,y,z)")
  139.             return nil
  140.         end
  141.         local obj = minetest.add_entity(wantedpos, npcname)
  142.        
  143.         return obj:get_luaentity()
  144.     end
  145.    
  146.     --for creating new events
  147.     --usage: event.EventName:connect(FiredFunction)
  148.     --event.EventName:fire(...) will throw the event with the arguments you specify
  149.     npc.Events = {}
  150.     npc.Events.OnSpawn = createEvent()
  151.     npc.Events.OnDied = createEvent()
  152.     npc.Events.OnUpdate = createEvent()
  153.     npc.Events.OnSave = createEvent()
  154.     npc.Events.OnLoad = createEvent()
  155.    
  156.     table.insert(NPCEngine.Data.objects, npc)
  157.    
  158.     return npc
  159. end
  160.  
  161.  
  162.  
  163. minetest.NPC = NPCEngine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement