Forum

> > CS2D > Scripts > Locate the center of an explosion
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Locate the center of an explosion

8 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Locate the center of an explosion

FishyFinThing
User Off Offline

Zitieren
Hi there!

I want to know if it is possible to locate the center of every kind of explosion in this game.
I'm experimenting on knockback function and I'm thinking of making explosion with knockback.

If I use a parse command to create an explosion, that will be easy. But what about grenade and mine?

How to know when and where a grenade or a mine explode?

alt Re: Locate the center of an explosion

FishyFinThing
User Off Offline

Zitieren
Thanks!
I used the hook you said and find out where grenade impact. But it will only work for projectile, not something on the ground like mine and laser mine.

Now one more thing left is to locate the explosion from mine and laser mine.

alt Re: Locate the center of an explosion

FishyFinThing
User Off Offline

Zitieren
Thank @user Baloon: for the tip. Now I can also locate the mine.

However, I can't get the accurate angle to get the victim knockbacked toward the right direction.

All I know is that I need distance between the center of explosion and victim. Which mean:
distance_x = victim_x - object_x
distance_y = victim_y - object_y

I think that arctan of distance_x/distance_y is the angle.

But when the code run, it kind of mess up.
The knockback direction should be directly further away from the explosion. However, in some quadrant the direction isn't going as intended.
Perhaps, it's because of the inconsistency about x,y and direction

Here's the code that I use for testing.
It will spawn a mine at tile (4|4) or at x=128 y=128 in term of pixel and tell the player what the angle x,y,distance,direction are.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
color = {'©255200000','©000255000','©160160255','©255000000','©240000240','©255128064','©255255255','©155255000','©100100255'}

--ARRAY GENERATOR
function initArray(size, iValue)
	local array = {}
	for i = 1, size do
		array[i]=iValue
	end
	return array
end

function cw_hud2(id,txtID,c,txt,x,y,a,v,s)
  local screen_h = player(id,"screenh")
  local screen_w = player(id,"screenw")
  local posX = 0
  local posY = 0

  local align = 0
  local valign = 0
  local size = 13
  local text = ""

  if (screen_h and screen_w) then
    posX = screen_h / 480 * x 
    posY = screen_w / 850 * y
  end

  if (a) then
    align = a
  end

  if (v) then
    valign = v
  end

  if (s) then
    size = s
  end

  if(player(id,"exists") and player(id,"bot") ~= true) then
    text = color[c] .. txt
    parse("hudtxt2 " ..id.. " " ..txtID.. ' "' ..color[c]..txt.. '" '  ..posX.. " " ..posY.. " " ..align.. " " ..valign.. " " ..size)
  end
end

addhook("ms100","hud_angle")
function hud_angle()
  parse("spawnobject 20 4 4 0 0 0")
  local cx = 128
  local cy = 128

  local px = 0
  local py = 0

  local disX = 0
  local disY = 0

  local angle = 0
  
  if(player(1,"exists")) then
    px = player(1,"x")
    py = player(1,"y")
	disX = px - cx
    disY = -(py - cy)
	angle = math.deg(math.atan(disX/disY))
  end

  cw_hud2(1,20,2,"Angle "..angle,200,200,0,0,13)
  cw_hud2(1,21,3,"px "..px,200,210,0,0,13)
  cw_hud2(1,22,4,"py "..py,200,220,0,0,13)
  cw_hud2(1,23,3,"cx "..cx,200,240,0,0,13)
  cw_hud2(1,24,4,"cx "..cy,200,250,0,0,13)
  cw_hud2(1,25,3,"disx "..disX,200,270,0,0,13)
  cw_hud2(1,26,4,"disy "..disY,200,280,0,0,13)
end

addhook("objectkill","cw_knockback_mine")
function cw_knockback_mine(obj)
  parse("spawnobject 20 4 4 0 0 0")
  local x = object(obj,"x")
  local y = object(obj,"y")
  local hitX
  local hitY
  local disX
  local disY
  local angle
  local toX = 0
  local toY = 0
  local knockback_distance = 48
  local hit_List = closeplayers(x,y,96)

  if(table.getn(hit_List) > 0) then
    for id = 1, table.getn(hit_List) do
      hitX = player(hit_List[id],"x")
      hitY = player(hit_List[id],"y")
      disX = hitX - x
      disY = -(hitY - y)
      angle = math.deg(math.atan(disX/disY))
      --adjusting angle to start at 3 o'clock counter clockwise
      --angle = 90 - angle
      --if (angle < 0) then angle = 360 + angle end

      if(player(id,"exists")) then
      toX = toX + knockback_distance * math.cos(angle)
      toY = toY + knockback_distance * math.sin(angle)
      end

      msg("target "..hit_List[id])
      msg("at x"..hitX.." y"..hitX)
      msg("angle "..angle)
      msg("impact center x"..x.." y"..y)
      msg("toX "..toX)
      msg("toY "..toY)
    end
  end
end

alt Re: Locate the center of an explosion

TrialAndError
User Off Offline

Zitieren
Try this, it's untested but it should get the job done for mines. I don't really know how effective it will be against laser mines because the "explosion center" (x, y in the code) is where the object is placed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
local knockback_distance = 48

addhook("objectkill","cw_knockback_mine")
function cw_knockback_mine(obj)
    local x, y = object(obj,"x") + 16, object(obj,"y") + 16 -- add 16 to get the center of the tile
    local hit_List = closeplayers(x, y, 96)

    if(#hit_List > 0) then
        for id = 1, #hit_List do 
            local target = hit_List[id]
            if(player(target,"exists")) then
                local targetX = player(target, "x")
                local targetY = player(target, "y")
                local angle = math.atan2(targetY - y, targetX - x)
                toX = targetX + knockback_distance * math.cos(angle)
                toY = targetY + knockback_distance * math.sin(angle)
                parse("setpos "..target.." "..toX.." "..toY)
            end
        end
    end
end
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht