protected int Wander(IntPtr L) { var minDistance = 30; var maxDistance = 50; if (lua_gettop(L) > 0) { minDistance = maxDistance = luaL_checkinteger(L, 1); } if (lua_gettop(L) > 1) { maxDistance = luaL_checkinteger(L, 2); } lua_settop(L, 0); // Try to find a valid random destination. Official AIs // seem to only wander in one of the 4 major directions, // so if we check all 4 and don't find a valid destination, // we can't move. Just wait for a moment in that case. var destination = Position.Zero; var distance = _rnd.Next(minDistance, maxDistance + 1); var validDest = false; var pos = this.Entity.Position; var firstDir = _rnd.Next(4); for (var i = 0; i < 4; ++i) { var angle = ((firstDir + i) % 4) * 90; destination = pos.GetRelative(new Direction(angle), distance); if (this.Entity.Map.Ground.IsValidPosition(destination)) { validDest = true; break; } } if (validDest) { var routine = new MoveToRoutine(this, destination); this.StartRoutine(routine); } else { this.StartRoutine(new WaitRoutine(TimeSpan.FromSeconds(2))); } return(lua_yield(L, 0)); }
protected int MoveRandom(IntPtr L) { var radius = (float)luaL_checknumber(L, 1); lua_settop(L, 0); var destination = this.Entity.Position.GetRandomInRange2D((int)radius, _rnd); if (this.Entity.Map.Ground.IsValidPosition(destination)) { var routine = new MoveToRoutine(this, destination); this.StartRoutine(routine); } else { this.StartRoutine(new WaitRoutine(TimeSpan.FromSeconds(2))); } return(lua_yield(L, 0)); }
protected int MoveTo(IntPtr L) { var x = (float)luaL_checknumber(L, 1); var z = (float)luaL_checknumber(L, 2); lua_settop(L, 0); var destination = new Position(x, 0, z); if (this.Entity.Map.Ground.IsValidPosition(destination)) { var routine = new MoveToRoutine(this, destination); this.StartRoutine(routine); } else { this.StartRoutine(new WaitRoutine(TimeSpan.FromSeconds(2))); } return(lua_yield(L, 0)); }
protected int MoveRelative(IntPtr L) { var addX = (float)luaL_checknumber(L, 1); var addZ = (float)luaL_checknumber(L, 2); lua_settop(L, 0); var destination = this.Entity.Position; destination.X += addX; destination.Z += addZ; if (this.Entity.Map.Ground.IsValidPosition(destination)) { var routine = new MoveToRoutine(this, destination); this.StartRoutine(routine); } else { this.StartRoutine(new WaitRoutine(TimeSpan.FromSeconds(2))); } return(lua_yield(L, 0)); }