Forum

> > CS2D > Scripts > NPC - Moving/Attacking Detection
Forums overviewCS2D overview Scripts overviewLog in to reply

English NPC - Moving/Attacking Detection

7 replies
To the start Previous 1 Next To the start

old NPC - Moving/Attacking Detection

AtomKuh
User Off Offline

Quote
Hey,

is it possible to detect whether a NPC sighted a Player (attacking, moving to him) or not? One simple idea would be to check if they were moving or not, but they juyt could be stuck behind an obstacle.

Thank you in advance!

old Re: NPC - Moving/Attacking Detection

Pagyra
User Off Offline

Quote
Try to check cs2d lua cmd object values.

My CS2D NPCs info >

Examples of lua code :

Check and stop npc after attack 1 player (for stop npc attack) >
Move example >

And to improve your NPC's skins you can try file cs2d My npc pack
edited 3×, last 10.04.17 10:36:47 pm

old Re: NPC - Moving/Attacking Detection

AtomKuh
User Off Offline

Quote
Thanks so far Pagyra^^

I will check your script in some minutes
your first code doesnt work

The point is that I dont want to give players the opportunity to hurt npcs while being not reachable for them.


This is a new question: Is it possible to add a text above every NPC that contains the value of the left HP?

I already made a script that checks the HP of each NPC after every taken damage.

1
2
3
4
5
6
7
8
9
addhook("objectdamage","NPCHealth")
function NPCHealth(id,damage)
if (object(id,"type") == 30) then 
	 local hp = object(id,"health")
	 local dm = damage
	 local hpdm = hp - dm
	 msg(hpdm)
     end
end

I want to have the value of the variable local hpdm above their head which should align to the position of the NPC if they are walking.

Thanks in advance!
edited 2×, last 10.04.17 08:34:53 pm

old Re: NPC - Moving/Attacking Detection

TimeQuesT
User Off Offline

Quote
Heyo AtomKuh. You can use this script snippet.
Check bottom of script for example usage.

Keep in mind that this script won't work with the upcoming version of CS2D due to the dynamic camera.

...And Unrealsoftware messes with my nice tabs again
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
constants 				= {};
constants.ScreenWidth 	= 640;
constants.ScreenHeight 	= 480;

function AbsoluteHud(hudid, txt, pid, x, y)
	local x_player = player(pid, "x");
	local y_player = player(pid, "y");

	local x_left 	= x_player-constants.ScreenWidth/2;
	local x_right 	= x_player+constants.ScreenWidth/2;
	local y_top 	= y_player-constants.ScreenHeight/2;
	local y_bottom 	= y_player+constants.ScreenHeight/2;
	
	if 
	(
		x < x_left or x > x_right
		or
		y < y_top or y > y_bottom
	)
	then
		return false; -- not in players view
	end

	local relative_x = x - x_left;
	local relative_y = y - y_top;

	parse("hudtxt2 "..pid.." "..hudid.." \""..txt.."\" "..relative_x.." "..relative_y.." 1"); --centered hudtxt2
	
	return true;
end

--Example usage
print("Example");
addhook("hit", "example_hit");
function example_hit(id, src, wpn, hp_dmg)
	--Is player?
	if (src <= 0 and src > 32) then return 0; end;
	
	--Show damage done to attacker on victims position
	AbsoluteHud(0, tostring(hp_dmg), src, player(id, "x"), player(id, "y"));
end

This should do your job. (Don't forget to include AbsoluteHud and constants to your project)
1
2
3
4
5
6
7
8
9
addhook("objectdamage","NPCHealth")
function NPCHealth(id,damage, src)
if (object(id,"type") == 30 and src > 0) then 
      local hp = object(id,"health")
      local dm = damage
      local hpdm = hp - dm
      AbsoluteHud(0, tostring(hpdm), src, object(id, "x"), object(id, "y"));
     end
end

old Re: NPC - Moving/Attacking Detection

Masea
Super User Off Offline

Quote
@user TimeQuesT: You missed one place he said. He wants like the hudtext should be constant to their head even if they're walking/moving but your work does not do that.

Nevertheless, I highly suggest that you should use user TimeQuesT's code because whatever it is you want, we need to use ms100 or always hook which could beat a path for lags.

old Re: NPC - Moving/Attacking Detection

AtomKuh
User Off Offline

Quote
Thanks @user TimeQuesT: for your script even if it is not what I wanted exactly

@user Masea: According to your always or ms100 hooks, take a look at this script: http://www.unrealsoftware.de/files_show.php?file=5103

The creator even made it possible showing a HP bar for NPC's.
There was no usage of the always or ms100 hook, however because of my basic lua knowledge, I don't know if it's easier to display an image instead of a hudtxt. o_O:
user archmage has written
Replace the code in main.lua with this code.
Updated Code >

old Re: NPC - Moving/Attacking Detection

TimeQuesT
User Off Offline

Quote
Just make an array, store hud & object info there and reparse it every x ms.

Script not tested - may contain crucial errors, but the logic should be right.

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
huddata = {};
huddata.objectid = {};
huddata.data = {};

--bring hud to screen (location depends foreach player)
addhook("ms100", "ichbinflummidasreh");
function ichbinflummidasreh()
	for i = 0, 49 do
		if (huddata.objectid[i] ~= nil) then
			for pid = 1, 32 do 
			AbsoluteHud(i, huddata.data[i], pid, object(huddata.objectid[i], "x"), object(huddata.objectid[i], "y") ); 
			end
		end
	end
end

--update hud data
addhook("objectdamage","NPCHealth")
function NPCHealth(id,damage, src)
if (object(id,"type") == 30 and src > 0) then 
      local hp = object(id,"health")
      local dm = damage
      local hpdm = hp - dm
     	for i = 0, 49 do
		if (huddata.objectid[i] == nil) then
			huddata.objectid[i] = id;
			huddata.data[i] = tostring(hpdm);
			break;
		end
	end
     end
end

--Free object from hudlist
addhook("objectkill", "oblivioninmyass");
function oblivioninmyass(obj)
	for i = 0, 49 do
		if (huddata.objectid[i] ~= nil) then
			if (huddata.objectid[i] == obj) then
				huddata.objectid[i] = nil;
				for pid = 1, 32 do parse("hudtxt2 "..pid.." "..i.."\"\" 0 0"); end; --remove old hudtxt
				break;
			end
		end
	end
end
edited 1×, last 13.04.17 12:42:28 am
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview