Page 1 of 1

Randomizer

Posted: Tue Jul 29, 2025 12:53 am
by Lunacy_1984
Hello. I am looking to retire the last of my old eventscripts that I am currently using with the ES_emulator.
This is a fairly simple script I use to change the spawn weapon of my bots any time there is a death of any kind.
This triggers a random number to place a console command

hrcbot_player_spawnweapon pistol The weapons can be changed.

I am hoping to use this randomizer for several other scripts I am looking to replace with other command types.

// if (event_var(es_steamid) equalto "BOT") do

event player_death
{
es_doblock randombotweapon/randomize
}
block randomize
{
es_setinfo botweapon 0
es_rand botweapon 1 1100

if (server_var(botweapon) >= 100) do
{
hrcbot_player_spawnweapon pistol
}
if (server_var(botweapon) >= 150) do
{
hrcbot_player_spawnweapon crowbar
}
if (server_var(botweapon) >= 200) do
{
hrcbot_player_spawnweapon stunstick
}
if (server_var(botweapon) >= 250) do
{
hrcbot_player_spawnweapon frag
}
if (server_var(botweapon) >= 300) do
{
hrcbot_player_spawnweapon smg1
}
if (server_var(botweapon) >= 350) do
{
hrcbot_player_spawnweapon ar2
}
if (server_var(botweapon) >= 400) do
{
hrcbot_player_spawnweapon shotgun
}
if (server_var(botweapon) >= 450) do
{
hrcbot_player_spawnweapon slam
}
if (server_var(botweapon) >= 500) do
{
hrcbot_player_spawnweapon rpg
}
if (server_var(botweapon) >= 550) do
{
hrcbot_player_spawnweapon crossbow
}
if (server_var(botweapon) >= 600) do
{
hrcbot_player_spawnweapon frag
}
if (server_var(botweapon) >= 650) do
{
hrcbot_player_spawnweapon pistol
}
if (server_var(botweapon) >= 700) do
{
hrcbot_player_spawnweapon crowbar
}
if (server_var(botweapon) >= 750) do
{
hrcbot_player_spawnweapon stunstick
}
if (server_var(botweapon) >= 800) do
{
hrcbot_player_spawnweapon frag
}
if (server_var(botweapon) >= 850) do
{
hrcbot_player_spawnweapon smg1
}
if (server_var(botweapon) >= 900) do
{
hrcbot_player_spawnweapon ar2
}
if (server_var(botweapon) >= 950) do
{
hrcbot_player_spawnweapon shotgun
}
if (server_var(botweapon) >= 1000) do
{
hrcbot_player_spawnweapon slam
}
if (server_var(botweapon) >= 1025) do
{
hrcbot_player_spawnweapon crossbow
}
if (server_var(botweapon) >= 1050) do
{
hrcbot_player_spawnweapon frag
}

}

Re: Randomizer

Posted: Tue Jul 29, 2025 8:04 am
by satoon101
Try this:

Syntax: Select all

from random import randint

from cvars import ConVar
from events import Event


hrcbot_player_spawnweapon = ConVar("hrcbot_player_spawnweapon")
weapon_dict = {
100: "pistol",
150: "crowbar",
200: "stunstick",
250: "frag",
300: "smg1",
350: "ar2",
400: "shotgun",
450: "slam",
500: "rpg",
550: "crossbow",
600: "frag",
650: "pistol",
700: "crowbar",
750: "stunstick",
800: "frag",
850: "smg1",
900: "ar2",
950: "shotgun",
1000: "slam",
1025: "crossbow",
1050: "frag",
}

@Event("player_death")
def _set_spawn_weapon(game_event):
weapon = get_weapon()
if weapon is not None:
hrcbot_player_spawnweapon.set_string(weapon)


def get_weapon():
check_value = randint(1, 1100)
for key, value in reversed(sorted(weapon_dict.items())):
if check_value >= key:
return value

return None

Re: Randomizer

Posted: Wed Jul 30, 2025 10:55 pm
by Lunacy_1984
That simple. Thankyou for the script. I will be using this as an example to rewrite more of my old material.
HL2DM servers: Brand New For 1979, West Coast Canadian, and Pacific Coast Pirates

Re: Randomizer

Posted: Thu Jul 31, 2025 12:50 am
by Lunacy_1984
A bit of a change and so far so good.

Code: Select all

from random import randint

from cvars import ConVar
from events import Event

hrcbot_player_spawnweapon = ConVar("hrcbot_player_spawnweapon")
weapon_dict = {
    1: "pistol",
    2: "crowbar",
    3: "stunstick",
    4: "frag",
    5: "smg1",
    6: "ar2",
    7: "shotgun",
    8: "slam",
    9: "rpg",
    10: "crossbow",
    11: "frag",
    12: "pistol",
    13: "crowbar",
    14: "stunstick",
    15: "frag",
    16: "smg1",
    17: "ar2",
    18: "shotgun",
    19: "slam",
    20: "crossbow",
    21: "frag",
}

