lua 游戏架构 之 游戏 AI (五)ai_autofight_find_way
这段Lua脚本定义了一个名为 `ai_autofight_find_way` 的类,继承自 `ai_base` 类。
lua 游戏架构 之 游戏 AI (一)ai_base-CSDN博客文章浏览阅读238次。定义了一套接口和属性,可以基于这个基础类派生出具有特定行为的AI组件。例如,可以创建追逐敌人的AI、巡逻的AI或使用特定策略的AI等,都继承自这个基础类https://blog.csdn.net/heyuchang666/article/details/140624481?spm=1001.2014.3001.5502
这个类用于处理游戏中AI在自动战斗模式下寻找路径的逻辑。以下是对代码的具体解释:
1. **引入基类**:
- 使用 `require` 函数引入 `ai_base` 类,作为基础类。
2. **定义 `ai_autofight_find_way` 类**:
- 使用 `class` 关键字定义了 `ai_autofight_find_way` 类,并继承自 `BASE`(即 `ai_base`)。
3. **构造函数 (`ctor`)**:
- 构造函数接受一个 `entity` 参数,并设置 `_type` 属性为 `eAType_AUTOFIGHT_FIND_WAY`,表示自动战斗中寻找路径的行为。
- 初始化 `_target` 为 `nil`,用于后续存储找到的目标。
4. **`IsValid` 方法**:
- - 这个方法用于验证AI是否应该寻找路径。它首先检查实体是否开启了自动战斗(`_AutoFight`),是否死亡或无法攻击。
- - 检查实体的行为,如果处于准备战斗或禁止攻击状态,则返回 `false`。
- - 计算警报范围 `radius`,可能基于实体的属性或世界配置。
- - 根据不同的地图类型和条件,确定是否需要寻找路径。
5. **`OnEnter` 方法**:
- 当AI组件进入激活状态时执行。根据当前地图类型和条件,计算目标位置并使实体移动到该位置。
6. **`OnLeave` 方法**:
- 当AI组件离开激活状态时执行。当前实现中直接返回 `true`。
7. **`OnUpdate` 方法**:
- 每帧调用,用于更新AI状态。如果基类的 `OnUpdate` 方法返回 `true`,则当前方法也返回 `true`。
8. **`OnLogic` 方法**:
- 逻辑更新方法,如果基类的 `OnLogic` 方法返回 `true`,则当前方法返回 `false`,表示只执行一次。
9. **创建组件函数**:
- `create_component` 函数用于创建 `ai_autofight_find_way` 类的新实例,传入一个实体和一个优先级。
代码中的一些关键点:
- - `IsDead()`:检查实体是否死亡。
- - `CanAttack()`:检查实体是否可以攻击。
- - `GetPropertyValue(ePropID_alertRange)`:获取实体的警报范围属性。
- - `game_get_world()`:获取游戏世界配置。
- - `Test(eEBPrepareFight)` 和 `Test(eEBDisAttack)`:检查实体的行为状态。
- - `MoveTo()`:移动到指定位置。
这个脚本为游戏中的AI提供了一个自动战斗中寻找路径的基础框架,可以根据具体游戏的需求进行扩展和修改。以下是一些具体的逻辑处理:
- - 根据不同的地图类型(如 `g_BASE_DUNGEON`、`g_ACTIVITY` 等),AI的行为可能会有所不同。
- - 计算与目标的距离,并根据距离决定是否移动。
- - 考虑地图上的特定点(如物品掉落点、怪物刷新点)来决定移动路径。
- - 使用 `vec3_dist` 函数计算两个位置之间的距离,并根据距离决定是否移动到该位置。
整体而言,这个类的目的是在自动战斗模式下,根据游戏世界的当前状态和配置,为AI实体找到合适的移动路径。
重点解释一下 OnEnter:
function ai_autofight_find_way:OnEnter()if BASE.OnEnter(self) thenlocal entity = self._entity;local radius = entity:GetPropertyValue(ePropID_alertRange);local logic = game_get_logic();local world = game_get_world();if world then-- 如果世界配置中有自动战斗半径,则使用该值if world._cfg.autofightradius thenradius = world._cfg.autofightradius;end-- 根据不同的地图类型执行不同的逻辑if world._mapType == g_BASE_DUNGEON or world._mapType == g_ACTIVITY or ... then-- 检查所有掉落物品,如果物品处于激活状态,则移动到该物品位置for k,v in pairs(world._ItemDrops) doif v and v:GetStatus() == eSItemDropActive thenlocal _pos = logic_pos_to_world_pos(v._curPos);entity:MoveTo(_pos);return false; -- 移动到物品位置后,退出函数endend-- 如果地图类型是开放区域,并且有怪物刷新点或当前活动区域if world._openType == g_FIELD then-- 寻找一个有活着的怪物的刷新点local _pos = nil;local isfind = false;for k1,v1 in pairs(world._curArea._spawns) dofor k2,v2 in pairs(v1._monsters) doif not v2:IsDead() thenisfind = true;break;endendif isfind then_pos = v1._cfg.pos;break;endend-- 如果没有找到有活着的怪物的刷新点,使用第一个刷新点的位置if not _pos then_pos = world._curArea._spawns[1]._cfg.pos;end-- 计算实体当前位置到刷新点或地图增益点的距离local dist = vec3_dist(entity._curPos,world_pos_to_logic_pos(_pos));local mindist = dist;-- 寻找最近的地图增益点for k,v in pairs(world._mapbuffs) doif v and v:GetStatus() == 1 thenlocal distbuff = vec3_dist(v._curPos,entity._curPos);if distbuff < mindist and distbuff < db_common.droppick.AutoFightMapbuffAutoRange thenmindist = distbuff;_pos = logic_pos_to_world_pos(v._curPos);endendend-- 移动实体到计算出的位置entity:MoveTo(_pos);end-- 其他地图类型的逻辑...elseif world._mapType == g_FIELD or world._mapType == g_Life then-- 对于其他地图类型,寻找最近的地图增益点并移动实体-- ...endendreturn false; -- 如果没有找到目标位置或执行了移动逻辑,则返回falseendreturn false; -- 如果没有调用基类的OnEnter或基类返回false,则返回false
end
在 OnEnter
方法中,首先调用基类的 OnEnter
方法,如果它返回 false
,则直接返回 false
。如果基类的 OnEnter
方法返回 true
,则继续执行以下逻辑:
- 获取实体的警报范围
radius
。 - 检查游戏世界配置,如果存在自动战斗半径配置,则使用该配置值覆盖实体的警报范围。
- 根据当前的地图类型,执行不同的逻辑来寻找目标位置。例如:
- 如果是
g_BASE_DUNGEON
、g_ACTIVITY
等地图类型,会检查所有物品掉落点,寻找激活的物品并移动到该位置。 - 如果是开放区域(
g_FIELD
),会寻找有活着的怪物的刷新点或最近的地图增益点,并移动实体到该位置。
- 如果是
- 使用
vec3_dist
函数计算实体当前位置到目标位置的距离,并根据这个距离来确定是否移动实体。 - 如果找到目标位置,则调用
entity:MoveTo(_pos)
方法移动实体到该位置,然后返回false
退出函数。 - 如果没有找到目标位置或不满足移动条件,则返回
false
。
整体而言,OnEnter
方法的目的是确定AI在自动战斗模式下应该移动到哪个位置,并执行移动操作。
全部代码实现:
----------------------------------------------------------------
module(..., package.seeall)local require = requirelocal BASE = require("logic/entity/ai/ai_base").ai_base;------------------------------------------------------
ai_autofight_find_way = class("ai_autofight_find_way", BASE);
function ai_autofight_find_way:ctor(entity)self._type = eAType_AUTOFIGHT_FIND_WAY;self._target = nil;
endfunction ai_autofight_find_way:IsValid()local entity = self._entity;if not entity._AutoFight thenreturn false;endif entity:IsDead() or not entity:CanAttack() thenreturn false;endif entity._behavior:Test(eEBPrepareFight) thenreturn false;endif entity._behavior:Test(eEBDisAttack) thenreturn false;endlocal radius = entity:GetPropertyValue(ePropID_alertRange);local world = game_get_world();if world thenif world._cfg.autofightradius thenradius = world._cfg.autofightradius;endlocal target = entity._alives[2][1]; -- 敌方if entity._alives[3][1] then--中立local trap = entity._alives[3][1];if trap.entity and trap.entity._traptype == eSTrapActive thentarget = entity._alives[3][1]; endendif target thenif target.dist < radius thenif target.entity._groupType == eGroupType_N and target.dist > db_common.droppick.AutoFightMapbuffAutoRange thenelsereturn false;endendelseif world._mapType == g_TOURNAMENT thenreturn false;endendif world._mapType == g_BASE_DUNGEON or world._mapType == g_ACTIVITY or world._mapType == g_FACTION_DUNGEON or world._mapType == g_TOWER or world._mapType == g_WEAPON_NPC or world._mapType == g_RIGHTHEART or world._mapType == g_ANNUNCIATE or world._mapType == g_FIGHT_NPC or world._mapType == g_Pet_Waken thenif world._openType == g_FIELD thenif #world._spawns == 0 and not world._curArea then return falseendelselocal spawnID = math.abs(g_game_context:GetDungeonSpawnID())if spawnID == 0 thenreturn falseendlocal dist = nil;if spawnID ~= 0 thenspawnPointID = db_spawn_area[spawnID].spawnPoints[1]_pos = db_spawn_point[spawnPointID].posdist = vec3_dist(entity._curPos,world_pos_to_logic_pos(_pos))if dist and dist < 100 thenreturn falseend endendelseif world._mapType == g_FIELD or world._mapType == g_Life thenif entity._PVPStatus ~= g_PeaceMode thenreturn false;endlocal dist = vec3_dist(entity._curPos,entity._AutoFight_Point)if dist < radius thenreturn false;endlocal value = g_game_context:getAutoFightRadius()if value and value == g_OneMap thenreturn false;endelse -- TODOreturn false;endendreturn true;
endfunction ai_autofight_find_way:OnEnter()if BASE.OnEnter(self) thenlocal entity = self._entity;local radius = entity:GetPropertyValue(ePropID_alertRange)local logic = game_get_logic();local world = game_get_world();if world thenif world._cfg.autofightradius thenradius = world._cfg.autofightradiusendif world._mapType == g_BASE_DUNGEON or world._mapType == g_ACTIVITY or world._mapType == g_FACTION_DUNGEON or world._mapType == g_TOWER or world._mapType == g_WEAPON_NPC or world._mapType == g_RIGHTHEART or world._mapType == g_ANNUNCIATE or world._mapType == g_FIGHT_NPC or world._mapType == g_Pet_Waken thenfor k,v in pairs(world._ItemDrops) doif v and v:GetStatus() == eSItemDropActive thenlocal _pos = logic_pos_to_world_pos(v._curPos)entity:MoveTo(_pos)return false;endendif world._openType == g_FIELD thenif #world._spawns > 0 or world._curArea thenlocal _pos = nil;local isfind = falsefor k1,v1 in pairs(world._curArea._spawns) dofor k2,v2 in pairs(v1._monsters) doif not v2:IsDead() thenisfind = true;break;endendif isfind then_pos = v1._cfg.pos;break;endendif not _pos then_pos = world._curArea._spawns[1]._cfg.posend--local _pos = world._curArea._spawns[1]._cfg.poslocal dist = vec3_dist(entity._curPos,world_pos_to_logic_pos(_pos))local mindist = distfor k,v in pairs(world._mapbuffs) doif v and v:GetStatus() == 1 thenlocal distbuff = vec3_dist(v._curPos,entity._curPos)if distbuff < mindist and distbuff < db_common.droppick.AutoFightMapbuffAutoRange thenmindist = distbuff_pos = logic_pos_to_world_pos(v._curPos)endendendentity:MoveTo(_pos)endelselocal _pos = nillocal spawnID = math.abs(g_game_context:GetDungeonSpawnID())local dist = 99999999999;if spawnID ~= 0 thenspawnPointID = db_spawn_area[spawnID].spawnPoints[1]_pos = db_spawn_point[spawnPointID].posdist = vec3_dist(entity._curPos,world_pos_to_logic_pos(_pos)) endlocal mindist = distlocal isspawn = truefor k,v in pairs(world._mapbuffs) doif v and v:GetStatus() == 1 thenlocal distbuff = vec3_dist(v._curPos,entity._curPos)if distbuff < mindist and distbuff < db_common.droppick.AutoFightMapbuffAutoRange thenmindist = distbuffisspawn = false;_pos = logic_pos_to_world_pos(v._curPos)endendendif mindist < 150 and isspawn and g_game_context:GetDungeonSpawnID() < 0 theng_game_context:SetDungeonSpawnID(0);_pos = nil;endif _pos thenentity:MoveTo(_pos)endendelseif world._mapType == g_FIELD or world._mapType == g_Life thenfor k,v in pairs(world._mapbuffs) doif v and v:GetStatus() == 1 thenlocal distbuff = vec3_dist(v._curPos,entity._AutoFight_Point)if distbuff < radius and distbuff < db_common.droppick.AutoFightMapbuffAutoRange thenlocal _pos = logic_pos_to_world_pos(v._curPos)entity:MoveTo(_pos)return false;endendendendendreturn false;endreturn false;
endfunction ai_autofight_find_way:OnLeave()if BASE.OnLeave(self) thenreturn true;endreturn false;
endfunction ai_autofight_find_way:OnUpdate(dTime)if BASE.OnUpdate(self, dTime) thenreturn true;endreturn false;
endfunction ai_autofight_find_way:OnLogic(dTick)if BASE.OnLogic(self, dTick) thenreturn false; -- only one frameendreturn false;
endfunction create_component(entity, priority)return ai_autofight_find_way.new(entity, priority);
end
相关文章:
lua 游戏架构 之 游戏 AI (五)ai_autofight_find_way
这段Lua脚本定义了一个名为 ai_autofight_find_way 的类,继承自 ai_base 类。 lua 游戏架构 之 游戏 AI (一)ai_base-CSDN博客文章浏览阅读238次。定义了一套接口和属性,可以基于这个基础类派生出具有特定行为的AI组件。例如&…...

vue3+openLayers点击标记事件
<template><!--地图--><div class"distributeMap" id"distributeMap"></div> </template> <script lang"ts" setup> import { onMounted, reactive } from "vue"; import { Feature, Map, View }…...

深入分析 Android ContentProvider (三)
文章目录 深入分析 Android ContentProvider (三)ContentProvider 的高级使用和性能优化1. 高级使用场景1.1. 数据分页加载示例:分页加载 1.2. 使用 Loader 实现异步加载示例:使用 CursorLoader 加载数据 1.3. ContentProvider 与权限管理示例࿱…...

养宠浮毛异味双困扰?性价比高的宠物空气净化器推荐
家里养了两只银渐层,谁懂啊!一下班打开家门就看到家里飘满了猫浮毛雪,空气中还传来隐隐约约的异味。每天不是在吸毛的路上,就是在洗猫砂盆的路上,而且空气中的浮毛还很难清理干净,这是最让人头疼的问题。 …...

maven项目容器化运行之3-优雅的利用Jenkins和maven使用docker插件调用远程docker构建服务并在1Panel中运行
一.背景 在《maven项目容器化运行之1》中,我们开启了1Panel环境中docker构建服务给到了局域网。在《maven项目容器化运行之2》中,我们基本实现了maven工程创建、远程调用docker构建镜像、在1Panel选择镜像运行容器三大步骤。 但是,存在一个问…...

