forgottenserver
forgottenserver copied to clipboard
Move raid system to globalevents
This effectively nukes the raid system in favor of global events, which permit better management for raids. It's easier to define raids on interval, on exact times, force execution, force scheduling, add monsters on different delays and randomized amounts and positions... and also gets rid of a command.
I added a lib with helper functions to add monsters to a raid. The event is called onRaid
and there is an example bundled pretty equivalent to the previous XML raid example.
Great job, but is it even necessary to have a seprate event for raid? We could just use onThink or onTime. If they wish and just create a raid library in lua.
It should be possible?
onRaid
falls between onTime
and onInterval
here, it works as both. Yes it is possible to completely ditch onRaid
and use just those two.
testraid.lua should use Position() constructor instead of the table constructor with x,y,z-fields. I think it would be cleaner if you could pass the amount of creatures to the addSpawn function.
True that, I will fix it to use the Position constructor later.
I will also patch the addSpawn function to allow another parameter, but I see it as removing clutter from the raid (explicit) to move to a lib function (implicit).
actually this entire thing should be handled completly in lua by coroutines and a simple addEvent timer at startup, could get rid of all c code then and we wouldn't hit server performence.
@EvilHero90 I agree with handling completely on Lua, but I couldn't figure how to use coroutines. Could you give me sample code for how you see raids?
I can do that on monday, I'm busy with work the entire weekend
This PR is (almost) on it's final form. I added a generic interface to search and force execute global events. I couldn't find an use to force execute startup, shutdown or record events, so they are ignored.
Also, it means it's possible to force trigger a server save on the Lua side.
Edit: actually I'll just reuse the previous Game.startRaid
function, just renamed to Game.startEvent
.
@ranisalt since seems he doesn't got time yet i made an example with coroutines.
addEvent(checkRaids, 15000)
local inProgress = false
function ratPlague()
local pos = Position(146, 388, 7)
broadcastMessage("Rat plague in city!", MESSAGE_STATUS_WARNING)
Game.createMonster("Rat", pos)
coroutine.yield()
inProgress = false
end
function checkRaids()
print(coroutine.status(start))
if not inProgress then
if math.random(1, 1) == 1 then
inProgress = true
coroutine.resume(start)
end
end
addEvent(checkRaids, 15000)
end
start = coroutine.create(ratPlague)
to make this a real raid system with coroutines would needed a lib as big as npcs is, i personally don't know performence impact with this tho, in my opinion coroutines are just a complicated way to call functions.
If that is, I don't know how much better this is than addEvent
.
i don't see the point also, if is needed to call a addEvent it becomes a complicated way to call functions, tho im curious to see how he would do that.
Rebased against master.
I still intend to keep it up to date and merge when possible. This alone shaves 20 seconds off the build time 😄 besides -800 line delta.
Only thing I can think you should "add" is a converter XML -> Lua Maybe this should wait for the next stable release, but looks good to me :+1:
@WibbenZ working on it! Check the new stuff under tools/ 😄
Rebased this into https://github.com/otland/forgottenserver/tree/bye-bye-raids because I couldn't force push to fork repo.
Ok, gonna rebase it and fix compilation issues.
@djarek I added backwards compatibility for Game.startRaid
.
Still didn't finish conversion tool, few stuff missing.
- [x] store raid to file
- [x] use
boost::filesystem
- [ ] add script tags to globalevents.xml
@ranisalt Would be good if that tool is not related to the TFS source code, so people who use the appveyor artifacts can still use the tool. Aka come pre-compiled or as a migration.
@WibbenZ I am not sure how to do that. Is it possible to add a second artifact to appveyor?
Edit: I see that it is, but I can't try on Windows to get the correct executable path help wanted
Would it make sense to move the conversion tool into it's own repo under the otland organization?
Well we don't need to use appveyor but maybe compile it, zip it and add a link in the readme or something like that. But if we want to we should do what @soul4soul said.
The point was so people who don't want to / can't compile can still use this tool.
Since the tool uses much of TFS code, how would you do that?
Good question, nvm it. Someone will most likely compile and upload for those who can't or they can convert them manually.
So you basically removed raid system. But how to write a raid being a beginner? :P Your provided Lua example is complicated for newbie IMO. The old raid system was way easier.
You mentioned something about helper lib, but I can't seem to find it in this PR?
Your provided Lua example is complicated for newbie IMO
Why? It is simple Lua code that a lot of actions, events and movements already do (e.g. annihilation quest summons monsters, server save broadcasts message). I thought this was enough as example.
I didn't implement the helper lib since all functions already existed in TFS.
In old raid system, you needed to know some basic XML syntax, and copy-paste to add more monsters/spawns. Here you need to know some basic programming functions if you want to modify it. It is good as an example, but to make something more advanced from this, as a beginner it wouldn't be easy.
Especially I mean that "for" loops for creating more spawns. Even for me it took some time to understand it.
I would suggest either make a parser that parses old raids, so you can still use them, or make some helper library in Lua, with some functions to simplicify the whole process of creating a raid.
Like instead of this in old raid system:
<areaspawn delay="30000" fromx="89" fromy="122" fromz="7" tox="99" toy="130" toz="7">
<monster name="Rat" minamount="4" maxamount="10" /><!-- Random amount in specified range -->
</areaspawn>
you could write some helper function like this in Lua:
Raids.spawn(30000, {from = {x = 89, y = 122, z = 7}, to = {x = 99, y = 130, z = 7}},
{
{"Rat", 1, 4}, -- name, minamount, maxamount
{"Cave Rat", 1, 4}, -- name, minamount, maxamount
}
)
So you can easily add more monsters to this by copying lines.
There's a parser that converts old raids to new Lua code. I didn't wish to introduce new functions since I didn't want to repeat code. I could add a more extensive documentation though.
Ok then except that it looks good. Didn't saw that converter, cause it was hidden by default.
Good job!