Exemple #1
0
        /// <summary>
        ///     Unloads the map data from the memory.
        /// </summary>
        public static void Unload()
        {
            if (_usePlugin)
            {
                _nativeUnload.Invoke();

                _usePlugin = false;
                _mode      = MapAndreasMode.None;
                return;
            }

            switch (_mode)
            {
            case MapAndreasMode.NoBuffer:
                _fileStream.Dispose();
                break;

            default:
                _data = null;
                break;
            }

            _data = null;
            _mode = MapAndreasMode.None;
        }
Exemple #2
0
 public void Visit(Arguments t, IEnvironment callerEnv, object value)
 {
     if (value is NativeFunction)
     {
         NativeFunction func    = value as NativeFunction;
         int            nparams = func.NumOfParameters;
         if (t.Size != nparams)
         {
             result = new ErrorValue("bad number of arguments", t);
             return;
         }
         object[] args = new object[nparams];
         int      num  = 0;
         foreach (ASTree a in t)
         {
             a.Accept(this, callerEnv);
             if (result is ErrorValue)
             {
                 return;
             }
             args[num++] = result;
         }
         result = func.Invoke(args, t);
         return;
     }
     if (value is Function)
     {
         Function      func = value as Function;
         ParameterList pars = func.Parameters;
         if (t.Size != pars.Size)
         {
             result = new ErrorValue("bad number of arguments", t);
             return;
         }
         IEnvironment newEnv = func.MakeEnv();
         int          num    = 0;
         foreach (ASTree a in t)
         {
             a.Accept(this, callerEnv);
             if (result is ErrorValue)
             {
                 return;
             }
             pars.Accept(this, newEnv, num++, result);
             if (result is ErrorValue)
             {
                 return;
             }
         }
         func.Body.Accept(this, newEnv);
         return;
     }
     result = new ErrorValue("bad function", t);
     return;
 }
Exemple #3
0
        private object EvalNativeFunction(IEnvironment environment, object value)
        {
            NativeFunction nativeFunction = (NativeFunction)value;

            if (this.Size != nativeFunction.NumberOfParameters)
            {
                throw new StoneException("Bad number of arguments", this);
            }

            int index = 0;

            object[] arguments = new object[nativeFunction.NumberOfParameters];

            foreach (ASTNode astNode in this)
            {
                arguments[index++] = astNode.Eval(environment);
            }

            return(nativeFunction.Invoke(arguments, this));
        }
Exemple #4
0
        // End test code.

        #region Overrides of BaseMode

        protected override void OnInitialized(EventArgs args)
        {
            float x, y, z;

            SetGameModeText("ABCDE");
            int id = Native.CreateVehicle(400, 10f, 20f, 30f, 40f, -1, -1, 30);

            var func = new NativeFunction("GetVehiclePos", typeof(int), typeof(float).MakeByRefType(),
                                          typeof(float).MakeByRefType(), typeof(float).MakeByRefType());

            func.Invoke(__arglist(id, out x, out y, out z));
            Console.WriteLine("Invoke NativeFunction: {0}, {1}, {2}", x, y, z);

            GetVehiclePos(id, out x, out y, out z);
            Console.WriteLine("Delegate: {0}, {1}, {2}", x, y, z);
            string outp;

            GetNetworkStats(out outp, 500);
            //Console.WriteLine(outp);
            return;

            Console.WriteLine("TestMode for SampSharp");
            Console.WriteLine("----------------------");

            Server.ToggleDebugOutput(true);
            SetGameModeText("sa-mp# testmode");
            UsePlayerPedAnimations();

            AddPlayerClass(65, new Vector3(5), 0);

            foreach (ITest test in _tests)
            {
                Console.WriteLine("=========");
                Console.WriteLine("Starting test: {0}", test);
                test.Start(this);
                Console.WriteLine();
            }

            base.OnInitialized(args);
        }
Exemple #5
0
        /// <summary>
        ///     Finds highest Z point (ground level) for the provided point.
        /// </summary>
        /// <param name="x">X-coordinate of the point.</param>
        /// <param name="y">Y-coordinate of the point.</param>
        /// <returns>Ground level at the given point.</returns>
        public static float Find(float x, float y)
        {
            if (_mode == MapAndreasMode.None)
            {
                return(0);
            }

            if (_usePlugin)
            {
                float result;
                _nativeFindZ.Invoke(__arglist(x, y, out result));
                return(result);
            }
            // check for a co-ord outside the map
            if (x < -3000.0f || x > 3000.0f || y > 3000.0f || y < -3000.0f)
            {
                return(0.0f);
            }

            // get row/col on 6000x6000 grid
            int iGridX = ((int)x) + 3000;
            int iGridY = (((int)y) - 3000) * -1;
            int iDataPos;

            switch (_mode)
            {
            case MapAndreasMode.Full:
                iDataPos = (iGridY * 6000) + iGridX;
                // for every Y, increment by the number of cols, add the col index.
                return(_data[iDataPos] / 100.0f);  // the data is a float stored as ushort * 100

            case MapAndreasMode.Minimal:
                iDataPos = ((iGridY / 3) * 2000) + iGridX / 3;
                // for every Y, increment by the number of cols, add the col index.
                return(_data[iDataPos] / 100.0f);  // the data is a float stored as ushort * 100
            }
            return(0.0f);
        }