docker 打包orbbec
docker pull humble容器 sudo docker run -it osrf/ros:humble-desktop docker 启动容器 sudo docker run -u root --device/dev/bus/usb:/dev/bus/usb -it -v /home/wl:/share --name wl4 osrf/ros:humble-desktop /bin/bash新开一个终端 查看本地存在的容器:…...

无涯·问知财报解读,辅助更加明智的决策
财报解读就像是给公司做一次全面的体检,是理解公司内部运作机制和市场表现的一把钥匙,能够有效帮助投资者、分析师、管理层以及所有市场参与者判断一家公司的健康程度和发展潜力。 星环科技无涯问知的财经库内置了企业年报及财经类信息,并对…...

【Apache Doris】数据副本问题排查指南
【Apache Doris】数据副本问题排查指南 一、问题现象二、问题定位三、问题处理 本文主要分享Doris中数据副本异常的问题现象、问题定位以及如何处理此类问题。 一、问题现象 问题日志 查询报错 Failed to initialize storage reader, tablet{tablet_id}.xxx.xxx问题说明 查…...

【HarmonyOS】关于鸿蒙消息推送的心得体会(二)
【HarmonyOS】关于鸿蒙消息推送的心得体会(二) 前言 推送功能的开发与传统功能开发还是有很大区别。首先最大的区别点就在于需要多部门之间的协同,作为鸿蒙客户端开发,你需要和产品,运营,以及后台开发一起…...

