Exemplo n.º 1
0
        /// <summary>
        /// Adds the specified amount of items to the character's inventory.
        /// </summary>
        /// <remarks>
        /// Parameters:
        /// - int itemId
        /// - int amount
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int additem(IntPtr L)
        {
            var conn      = this.GetConnectionFromState(L);
            var character = conn.SelectedCharacter;

            var itemId = Melua.luaL_checkinteger(L, 1);
            var amount = Melua.luaL_checkinteger(L, 2);

            Melua.lua_pop(L, 2);

            var itemData = ChannelServer.Instance.Data.ItemDb.Find(itemId);

            if (itemData == null)
            {
                return(Melua.melua_error(L, "Unknown item id."));
            }

            try
            {
                character.Inventory.Add(itemId, amount, InventoryAddType.PickUp);
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
                return(Melua.melua_error(L, "Failed to add item to inventory."));
            }

            return(0);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sends dialog numberrange message, showing a message and a small text field,
        /// for the user to put in a number.
        /// </summary>
        /// <remarks>
        /// NUMBERRANGE uses CZ_DIALOG_SELECT for its response,
        /// which means the number range is that of a byte, 0~255.
        ///
        /// Parameters:
        /// - string message
        /// - int min (optional, defaults to 0)
        /// - int max (optional, defaults to 255)
        ///
        /// Result:
        /// The number put in by the user.
        /// Returns 0 on error.
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int numinput(IntPtr L)
        {
            // Check arguments and return 0 on error
            var argc = Melua.lua_gettop(L);

            if (argc == 0)
            {
                Log.Warning("numinput: No arguments.");
                Melua.lua_pushinteger(L, 0);
                return(1);
            }

            var conn = this.GetConnectionFromState(L);

            int min = 0, max = 255;

            // Get arguments
            var msg = Melua.luaL_checkstring(L, 1);

            if (argc >= 3)
            {
                min = Melua.luaL_checkinteger(L, 2);
                max = Melua.luaL_checkinteger(L, 3);
            }

            Melua.lua_pop(L, argc);

            this.HandleCustomCode(conn, ref msg);
            this.AttachNpcName(conn, ref msg);

            Send.ZC_DIALOG_NUMBERRANGE(conn, msg, min, max);

            return(Melua.lua_yield(L, 1));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Removes the specified amount of items with the given id from
        /// character's inventory.
        /// </summary>
        /// <remarks>
        /// Parameters:
        /// - int itemId
        /// - int amount
        ///
        /// Result:
        /// - int removedCount
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int removeitem(IntPtr L)
        {
            var conn      = this.GetConnectionFromState(L);
            var character = conn.SelectedCharacter;

            var itemId = Melua.luaL_checkinteger(L, 1);
            var amount = Melua.luaL_checkinteger(L, 2);

            Melua.lua_pop(L, 2);

            var itemData = ChannelServer.Instance.Data.ItemDb.Find(itemId);

            if (itemData == null)
            {
                return(Melua.melua_error(L, "Unknown item id."));
            }

            amount = Math.Max(0, amount);

            var removed = character.Inventory.Remove(itemId, amount, InventoryItemRemoveMsg.Given);

            Melua.lua_pushinteger(L, removed);

            return(1);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Spawns monster.
        /// </summary>
        /// <remarks>
        /// Parameters:
        /// - int    monsterId
        /// - string mapName
        /// - float  x
        /// - float  y
        /// - float  z
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int spawn(IntPtr L)
        {
            var monsterId = Melua.luaL_checkinteger(L, 1);
            var mapName   = Melua.luaL_checkstring(L, 2);
            var x         = (float)Melua.luaL_checknumber(L, 3);
            var y         = (float)Melua.luaL_checknumber(L, 4);
            var z         = (float)Melua.luaL_checknumber(L, 5);

            Melua.lua_pop(L, 5);

            var map = ChannelServer.Instance.World.GetMap(mapName);

            if (map == null)
            {
                return(Melua.melua_error(L, "Map '{0}' not found.", mapName));
            }

            var monster = new Monster(monsterId, NpcType.Monster);

            monster.Position = new Position(x, y, z);

            map.AddMonster(monster);

            return(0);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Changes the player's hairstyle.
        /// </summary>
        /// <remarks>
        /// Parameters:
        /// - int hairId
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int changehair(IntPtr L)
        {
            var conn      = this.GetConnectionFromState(L);
            var character = conn.SelectedCharacter;

            var hairId = Melua.luaL_checkinteger(L, 1);

            Melua.lua_pop(L, 1);

            character.Hair = (byte)hairId;
            Send.ZC_UPDATED_PCAPPEARANCE(character);

            return(0);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Returns amount of items with the given id in character's inventory.
        /// </summary>
        /// <remarks>
        /// Parameters:
        /// - int itemId
        ///
        /// Result:
        /// - int amount
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int countitem(IntPtr L)
        {
            var conn      = this.GetConnectionFromState(L);
            var character = conn.SelectedCharacter;

            var itemId = Melua.luaL_checkinteger(L, 1);

            Melua.lua_pop(L, 1);

            var result = character.Inventory.CountItem(itemId);

            Melua.lua_pushinteger(L, result);

            return(1);
        }
Exemplo n.º 7
0
        public void createtable()
        {
            var L = Melua.luaL_newstate();

            Melua.melua_openlibs(L);

            var rnd = new Random(Environment.TickCount);

            var foo      = 0;
            var bar      = "";
            var xyz      = "";
            var checkFoo = rnd.Next();
            var checkBar = rnd.Next().ToString("X8").Substring(1, 6);
            var checkXyz = rnd.Next().ToString("X8").Substring(2, 5);

            Melua.melua_register(L, "getdata", NL =>
            {
                Melua.melua_createtable(NL);
                Melua.melua_createfield(NL, "foo", checkFoo);
                Melua.melua_createfield(NL, "bar", checkBar);

                Melua.melua_startsubtable(NL, "foobar");
                Melua.melua_createfield(NL, "xyz", checkXyz);
                Melua.melua_endsubtable(NL);

                return(1);
            });

            Melua.melua_register(L, "checkdata", NL =>
            {
                foo = Melua.luaL_checkinteger(NL, 1);
                bar = Melua.luaL_checkstring(NL, 2);
                xyz = Melua.luaL_checkstring(NL, 3);

                Melua.lua_settop(L, 0);

                return(0);
            });

            var result = Melua.luaL_dostring(L, @"
local data = getdata()
checkdata(data.foo, data.bar, data.foobar.xyz)
");

            Assert.Equal(checkFoo, foo);
            Assert.Equal(checkBar, bar);
            Assert.Equal(checkXyz, xyz);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Adds NPC to world.
        /// </summary>
        /// <remarks>
        /// The parameter `dialogFunctionName` can be the name of a Lua
        /// function name, the name of a client-side dialog, or a
        /// localization key. A client-side dialog controls the NPC name
        /// and appearance, while a localization key will simply send the
        /// key in one message. A Lua function allows for completely
        /// custom dialog.
        ///
        /// Parameters:
        /// - int monsterId
        /// - string name / dictId
        /// - string mapName
        /// - number x
        /// - number y
        /// - number z
        /// - int    direction
        /// - string dialogFunctionName
        /// </remarks>
        /// <param name="L"></param>
        /// <returns></returns>
        private int addnpc(IntPtr L)
        {
            var monsterId = Melua.luaL_checkinteger(L, 1);
            var name      = Melua.luaL_checkstring(L, 2);
            var mapName   = Melua.luaL_checkstring(L, 3);
            var x         = (float)Melua.luaL_checknumber(L, 4);
            var y         = (float)Melua.luaL_checknumber(L, 5);
            var z         = (float)Melua.luaL_checknumber(L, 6);
            var direction = Melua.luaL_checkinteger(L, 7);
            var dialog    = Melua.luaL_checkstring(L, 8);

            Melua.lua_pop(L, 8);

            var map = ChannelServer.Instance.World.GetMap(mapName);

            if (map == null)
            {
                return(Melua.melua_error(L, "Map '{0}' not found.", mapName));
            }

            // Wrap name in localization code if applicable
            if (this.IsLocalizationKey(name))
            {
                name = this.WrapLocalizationKey(name);
            }

            var monster = new Monster(monsterId, NpcType.NPC);

            monster.Name       = name;
            monster.DialogName = dialog;
            monster.Position   = new Position(x, y, z);
            monster.Direction  = new Direction(direction);

            map.AddMonster(monster);

            return(0);
        }
Exemplo n.º 9
0
Arquivo: Melua.cs Projeto: xyfc/Melua
        public void userdata()
        {
            var L = Melua.luaL_newstate();

            Melua.melua_opensafelibs(L);

            var n1 = 0;

            // Ctor
            Melua.luaL_register(L, "Test", new[]
            {
                new MeluaLib.Melua.LuaLib("new", NL =>
                {
                    var test = new UserDataTest()
                    {
                        N1 = 1234
                    };
                    var size = Marshal.SizeOf(test);

                    var ptr = Melua.lua_newuserdata(L, size);
                    Melua.luaL_getmetatable(L, "Melua.Test");
                    Melua.lua_setmetatable(L, -2);

                    Marshal.StructureToPtr(test, ptr, true);

                    return(1);
                })
            });

            // Meta table for test userdata type
            Melua.luaL_newmetatable(L, "Melua.Test");
            Melua.lua_pushstring(L, "__index");
            Melua.lua_pushvalue(L, -2);
            Melua.lua_settable(L, -3);

            Melua.luaL_register(L, null, new[]
            {
                new MeluaLib.Melua.LuaLib("setN1", _ =>
                {
                    var ptr = Melua.luaL_checkudata(L, 1, "Melua.Test");
                    var val = Melua.luaL_checkinteger(L, 2);

                    // Either marshal back and forth or use unsafe
                    var test = (UserDataTest)Marshal.PtrToStructure(ptr, typeof(UserDataTest));
                    test.N1  = val;
                    Marshal.StructureToPtr(test, ptr, true);

                    //unsafe
                    //{
                    //	var test = (UserDataTest*)ptr;
                    //	test->N1 = val;
                    //}

                    return(0);
                }),
                new MeluaLib.Melua.LuaLib("getN1", _ =>
                {
                    var ptr  = Melua.luaL_checkudata(L, 1, "Melua.Test");
                    var test = (UserDataTest)Marshal.PtrToStructure(ptr, typeof(UserDataTest));

                    Melua.lua_pushinteger(L, test.N1);

                    return(1);
                })
            });

            // Test method
            Melua.melua_register(L, "testgetn1", _ =>
            {
                n1 = Melua.lua_tointeger(L, -1);

                return(0);
            });

            var result = Melua.luaL_dostring(L, @"
local t = Test.new()
t:setN1(5678)
testgetn1(t:getN1())
");

            if (result != 0)
            {
                throw new Exception(Melua.lua_tostring(L, -1));
            }

            Assert.Equal(n1, 5678);
        }