Forum

> > CS2D > Scripts > Zombie attack function
Forums overviewCS2D overview Scripts overviewLog in to reply

English Zombie attack function

17 replies
To the start Previous 1 Next To the start

old Zombie attack function

Mami Tomoe
User Off Offline

Quote
Hello, I need a function that will place NPCs randomly on the map, now that is easy to make I know but I've got a problem; I need to check if the NPC is being placed under a block or not to save lags (by block I mean player buildings or map walls)

So far I've got this:
1
2
3
4
math.random(1,25) - for the zombies
math.random(1,50) - for the head-crabs
math.random(1,75) - for the snarks
math.random(1,100) - for the vortigaunts

Now I want this to last for 3 seconds only so I'll add a delay

1
2
3
4
timer("3000",stop()) - I forgot how to use these :P
function stop()
     zmatk=false
end

now for the actual function

1
2
3
4
5
6
function zmatk()
     if zmatk==false then
         zmatk=true
         timer("3000",stop()) 
     end
end

So that's all I've got, not a lot but I tried

I need:
3 Seconds attack (can be changed if needed)
math.random to place random NPCs
and everything else above

old Re: Zombie attack function

GeoB99
Moderator Off Offline

Quote
user Mami Tomoe has written
3 Seconds attack (can be changed if needed)

Modifying the delay attacks of NPCs is not actually possible as far I know unless you make your custom NPCs with AI commands.
1
2
3
4
5
6
7
function RandomNPC()
	local x = math.random(0, map("xsize"))
	local y = math.random(0, map("ysize"))
	if tile(x, y, "walkable") then	
		parse("spawnnpc 1 2 3 4 "..x.." "..y)
	end
end
It will not place them outside of the map because it checks for "walkable" thus they'll be placed inside the map.

old Re: Zombie attack function

THEMUD
User Off Offline

Quote
First of all, here is the right syntax of a timer function:
1
timer(TIME, "FUNCTION_NAME")
You have to put the function's name into double quotes.

Second of all, you can't set up a function and then turn it into a boolean value. So rename that function to something else.
And finally, here is a code about what I have understood from you.
1
2
3
4
5
6
7
8
9
10
function stop()
	zmatk = false;
end

function ZombieDelay()
     if zmatk == false then
         zmatk = true;
         timer(3000, "stop");
     end
end

old Re: Zombie attack function

THEMUD
User Off Offline

Quote
Then you can add @user GeoB99's code with the function I put above.
Like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function stop()
	zmatk = false;
end

function ZombieDelay()
	if zmatk == false then
		local x = math.random(0, map("xsize"))
		local y = math.random(0, map("ysize"))
		if tile(x, y, "walkable") then     
			parse("spawnnpc 1 "..x.." "..y.." 0");
			zmatk = true;
			timer(3000, "stop");
		end
	end
end

old Re: Zombie attack function

Cure Pikachu
User Off Offline

Quote
@user THEMUD: I can't see your code spawning multiple NPCs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
spawnNPCs()
timer(100,"spawnNPCs","",0)
timer(3000,"stopspawnNPCs")

function spawnNPCs()
	local x, y
	repeat
		x, y = math.random(0,map("xsize")), math.random(0,map("ysize"))
	until (tile(x,y,"walkable") and tile(x,y,"frame") > 0)
	parse("spawnnpc "..math.random(1,5).." "..x.." "..y.." "..math.random(0,360))
end

function stopspawnNPCs()
	freetimer("spawnNPCs")
end
edited 1×, last 24.01.16 07:44:38 pm

old Re: Zombie attack function

Mami Tomoe
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
addhook("use","whene")

function whene(id,event,data,x,y)
	if x==64 and y==64 then
		msg("\169255000000Zombie attack has started@C")
		spawnNPCs()
		timer(100,"spawnNPCs","",0)
		timer(10000,"stopspawnNPCs")
	end
end

For some reason that doesn't work
(Your script works just my use hook wont, do I need a map trigger for that hook to work?)

old Re: Zombie attack function

GeoB99
Moderator Off Offline

Quote
@user Mami Tomoe: That script will not work without defining the event and triggering with trigger_use.
1
2
3
4
5
6
7
8
9
10
11
12
function whene(id, event, data, x, y)
	if event == 100 then
		if x == 64 and y == 64 then
			msg("\169255000000Zombie attack has started@C")
			spawnNPCs()
			timer(100,"spawnNPCs","",0)
			timer(10000,"stopspawnNPCs")
		end
	end
end

addhook("use","whene")
Your only task is by adding trigger_use entity at correct x, y coordinations (in this case it's 64 for X and 64 for Y) through map editor.

old Re: Zombie attack function

Mami Tomoe
User Off Offline

Quote
For some weird reason all of the NPCs spawned at x64y64 (the button location)

Spoiler >


Also @user GeoB99: why do you place the addhook under the function? Is that important?

old Re: Zombie attack function

GeoB99
Moderator Off Offline

Quote
@user Mami Tomoe: 1 and 2 line are useless because they aren't defined anywhere in your code. By the way, I've tested it and it works fine to me. Aside from this, that's my code style of placing addhooks in the bottom of the code so no it's not important.

old Re: Zombie attack function

GeoB99
Moderator Off Offline

Quote
@user Mami Tomoe: What on earth kind of map is this? Lol?
Of course it spawns at the button coordination because from what I see on this screenshot you've put only one tile. Try with de_dust2 map for example instead. Not forget to edit the coordinations path.

old Re: Zombie attack function

Cure Pikachu
User Off Offline

Quote
Can you use another map like con_tinysandbox and test there instead of using an empty map which theoretically f*cks up the script due to an endless loop, like what user GeoB99 said?
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview