/* ************************************************************************
*  file: act.offensive.c , Implementation of commands.    Part of DIKUMUD *
*  Usage : Offensive commands.                                            *
*  Copyright (C) 1990, 1991 - see 'license.doc' for complete information. *
************************************************************************* */

#include <stdio.h>
#include <strings.h>

#include "structs.h"
#include "utils.h"
#include "comm.h"
#include "interpreter.h"
#include "handler.h"
#include "db.h"
#include "spells.h"
#include "limits.h"

/* extern variables */

extern struct room_data *world;
extern struct descriptor_data *descriptor_list;
extern struct zone_data *zone_table;
extern char *command[];


void raw_kill(struct char_data *ch);
void check_killer(struct char_data *ch, struct char_data *victim);
void lose_exp_by_flight(struct char_data *ch,struct char_data *was_fighting);
bool is_in_safe(struct char_data *ch, struct char_data *victim);
bool is_first_level(struct char_data *ch, struct char_data *victim);
bool nokill(struct char_data *ch, struct char_data *victim);
bool notwithinsixlevels(struct char_data *ch, struct char_data *victim);
bool isdweeb(struct char_data *ch);

/* Nokill--user can toggle this option so they don't inadvertently
get the flag for killing a player. Swiftest.
*/

bool nokill(struct char_data *ch, struct char_data *victim)
{
	if((ch->specials.nokill==TRUE)&&!IS_NPC(ch)&&!IS_NPC(victim)&&
	   !(IS_SET(victim->specials.act,PLR_ISKILLER|PLR_ISTHIEF)) &&
	   (ch!=victim)&&!(IS_SET(world[ch->in_room].room_flags,ARENA))){
		if(victim->specials.fighting==ch){ /* NOKILL ignored if attacked */
			return(FALSE);
		}
		send_to_char("*** Turn NOKILL off first ***",ch);
		return(TRUE);
	}
	return(FALSE);
}
		
/* Do not allow a first level char to attack another player until 2nd lv */
/* and do not allow someone to kill a first level player */
bool is_first_level(struct char_data *ch, struct char_data *victim)
{
	if (IS_NPC(victim)||IS_NPC(ch)){
		return FALSE;
	}
	/* No rule exists for NPCs.. only PCs should get to next statement */
	if ((!IS_NPC(victim))&&(GET_EXP(ch)<1500)){
		send_to_char("You may not attack a player until you have at least 1,500 exp!\n\r",ch);
		return TRUE;
	} else if ( (GET_LEVEL(ch)>1)&&(GET_LEVEL(victim)<2) ){
		send_to_char("You may not attack a first level player.\n\r",ch);
		return TRUE;
	} else {
		return FALSE;
	}
}

bool is_in_safe(struct char_data *ch, struct char_data *victim)
	/* checks to see if PC is in safe room*/
{
            /* People had been able to take NPC's into safe areas */
	    /* and have them kill PC's...not too good.  --Sman    */
	    /* Realized that one-sided is no good, so I have      */
            /* Forbidden all fighting in safe areas.    --Sman    */
/*	if( IS_NPC(ch)|| IS_NPC(victim)){
		return FALSE;
	} */
	if(IS_SET(world[ch->in_room].room_flags,SAFE)){
		send_to_char("No fighting permitted in this room.\n\r",ch);
		return TRUE;
	} else if(IS_SET(zone_table[world[ch->in_room].zone].flags,
			ZONE_SAFE)) {
		send_to_char("You decide not to for some reason.\n\r",ch);
		return TRUE;
	} else {
		return FALSE;
	}
}

bool isdweeb(struct char_data *ch)
{
	if (!IS_NPC(ch) && IS_SET(ch->specials.act, PLR_ISDWEEB)){
		send_to_char("You're a dweeb!\n\r", ch);
		return TRUE;
	} else {
		return FALSE;
	}
}

bool notwithinsixlevels(struct char_data *ch,struct char_data *victim)
{
	if(IS_NPC(ch)||IS_NPC(victim)||IS_SET(victim->specials.act,PLR_ISKILLER)
			|| IS_SET(victim->specials.act,PLR_ISTHIEF) ||
			IS_SET(world[ch->in_room].room_flags,ARENA))
		return (FALSE);

	if((GET_LEVEL(ch) < GET_LEVEL(victim) - 6) ||
		   (GET_LEVEL(ch) > GET_LEVEL(victim) + 6) ||
			(IS_MULTICLASSED(ch)!=IS_MULTICLASSED(victim))) {
		act("$N isn't within 6 levels of you.",
					FALSE,ch,0,victim,TO_CHAR);
					return(TRUE);
	}
	return(FALSE);
}

void do_hit(struct char_data *ch, char *argument, int cmd)
{
	char arg[MAX_STRING_LENGTH];
	char buffer[MAX_STRING_LENGTH];
	struct char_data *victim;

	one_argument(argument, arg);

	if (*arg) {
		victim = get_char_room_vis(ch, arg);
		if (victim) {
			if (!IS_NPC(victim) && !IS_NPC(ch)){
				send_to_char("You must MURDER a player.\n\r",ch);
				return;
			}
			if (victim == ch) {
				send_to_char("You hit yourself..OUCH!.\n\r", ch);
				act("$n hits $mself, and says OUCH!", FALSE, ch, 0, victim, TO_ROOM);
			} else {
				if (is_in_safe(ch,victim)==TRUE){
					return;
				}

				if (is_first_level(ch,victim)==TRUE){
					return;
				}
				if (isdweeb(ch)==TRUE){
					return;
				}
				if (IS_AFFECTED(ch, AFF_CHARM) && (ch->master == victim)) {
					act("$N is just such a good friend, you simply can't hit $M.",
					  FALSE, ch,0,victim,TO_CHAR);
					return;
				}
				if ((GET_POS(ch)>=POSITION_STANDING) &&
				    (victim != ch->specials.fighting)) {
					hit(ch, victim, TYPE_UNDEFINED);
					WAIT_STATE(ch, PULSE_VIOLENCE+2); /* HVORFOR DET?? */
				} else {
					send_to_char("You do the best you can!\n\r",ch);
				}
			}
		} else {
			send_to_char("They aren't here.\n\r", ch);
		}
	} else {
		send_to_char("Hit whom?\n\r", ch);
	}
}


void do_murder(struct char_data *ch, char *argument, int cmd)
{
	char arg[MAX_STRING_LENGTH];
	char buffer[MAX_STRING_LENGTH];
	struct char_data *victim;

	one_argument(argument, arg);

	if (*arg) {
		victim = get_char_room_vis(ch, arg);
		if (victim) {
			if (victim == ch) {
				send_to_char("You hit yourself..OUCH!.\n\r", ch);
				act("$n hits $mself, and says OUCH!", FALSE, ch, 0, victim, TO_ROOM);
			} else {
				if(is_in_safe(ch,victim)==TRUE){ return; }
				if(is_first_level(ch,victim)==TRUE){ return; }
				if(nokill(ch,victim)==TRUE){ return; }
				if(isdweeb(ch)==TRUE) { return; }

				if (IS_AFFECTED(ch, AFF_CHARM) && (ch->master == victim)) {
					act("$N is just such a good friend, you simply can't hit $M.",
					  FALSE, ch,0,victim,TO_CHAR);
					return;
				}

				if(notwithinsixlevels(ch,victim)) { return; }

				if ((GET_POS(ch)==POSITION_STANDING) &&
				    (victim != ch->specials.fighting)) {
					hit(ch, victim, TYPE_UNDEFINED);
					WAIT_STATE(ch, PULSE_VIOLENCE+2); /* HVORFOR DET?? */
				} else {
					send_to_char("You do the best you can!\n\r",ch);
				}
			}
		} else {
			send_to_char("They aren't here.\n\r", ch);
		}
	} else {
		send_to_char("Hit whom?\n\r", ch);
	}
}

void do_kill(struct char_data *ch, char *argument, int cmd)
{
	static char arg[MAX_STRING_LENGTH];
	char buf[70];
	struct char_data *victim;

	if (GET_LEVEL(ch) < LV_IMPL || IS_NPC(ch)) {
		do_hit(ch, argument, 0);
    		return;
	}

	one_argument(argument, arg);

	if (!*arg)
	{
		send_to_char("Slay whom?\n\r", ch);
	}
	else
	{
		if (!(victim = get_char_room_vis(ch, arg)))
   		send_to_char("He/she/it isn't here.\n\r", ch);
		else
   		if (ch == victim)
      	send_to_char("Your mother would be so sad.. :(\n\r", ch);
   		else {
				act("You chop $M to pieces! Ah! The blood!", FALSE, ch, 0, victim, TO_CHAR);
				act("$N chops you to pieces!", FALSE, victim, 0, ch, TO_CHAR);
				act("$n brutally slays $N", FALSE, ch, 0, victim, TO_NOTVICT);
				raw_kill(victim);
			}
	}
}



void do_backstab(struct char_data *ch, char *argument, int cmd)
{
	struct char_data *victim;
	struct affected_type af;
	char name[256];
	byte percent;

	one_argument(argument, name);


	if (!(victim = get_char_room_vis(ch, name))) {
		send_to_char("Backstab who?\n\r", ch);
		return;
	}
	
	if (victim == ch) {
		send_to_char("How can you sneak up on yourself?\n\r", ch);
		return;
	}

	if(GET_CLASS(ch)!=CLASS_THIEF && !IS_SET(ch->specials.act,PLR_ISMULTITH)) {
		send_to_char("You're no thief!\n\r",ch);
		return;
	}

	if(is_in_safe(ch,victim)==TRUE){ return; }
	if(is_first_level(ch,victim)==TRUE){ return; }
	if(nokill(ch,victim)==TRUE){ return; }
	if(notwithinsixlevels(ch,victim)==TRUE){ return; }
	if(isdweeb(ch)==TRUE) { return; }

	if (!ch->equipment[WIELD]) {
		send_to_char("You need to wield a weapon, to make it a succes.\n\r",ch);
		return;
	}

	if (ch->equipment[WIELD]->obj_flags.value[3] != 11) {
		send_to_char("Only piercing weapons can be used for backstabbing.\n\r",ch);
		return;
	}

	if (victim->specials.fighting) {
		send_to_char("You can't backstab a fighting person, too alert!\n\r", ch);
		return;
	}

	percent=number(1,101); /* 101% is a complete failure */

	if(IS_AFFECTED(victim,AFF_AWARE) && GET_POS(victim)>POSITION_RESTING) {
		act("$N seems way too alert to catch off guard.",FALSE,ch,0,victim,TO_CHAR);
		return;
	}

	if (AWAKE(victim) && (percent > ch->skills[SKILL_BACKSTAB].learned))
		damage(ch, victim, 0, SKILL_BACKSTAB);
	else {
		hit(ch,victim,SKILL_BACKSTAB);
		/* After getting hit once, this guy will be wary next time */
		af.type=SPELL_AWARENESS;
		af.duration=1;
		af.modifier=0;
		af.location=APPLY_NONE;
		af.bitvector=AFF_AWARE;
		affect_join(victim,&af,FALSE,FALSE);
	}
}



void do_order(struct char_data *ch, char *argument, int cmd)
{
	char name[MAX_INPUT_LENGTH], message[MAX_INPUT_LENGTH];
	char buf[MAX_STRING_LENGTH];
	bool found = FALSE;
	int org_room;
	struct char_data *victim;
	struct follow_type *k;

	half_chop(argument, name, message);

	if(isdweeb(ch)) { return; }
	if(GET_EXP(ch) < 1500) {
		send_to_char("Due to abuse, you must have at least 1500 experience to use this command.\n\r",ch);
		return;
	}
	if (!*name || !*message)
		send_to_char("Order who to do what?\n\r", ch);
	else if (!(victim = get_char_room_vis(ch, name)) &&
	         str_cmp("follower", name) && str_cmp("followers", name))
			send_to_char("That person isn't here.\n\r", ch);
	else if (ch == victim)
		send_to_char("You obviously suffer from schitzophrenia.\n\r", ch);

	else {
		if (IS_AFFECTED(ch, AFF_CHARM)) {
			send_to_char("Your superior would not aprove of you giving orders.\n\r",ch);
			return;
		}

		if (victim) {
			sprintf(buf, "$N orders you to '%s'", message);
			act(buf, FALSE, victim, 0, ch, TO_CHAR);
			act("$n gives $N an order.", FALSE, ch, 0, victim, TO_ROOM);

			if ( (victim->master!=ch) || !IS_AFFECTED(victim, AFF_CHARM) )
				act("$n has an indifferent look.", FALSE, victim, 0, 0, TO_ROOM);
			else {
				send_to_char("Ok.\n\r", ch);
				command_interpreter(victim, message);
			}
		} else {  /* This is order "followers" */
			sprintf(buf, "$n issues the order '%s'.", message);
			act(buf, FALSE, ch, 0, victim, TO_ROOM);

			org_room = ch->in_room;

			for (k = ch->followers; k; k = k->next) {
				if (org_room == k->follower->in_room)
					if (IS_AFFECTED(k->follower, AFF_CHARM)) {
						found = TRUE;
						command_interpreter(k->follower, message);
					}
			}
			if (found)
				send_to_char("Ok.\n\r", ch);
			else
				send_to_char("Nobody here is a loyal subject of yours!\n\r", ch);
		}
	}
}



void do_flee(struct char_data *ch, char *argument, int cmd)
{
	int i, attempt, start_room, orig_room;
	char buf[MAX_INPUT_LENGTH];
	struct char_data *was_fighting;

	void gain_exp(struct char_data *ch, int gain);
	int special(struct char_data *ch, int cmd, char *arg);

	if (!(ch->specials.fighting)) {
		for(i=0; i<6; i++) {
			attempt = number(0, 5);  /* Select a random direction */
			if (CAN_GO(ch, attempt) &&
			!IS_SET(world[EXIT(ch, attempt)->to_room].room_flags, DEATH)
			&& (!IS_NPC(ch) || !IS_SET(world[ch->in_room].room_flags,SAFE))) {
				act("$n panics, and attempts to flee.", TRUE, ch, 0, 0, TO_ROOM);
				/*if ((die = do_simple_move(ch, attempt, FALSE))== 1) {*/
				send_to_char("You attempt to flee...\n\r",ch);
				start_room=ch->in_room;
				strcpy(buf,command[attempt]);
				command_interpreter(ch,buf);
				if(start_room == ch->in_room) {
					act("$n tries to flee, but can't make it out of here!", TRUE, ch, 0, 0, TO_ROOM);
					send_to_char("PANIC! You couldn't escape!\n\r", ch);
					return;
				}
			}
		} /* for */
		return;
	}

	orig_room=ch->in_room;
	for(i=0; i<6; i++) {
		attempt = number(0, 5);  /* Select a random direction */
		if (CAN_GO(ch, attempt) &&
		   !IS_SET(world[EXIT(ch, attempt)->to_room].room_flags, DEATH)) {
			act("$n panics, and attempts to flee.", TRUE, ch, 0, 0, TO_ROOM);
			send_to_char("You attempt to flee...\n\r",ch);
			start_room=ch->in_room;
			/* This is dubious, but works */
			if(ch->specials.fighting) {
				was_fighting=ch->specials.fighting;
				stop_fighting(ch);
			}
			strcpy(buf,command[attempt]);
			command_interpreter(ch,buf);
			if(start_room != ch->in_room) {
				/* The escape has succeded */
				lose_exp_by_flight(ch,was_fighting);


				/* Insert later when using huntig system        */
				/* ch->specials.fighting->specials.hunting = ch */

				return;
			} else {
				act("$n tries to flee, but can't make it out of here!", TRUE, ch, 0, 0, TO_ROOM);
				send_to_char("PANIC! You couldn't escape!\n\r", ch);
				if(orig_room==ch->in_room)
					set_fighting(ch,was_fighting);
				return;
			}
		}
	} /* for */

}



void do_bash(struct char_data *ch, char *argument, int cmd)
{
	struct char_data *victim;
	char name[256], buf[256];
	byte percent;
	int learned;

	one_argument(argument, name);

	if ((!IS_NPC(ch) && GET_CLASS(ch) != CLASS_WARRIOR &&
	    !IS_SET(ch->specials.act,PLR_ISMULTIWA)) ||
	    (IS_NPC(ch) && !IS_SET(ch->specials.act, ACT_HAS_WA))) {
		send_to_char("You better leave all the martial arts to fighters.\n\r", ch);
		return;
	}

	if (!(victim = get_char_room_vis(ch, name))) {
		if (ch->specials.fighting &&
				ch->specials.fighting->in_room==ch->in_room) {
			victim = ch->specials.fighting;
		} else {
			send_to_char("Bash whom?\n\r", ch);
			return;
		}
	}
	
	if (victim == ch) {
		send_to_char("Aren't we funny today...\n\r", ch);
		return;
	}

	if(world[ch->in_room].sector_type >= SECT_WATER_SWIM) {
		send_to_char("You have no footing here!\n\r",ch);
		return;
	}

	if(is_in_safe(ch,victim)==TRUE) { return; }
	if(is_first_level(ch,victim)==TRUE){ return; }
	if(nokill(ch,victim)==TRUE){ return; }
	if(notwithinsixlevels(ch,victim)==TRUE){ return; }
	if(isdweeb(ch)==TRUE){ return; }

	if (!IS_NPC(ch) && !ch->equipment[WIELD]) {
		send_to_char("You need to wield a weapon, to make it a success.\n\r",ch);
		return;
	}

	percent=number(1,101); /* 101% is a complete failure */

	if(!IS_NPC(ch)){
		learned = ch->skills[SKILL_BASH].learned;
	} else { /* is NPC */
		learned = (GET_LEVEL(ch)>=10? 99: GET_LEVEL(ch)*10);
	}
	if (percent > learned) {
		damage(ch, victim, 0, SKILL_BASH);
		GET_POS(ch) = POSITION_SITTING;
	} else {
		damage(ch, victim, 1, SKILL_BASH);
		GET_POS(victim) = POSITION_SITTING;
		WAIT_STATE(victim, PULSE_VIOLENCE*2);
	}
	WAIT_STATE(ch, PULSE_VIOLENCE*2);
}



