1/*	$NetBSD: command7.c,v 1.5 2021/05/02 12:50:43 rillig Exp $	*/
2
3/*
4 * Copyright (c) 1983, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)com7.c	8.2 (Berkeley) 4/28/95";
36#else
37__RCSID("$NetBSD: command7.c,v 1.5 2021/05/02 12:50:43 rillig Exp $");
38#endif
39#endif				/* not lint */
40
41#include "extern.h"
42
43int
44fight(int enemy, int strength)
45{
46	int     lifeline = 0;
47	int     hurt;
48	char    auxbuf[LINELENGTH];
49	char   *next;
50	int     i;
51	int     exhaustion;
52
53	exhaustion = 0;
54fighton:
55	ourtime++;
56	snooze -= 5;
57	if (snooze > ourtime)
58		exhaustion = CYCLE / (snooze - ourtime);
59	else {
60		puts("You collapse exhausted, and he pulverizes your skull.");
61		die();
62	}
63	if (snooze - ourtime < 20)
64		puts("You look tired! I hope you're able to fight.");
65	next = getcom(auxbuf, LINELENGTH, "<fight!>-: ", 0);
66	for (i = 0; next && i < 10; i++)
67		next = getword(next, words[i], -1);
68	parse();
69	switch (wordvalue[wordnumber]) {
70
71	case KILL:
72	case SMITE:
73		if (testbit(inven, TWO_HANDED))
74			hurt = rnd(70) - 2 * card(injuries, NUMOFINJURIES) -
75			    ucard(wear) - exhaustion;
76		else if (testbit(inven, SWORD) || testbit(inven, BROAD))
77			hurt = rnd(50) % (WEIGHT - carrying) -
78			    card(injuries, NUMOFINJURIES) - encumber -
79			    exhaustion;
80		else if (testbit(inven, KNIFE) || testbit(inven, MALLET) ||
81		    testbit(inven, CHAIN) || testbit(inven, MACE) ||
82		    testbit(inven, HALBERD))
83			hurt = rnd(15) - card(injuries, NUMOFINJURIES) -
84			    exhaustion;
85		else
86			hurt = rnd(7) - encumber;
87		if (hurt < 5)
88			switch (rnd(3)) {
89
90			case 0:
91				puts("You swung wide and missed.");
92				break;
93			case 1:
94				puts("He checked your blow. CLASH! CLANG!");
95				break;
96			case 2:
97				printf("His filthy tunic hangs by one less ");
98				puts("thread.");
99				break;
100			}
101		else if (hurt < 10) {
102			switch (rnd(3)) {
103			case 0:
104				puts("He's bleeding.");
105				break;
106			case 1:
107				puts("A trickle of blood runs down his face.");
108				break;
109			case 2:
110				printf("A huge purple bruise is forming on ");
111				puts("the side of his face.");
112				break;
113			}
114			lifeline++;
115		} else if (hurt < 20) {
116			switch (rnd(3)) {
117			case 0:
118				puts("He staggers back quavering.");
119				break;
120			case 1:
121				printf("He jumps back with his hand over ");
122				puts("the wound.");
123				break;
124			case 2:
125				printf("His shirt falls open with a swath ");
126				puts("across the chest.");
127				break;
128			}
129			lifeline += 5;
130		} else if (hurt < 30) {
131			switch (rnd(3)) {
132			case 0:
133				printf("A bloody gash opens up on his %s ",
134				    (rnd(2) ? "left" : "right"));
135				printf("side.\n");
136				break;
137			case 1:
138				printf("The steel bites home and scrapes ");
139				puts("along his ribs.");
140				break;
141			case 2:
142				printf("You pierce him, and his breath ");
143				puts("hisses through clenched teeth.");
144				break;
145			}
146			lifeline += 10;
147		} else if (hurt < 40) {
148			switch (rnd(3)) {
149			case 0:
150				puts("You smite him to the ground.");
151				if (strength - lifeline > 20) {
152					printf("But in a flurry of steel he ");
153					puts("regains his feet!");
154				}
155				break;
156			case 1:
157				printf("The force of your blow sends him to ");
158				puts("his knees.");
159				puts("His arm swings lifeless at his side.");
160				break;
161			case 2:
162				printf("Clutching his blood drenched shirt, ");
163				puts("he collapses stunned.");
164				break;
165			}
166			lifeline += 20;
167		} else {
168			switch (rnd(3)) {
169			case 0:
170				printf("His ribs crack under your powerful ");
171				puts("swing, flooding his lungs with blood.");
172				break;
173			case 1:
174				printf("You shatter his upheld arm in a ");
175				printf("spray of blood.  The blade ");
176				puts("continues deep");
177				printf("into his back, severing the ");
178				puts("spinal cord.");
179				lifeline += 25;
180				break;
181			case 2:
182				printf("With a mighty lunge the steel ");
183				printf("slides in, and gasping, he falls ");
184				puts("to the ground.");
185				lifeline += 25;
186				break;
187			}
188			lifeline += 30;
189		}
190		break;
191
192	case BACK:
193		if (enemy == DARK && lifeline > strength * 0.33) {
194			printf("He throws you back against the rock and ");
195			puts("pummels your face.");
196			if (testbit(inven, AMULET) || testbit(wear, AMULET)) {
197				printf("Lifting the amulet from you, ");
198				if (testbit(inven, MEDALION) ||
199				    testbit(wear, MEDALION)) {
200					printf("his power grows and the ");
201					printf("walls of\nthe earth ");
202					printf("tremble.\n");
203					printf("When he touches the ");
204					printf("medallion, your chest ");
205					printf("explodes and the foundations ");
206					printf("of the\nearth collapse.\n");
207					printf("The planet is consumed by ");
208					puts("darkness.");
209					die();
210				}
211				if (testbit(inven, AMULET)) {
212					clearbit(inven, AMULET);
213					carrying -= objwt[AMULET];
214					encumber -= objcumber[AMULET];
215				} else
216					clearbit(wear, AMULET);
217				puts("he flees down the dark caverns.");
218				clearbit(location[position].objects, DARK);
219				injuries[SKULL] = 1;
220				followfight = ourtime;
221				return (0);
222			} else {
223				puts("I'm afraid you have been killed.");
224				die();
225			}
226		} else {
227			printf("You escape stunned and disoriented from ");
228			puts("the fight.");
229			printf("A victorious bellow echoes from the ");
230			puts("battlescene.");
231			if (back && position != back)
232				moveplayer(back, BACK);
233			else if (ahead && position != ahead)
234				moveplayer(ahead, AHEAD);
235			else if (left && position != left)
236				moveplayer(left, LEFT);
237			else if (right && position != right)
238				moveplayer(right, RIGHT);
239			else
240				moveplayer(location[position].down, AHEAD);
241			return (0);
242		}
243
244	case SHOOT:
245		if (testbit(inven, LASER)) {
246			if (strength - lifeline <= 50) {
247				printf("The %s took a direct hit!\n",
248				    objsht[enemy]);
249				lifeline += 50;
250			} else {
251				printf("With his bare hand he deflects the ");
252				printf("laser blast and whips the pistol ");
253				puts("from you!");
254				clearbit(inven, LASER);
255				setbit(location[position].objects, LASER);
256				carrying -= objwt[LASER];
257				encumber -= objcumber[LASER];
258			}
259		} else
260			puts("Unfortunately, you don't have a blaster handy.");
261		break;
262
263	case DROP:
264	case DRAW:
265		cypher();
266		ourtime--;
267		break;
268
269	default:
270		puts("You don't have a chance; he is too quick.");
271		break;
272
273	}
274	if (lifeline >= strength) {
275		printf("You have killed the %s.\n", objsht[enemy]);
276		if (enemy == ELF || enemy == DARK) {
277			printf("A watery black smoke consumes his body and ");
278			puts("then vanishes with a peal of thunder!");
279		}
280		clearbit(location[position].objects, enemy);
281		power += 2;
282		notes[JINXED]++;
283		return (0);
284	}
285	puts("He attacks...");
286	/* Some embellishments. */
287	hurt = rnd(NUMOFINJURIES) - (testbit(inven, SHIELD) != 0) -
288	    (testbit(wear, MAIL) != 0) - (testbit(wear, HELM) != 0);
289	hurt += (testbit(wear, AMULET) != 0) +
290	    (testbit(wear, MEDALION) != 0) + (testbit(wear, TALISMAN) != 0);
291	hurt = hurt < 0 ? 0 : hurt;
292	hurt = hurt >= NUMOFINJURIES ? NUMOFINJURIES - 1 : hurt;
293	if (!injuries[hurt]) {
294		injuries[hurt] = 1;
295		printf("I'm afraid you have suffered %s.\n", ouch[hurt]);
296	} else
297		puts("You emerge unscathed.");
298	if (injuries[SKULL] && injuries[INCISE] && injuries[NECK]) {
299		puts("I'm afraid you have suffered fatal injuries.");
300		die();
301	}
302	goto fighton;
303}
304