Forum

> > CS2D > Scripts > Spawn weapons randomly
Forums overviewCS2D overview Scripts overviewLog in to reply

English Spawn weapons randomly

5 replies
To the start Previous 1 Next To the start

old Spawn weapons randomly

knight-
User Off Offline

Quote
Hi everyone.
I want to make a lua that spawn weapon(Primary ammo,Secondary ammo,AK-47 and M4A1)one of them randomly in the random place of the map.I want it per 30s spawn one time.
It is hard for me so please help.

old Re: Spawn weapons randomly

mafia_man
User Off Offline

Quote
There's script:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
spawning_items = {
	30,32,61,62;
}

function placeitem()
	local x, y;
	x = math.random(0, map('xsize'));
	y = math.random(0, map('ysize'));
	if tile(x, y, 'walkable') and tile(x, y, 'frame') > 0 then
		local i = math.random(1,#spawning_items);
		parse('spawnitem '.. i ..' '.. x ..' '.. y);
		return 0;
	end
	placeitem();
end

seconds = 0;
addhook("second", "everySecond")
function everySecond()
	seconds = seconds + 1;
	if seconds >= 30 then
		seconds = 0;
		placeitem();
	end
end

old Re: Spawn weapons randomly

Avo
User Off Offline

Quote
There are lots of way to do this. You can use entities for example. You must remember than position of item must be walkable tile which player can reach (de_dust - there are some empty places around a map, items would spawn there, too.)
@user mafia_man Remember that if frame isn't 0 it doesn't mean player can go there

Here is my idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
walkable_tiles={}

addhook("second","_second",2)
function _second()
	math.randomseed(os.time())
	if #player(0,"tableliving")>0 then
		msg("spawn set"..os.time())
		local pl=player(0,"tableliving")[math.random(1,#player(0,"tableliving"))]
		table.insert(walkable_tiles,{player(pl,"tilex"),player(pl,"tiley")})
	end
end
timer(30000,"_minute","",0)
function _minute()
	math.randomseed(os.time())
	local wp={31,32,33,45,51,61,62,76,83,89,90,91,88,85,82,64,10}
	local r=wp[math.random(1,math.random(1,#wp))]
	local pos=math.random(1,#walkable_tiles)
	pos=walkable_tiles[pos]
	parse("spawnitem "..r.." "..pos[1].." "..pos[2])
	msg("spawned")
	walkable_tiles={}
end

EDIT:
@user mafia_man again:
1
2
local i = math.random(1,#spawning_items);
parse('spawnitem '.. i ..' '.. x ..' '.. y);
with this part of code, only pistols would be spawned if table spawning_items have 4 values.

old Re: Spawn weapons randomly

Avo
User Off Offline

Quote
Edit:
1
local wp={31,32,33,45,51,61,62,76,83,89,90,91,88,85,82,64,10}
(my)

or
1
2
3
spawning_items = {
     30,32,61,62;
}
(mafia_man)

old Re: Spawn weapons randomly

knight-
User Off Offline

Quote
user Avo has written
Edit:
1
local wp={31,32,33,45,51,61,62,76,83,89,90,91,88,85,82,64,10}
(my)

or
1
2
3
spawning_items = {
     30,32,61,62;
}
(mafia_man)

@user Avo: Can u fix your version?
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview