forgottenserver icon indicating copy to clipboard operation
forgottenserver copied to clipboard

Move raid system to globalevents

Open ranisalt opened this issue 8 years ago • 55 comments

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.

ranisalt avatar Jun 02 '16 10:06 ranisalt

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?

PrinterLUA avatar Jun 02 '16 12:06 PrinterLUA

onRaid falls between onTime and onInterval here, it works as both. Yes it is possible to completely ditch onRaid and use just those two.

ranisalt avatar Jun 02 '16 12:06 ranisalt

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.

TheSumm avatar Jun 02 '16 12:06 TheSumm

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).

ranisalt avatar Jun 02 '16 12:06 ranisalt

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 avatar Jun 02 '16 20:06 EvilHero90

@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?

ranisalt avatar Jun 04 '16 02:06 ranisalt

I can do that on monday, I'm busy with work the entire weekend

EvilHero90 avatar Jun 04 '16 09:06 EvilHero90

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 avatar Jun 28 '16 03:06 ranisalt

@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.

wgrr avatar Jul 07 '16 06:07 wgrr

If that is, I don't know how much better this is than addEvent.

ranisalt avatar Jul 07 '16 07:07 ranisalt

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.

wgrr avatar Jul 07 '16 17:07 wgrr

Rebased against master.

ranisalt avatar Dec 28 '16 04:12 ranisalt

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.

ranisalt avatar Oct 03 '17 18:10 ranisalt

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 avatar Oct 03 '17 18:10 WibbenZ

@WibbenZ working on it! Check the new stuff under tools/ 😄

ranisalt avatar Oct 04 '17 05:10 ranisalt

Rebased this into https://github.com/otland/forgottenserver/tree/bye-bye-raids because I couldn't force push to fork repo.

DSpeichert avatar Dec 17 '17 20:12 DSpeichert

Ok, gonna rebase it and fix compilation issues.

ranisalt avatar Dec 19 '17 02:12 ranisalt

@djarek I added backwards compatibility for Game.startRaid.

ranisalt avatar Dec 23 '17 18:12 ranisalt

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 avatar Dec 24 '17 17:12 ranisalt

@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 avatar Dec 24 '17 17:12 WibbenZ

@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

ranisalt avatar Dec 24 '17 17:12 ranisalt

Would it make sense to move the conversion tool into it's own repo under the otland organization?

soul4soul avatar Dec 24 '17 19:12 soul4soul

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.

WibbenZ avatar Dec 25 '17 10:12 WibbenZ

Since the tool uses much of TFS code, how would you do that?

ranisalt avatar Dec 25 '17 21:12 ranisalt

Good question, nvm it. Someone will most likely compile and upload for those who can't or they can convert them manually.

WibbenZ avatar Dec 26 '17 09:12 WibbenZ

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?

slawkens avatar Apr 02 '18 20:04 slawkens

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.

ranisalt avatar Apr 02 '18 20:04 ranisalt

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.

slawkens avatar Apr 02 '18 20:04 slawkens

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.

ranisalt avatar Apr 02 '18 21:04 ranisalt

Ok then except that it looks good. Didn't saw that converter, cause it was hidden by default.

Good job!

slawkens avatar Apr 02 '18 21:04 slawkens