Forum

> > CS2D > Scripts > How to avoid crashing with grenades ?
Forums overviewCS2D overview Scripts overviewLog in to reply

English How to avoid crashing with grenades ?

12 replies
To the start Previous 1 Next To the start

old How to avoid crashing with grenades ?

Rainoth
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
elseif class[id]== 26 then
		if wep==72 then
			for i=0,315,45 do
				parse("spawnprojectile "..id.." 72 "..x.." "..y.." "..math.random(800,950).." "..i)
				timer(300,"parse","freeprojectile 72 0")
			end
		end
	end
this is part of "Projectile" hook. I want for :
Player to throw projectile(gas grenade for example) and then for it to split into 8 others. I used that "for" from sample and I got good angles, now the problem is that I throw one grenade and it goes as following

1 > 8 > 64 > 64x64 > ZxZ > ... > Crash !

I tried to use "Freeprojectile" but it does completely nothing I tried with both "..id) parameter and with "0" since I didn't know whether the new projectiles were spawned by player or not, but neither worked ...

old Re: How to avoid crashing with grenades ?

DC
Admin Off Offline

Quote
You have to keep track of the fact which grenades are thrown by the player and which are created by script. Make a table which contains all script-created projectile IDs for example or something like that and test in your script if the ID is in that table. If it's in there: Don't spawn new projectiles and remove it from the list.

old Re: How to avoid crashing with grenades ?

Rainoth
Moderator Off Offline

Quote
No.. If I were to have a very good pc and throw grenade in con_bigsandbox center or whatever and I wouldn't crash, you would see that the whole map would be full of gas (if I were to use gas grenade) Here you'll see what I'm talking about. I want the projectiles to go exactly in those directions (and they do) but they don't stop multiplying at all.

old Re: How to avoid crashing with grenades ?

Deleted User

Quote
DC said it all, you have to keep track of the grenades that are created by player and that are created by script.

I'd helped you right away, but I'm a bit busy now. lol.

old Re: How to avoid crashing with grenades ?

Rainoth
Moderator Off Offline

Quote
user Conscience has written
Edit: Oohhh.. Your code executes on projectile? I'm pretty sure that it will create more grenades because it will keep on thinking you just threw a new grenade.


Exactly.
edited 1×, last 28.03.13 07:24:27 pm

old Re: How to avoid crashing with grenades ?

Conscience
User Off Offline

Quote
Well each projectile has its own ID. Save this in a table and then check whether it is in the table before you spawn the projectile. This way it won't loop.

It's actually what DarkCorner said.

old Re: How to avoid crashing with grenades ?

Deleted User

Quote
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
26
27
28
29
30
31
32
33
34
35
36
function table_delete(mtable, elements)
	for k, v in pairs(mtable) do
		for _k, _v in pairs(elements) do
			if v == _v then
				table.remove(mtable, k)
			end
		end
	end
	return mtable
end

disallowList = {}

addhook('projectile', 'projectile_hook')
function projectile_hook(id, weapon, x, y)
	for k, v in pairs(projectilelist(1, id)) do
		local prx, pry = math.floor(projectile(v.id, v.player, "x") / 32), math.floor(projectile(v.id, v.player, "y") / 32)
		local ix, iy = math.floor(x / 32), math.floor(y / 32)
		if prx == ix and pry == iy and projectile(v.id, v.player, "type") == 72 then
			for ___, proj in pairs(disallowList) do
				if proj.id == v.id and proj.player == v.player then
					table.remove(disallowList, ___)
					return
				end
			end
			local beforeList = projectilelist(0)
			for i=0,315,45 do
				parse("spawnprojectile "..id.." 72 "..x.." "..y.." "..math.random(800,950).." "..i)
			end
			local newList = table_delete(projectilelist(0), beforeList)
			for ___, proj in pairs(newList) do
				table.insert(disallowList, proj)
			end
		end
	end
end

Mindfucky... but at least working.
Sorry if it looks too "pro". lol
edited 1×, last 28.03.13 08:26:18 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview