MOD中国同盟社

 找回密码
 
查看: 3232|回复: 14

我编写的产生NPC的插件源码(会产生虚拟BOT) [复制链接]

Rank: 5Rank: 5Rank: 5Rank: 5Rank: 5

帖子
70
精华
1
879
239
25
  1. #include <amxmodx>
  2. #include <cstrike>
  3. #include <engine>
  4. #include <amxmisc>
  5. #include <fakemeta>
  6. #include <fun>

  7. #define FAKEPLAYERSPEED 200 //dynamic fake-player run speed

  8. new models[32][33],num
  9. new body_ents[33][999]
  10. new creators[33]
  11. new exp
  12. new bool:dynamic_ent[999]
  13. new g_fwid
  14. new t

  15. public plugin_init(){
  16. register_plugin("fake_player"," 0.8", "Enigmaya")
  17. register_clcmd("amx_create_fake","create_dynamic")
  18. register_think("fake_player","ent_think")
  19. unregister_forward(FM_PrecacheEvent, g_fwid, 1)
  20. register_forward(FM_PlaybackEvent, "fwPlaybackEvent")
  21. createBots()
  22. set_task(3.0,"fake_create")
  23. }

  24. public fake_create(){
  25. create_dynamic(t)
  26. set_task(3.0,"fake_create")
  27. }

  28. public createBots(){
  29. new bot, ptr[128]
  30. bot = engfunc(EngFunc_CreateFakeClient, "Fake Player")
  31. dllfunc(DLLFunc_ClientConnect, bot, "Fake Player", "127.0.0.1", ptr )
  32. dllfunc(DLLFunc_ClientPutInServer, bot)
  33. cs_set_user_team(bot,CS_TEAM_T)
  34. t=bot
  35. }


  36. public plugin_precache(){
  37. exp= precache_model("sprites/shockwave.spr")
  38. num = get_models(models,32)
  39. for(new i=0;i<num;i++){
  40. new modelstring[64];
  41. format(modelstring,63,"models/player/%s/%s.mdl",models[i],models[i])
  42. precache_model(modelstring)
  43. }
  44. g_fwid = register_forward(FM_PrecacheEvent, "fwPrecacheEvent", 1)
  45. }

  46. public fwPlaybackEvent(flags, id, eventid) {
  47. new ent,body,ent_name[32],ent_ori[3],Float:ent_health
  48. get_user_aiming (id,ent,body)
  49. if (ent){
  50. entity_get_string (ent, EV_SZ_classname, ent_name, 31)
  51. ent_health=entity_get_float(ent,EV_FL_health)
  52. get_user_origin(id,ent_ori,3)
  53. if(equal(ent_name, "fake_player")){
  54. if(ent_health<=0){
  55. makeexp(ent_ori,20)
  56. score(id)
  57. remove_entity(ent)
  58. }
  59. }
  60. }
  61. }

  62. public showkill(killer,victim,head,weaponstr[]){

  63. message_begin(MSG_ALL,get_user_msgid("DeathMsg"))

  64. write_byte(killer)

  65. write_byte(victim)

  66. write_byte(head)

  67. write_string(weaponstr)

  68. message_end()
  69. }

  70. public score(killer){
  71. showkill(killer,t,1,"")
  72. }

  73. public get_models(array[32][],len){
  74. new dirpos, output[64], outlen, filledamt

  75. // go through custom models
  76. while((dirpos = read_dir("models/player",dirpos,output,255,outlen)) != 0) {

  77. if(containi(output,".") == -1){
  78. new modelfile[64]
  79. format(modelfile,63,"models/player/%s/%s.mdl",output,output)
  80. if(file_exists(modelfile))
  81. {
  82. format(array[filledamt],len,"%s",output)
  83. filledamt += 1
  84. }
  85. if(filledamt > 32)
  86. {
  87. return filledamt
  88. }
  89. }
  90. }
  91. return filledamt
  92. }

  93. public create_dynamic(id){
  94. new entid=try_build(id)
  95. if( !entid )
  96. return PLUGIN_HANDLED
  97. show_explode(id)
  98. set_dynamic_sequence(id,entid)
  99. entity_set_float(entid,EV_FL_nextthink,halflife_time() + 0.5)
  100. dynamic_ent[entid]=true

  101. return PLUGIN_HANDLED
  102. }

  103. try_build(id){
  104. new Float:pOri[3]
  105. entity_get_vector(id, EV_VEC_origin, pOri)
  106. new entid=build_now(pOri, id)
  107. drop_to_floor(entid)
  108. return entid
  109. }



  110. set_dynamic_sequence(id,entPlayer){
  111. new Float:Vel[3]
  112. VelocityByAim(id,FAKEPLAYERSPEED,Vel)

  113. new motion=4
  114. Vel[2]=float(0)
  115. entity_set_vector(entPlayer,EV_VEC_velocity,Vel)//set Velocity
  116. entity_set_int(entPlayer,EV_INT_sequence,motion)
  117. }

  118. show_explode(id)
  119. {
  120. new ori[3]
  121. get_user_origin(id,ori)
  122. makeexp(ori,20)
  123. }


  124. public change_origin(id,entid,Float:pOri[3],Float:eOri[3])
  125. {
  126. if(!is_user_alive(id) )
  127. return PLUGIN_HANDLED

  128. new ran=random(2)

  129. switch(ran){
  130. case 0:{
  131. pOri[2]+=70.0
  132. eOri[2]+=65.0
  133. entity_set_origin(id,pOri)
  134. entity_set_origin(entid,eOri)
  135. }
  136. case 1:{
  137. pOri[2]+=65.0
  138. eOri[2]+=70.0
  139. entity_set_origin(id,eOri)
  140. entity_set_origin(entid,pOri)
  141. }
  142. }
  143. return PLUGIN_CONTINUE
  144. }



  145. //----------------------------------build fake-player------------------------------
  146. public build_now(Float:Ori[3],id)
  147. {
  148. new Float:Vel[3],Float:angle[3],pmodel[33]

  149. new entPlayer= create_entity("info_target")
  150. if( !entPlayer )
  151. return 0

  152. entity_set_string(entPlayer, EV_SZ_classname, "fake_player")

  153. //--------set model----------
  154. cs_get_user_model ( id, pmodel, 32 )
  155. new mstring[64]
  156. pmodel=models[random_num(0,num-1)]
  157. format(mstring,63,"models/player/%s/%s.mdl",pmodel,pmodel)
  158. entity_set_model(entPlayer, mstring)

  159. //-------base option-------
  160. new Float:maxs[3] = {16.0,16.0,36.0}
  161. new Float:mins[3] = {-16.0,-16.0,-36.0}
  162. entity_set_size(entPlayer,mins,maxs)
  163. entity_set_int(entPlayer, EV_INT_solid, 2)//SOLID_BBOX)
  164. entity_set_int(entPlayer, EV_INT_movetype,MOVETYPE_NOCLIP)
  165. entity_set_edict(entPlayer, EV_ENT_owner, t)

  166. entity_set_float(entPlayer,EV_FL_takedamage,1.0)
  167. entity_set_float(entPlayer,EV_FL_health,1.00)
  168. entity_set_float(entPlayer,EV_FL_max_health,100.00)


  169. //-------set origin--------

  170. entity_set_origin(entPlayer, Ori)

  171. //--------set animation-------
  172. entity_set_float(entPlayer,EV_FL_animtime,2.0)
  173. entity_set_float(entPlayer,EV_FL_framerate,1.0)

  174. //-----------set angle-----------
  175. VelocityByAim(id,FAKEPLAYERSPEED,Vel)
  176. Vel[2]=float(0)
  177. vector_to_angle(Vel,angle)
  178. entity_set_vector(entPlayer,EV_VEC_angles,angle)
  179. entity_set_vector(entPlayer,EV_VEC_v_angle,angle)

  180. drop_to_floor(entPlayer)

  181. body_ents[id][creators[id]] = entPlayer
  182. dynamic_ent[entPlayer]=false//initialize bool:dynamic_ent
  183. return entPlayer
  184. }




  185. //-----------------------fake-player think--------------------------


  186. public ent_think(entid){
  187. if ( is_valid_ent(entid) ){
  188. drop_to_floor(entid)
  189. new Float:fOri[3]
  190. entity_get_vector(entid,EV_VEC_origin,fOri)
  191. find_way(entid,fOri)
  192. entity_set_float(entid,EV_FL_nextthink,halflife_time() + 0.1)
  193. }
  194. return PLUGIN_CONTINUE
  195. }

  196. stop_fake(entid)
  197. {
  198. new Float:Vel[3]
  199. Vel[0]=0.0
  200. Vel[1]=0.0
  201. Vel[2]=0.0
  202. entity_set_vector(entid,EV_VEC_velocity,Vel)
  203. entity_set_int(entid,EV_INT_sequence,1)
  204. }

  205. stock DirectedVec(Float:start[3],Float:end[3],Float:reOri[3])
  206. {
  207. //-------code from Hydralisk's 'Admin Advantage'-------//
  208. new Float:v3[3]
  209. v3[0]=start[0]-end[0]
  210. v3[1]=start[1]-end[1]
  211. v3[2]=start[2]-end[2]
  212. new Float:vl = vector_length(v3)
  213. reOri[0] = v3[0] / vl
  214. reOri[1] = v3[1] / vl
  215. reOri[2] = v3[2] / vl
  216. }

  217. public makeexp(ori[3],times){
  218. message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
  219. write_byte(3)
  220. write_coord(ori[0])
  221. write_coord(ori[1])
  222. write_coord(ori[2])
  223. write_short(exp)
  224. write_byte(times)
  225. write_byte(10)
  226. write_byte(0)
  227. message_end()
  228. }

  229. find_way(entid,Float:fOri[3])
  230. {
  231. new Float:vTrace[3],Float:vTraceEnd[3],Float:hitOri[3],Float:Vel[3],Float:angle[3]
  232. // set a entPos to trace a line
  233. velocity_by_aim(entid, 64, vTrace)
  234. vTraceEnd[0] = vTrace[0] + fOri[0]
  235. vTraceEnd[1] = vTrace[1] + fOri[1]
  236. vTraceEnd[2] = vTrace[2] + fOri[2]+25
  237. new hitent=trace_line(entid, fOri, vTraceEnd, hitOri)

  238. //check the trace return values to check is player hit something...
  239. //doesn't check the hit entity,
  240. //because if hit nothing,will return 0. and if hit the wall,also return 0.
  241. new Float:gdis=vector_distance(fOri,hitOri)

  242. //set another entPos to trace another line
  243. velocity_by_aim(entid, 45, vTrace)
  244. vTraceEnd[0] = vTrace[0] + fOri[0]
  245. vTraceEnd[1] = vTrace[1] + fOri[1]
  246. vTraceEnd[2] = vTrace[2] + fOri[2]-45// lower than first dot
  247. trace_line(entid, fOri, vTraceEnd, hitOri)

  248. new Float:gdis2=vector_distance(fOri,hitOri)

  249. if( gdis2<43 ){
  250. entity_get_vector(entid,EV_VEC_origin,fOri)
  251. fOri[2]+=10
  252. entity_set_vector(entid,EV_VEC_origin,fOri)
  253. }

  254. entity_get_vector(entid,EV_VEC_velocity,Vel)
  255. if( hitent || gdis<60 ){
  256. //stop
  257. stop_fake(entid)

  258. //turn random angle
  259. entity_get_vector(entid,EV_VEC_v_angle,angle)
  260. new Float:fnum=random_float(-90.0,90.0)
  261. angle[1]+=fnum
  262. //angle[1]+=90.0
  263. entity_set_vector(entid,EV_VEC_v_angle,angle)
  264. return
  265. }
  266. if( Vel[0]==0.0 || Vel[1]==0.0 ){
  267. VelocityByAim(entid,FAKEPLAYERSPEED,Vel)
  268. Vel[2]=0.0
  269. vector_to_angle(Vel,angle)
  270. entity_set_vector(entid,EV_VEC_angles,angle)
  271. entity_set_vector(entid,EV_VEC_velocity,Vel)
  272. entity_set_int(entid,EV_INT_sequence,4)
  273. }
  274. }
