clan/
class/
data/
doc/ideas/
doc/mobprogs/
gods/
log/
player/
time/
/*
 *  The unique portions of SunderMud code as well as the integration efforts
 *  for code from other sources is based on the efforts of:
 *
 *  Lotherius (elfren@aros.net)
 *
 *  This code can only be used under the terms of the DikuMud, Merc,
 *  and ROM licenses. The same requirements apply to the changes that
 *  have been made.
 *
 * All other copyrights remain in place and in force.
*/


/* Zeran - obj_cond.c written to support object conditions and all related
functions */


#include "everything.h"

void show_obj_cond (CHAR_DATA *ch, OBJ_DATA *obj);
void check_damage_obj (CHAR_DATA *ch, OBJ_DATA *obj, int chance);
void damage_obj (CHAR_DATA *ch,	OBJ_DATA *obj, int damage);
void set_obj_cond (OBJ_DATA *obj, int condition);

void show_obj_cond (CHAR_DATA *ch, OBJ_DATA *obj)
	{
	char buf[MAX_STRING_LENGTH];
	int condition=0;
	
	if (IS_SET(obj->extra_flags, ITEM_NO_COND)) /*no show condition */
		return;
	
	if (obj->condition == 100)
		condition = 0;
	else if (obj->condition > 90)
		condition = 1 ;
	else if (obj->condition > 75)
		condition = 2;
	else if (obj->condition > 50)
		condition = 3;
	else if (obj->condition > 25)
		condition = 4;
	else if (obj->condition > 10)
		condition = 5;
	else if (obj->condition >  0)
		condition = 6;
	else if (obj->condition == 0)
		condition = 7;
	
	sprintf (buf, "Condition: %s\n\r", cond_table[condition]);
	send_to_char (buf, ch);
	return;
	}	

void check_damage_obj (CHAR_DATA *ch, OBJ_DATA *obj, int chance)
	{ 
	/* Assumption - NULL obj means check all equipment */
	bool checkall=FALSE;
	bool done=FALSE;
	int damage_pos;
	OBJ_DATA *dobj=NULL;
	int stop=0;
	
	if (obj == NULL)
		checkall=TRUE;
	
	if (checkall) /*damage random equipped item*/
		{
		if (number_percent () <= chance) /*something dinged up*/
			{
			while ((!done) && (stop <= 20)) /* stop prevents infinite loop */
				{
				damage_pos = number_range (1, MAX_WEAR);
				if ((dobj = get_eq_char (ch, damage_pos)) != NULL) 
					done=TRUE;
				stop++;
				}
			if (done)
				damage_obj (ch, dobj, 1);
			}
		else return;
		}
	else if (number_percent () <= chance) /*damage passed in object*/
		{
		damage_obj(ch, obj, 1);
		return;
		}
	return;
	}		

void damage_obj (CHAR_DATA *ch, OBJ_DATA *obj, int damage)
	{
	int counter;
	
	if (obj == NULL)
		{
		bug ("NULL obj passed to damage_obj",0);
		return;
		}

	obj->condition -= damage;
	obj->condition = URANGE (0, obj->condition, 100);
	 
	/*Check for item falling apart*/
	if (obj->condition == 0)
		{
		char mesbuf[256];
		sprintf (mesbuf, "{c%s{c has become too badly damaged to use!{x\n\r", 
		( (obj->short_descr && obj->short_descr[0] != '\0') ? obj->short_descr :
			"Something") );
		send_to_char (mesbuf, ch);
		unequip_char (ch, obj);
		return;
		}
	/* Now make adjustments to armor values and modify char AC */	
		if (obj->item_type == ITEM_ARMOR)
			{
			/* restore char AC as if not wearing object, then damage
				the object, then recalculate character AC */
			for (counter=0 ; counter < 4 ; counter ++)		
				{
				ch->armor[counter] += apply_ac (obj, obj->wear_loc, counter);
				obj->value[counter] = obj->valueorig[counter]*(obj->condition)/100;
				if ((obj->value[counter] == 0) && (obj->valueorig[counter] != 0))  
					obj->value[counter] = 1; /*always worth something til it breaks*/
				ch->armor[counter] -= apply_ac (obj, obj->wear_loc, counter);
				}
			}
	return;
	}			

void set_obj_cond (OBJ_DATA *obj, int condition)
	{
	int counter;

	obj->condition = condition;
	if (obj->item_type == ITEM_ARMOR)
		for (counter=0 ; counter < 4 ; counter ++)		
			{
			obj->value[counter] = obj->valueorig[counter]*(obj->condition)/100;
			if ((obj->value[counter] == 0) && (obj->valueorig[counter] != 0))  
				obj->value[counter] = 1; /*always worth something til it breaks*/
			}
	return;
	}