以下为你分享一些在Gom引擎架设传奇游戏时优化脚本性能的具体案例:
### 怪物脚本优化案例
#### 案例背景
在一个传奇游戏中,地图上怪物众多,每个怪物都有自己的AI逻辑,包括移动、攻击、回血等。但随着游戏运营,玩家反馈怪物移动和攻击卡顿,服务器负载明显升高。
#### 优化前的脚本情况
原本的怪物脚本中,每个怪物的AI逻辑是在一个大循环里依次执行,并且每次循环都会检查所有怪物的状态,不管该怪物当前是否活跃(例如是否已经死亡)。以下是简化示例:
```lua
while true do
for _, monster in ipairs(monsters) do
-- 检查怪物是否存活
if monster:isAlive() then
-- 怪物移动逻辑
monster:move()
-- 怪物攻击逻辑
if monster:canAttack() then
monster:attack()
end
-- 怪物回血逻辑
monster:recoverHP()
end
end
-- 短暂延迟
os.execute("sleep 0.1")
end
```
#### 优化策略及代码
1. **减少不必要的检查**:只对活跃的怪物进行AI逻辑处理。可以维护一个活跃怪物列表,当怪物死亡时将其从列表中移除。
```lua
-- 初始化活跃怪物列表
local activeMonsters = {}
for _, monster in ipairs(monsters) do
if monster:isAlive() then
table.insert(activeMonsters, monster)
end
end
while true do
for i = #activeMonsters, 1, -1 do
local monster = activeMonsters[i]
if monster:isAlive() then
monster:move()
if monster:canAttack() then
monster:attack()
end
monster:recoverHP()
else
-- 怪物死亡,从活跃列表中移除
table.remove(activeMonsters, i)
end
end
os.execute("sleep 0.1")
end
```
2. **分散AI逻辑执行时间**:将怪物的AI逻辑分散到不同的时间点执行,避免同一时间处理大量怪物的逻辑。例如,将回血逻辑放到另一个循环中,每隔一段时间执行一次。
```lua
local activeMonsters = {}
for _, monster in ipairs(monsters) do
if monster:isAlive() then
table.insert(activeMonsters, monster)
end
end
local recoverCounter = 0
while true do
for i = #activeMonsters, 1, -1 do
local monster = activeMonsters[i]
if monster:isAlive() then
monster:move()
if monster:canAttack() then
monster:attack()
end
else
table.remove(activeMonsters, i)
end
end
-- 每5次循环执行一次回血逻辑
recoverCounter = recoverCounter + 1
if recoverCounter % 5 == 0 then
for _, monster in ipairs(activeMonsters) do
if monster:isAlive() then
monster:recoverHP()
end
end
end
os.execute("sleep 0.1")
end
```
#### 优化效果
优化后,服务器负载明显降低,怪物的移动和攻击变得更加流畅,玩家反馈卡顿问题得到显著改善。
### 玩家交易脚本优化案例
#### 案例背景
游戏中玩家交易系统使用频繁,但在交易高峰时段,玩家反馈交易响应缓慢,甚至出现交易失败的情况。
#### 优化前的脚本情况
原本的交易脚本在处理玩家交易时,会先查询双方玩家的物品和金币信息,然后进行物品和金币的转移操作。但查询和转移操作是串行执行的,并且没有对并发交易进行处理。以下是简化示例:
```lua
function handleTrade(player1, player2, items, gold)
-- 查询玩家1的物品和金币信息
local player1Items = player1:getItems()
local player1Gold = player1:getGold()
-- 查询玩家2的物品和金币信息
local player2Items = player2:getItems()
local player2Gold = player2:getGold()
-- 检查玩家1是否有足够的物品和玩家2是否有足够的金币
if player1:hasItems(items) and player2:hasGold(gold) then
-- 进行物品和金币的转移
player1:removeItems(items)
player2:addItems(items)
player1:addGold(gold)
player2:removeGold(gold)
end
end
```
#### 优化策略及代码
1. **并发处理**:使用多线程或协程来处理并发交易,提高交易处理的效率。在Gom引擎中可以使用协程来实现。
```lua
local coroutinePool = {}
function handleTrade(player1, player2, items, gold)
local co = coroutine.create(function()
-- 查询玩家1的物品和金币信息
local player1Items = player1:getItems()
local player1Gold = player1:getGold()
-- 查询玩家2的物品和金币信息
local player2Items = player2:getItems()
local player2Gold = player2:getGold()
-- 检查玩家1是否有足够的物品和玩家2是否有足够的金币
if player1:hasItems(items) and player2:hasGold(gold) then
-- 进行物品和金币的转移
player1:removeItems(items)
player2:addItems(items)
player1:addGold(gold)
player2:removeGold(gold)
end
end)
table.insert(coroutinePool, co)
coroutine.resume(co)
end
-- 定期清理已完成的协程
function cleanCoroutinePool()
for i = #coroutinePool, 1, -1 do
local co = coroutinePool[i]
if coroutine.status(co) == "dead" then
table.remove(coroutinePool, i)
end
end
end
while true do
cleanCoroutinePool()
os.execute("sleep 1")
end
```
2. **事务处理**:在数据库层面使用事务来确保交易的原子性,避免数据不一致的问题。
```lua
function handleTrade(player1, player2, items, gold)
local co = coroutine.create(function()
db:beginTransaction()
local success = true
-- 查询玩家1的物品和金币信息
local player1Items = player1:getItems()
local player1Gold = player1:getGold()
-- 查询玩家2的物品和金币信息
local player2Items = player2:getItems()
local player2Gold = player2:getGold()
-- 检查玩家1是否有足够的物品和玩家2是否有足够的金币
if player1:hasItems(items) and player2:hasGold(gold) then
-- 进行物品和金币的转移
success = success and player1:removeItems(items)
success = success and player2:addItems(items)
success = success and player1:addGold(gold)
success = success and player2:removeGold(gold)
else
success = false
end
if success then
db:commit()
else
db:rollback()
end
end)
table.insert(coroutinePool, co)
coroutine.resume(co)
end
```
#### 优化效果
优化后,交易系统的响应速度明显提高,并发处理能力增强,交易失败的情况大幅减少,玩家的交易体验得到了显著提升。

