Stuyk

Code Snippets

Recommended Posts

More of a general utility.

    internal class UniqueNumbersGenerator
    {
        private static readonly Random Random = new Random(Guid.NewGuid().GetHashCode());

        private List<int> _randomNumbersList = new List<int>();
        private int _lastUsedIndex;
        private readonly int _rangeStart;
        private readonly int _rangeEnd;

        /// <summary>
        /// Initializes a new instance of the <see cref="UniqueNumbersGenerator"/> class which
        /// generates unique (non-repeating) pseudo-Random numbers between the specified range.
        /// </summary>
        /// <param name="rangeStart">The starting number (inclusive) of the range.</param>
        /// <param name="rangeEnd">The ending number (inclusive) of the range.</param>
        public UniqueNumbersGenerator(int rangeStart, int rangeEnd)
        {
            _rangeStart = rangeStart;
            _rangeEnd = rangeEnd;
            GenerateRandomList();
        }

        /// <summary>
        /// Retrieves the next pseudo-Random number from the list. Upon exhaustion
        ///  of all the numbers the list gets reinitialized to the specified range. 
        /// </summary>
        public int Next()
        {
            if (_lastUsedIndex == _randomNumbersList.Count)
            {
                GenerateRandomList();
            }

            return _randomNumbersList[_lastUsedIndex++];
        }

        private void GenerateRandomList()
        {
            // Add values in sequence from _rageStart to _rangeEnd
            _randomNumbersList = Enumerable.Range(_rangeStart, _rangeEnd - _rangeStart + 1).ToList();

            // Shuffle the list using Fisher–Yates shuffle
            int i = _randomNumbersList.Count;
            while (i > 1)
            {
                i--;
                int randomIndex = Random.Next(i + 1);
                int temp = _randomNumbersList[randomIndex];
                _randomNumbersList[randomIndex] = _randomNumbersList[i];
                _randomNumbersList[i] = temp;
            }

            // Start tracking which elements have been used
            _lastUsedIndex = 0;
        }
    }

Example usage would be say for a postman job where you want them to deliver mail to mailboxes. You don't want them going back and forth between the same mailboxes. Instead of using raw Random you can use this.

Share this post


Link to post
Share on other sites
worldCoordsToPixels = (position) => {
	const clientScreen = mp.game.graphics.getScreenActiveResolution(0, 0);
	var toScreen = mp.game.graphics.world3dToScreen2d(position) || { x : 0, y : 0};
	
	return {
		x: Math.floor(clientScreen.x * toScreen.x) + "px",
		y: Math.floor(clientScreen.y * toScreen.y) + "px"
	};
}

 

Share this post


Link to post
Share on other sites

Various ways to save data when shutting down a server

NOTE: All of the below code cannot run any asynchronous code, they can only contain synchronous code

/shutdown command

mp.events.addCommand('shutdown', () => {
    process.exit(0);
});

process.on('exit', (code) => {
   // All your saving code here
});

Events for when you Ctrl + C inside your console or you press the close button on your server.exe window

process.on('SIGINT', shutdownProcess);  // Runs when you Ctrl + C in console
process.on('SIGHUP', shutdownProcess);  // Runs when you press the 'Close' button on your server.exe window

function shutdownProcess(){
    // Shutdown code here
    process.exit(0);
}

 

Share this post


Link to post
Share on other sites
В 28.07.2018 в 00:18, Stuyk сказал:

Get Chat Box Status:

var isChatOpen = toggle;

mp.events.add('render', () => {
if (mp.game.controls.isDisabledControlJustReleased(0, 245)) {
    if (isChatOpen)
         return;
    mp.gui.execute("const _enableChatInput = enableChatInput;enableChatInput = (enable) => { mp.trigger('chatEnabled', enable); _enableChatInput(enable) };");
    }
}

 

^^^ - bad code...,,,

original code - 

mp.gui.execute("const _enableChatInput = enableChatInput;enableChatInput = (enable) => { mp.trigger('chatEnabled', enable); _enableChatInput(enable) };");

mp.events.add('chatEnabled', (isEnabled) => {
    mp.gui.chat.enabled = isEnabled;
});

mp.keys.bind(0x01, true, () => {
    mp.gui.chat.push(`Chat ${mp.gui.chat.enabled ? 'opened' : 'closed'}`);
});

 

Share this post


Link to post
Share on other sites
Posted (edited)

Disables city ambient sounds and buildings music, useful for servers that don't want their players to hear weird horns in empty streets and musics in buildings where no one is working or living, client-side only:

 

EDIT: "MIC1_RADIO_DISABLE" seems to also disable vehicle radio sound.

mp.game.audio.startAudioScene("FBI_HEIST_H5_MUTE_AMBIENCE_SCENE");
mp.game.audio.startAudioScene("MIC1_RADIO_DISABLE");
Edited by n0minal
  • Like 3

Share this post


Link to post
Share on other sites
Am 31.3.2019 um 06:34 schrieb n0minal:

Disables city ambient sounds and buildings music, useful for servers that don't want their players to hear weird horns in empty streets and musics in buildings where no one is working or living, client-side only:

 

EDIT: "MIC1_RADIO_DISABLE" seems to also disable vehicle radio sound.

mp.game.audio.startAudioScene("FBI_HEIST_H5_MUTE_AMBIENCE_SCENE");
mp.game.audio.startAudioScene("MIC1_RADIO_DISABLE");

Absolutelly Amazing! Thank you very much ❤️

  • Like 1

Share this post


Link to post
Share on other sites
Posted (edited)

Max player stats (clientside):

const statNames = ["SP0_STAMINA", "SP0_STRENGTH", "SP0_LUNG_CAPACITY", "SP0_WHEELIE_ABILITY", "SP0_FLYING_ABILITY", "SP0_SHOOTING_ABILITY", "SP0_STEALTH_ABILITY"];

// maybe playerReady can be used instead, haven't tested
mp.events.add("playerSpawn", () => {
    for (const stat of statNames) mp.game.stats.statSetInt(mp.game.joaat(stat), 100, false);
});

Why?

  • Players won't run out of breath after a little bit of running.
  • Players will be able to carry max ammo. (9999 instead of 500-600? not sure about this)
  • Everyone will have the same stats so it bridges the gap between players with 100% game save and players with lets say 10% game save, at least stat wise.
Edited by rootcause
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.