void do_rescue(struct char_data *ch, char *argument, int cmd)
{
	struct char_data *victim, *tmp_ch;
	int percent;
	char victim_name[240];
	char buf[240];

	if(isdweeb(ch)==TRUE) { return; }
	one_argument(argument, victim_name);

	if (!(victim = get_char_room_vis(ch, victim_name))) {
		send_to_char("Who do you want to rescue?\n\r", ch);
		return;
	}

	if (victim == ch) {
		send_to_char("What about fleeing instead?\n\r", ch);
		return;
	}

	if (ch->specials.fighting == victim) {
		send_to_char("How can you rescue someone you are trying to kill?\n\r",ch);
		return;
	}

	for (tmp_ch=world[ch->in_room].people; tmp_ch &&
	    (tmp_ch->specials.fighting != victim); tmp_ch=tmp_ch->next_in_room)  ;

	if (!tmp_ch) {
		act("But nobody is fighting $M?", FALSE, ch, 0, victim, TO_CHAR);
		return;
	}

	if(nokill(ch,tmp_ch))
		return;

	if ((GET_CLASS(ch) != CLASS_WARRIOR) &&
	    (!IS_SET(ch->specials.act,PLR_ISMULTIWA)))
		send_to_char("But only true warriors can do this!", ch);
	else {
		percent=number(1,101); /* 101% is a complete failure */
		if (percent > ch->skills[SKILL_RESCUE].learned) {
			send_to_char("You fail the rescue.\n\r", ch);
			return;
		}

		send_to_char("Banzai! To the rescue...\n\r", ch);
		act("You are rescued by $N, you are confused!", FALSE, victim, 0, ch, TO_CHAR);
		act("$n heroically rescues $N.", FALSE, ch, 0, victim, TO_NOTVICT);

		if (victim->specials.fighting == tmp_ch)
			stop_fighting(victim);
		if (tmp_ch->specials.fighting)
			stop_fighting(tmp_ch);
		if (ch->specials.fighting)
			stop_fighting(ch);

		check_killer(ch, tmp_ch); /* so rescuing an NPC who is fighting a PC does not result in the other guy getting killer flag */
		set_fighting(ch, tmp_ch);
		set_fighting(tmp_ch, ch);

		WAIT_STATE(victim, 2*PULSE_VIOLENCE);
	}

}



void do_kick(struct char_data *ch, char *argument, int cmd)
{
	struct char_data *victim;
	char name[256], buf[256];
	byte percent;
	int learned;

	if ((!IS_NPC(ch) && GET_CLASS(ch) != CLASS_WARRIOR &&
	    !IS_SET(ch->specials.act,PLR_ISMULTIWA)) ||
	    (IS_NPC(ch) && !IS_SET(ch->specials.act, ACT_HAS_WA))) {
		send_to_char("You better leave all the martial arts to fighters.\n\r", ch);
		return;
	}

	one_argument(argument, name);

	if (!(victim = get_char_room_vis(ch, name))) {
		if (ch->specials.fighting &&
				ch->specials.fighting->in_room==ch->in_room) {
			victim = ch->specials.fighting;
		} else {
			send_to_char("Kick whom?\n\r", ch);
			return;
		}
	}

	if (victim == ch) {
		send_to_char("Aren't we funny today...\n\r", ch);
		return;
	}

	if(is_in_safe(ch,victim)==TRUE){ return; }
	if(is_first_level(ch,victim)==TRUE){ return; }
	if(nokill(ch,victim)==TRUE){ return; }
	if(notwithinsixlevels(ch,victim)==TRUE){ return; }
	if(isdweeb(ch)==TRUE){ return; }

	percent=((10-(GET_AC(victim)/10))<<1) + number(1,101); /* 101% is a complete failure */

	if(!IS_NPC(ch)){
		learned = ch->skills[SKILL_KICK].learned;
	} else { /* is npc */
		learned = (GET_LEVEL(ch)>=10? 99: 10*GET_LEVEL(ch));
	}
	if (percent > learned){
		damage(ch, victim, 0, SKILL_KICK);
	} else {
		damage(ch, victim, GET_LEVEL(ch)>>1, SKILL_KICK);
	}
	WAIT_STATE(ch, PULSE_VIOLENCE*3);
}