零基础入门:创建一个简单的Python爬虫管理系统
摘要: 本文将手把手教你,从零开始构建一个简易的Python爬虫管理系统,无需编程基础,轻松掌握数据抓取技巧。通过实战演练,你将学会设置项目、编写基本爬虫代码、管理爬取任务与数据,为个人研究或企业需求奠…...

【Node.js基础04】node.js模块化
一:什么是模块化 在Node.js中,每个文件都可视为一个独立的模块。模块化提高了代码的复用性,按需加载,具有独立的作用域 二:如何实现多个文件间导入和导出 1 CommonJS标准(默认)-导入和导出 …...

数据库——单表查询
一、建立数据库mydb8_worker mysql> use mydb8_worker; 二、建立表 1.创建表 mysql> create table t_worker(department_id int(11) not null comment 部门号,-> worder_id int(11) primary key not null comment 职工号,-> worker_date date not null comment…...
dsa加训
refs: OI Wiki - OI Wiki (oi-wiki.org) 1. 枚举 POJ 2811 熄灯问题 refs : OpenJudge - 2811:熄灯问题 如果要枚举每个灯开或者不开的情况,总计2^30种情况,显然T。 不过我们可以发现:若第i行的某个灯亮了,那么有且仅有第i行和第…...

SpringBoot源码(1)ApplicationContext和BeanFactory
1、调用getBean方法 SpringBootApplication public class SpringBootDemoApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext SpringApplication.run(SpringBootDemoApplication.class, args);applicationContext.get…...

CANoe编程实例--TCP/IP通信
1、简介 本实例将使用目前常用的开发工具C#来开发服务器端,以CANoe端作为客户端。服务器端和客户端,通过TCP/IP连接,实现数据交换。 首先在服务器端建立一个监听Socket,自动创建一个监听线程,随时监听是否有客户端的连…...

Neuron协议网关的北向应用插件开发
目录 概述 指令处理层开发 应用层开发 .open .close .init .uninit .start .stop .setting .request 插件设置文件 适配华为的思路 概述 最近研究了一段时间的Neuron协议网关,前面的博文也提到它虽然能够把数据发到华为的IoT平台上…...

【BUG】已解决:You are using pip version 10.0.1, however version 21.3.1 is available.
You are using pip version 10.0.1, however version 21.3.1 is available. 目录 You are using pip version 10.0.1, however version 21.3.1 is available. 【常见模块错误】 【解决方案】 欢迎来到英杰社区https://bbs.csdn.net/topics/617804998 欢迎来到我的主页&#…...

electron-builder打包vue2项目不显示element-ui图标
1、使用版本 vue ^2.6.14element-ui ^2.15.14vue-cli-plugin-electron-builder 2.1.1 2、解决办法 1) 如果是简单的图标可以使用图片代替(这种对于elementui组件的图标还是不会显示) 2)在vue.config.js配置 const { defineCon…...

controller层-请求格式为json-请求方法为get
前置条件 get请求映射,内容和PostMapping一致,需要请求参数更换为get数据 请求过程:用户请求--初始化DispatcherServlet及对接和分发用户请求--controller--service 用户请求:http://ip:port/user/getinfo 请求方法:ge…...

【Linux】网络通信基础:应用层协议、HTTP、序列化与会话管理
文章目录 前言1. 应用层自定义协议与序列化1.1 什么是应用层?1.2 再谈 "协议"1.3 序列化 和 反序列化 2. HTTP 协议3. 认识 URL(统一资源定位符)4. urlencode和urldecode5. HTTP 协议请求与响应格式5.1 HTTP 请求5.2 HTTP 响应 6. HTTP 的方法6.1 GET 方法…...

@NotNull、@NotEmpty 和 @NotBlank 区别
NotNull、NotEmpty 和 NotBlank 是 Java Bean Validation (JSR 380) 规范中定义的注解,通常用于验证对象的属性是否满足特定的条件。这些注解常用于后端验证,确保接收到的数据符合预期。 NotNull 用途:验证一个对象是否不为null。 注意&#…...

大模型应用—大模型赋能网络爬虫
大模型赋能网络爬虫 简单来说,网页抓取就是从网站抓取数据和内容,然后将这些数据保存为XML、Excel或SQL格式。除了用于生成潜在客户、监控竞争对手和市场研究外,网页抓取工具还可以用于自动化你的数据收集过程。 借助AI网页抓取工具,可以解决手动或纯基于代码的抓取工具的…...

在 Qt 中获取 MouseMove 事件
在编写 Qt 程序时,我希望在鼠标移动时(即使鼠标在另一个窗口上)能够调用 mouseMoveEvent(QMouseEvent* event) 方法。目前,在我的 mainwindow.cpp 文件中,我有如下代码: void MainWindow::mouseMoveEvent(…...

自动驾驶系列—智能巡航辅助功能中的路口通行功能介绍
自动驾驶系列—智能巡航辅助功能中的车道中央保持功能介绍 自动驾驶系列—智能巡航辅助功能中的车道变换功能介绍 自动驾驶系列—智能巡航辅助功能中的横向避让功能介绍 自动驾驶系列—智能巡航辅助功能中的路口通行功能介绍 文章目录 2. 功能定义3. 功能原理4. 传感器架构5. 实…...

如何为WordPress网站设置多语言站点
随着全球化的发展,拥有一个支持多语言的站点已成为提升用户体验、扩大受众范围的重要手段。本文将详细介绍如何为WordPress网站设置多语言站点,提供两种最佳方案详解,帮助您轻松实现多语言站点的搭建与管理。无论您是选择在同一站点内发布多语…...

【RHCE】综合真机实验(shell完成)
目录 题目: 需求描述 实操 一、服务端(servera) 1.ip配置 2.更改主机名 3.创建本地仓库 4.DNS服务 1.下载软件包和防火墙允许 2.配置主配置文件 3.配置区域文件 1.named.exam 2.named.fangxiang 4.重启服务 5.验证结果&#x…...

【Python】成功解决conda创建虚拟环境时出现的CondaHTTPError: HTTP 000 CONNECTION FAILED错误
【Python】成功解决conda创建虚拟环境时出现的CondaHTTPError: HTTP 000 CONNECTION FAILED错误 🌈 欢迎莅临我的个人主页👈这里是我深耕Python编程、机器学习和自然语言处理(NLP)领域,并乐于分享知识与经验的小天地&a…...

苹果笔记本电脑如何优化系统 苹果电脑系统优化软件哪个好 cleanmymac x怎么用
随着时间的推移,你可能会发现你的MacBook运行速度变慢,甚至在执行一些基本任务时也会感觉到卡顿。这不仅影响了工作效率,也大大降低了使用体验。但别担心,优化你的Mac系统比做早餐还简单。本文将用一种轻松的风格向你介绍7种简单易…...

Vue数组操作之sort详解
在 Vue.js 中,sort() 方法用于对数组进行排序。它会改变原数组,并返回排序后的数组。默认情况下,sort() 方法按照字母顺序(Unicode 编码顺序)对数组中的元素进行排序。如果需要按照其他规则排序,可以传递一…...

解决 Android 应用安装错误:INSTALL_FAILED_BAD_PERMISSION_GROUP
解决 Android 应用安装错误:INSTALL_FAILED_BAD_PERMISSION_GROUP 在开发 Android 应用时,我们有时会遇到安装错误。这篇文章将讨论一种常见的错误:INSTALL_FAILED_BAD_PERMISSION_GROUP,并介绍解决方法。 问题描述 在尝试安装…...