Battling a character

I would like the player to travel to a certain room and meet a character. After talking to this character I would like the character and the player to go to a different room, where they will ‘battle’. Ideas on how to code this?
Edit: I’d also like the player to be able to use certain items they’ve collected along the way in that battle.

It really depends how you want the battle to go. Do you want randomized D&D-style die rolls? Pure strategy?

You could look at the ATTACK extension, the Reliques of Tolti-Aph sample code, or the Lanista examples for a few ideas to get you started.

I’m wanting to try something like, the character throws an attack at you and you have to pick which weapon to use. That sort of thing. Are the ATTACK extensions on the Inform 7 website?

The older standalone version of the ATTACK extension is here and the current version (integrated with Kerkerkruip) is here. Note the GPL 3 license. This thread discusses the licensing (see page 4) and says that the original manual contains some exception clauses that relax the usual constraints, but, looking through the files on github, I’m not able to find the manual or that exception text.

The Reliques of Tolti-Aph sample code is here. It was written for an earlier version of Inform 7 and may not have been updated, but it should give you some ideas.

The Lanista 1 and Lanista 2 examples are in the Writing with Inform manual.

So I’ve gotten to this: the character starts in a room, and when asked about a certain topic is moved with the player to the Battlegrounds and the fight begins. My goal is to get the player to attack Leibniz with an object from their inventory every turn, and to have Leibniz attack with damage between 5 and 10. The items the player holds carry different damages too, like knowledge deals 25.
This is what I’ve got so far:

[code]Grassy Field is a room.
Battlegrounds is a room.
A man called Leibniz is in Grassy Field. the health of Leibniz is 50.
After asking Leibniz about “battling/fighting/fight/battle/duelling”:
move player to Battlegrounds;
move Leibniz to Battlegrounds;
Leibniz is now hostile;
say “‘You want a piece of this, eh?’ Leibniz lifted his shirt, but you don’t see anything riveting. 'I’ve been training my body and my mind, punk. Better watch out ‘cause the abs will stop any punch.’[line break]Leibniz throws a high kick at your head, and you are rather impressed! He might need a good smack, but you have to admire the work he’s put into becoming stronger. You too, however have become stronger…[line break](Leibniz attacks you with a high kick, what do you do!?)”.

A thing has a number called damage. The damage of a thing is usually 10.
A sword is here. The description of the sword is “Looks like it could chop up some people.”
The damage of the sword is 25.

To decide whether combat proceeds:
if a hostile alive person is in the location, decide yes;
decide no.
Before reading a commands:
if combat proceeds, change the command prompt to “Combat>”;
otherwise change the command prompt to “>”.
After waiting when combat proceeds: stop the action.

Instead of going:
say “You have to finish this through.”.

To mount an attack from Leibniz:
say “Leibniz attacks!”

Every turn when a hostile person is here:
make the player strike a blow against the hostile;
reduce the health of player by (random number between 5 and 10):
say “Leibniz deals [number] damage!”
if the player is killed, stop;
otherwise make the opponent strike a blow against the player.

Before attacking Leibniz with item:
subtract damage of item from health of Leibniz:
say “You strike Leibniz with [the noun]!”
if Leibniz is killed, stop:
say “You’ve won!”;
end game.

A person has a number called health. The health of a person is usually 50.
Definition: a person is alive rather than dead if its health is greater than 0.
A person can be hostile, passive or friendly. The player is friendly.[/code]

Here’s what I came up with.

It doesn’t force the player to attack each turn in combat, but requires an explicit attack command. If you do want to force attacks, check out the body of the berserking action for an example of how to have the player attack a random adversary.

It also allows the player to start a fight at any time. You could stick in rules to prevent fighting outside the Battleground or against someone who’s not already hostile.

The player can fight barehanded if unarmed, and a vague command (like ATTACK LEIBNIZ) will choose the best weapon that the player has available.

I added a second adversary to help test the code and changed the win condition so that all other people need to be dead to win. If you have friendly people in your game, you could make a special kind for enemies.

A person has a number called health. The health of a person is usually 50.
Definition: A person is living rather than dead if its health is greater than 0.
A person can be hostile. A person is usually not hostile.
A person can be enraged. A person is usually not enraged.
Definition: A person is other if it is not the player.

A weapon is a kind of thing. A weapon has a number called damage. The damage of a weapon is usually 10. Understand "weapon" as a weapon.

Some hands are a kind of weapon. Hands are part of every person. Understand "hands" as hands. The damage of hands is usually 5.

A person has a weapon called the best carried weapon.

To decide what weapon is the best weapon of (P - person):
	let best be random hands that are part of P;
	repeat with W running through weapons carried by P:
		if the damage of W > the damage of best, now best is W;
	decide on best.

When play begins:
	repeat with P running through people:
		now best carried weapon of P is best weapon of P;
		
After an actor taking a weapon:
	now best carried weapon of actor is best weapon of actor;
	continue the action.
	
After an actor dropping a weapon:
	now best carried weapon of actor is best weapon of actor;
	continue the action.

[Tests for other kinds of acquisition besides taking and dropping?]

Grassy Field is a room.
A man called Leibniz is in Grassy Field. The health of Leibniz is 50.
Understand "man" as Leibniz.

After asking Leibniz about "battling/fighting/fight/battle/duelling/combat":
	if combat is happening, instead say "You're in the middle of one right now!";
	say "Leibniz grins widely and leads you to...";
	move player to Battlegrounds;
	move Leibniz to Battlegrounds;
	now Leibniz is hostile;
	say "'You want a piece of this, eh?' Leibniz lifts his shirt, but you don't see anything riveting. 'I've been training my body and my mind, punk. Better watch out [']cause the abs will stop any punch.'[paragraph break]Leibniz throws a high kick at your head, and you are rather impressed! He might need a good smack, but you have to admire the work he's put into becoming stronger. You too, however have become stronger...".

Battlegrounds is a room. 

The sword is a weapon in Battlegrounds. The description of the sword is "Looks like it could chop up some people." The damage of the sword is 25.

The mace is a weapon in Battlegrounds. The description of the mace is "Looks like it could bash some skulls."

A feather is in the Grassy Field.

The Road South is south of Battlegrounds.

Combat is a recurring scene.
Combat begins when a hostile living person is in the location of the player and the player is living.
Combat ends when no hostile living person is in the location of the player or the player is dead.

When combat begins:
	now the command prompt is "Combat>".
	
When combat ends:
	now the command prompt is ">";
	repeat with adversary running through hostile people:
		now adversary is not enraged;
	if all other people are dead:
		end the story finally saying "You have defeated your foes";	

Instead of going during combat:
	say "You have to finish this through."

Asking Leibniz about "Newton" is mentioning Newton.
Telling Leibniz about "Newton" is mentioning Newton.

After mentioning Newton:
	say "Leibniz's lips curl at your mention of his nemesis.";
	if combat is happening and Leibniz is not enraged:
		now Leibniz is enraged;
		say "Leibniz enrages!"

[This could be more elaborate, with different values based on the opponent and their weapon.]
To decide which number is the damage to the player from an attack by (adversary - person):
	let D be a random number between 5 and 10;
	if adversary is enraged, now D is D * 2;
	decide on D.

[This could be more elaborate and have a random factor. Could be unified with the above.]
To decide which number is the damage to (adversary - person) from an attack by the player with (W - weapon):
	decide on the damage of W.

Every turn during combat:
	repeat with adversary running through hostile people in location:
		say "[Adversary] attacks[if adversary is enraged] (enraged)[end if]!";
		let dmg be the damage to the player from an attack by adversary;
		now the health of the player is the health of the  player - dmg;
		say "[Adversary] deals [dmg] damage!";
		say "Your health is now [the health of the player].[paragraph break]";
		if the player is dead:
			end the story saying "You have been slain";
			
[Not specifying that thing must be carried so that player can attack with hands.]
[For now, this action is only for use by player. Could generalize it and use it for attacks by adversaries also.]
Attacking it with is an action applying to one visible thing and one thing.
Understand "attack [someone] with [something preferably held]" as attacking it with. 

[This lets us keep all reporting in the "report attacking it with" rule.]
The attacking it with action has a number called the damage attacked with.

Check attacking it with:
	if the noun is the actor:
		instead say "[The actor] should cut [the actor] some slack.";
	if the second noun is not a weapon:
		instead say "You can't fight with [a second noun]!";
	if the second noun is not carried by the player and the second noun is not your hands	:
		say "(first taking [the second noun])[command clarification break]";
		silently try taking the second noun;
		if the second noun is not carried by the player, stop the action.

Carry out attacking it with:
	now the noun is hostile;
	now the damage attacked with is the damage to the noun from an attack by the player with the second noun;
	now the health of the noun is the health of the noun - the damage attacked with;
	if the noun is dead:
		now the noun is not hostile;
		now the noun is nowhere;
		[Should maybe replace adversary with a corpse here.]
	
Report attacking it with:
	say "You strike at [the noun] with [the second noun]!";
	say "You deal [damage attacked with] damage!";
	say "[Regarding the noun][possessive] health is now [the health of the noun].";
	if the noun is dead:
		say "You have slain [the noun]!"
	
Instead of attacking someone:
	try attacking the noun with the best carried weapon of the actor.	

Does the player mean attacking a person: it is very likely.
Does the player mean attacking someone with a weapon: it is very likely.
Does the player mean attacking someone with hands that are part of other person: It is very unlikely.

Ada Lovelace is a hostile woman in the Battlegrounds.

Berserking is an action applying to nothing.
Understand "berserk" as berserking.

Check berserking:
	if combat is not happening, instead say "You see no adversaries."
	
Carry out berserking:
	let target be a random hostile living person in location;
	try attacking target.

Thanks, it worked :3