Exemple #6
0
 public object Eval(Environment env, object value)
 {
     if (value.GetType() == typeof(SheFunction))
     {
         SheFunction   func    = (SheFunction)value;
         ParameterList @params = func.Parameters;
         if (Size() != @params.Size())
         {
             throw new SheException("bad number of arguments", this);
         }
         Environment newEnv = new NestedEnvironment(env);
         int         num    = 0;
         foreach (ASTree ast in this)
         {
             @params.Eval(newEnv, num++, ast.Eval(env));
         }
         return(func.Body.Eval(newEnv));
     }
     else if (value.GetType() == typeof(NativeFunction))
     {
         NativeFunction func = (NativeFunction)value;
         if (func.NumParams != NativeFunction.VariadicArg && func.NumParams != NumChildren())
         {
             throw new SheException("bad number of arguments", this);
         }
         object[] @params = new object[NumChildren()];
         for (int i = 0; i < NumChildren(); i++)
         {
             @params[i] = GetChild(i).Eval(env);
         }
         return(func.Invoke(@params, this));
     }
     else
     {
         throw new SheException("bad function", this);
     }
 }
Exemple #7
0
        /// <summary>
        ///     Calculates a linear approximation of the ground level at the provided point.
        /// </summary>
        /// <param name="x">X-coordinate of the point.</param>
        /// <param name="y">Y-coordinate of the point.</param>
        /// <returns>A approximation of the ground level at the given point.</returns>
        public static float FindAverage(float x, float y)
        {
            if (_mode == MapAndreasMode.None)
            {
                return(0);
            }

            if (_usePlugin)
            {
                float result;
                _nativeFindAvgZ.Invoke(__arglist(x, y, out result));
                return(result);
            }

            float gridsize = _mode == MapAndreasMode.Full ? 1 : 3;

            // Get the Z value of 2 neighbor grids
            float p1 = Find(x, y);
            float p2 = x < 0.0f ? Find(x + gridsize, y) : Find(x - gridsize, y);
            float p3 = y < 0.0f ? Find(x, y + gridsize) : Find(x, y - gridsize);

            // Filter the decimal part only
            float xx = x % 1;
            float yy = y % 1;

            if (xx < 0)
            {
                x = -xx;         //Pointless? shouldn't it be xx = -xx? TODO: figure that out
            }
            if (yy < 0)
            {
                y = -yy;
            }

            // Calculate a linear approximation of the z coordinate
            return(p1 + xx * (p1 - p2) + yy * (p1 - p3));
        }
Exemple #8
0
        /// <summary>
        ///     Loads the map data into the memory.
        /// </summary>
        /// <param name="mode">
        ///     The <see cref="MapAndreasMode" /> to load with.
        /// </param>
        /// <exception cref="FileLoadException">
        ///     Thrown if the file couldn't be
        ///     loaded.
        /// </exception>
        public static void Load(MapAndreasMode mode)
        {
            if (_mode != MapAndreasMode.None)
            {
                return;
            }
            _mode = mode;

            if (IsPluginLoaded())
            {
                _nativeInit             = new NativeFunction("MapAndreas_Init", typeof(int), typeof(string), typeof(int));
                _nativeUnload           = new NativeFunction("MapAndreas_Unload");
                _nativeFindZ            = new NativeFunction("MapAndreas_FindZ_For2DCoord", typeof(float), typeof(float), typeof(float).MakeByRefType());
                _nativeFindAvgZ         = new NativeFunction("MapAndreas_FindAverageZ", typeof(float), typeof(float), typeof(float).MakeByRefType());
                _nativeSetZ             = new NativeFunction("MapAndreas_SetZ_For2DCoord", typeof(float), typeof(float), typeof(float));
                _native_SaveCurrentHMap = new NativeFunction("MapAndreas_SaveCurrentHMap", typeof(string));

                _nativeInit.Invoke((int)mode, string.Empty, 1);
                _usePlugin = true;
                return;
            }

            switch (mode)
            {
            case MapAndreasMode.Full:
                try
                {
                    using (var memstream = new FileStream(FullFile, FileMode.Open))
                    {
                        _data = new ushort[memstream.Length / 2];
                        var buffer = new byte[2];
                        int loc    = 0;
                        while ((memstream.Read(buffer, 0, 2)) == 2)
                        {
                            _data[loc++] = BitConverter.ToUInt16(buffer, 0);
                        }
                    }
                }
                catch (Exception e)
                {
                    _mode = MapAndreasMode.None;
                    throw new FileLoadException("Couldn't load " + FullFile, e);
                }
                break;

            case MapAndreasMode.Minimal:
                try
                {
                    using (var memstream = new FileStream(MinimalFile, FileMode.Open))
                    {
                        _data = new ushort[memstream.Length / 2];
                        var buffer = new byte[2];
                        int loc    = 0;
                        while ((memstream.Read(buffer, 0, 2)) == 2)
                        {
                            _data[loc++] = BitConverter.ToUInt16(buffer, 0);
                        }
                    }
                }
                catch (Exception e)
                {
                    _mode = MapAndreasMode.None;
                    throw new FileLoadException("Couldn't load " + MinimalFile, e);
                }
                break;

            case MapAndreasMode.NoBuffer:
                try
                {
                    _fileStream = new FileStream(FullFile, FileMode.Open);
                }
                catch (Exception e)
                {
                    _mode = MapAndreasMode.None;
                    throw new FileLoadException("Couldn't load " + MinimalFile, e);
                }
                break;
            }
        }