📄 ai.lua
字号:
dofile(GetResPath("script/ai/ai_sdk.lua"))
vvv = 0
function debugout(c, info)
--if GetChaDefaultName(c)=="Demonic Snowman" then
--ChaNotice("New age baby", info..vvv)
--vvv = vvv + 1
--end
end
---------------------------- 涉及流程的AI处理函数 --------------------------------------------
--AITable = {}
--
--AITable[1] = {}
--AITable[1][1] = {}
--AITable["monster"] = {}
--AITable["monster"]["seearea"] = {}
--AITable["monster"]["seearea"]["type"] = "line"
--AITable["monster"]["seearea"].func = ai_idle
--AITable["monster"]["seearea"].func(c)
-----------------------------
-- 没有事情, 随机移动或者巡逻
-----------------------------
function ai_idle(c)
if ai_host(c)==1 then return end
if is_patrol(c)==0 then
birth_rand_move(c, 600) --在出生点附近6米范围内随机移动
else
local px, py = GetChaPatrolPos(c) --取出巡逻点
local ox, oy = GetChaSpawnPos(c) --取出出生点
local patrol_state = GetChaPatrolState(c)
if patrol_state==0 then
ChaMove(c, px, py)
SetChaPatrolState(c, 2) --设置为移动中
elseif patrol_state==1 then
ChaMove(c, ox, oy)
SetChaPatrolState(c, 3)
elseif patrol_state==2 then
if is_near_pos(c, px, py, 40)==1 then
SetChaPatrolState(c, 1)
end
elseif patrol_state==3 then
if is_near_pos(c, ox, oy, 40)==1 then
SetChaPatrolState(c, 0)
end
end
end
end
-----------------------
-- 有主人时的处理
-----------------------
function ai_host(c)
if GetChaAIType(c)==AI_NONE then return 0 end --水雷怪, 不移动
local host = GetChaHost(c)
if host~=nil then
local dis = get_distance(c, host)
if dis > 400 then --保持在4米以内
local hx, hy = GetChaPos(host)
local rx = 200 - Rand(400)
local ry = 200 - Rand(400)
ChaMove(c, hx + rx, hy + ry)
end
return 1
end
return 0
end
-----------------------
-- 碰到障碍时的处理
-----------------------
function ai_block(c, t)
if GetChaBlockCnt(c) > 0 then --被障碍挡住
local flee_flag = Rand(10)
if t~=nil and flee_flag > 5 then
flee(c, t) --有目标时有50%的几率反方向移动
else
local move_flag = 1--Rand(2)
if move_flag==1 then rand_move(c, 400) end --4米范围内随机移动
end
SetChaBlockCnt(c, 0) --障碍计数清0
end
end
-------------------------
-- 处理怪物有目标时的表现
-------------------------
function ai_target(c, t)
local ai_type = GetChaAIType(c)
if ai_type ~= 4 then
if ai_update_target(c, t, ai_type)==1 then --如果经过更新检查后, 目标已变, 则返回
return
end
end
--不同的ai类型对待目标有不同的方式
if ai_type==AI_FLEE then --被攻击后, 只会逃跑
flee(c, t)
return
elseif ai_type==4 then --被攻击后, 只会逃跑
star_move_to(c, t)
return
elseif ai_type==AI_ATK_FLEE then --被逼近后才逃跑
if is_near(c, t, 400)==1 then --目标已经靠近
flee(c, t)
return
end
end
--特殊AI战斗处理过程(主要针对Boss怪物)
if ai_type>20 then --20以上为BossAI
special_ai(c, t,ai_type)
else
local skill_id = select_skill(c) --怪物按照比率选择自己的技能
ChaUseSkill(c, t, skill_id) --向目标移动并使用技能
end
--一般技能(填写在角色表中的技能)处理过程
if is_cha_can_summon(c)==1 then
summon_monster(c, t) --召集其他同类怪来攻击目标
end
end
---------------------------
-- 处理怪物没有目标时的表现
---------------------------
function ai_no_target(c)
local ai_type = GetChaAIType(c)
if ai_type==4 then --被攻击后, 只会逃跑
--Notice("ai_no_target&ai_type==4")
star_delete_to(c)
return
end
local x, y = GetChaSpawnPos(c)
if is_near_pos(c, x, y, 100)==1 then
SetChaPatrolState(c, 0)
end
if ai_seek_target(c)==0 then --没有找到目标
if is_moving_back(c)==0 then --没有往回走
local now_x, now_y = GetChaPos(c)
local dis = (now_x - x) * (now_x - x) + (now_y - y) * (now_y - y)
if dis > 5000 then
ChaMove(c, x, y)
debugout(c, "ai_no_target Snowman distance spawn further MoveToSleep")
else
ai_idle(c)
end
else
local move_flag = Rand(2)
if move_flag==1 then
ChaMove(c, x, y)
debugout(c, "ai_no_target Snowman MoveToSleep")
end
end
end
end
-----------------------------------
-- 主动攻击的怪,则为自己寻找一个目标
-----------------------------------
function ai_seek_target(c)
local ai_t = GetChaAIType(c)
if ai_t<=AI_R_ATK then
return 0
end
local host = GetChaHost(c)
if host~=nil then
local t = GetChaFirstTarget(host) --通过伤害判断取得优先目标
if t~=nil and is_near(c, t, GetChaVision(c))==1 then
SetChaTarget(c, t)
return 1
end
else
local t = find_target(c, 0) --没有目标, 则寻找一个, 没有找到则t为空
if t~=nil then
SetChaTarget(c, t)
return 1
end
end
return 0
end
---------------------------------------------------------------
-- 检查目标的更新情况, return 1表示目标已变, return 0表示目标未变
---------------------------------------------------------------
function ai_update_target(c, t, ai_type)
if ai_type<=AI_N_ATK or t==GetChaHost(c) then --如果目标为主人则清除
clear_target(c)
return 1
end
local tNew = nil
if Rand(100) < CHANGE_TARGET_RATIO then
tNew = GetChaFirstTarget(c) --通过伤害判断取得优先目标
if tNew~=nil and tNew~=t then
clear_target(c) --清除原目标
SetChaTarget(c, tNew) --设置新目标
return 1
end
end
local vision = GetChaVision(c)
if is_near(c, t, vision)==0 then --目标距离已经太远
clear_target(c) --清除目标
return 1
end
--目标已经离开追击范围, 不再追击
if is_chase(c)==0 then --检查追击范围, 炮塔不执行
clear_target(c) --清除目标
local x, y = GetChaSpawnPos(c)
ChaMoveToSleep(c, x, y)
set_moving_back(c, 1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -