Thread: Handling AI
View Single Post
Old 02-08-2011, 07:06 PM   #4
silvarilon
Member
 
Join Date: Dec 2009
Posts: 144
silvarilon is on a distinguished road
Re: Handling AI

Glad the idea is useful

Any reason why you don't include the players?
If you include the players in that list, then you'll be able to drop code onto the players, same as you can on items or mobs.

Which seems silly, but it's super useful.
For example, a swamp-gas-monster might explode and die if it's hit with any flaming weapons.
What if there's some spell or curse that turns a player into a swamp-gas-monster? If you can run the same code on the players, you have that spell just drop the code onto the player, and then you stop thinking about it - knowing that the code from the monsters will also work equally well on the players.

A less extreme example would be a "berserk" creature or player - When someone (mob or player) goes berserk, maybe instead of attacking their intended target, 30% of the time they instead attack a random target in the room.
Having the same code means that your monsters can attack other monsters as easily as attacking players. Similarly, it means a berserk player can attack other players as they would attack monsters.

I don't have any particular comments on this, since it's quite different to what I do.
What I've got is a parenting chain set up. Everything is just "an object" - but a specific sword might have a generic sword as the parent. That generic sword might have a generic weapon as the parent. That generic weapon might have a generic "item you can carry" as parent, and so on.
A specific male PC might have a generic male PC object as parent. And that would have a generic human object as parent.

That way I can put code for how weapons work in the generic weapon, but put specific code about how swords are different in the generic sword. And I can even put custom code about this *specific* sword in the sword that the player is holding. (usually code would do that. Enchant the sword to burst into flames, and the "this is burning" code would be put into the specific sword)

Similarly, code for logging in and out goes in the generic human object (since all players will, ultimately, have the generic human as a parent somewhere along the way)

Makes sense to me

Can you do introspection?
A dirty example... you've got different verbs the players can do.
- look at dragon
- stroke dragon
- kick dragon
- attack dragon

What if you use introspection to check if the respond_to_VERB() function exists in the dragon object?
So you look at it. respond_to_look() exists (I assume you'll make all objects respond to look, so people can see what they are...) - it runs the code, and gives the player a description.
You stroke it. It has no respond_to_stroke() function, so it doesn't run any code (and the player just sees a general "you stroke the dragon" message. Whatever they'd normally see when stroking an item.)
You attack it. It has respond_to_attack() which makes it go into combat.
You kick it. It has respond_to_kick() which makes it glance at the player with annoyance. It also counts how many times you've kicked it. Kicking it three times makes it attack. (or maybe every kick has a random chance of it attacking)

Fairly simple setup, where you only add in responses to the verbs that you want to code in. But already you're opening up new options. Instead of just fighting monsters... you've now got a new group quest possibility.
The town boys are boasting about how brave they are. They challenge you to go unarmed, and kick the dragon. Whoever is brave (or stupid) enough to kick the dragon the most times wins the title "dragon kicker" - at least until someone else kicks the dragons more times than them at takes the title.

You can see how adding a quest like that might be trivially easy. In the kick dragon, save a property about who most recently kicked it, and how many times. Also save the current high score and title holder.
If they kick it enough that it attacks them, reset their score to 0 when they die.
If they kick it enough that they beat the high score, update the current title holder to no longer have the title, and set them with the title and set them as the current holder.

Easy code that can be written in five minutes, once the underlying infrastructure is in place.


Often, yes.
But timers can also be used when interacting with players.
For example, imagine if you are attacking a goblin castle.
You burst into the main hallway. Two trolls attack you. A goblin runs to the crank, and starts lowering the portcullis.
We might use a timer here. Every 20 seconds it gives you a message about the goblin still lowering the portcullis. After long enough, the portcullis slams shut, and you can't get any deeper into the castle. If you kill the goblin before the portcullis is down, then it'll stop lowering, and you can continue your raid.

Of course, the trolls won't let you attack the goblin while they're still alive. (special code in the trolls or in the goblin) So you will need to use some sort of tactics. Bring three fighters (the trolls can only stop one fighter at a time) so the third can kill the goblin. Or use once-off magic items that help you kill the trolls faster, or something.

Timers might also be used for "loops" - for example, a squirrel mob's AI function might tell it "hide a nut" (while it's awake and the players are in the room. No point having squirrels hiding nuts while no players are around to witness it. A tree falling in the forest, and all that...)
So the squirrel gives a message about finding a nut. Then the timer makes it delay for a few seconds, before it looks around, another delay, it runs up a tree, another delay, it hides the nut, another delay, it scurries down from the tree, a final delay, it scurries out of the room.
All of that happens while the players are there (unless the players walk away) - the timer is just used to make the events happen in the right sequence. Makes it more interesting than the squirrel just having random messages like "A squirrel hides a nut"

Of course, I don't expect you to structure your game in exactly the same way as mine. But hopefully this is giving you ideas. Use whatever works best for you
silvarilon is offline   Reply With Quote