复制代码

Super Moderator

职业杀手

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

帖子
1592
精华
0
6312
1757
51

热心会员奖 黑暗使者 可爱小猫

本帖最后由 q123456zw 于 2011-9-26 12:05 编辑

- -不知道干嘛用的

Rank: 2Rank: 2

帖子
54
精华
0
123
16
0
同上,怎么用啊[ntz43]

Honorary Member

在人海出现,同时也在人海中沉没

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

帖子
4193
精华
3
7989
696
151

黑暗使者 Modchina灌水王

干什么的。。。

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

帖子
429
精华
1
1888
244
20

懒人勋章 可爱小猫

- -怎么用呢...
我怎么设置游戏地图产生点
我用冷血FGD做地图 用冷血的NPC很爽,,,,,;;16

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

帖子
565
精华
0
2971
52
36
0 0用那么多模块
include <cstrike>
include <engine>
include <amxmisc>
include <fun>
不是多余的么0 0

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

帖子
2086
精华
0
6361
853
12
貌似可以用来做大灾变!?...

Super Moderator

「Cusmo17」

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

帖子
1211
精华
0
3355
143
59

MTT职员 可爱小猫 懒人勋章

类似与实体BOT做NPC么。
为啥突然想起以前玩的某魔丧服后来改出的网游系统。。
打不死(无视伤害,因为那实体是石头。。)的NPC。。。

Rank: 5Rank: 5Rank: 5Rank: 5Rank: 5

帖子
417
精华
0
1609
321
6
一看就知道是高人  怎么多代码

Rank: 5Rank: 5Rank: 5Rank: 5Rank: 5

帖子
70
精华
1
879
239
25
镇·魔链 发表于 2011-9-26 12:18 static/image/common/back.gif
同上,怎么用啊

把上面代码复制下来编译然后在amxx插件系统里注册。进入游戏会发现有个fake player,还有会随机从匪家产生各种服装的NPC
回顶部