@Event("player_death")
def _set_spawn_weapon(game_event):
    weapon = get_weapon()
    if weapon is not None:
        hrcbot_player_spawnweapon.set_string(weapon)


def get_weapon():
    check_value = randint(1, 21)
    for key, value in reversed(sorted(weapon_dict.items())):
        if check_value >= key:
            return value

    return None

Re: Randomizer

Posted: Thu Jul 31, 2025 5:21 pm
by Ayuto
Well, the initial version includes some kind of probability per weapon. E. g. the chance to get a crossbow is much lower than a frag.

If you don't need that kind of probability you could also use a list of weapons and use random.choice(). The function get_weapon() then will be a one liner.

Re: Randomizer

Posted: Sat Aug 02, 2025 12:52 am
by Lunacy_1984
As I gain more experience with SP I may change how this works. I am mostly trying to stop bots spawning with a 357 but still have a rare chance of an RPG.
The main thing is I am using this script to help me make other randomizers as part of other scripts as I replace some of the still functioning eventscripts

Re: Randomizer

Posted: Thu Aug 07, 2025 12:57 am
by Lunacy_1984
After using the script for the weapon randomizer I wrote another script using the same principle. Player death triggers three commands to randomize settings via console command. I just need someone to check my work please.
********************************


Syntax: Select all

from random import randint

from cvars import ConVar
from events import Event

sm_cbomb_bomblet_count = ConVar("sm_cbomb_bomblet_count")
count_dict = {
1: "30",
2: "3",
3: "1",
4: "10",
5: "30",
6: "25",
7: "6",
8: "18",
9: "9",
10: "16",
11: "3",
12: "25",
13: "12",
14: "18",
15: "14" ,
16: "8",
17: "20",
18: "10",
19: "7",
20: "15",
21: "30",
22: "28",
23: "35",
24: "20",
25: "50",
}

sm_cbomb_bomblet_magnitude = ConVar("sm_cbomb_bomblet_magnitude")
mag_dict = {
1: "90",
2: "450",
3: "500",
4: "200",
5: "80",
6: "10",
7: "200",
8: "120",
9: "110",
10: "90",
11: "200",
12: "80",
13: "100",
14: "88",
15: "300",
16: "200",
17: "50",
18: "60",
19: "50",
20: "130",
21: "500",
22: "130",
23: "500",
24: "130",
25: "500",
}

sm_cbomb_detonation_delay = ConVar("sm_cbomb_detonation_delay")
delay_dict = {
1: "1",
2: "5",
3: "3",
4: "3",
5: "2",
6: "4",
7: "2",
8: "3",
9: "5",
10: "2",
11: "4",
12: "1",
13: "3",
14: "2",
15: "5",
16: "2",
17: "4",
18: "3",
19: "4",
20: "2",
21: "5",
22: "6",
23: "3",
24: "1",
25: "5",
}

@Event("player_death")
def _set_spawn_weapon(game_event):
weapon = get_weapon()
if weapon is not None:
sm_cbomb_bomblet_count.set_string(count)
sm_cbomb_bomblet_magnitude.set_string(mag)
sm_cbomb_detonation_delay.set_string(delay)

def get_weapon():
check_value = randint(1, 25)
for key, value in reversed(sorted(count_dict.items())):
if check_value >= key:
return value

return None

Re: Randomizer

Posted: Thu Aug 07, 2025 11:03 pm
by satoon101
A few pointers. First, when posting Python code on these forums, use [python]<your code here>[/python]. This helps to highlight the code properly and keep the spacing (indentation). I added this to your latest post for you.

Second, I highly recommend using a good IDE to write your code. I use Pycharm, but there are plenty of other options out there. This will help you to see issues in your code. In particular, this part has issues:

Syntax: Select all

@Event("player_death")
def _set_spawn_weapon(game_event):
weapon = get_weapon()
if weapon is not None:
sm_cbomb_bomblet_count.set_string(count)
sm_cbomb_bomblet_magnitude.set_string(mag)
sm_cbomb_detonation_delay.set_string(delay)

The variables count, mag, and delay are not defined anywhere in your code, so they will cause an error if you try to run this code.

Also, as Ayuto already mentioned, the reason I used a dictionary for the original script was because you had a lot of different options with completely different "weights" to them. If you are just going to use sequential keys in the dictionary, you should use a list instead:
https://docs.python.org/3/tutorial/datastructures.html

Re: Randomizer

Posted: Fri Aug 08, 2025 9:57 pm
by Lunacy_1984
Thankyou for the feedback. I will look into those tools. I will get the forum procedures and code figured out. Still a bit for me to learn after using the older systems for so long.