skylib_mudos_v1/
skylib_mudos_v1/bin/
skylib_mudos_v1/bin/db/
skylib_mudos_v1/mudlib/banish/a/
skylib_mudos_v1/mudlib/banish/b/
skylib_mudos_v1/mudlib/banish/c/
skylib_mudos_v1/mudlib/banish/d/
skylib_mudos_v1/mudlib/banish/e/
skylib_mudos_v1/mudlib/banish/f/
skylib_mudos_v1/mudlib/banish/g/
skylib_mudos_v1/mudlib/banish/h/
skylib_mudos_v1/mudlib/banish/j/
skylib_mudos_v1/mudlib/banish/l/
skylib_mudos_v1/mudlib/banish/m/
skylib_mudos_v1/mudlib/banish/n/
skylib_mudos_v1/mudlib/banish/o/
skylib_mudos_v1/mudlib/banish/p/
skylib_mudos_v1/mudlib/banish/r/
skylib_mudos_v1/mudlib/banish/s/
skylib_mudos_v1/mudlib/banish/t/
skylib_mudos_v1/mudlib/banish/u/
skylib_mudos_v1/mudlib/banish/w/
skylib_mudos_v1/mudlib/cmds/
skylib_mudos_v1/mudlib/cmds/admin/
skylib_mudos_v1/mudlib/cmds/guild-race/
skylib_mudos_v1/mudlib/cmds/guild-race/crafts/
skylib_mudos_v1/mudlib/cmds/guild-race/magic/
skylib_mudos_v1/mudlib/cmds/guild-race/other/
skylib_mudos_v1/mudlib/cmds/living/broken/
skylib_mudos_v1/mudlib/cmds/player/group_cmds/
skylib_mudos_v1/mudlib/d/admin/
skylib_mudos_v1/mudlib/d/admin/room/
skylib_mudos_v1/mudlib/d/admin/room/we_care/
skylib_mudos_v1/mudlib/d/admin/save/
skylib_mudos_v1/mudlib/d/admin/text/
skylib_mudos_v1/mudlib/d/learning/TinyTown/buildings/
skylib_mudos_v1/mudlib/d/learning/TinyTown/map/
skylib_mudos_v1/mudlib/d/learning/TinyTown/roads/
skylib_mudos_v1/mudlib/d/learning/chars/
skylib_mudos_v1/mudlib/d/learning/functions/
skylib_mudos_v1/mudlib/d/learning/handlers/
skylib_mudos_v1/mudlib/d/learning/help_topics/
skylib_mudos_v1/mudlib/d/learning/help_topics/npcs/
skylib_mudos_v1/mudlib/d/learning/help_topics/objects/
skylib_mudos_v1/mudlib/d/learning/help_topics/rcs_demo/
skylib_mudos_v1/mudlib/d/learning/help_topics/rcs_demo/RCS/
skylib_mudos_v1/mudlib/d/learning/help_topics/rooms/
skylib_mudos_v1/mudlib/d/learning/help_topics/rooms/crowd/
skylib_mudos_v1/mudlib/d/learning/help_topics/rooms/situations/
skylib_mudos_v1/mudlib/d/learning/save/
skylib_mudos_v1/mudlib/d/learning/school/
skylib_mudos_v1/mudlib/d/learning/school/add_sc/
skylib_mudos_v1/mudlib/d/learning/school/characters/
skylib_mudos_v1/mudlib/d/learning/school/general/
skylib_mudos_v1/mudlib/d/learning/school/getting-started/
skylib_mudos_v1/mudlib/d/learning/school/getting-started/basic_commands/
skylib_mudos_v1/mudlib/d/learning/school/getting-started/edtutor/
skylib_mudos_v1/mudlib/d/learning/school/getting-started/unix_tutor/
skylib_mudos_v1/mudlib/d/learning/school/items/
skylib_mudos_v1/mudlib/d/learning/school/npc_school/
skylib_mudos_v1/mudlib/d/learning/school/room_school/
skylib_mudos_v1/mudlib/d/learning/school/room_school/room_basic/
skylib_mudos_v1/mudlib/d/learning/school/room_school/situations/
skylib_mudos_v1/mudlib/d/learning/school/room_school/terrain_tutor/
skylib_mudos_v1/mudlib/d/learning/text/
skylib_mudos_v1/mudlib/d/liaison/
skylib_mudos_v1/mudlib/d/mudlib/
skylib_mudos_v1/mudlib/d/mudlib/changes/
skylib_mudos_v1/mudlib/d/playtesters/
skylib_mudos_v1/mudlib/d/playtesters/effects/
skylib_mudos_v1/mudlib/d/playtesters/handlers/
skylib_mudos_v1/mudlib/d/playtesters/items/
skylib_mudos_v1/mudlib/d/sage/
skylib_mudos_v1/mudlib/doc/
skylib_mudos_v1/mudlib/doc/creator/
skylib_mudos_v1/mudlib/doc/driver/
skylib_mudos_v1/mudlib/doc/driver/efuns/arrays/
skylib_mudos_v1/mudlib/doc/driver/efuns/buffers/
skylib_mudos_v1/mudlib/doc/driver/efuns/compile/
skylib_mudos_v1/mudlib/doc/driver/efuns/filesystem/
skylib_mudos_v1/mudlib/doc/driver/efuns/floats/
skylib_mudos_v1/mudlib/doc/driver/efuns/functions/
skylib_mudos_v1/mudlib/doc/driver/efuns/general/
skylib_mudos_v1/mudlib/doc/driver/efuns/mappings/
skylib_mudos_v1/mudlib/doc/driver/efuns/mixed/
skylib_mudos_v1/mudlib/doc/driver/efuns/mudlib/
skylib_mudos_v1/mudlib/doc/driver/efuns/numbers/
skylib_mudos_v1/mudlib/doc/driver/efuns/parsing/
skylib_mudos_v1/mudlib/doc/known_command/
skylib_mudos_v1/mudlib/doc/login/
skylib_mudos_v1/mudlib/doc/lpc/basic_manual/
skylib_mudos_v1/mudlib/doc/lpc/intermediate/
skylib_mudos_v1/mudlib/doc/new/add_command/
skylib_mudos_v1/mudlib/doc/new/events/
skylib_mudos_v1/mudlib/doc/new/handlers/
skylib_mudos_v1/mudlib/doc/new/living/race/
skylib_mudos_v1/mudlib/doc/new/living/spells/
skylib_mudos_v1/mudlib/doc/new/object/
skylib_mudos_v1/mudlib/doc/new/player/
skylib_mudos_v1/mudlib/doc/new/room/guild/
skylib_mudos_v1/mudlib/doc/new/room/outside/
skylib_mudos_v1/mudlib/doc/new/room/storeroom/
skylib_mudos_v1/mudlib/doc/object/
skylib_mudos_v1/mudlib/doc/playtesters/
skylib_mudos_v1/mudlib/doc/policy/
skylib_mudos_v1/mudlib/doc/weapons/
skylib_mudos_v1/mudlib/global/
skylib_mudos_v1/mudlib/global/creator/
skylib_mudos_v1/mudlib/global/handlers/
skylib_mudos_v1/mudlib/global/virtual/setup_compiler/
skylib_mudos_v1/mudlib/include/cmds/
skylib_mudos_v1/mudlib/include/effects/
skylib_mudos_v1/mudlib/include/npc/
skylib_mudos_v1/mudlib/include/room/
skylib_mudos_v1/mudlib/include/shops/
skylib_mudos_v1/mudlib/net/daemon/
skylib_mudos_v1/mudlib/net/daemon/chars/
skylib_mudos_v1/mudlib/net/inherit/
skylib_mudos_v1/mudlib/net/obj/
skylib_mudos_v1/mudlib/obj/amulets/
skylib_mudos_v1/mudlib/obj/b_day/
skylib_mudos_v1/mudlib/obj/clothes/
skylib_mudos_v1/mudlib/obj/dwarmours/plate/
skylib_mudos_v1/mudlib/obj/dwclothes/transport/horse/
skylib_mudos_v1/mudlib/obj/dwscabbards/
skylib_mudos_v1/mudlib/obj/dwweapons/axes/
skylib_mudos_v1/mudlib/obj/dwweapons/chains/
skylib_mudos_v1/mudlib/obj/faith/symbols/
skylib_mudos_v1/mudlib/obj/fungi/
skylib_mudos_v1/mudlib/obj/gatherables/
skylib_mudos_v1/mudlib/obj/instruments/
skylib_mudos_v1/mudlib/obj/magic/
skylib_mudos_v1/mudlib/obj/media/
skylib_mudos_v1/mudlib/obj/misc/player_shop/
skylib_mudos_v1/mudlib/obj/monster/godmother/
skylib_mudos_v1/mudlib/obj/monster/transport/
skylib_mudos_v1/mudlib/obj/rings/
skylib_mudos_v1/mudlib/obj/spells/
skylib_mudos_v1/mudlib/obj/stationery/
skylib_mudos_v1/mudlib/obj/stationery/envelopes/
skylib_mudos_v1/mudlib/obj/stationery/papers/
skylib_mudos_v1/mudlib/obj/toys/
skylib_mudos_v1/mudlib/obj/vessels/
skylib_mudos_v1/mudlib/obj/weapons/swords/
skylib_mudos_v1/mudlib/save/autodoc/
skylib_mudos_v1/mudlib/save/leaflets/
skylib_mudos_v1/mudlib/save/mail/
skylib_mudos_v1/mudlib/save/new_soul/data/
skylib_mudos_v1/mudlib/save/parcels/
skylib_mudos_v1/mudlib/save/playerinfo/
skylib_mudos_v1/mudlib/save/players/d/
skylib_mudos_v1/mudlib/save/random_names/
skylib_mudos_v1/mudlib/save/random_names/data/
skylib_mudos_v1/mudlib/save/terrains/
skylib_mudos_v1/mudlib/save/terrains/tutorial_desert/
skylib_mudos_v1/mudlib/save/terrains/tutorial_grassy_field/
skylib_mudos_v1/mudlib/save/terrains/tutorial_mountain/
skylib_mudos_v1/mudlib/save/todo_lists/
skylib_mudos_v1/mudlib/secure/
skylib_mudos_v1/mudlib/secure/cmds/admin/
skylib_mudos_v1/mudlib/secure/cmds/lord/
skylib_mudos_v1/mudlib/secure/config/
skylib_mudos_v1/mudlib/secure/handlers/autodoc/
skylib_mudos_v1/mudlib/secure/handlers/intermud/
skylib_mudos_v1/mudlib/secure/include/global/
skylib_mudos_v1/mudlib/secure/save/
skylib_mudos_v1/mudlib/secure/save/handlers/
skylib_mudos_v1/mudlib/secure/std/classes/
skylib_mudos_v1/mudlib/secure/std/modules/
skylib_mudos_v1/mudlib/std/commands/
skylib_mudos_v1/mudlib/std/commands/shadows/
skylib_mudos_v1/mudlib/std/creator/
skylib_mudos_v1/mudlib/std/dom/
skylib_mudos_v1/mudlib/std/effects/
skylib_mudos_v1/mudlib/std/effects/external/
skylib_mudos_v1/mudlib/std/effects/fighting/
skylib_mudos_v1/mudlib/std/effects/priest/
skylib_mudos_v1/mudlib/std/effects/room/
skylib_mudos_v1/mudlib/std/environ/
skylib_mudos_v1/mudlib/std/guilds/
skylib_mudos_v1/mudlib/std/guilds/old/
skylib_mudos_v1/mudlib/std/languages/
skylib_mudos_v1/mudlib/std/languages/BACKUPS/
skylib_mudos_v1/mudlib/std/liquids/
skylib_mudos_v1/mudlib/std/npc/
skylib_mudos_v1/mudlib/std/npc/goals/
skylib_mudos_v1/mudlib/std/npc/goals/basic/
skylib_mudos_v1/mudlib/std/npc/goals/misc/
skylib_mudos_v1/mudlib/std/npc/plans/
skylib_mudos_v1/mudlib/std/npc/plans/basic/
skylib_mudos_v1/mudlib/std/npc/types/
skylib_mudos_v1/mudlib/std/npc/types/helper/
skylib_mudos_v1/mudlib/std/npcs/
skylib_mudos_v1/mudlib/std/outsides/
skylib_mudos_v1/mudlib/std/races/shadows/
skylib_mudos_v1/mudlib/std/room/basic/topography/
skylib_mudos_v1/mudlib/std/room/controller/
skylib_mudos_v1/mudlib/std/room/inherit/topography/
skylib_mudos_v1/mudlib/std/room/topography/area/
skylib_mudos_v1/mudlib/std/room/topography/iroom/
skylib_mudos_v1/mudlib/std/room/topography/milestone/
skylib_mudos_v1/mudlib/std/shadows/curses/
skylib_mudos_v1/mudlib/std/shadows/disease/
skylib_mudos_v1/mudlib/std/shadows/fighting/
skylib_mudos_v1/mudlib/std/shadows/healing/
skylib_mudos_v1/mudlib/std/shadows/magic/
skylib_mudos_v1/mudlib/std/shadows/poison/
skylib_mudos_v1/mudlib/std/shadows/rituals/
skylib_mudos_v1/mudlib/std/shadows/room/
skylib_mudos_v1/mudlib/std/shops/controllers/
skylib_mudos_v1/mudlib/std/shops/objs/
skylib_mudos_v1/mudlib/std/shops/player_shop/
skylib_mudos_v1/mudlib/std/socket/
skylib_mudos_v1/mudlib/std/soul/
skylib_mudos_v1/mudlib/std/soul/d/
skylib_mudos_v1/mudlib/std/soul/e/
skylib_mudos_v1/mudlib/std/soul/i/
skylib_mudos_v1/mudlib/std/soul/j/
skylib_mudos_v1/mudlib/std/soul/k/
skylib_mudos_v1/mudlib/std/soul/l/
skylib_mudos_v1/mudlib/std/soul/n/
skylib_mudos_v1/mudlib/std/soul/o/
skylib_mudos_v1/mudlib/std/soul/q/
skylib_mudos_v1/mudlib/std/soul/u/
skylib_mudos_v1/mudlib/std/soul/v/
skylib_mudos_v1/mudlib/std/soul/y/
skylib_mudos_v1/mudlib/std/soul/z/
skylib_mudos_v1/mudlib/std/stationery/
skylib_mudos_v1/mudlib/w/
skylib_mudos_v1/mudlib/w/default/
skylib_mudos_v1/mudlib/w/default/armour/
skylib_mudos_v1/mudlib/w/default/clothes/
skylib_mudos_v1/mudlib/w/default/item/
skylib_mudos_v1/mudlib/w/default/npc/
skylib_mudos_v1/mudlib/w/default/room/
skylib_mudos_v1/mudlib/w/default/weapon/
skylib_mudos_v1/mudlib/www/
skylib_mudos_v1/mudlib/www/download/
skylib_mudos_v1/mudlib/www/java/
skylib_mudos_v1/mudlib/www/secure/
skylib_mudos_v1/mudlib/www/secure/lpc/advanced/
skylib_mudos_v1/mudlib/www/secure/lpc/intermediate/
skylib_mudos_v1/v22.2b14-DSv10/
skylib_mudos_v1/v22.2b14-DSv10/ChangeLog.old/
skylib_mudos_v1/v22.2b14-DSv10/Win32/
skylib_mudos_v1/v22.2b14-DSv10/compat/
skylib_mudos_v1/v22.2b14-DSv10/compat/simuls/
skylib_mudos_v1/v22.2b14-DSv10/include/
skylib_mudos_v1/v22.2b14-DSv10/mudlib/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/clone/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/command/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/data/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/etc/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/include/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/inherit/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/inherit/master/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/log/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/single/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/single/tests/compiler/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/single/tests/efuns/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/single/tests/operators/
skylib_mudos_v1/v22.2b14-DSv10/testsuite/u/
skylib_mudos_v1/v22.2b14-DSv10/tmp/
skylib_mudos_v1/v22.2b14-DSv10/windows/
/*
 * Modifications :
 *
 * Changed to use general.language instead of other.language - Shaydz
 *
 * Tried to improve the level of information returned by the
 * perform task function. - Shiannar.
 *
 * Added a new function to handle TMing points skills - Sandoz
 *
 */

#include <tasks.h>

#define SAVE_FILES       "/save/tasks/"
#define TIME_PER_USER    1800

// #define DEBUG "shiannar"
#define WATCH_PLAYER "raistlin"
#define MIN_LEVEL 20
#define LOG_STATS 2
#undef LOGGING

mapping stats;
nosave int last_save;

nosave int last;
nosave string skill;
nosave mixed *control;

/** @ignore yes */
void create() { seteuid( "Root" ); }

varargs mixed perform_task( object person, string skill, int difficulty,
                            int tm_type, int use_class);
varargs mixed attempt_task( int difficulty, int bonus, int upper, mixed extra,
                            int use_class );
varargs mixed attempt_task_e( int difficulty, int bonus, int upper, int half,
                              int use_class );
int point_tasker( object person, string type, int amount );

/** @ignore yes */
int is_valid_tm( object person, string skill ) {

    skill = explode( skill, "." )[0];

    if( interactive(person) && !person->query_property("tm_"+skill) ) {
        person->remove_property("player_tm"); // remove the old prop.
        person->add_property("tm_"+skill, 1, 180 );
        return 1;
    }

    return 0;

} /* is_valid_tm() */

/** @ignore yes */
mapping query_stats( string s_name ) {
    if( skill != s_name ) {
        skill = s_name;
        if( file_size( SAVE_FILES + skill +".o" ) > 0 )
            unguarded( (: restore_object, SAVE_FILES + skill :) );
        else
            stats = 0;
    }
    if( !stats )
        stats = ([ ]);
    return copy( stats );
} /* query_stats() */

/** @ignore yes */
int query_last() { return last; }

/** @ignore yes */
mixed *query_control() { return control; }

/**
 * This function should only be used in the very rare situations
 * where the last skill checked with query_skill_bonus() wasn't the
 * one relevant for the task attempt.
 *
 * @param args an array consisting of ({ object player, string skill })
 */
void set_control( mixed *args ) { control = args; }

/** @ignore yes */
void reset_control() { control = 0; }

/** @ignore yes */
void award_made( string p_name, string o_name, string s_name, int level ) {
    user_event( "inform", p_name +" gains a level in "+ s_name +" from "+
                o_name + " at level " + level, "skill" );
#ifdef LOG_STATS
#if LOG_STATS == 1
    query_stats( s_name );
    if( !stats[ level ] )
        stats[ level ] = ([ ]);
    stats[ level ][ explode( o_name, "#" )[ 0 ] ]++;
#else
    query_stats("summary");

    // these two just skew the stats so we don't record them.
    if( s_name[<7..] == ".points" || s_name[0..13] == "general.language")
        return;

    s_name = explode(s_name, ".")[0];

    if( !stats[s_name])
        stats[s_name] = ({ ({ 0 , 0 }),  ({ 0, 0 }),  ({ 0, 0 }) , ({0,0})});

    switch( level ) {
      case 0..149:
          stats[s_name][0] = ({ stats[s_name][0][0], stats[s_name][0][1]+1 });
      break;
      case 150..299:
          stats[s_name][1] = ({ stats[s_name][1][0], stats[s_name][1][1]+1 });
      break;
      case 300..599:
          stats[s_name][2] = ({ stats[s_name][2][0], stats[s_name][2][1]+1 });
      break;
      default:
          stats[s_name][3] = ({ stats[s_name][3][0], stats[s_name][3][1]+1 });
      break;
    }

    if( last_save < time() - 300 ) {
        unguarded( (: save_object, SAVE_FILES + "summary" :) );
        last_save = time();
    }
#endif
#endif
} /* award_made() */

#if LOG_STATS == 2
/** @ignore yes */
void skill_checked( string s_name, int level ) {
    query_stats("summary");
    if( s_name[<7..] == ".points" || s_name[0..13] == "general.language" )
        return;

    s_name = explode(s_name, ".")[0];

    if( !stats[s_name] )
        stats[s_name] = ({ ({ 0 , 0 }),  ({ 0, 0 }),  ({ 0, 0 }), ({0,0}) });

    switch(level) {
      case 0..149:
          stats[s_name][0] = ({ stats[s_name][0][0]+1, stats[s_name][0][1] });
      break;
      case 150..299:
          stats[s_name][1] = ({ stats[s_name][1][0]+1, stats[s_name][1][1] });
      break;
      case 300..599:
          stats[s_name][2] = ({ stats[s_name][2][0]+1, stats[s_name][2][1] });
      break;
      default:
          stats[s_name][3] = ({ stats[s_name][3][0]+1, stats[s_name][3][1] });
      break;
    }

    if( last_save < time() - 300 ) {
        unguarded( (: save_object, SAVE_FILES + "summary" :) );
        last_save = time();
    }
} /* skill_checked() */
#endif

/** @ignore yes */
string *query_skill_files() {
    return unguarded( (: get_dir, SAVE_FILES +"*.o" :) );
} /* query_skill_files() */

/** @ignore yes */
void clear_skill_files() {
    string word;
    foreach ( word in unguarded( (: get_dir, SAVE_FILES +"*.o" :) ) )
    unguarded( (: rm, SAVE_FILES + word :) );
    skill = 0;
} /* clear_skill_files() */

/**
 * This function will compare the skills of two objects and return which one
 * won and if the winner got a TM.
 * With equal skills the chances are 50/50 as to who will win. As the balance
 * shifts so do the chances. Additionally a modifier can be applied to
 * account for conditions favouring one or the other. This should be
 * considered a percentage eg. -50 will add 50% to the defenders chances of
 * winning.
 *
 * @param offob The attacking object
 * @param offskill The name of the skill the attacker is using
 * @param deffob The defending object
 * @param deffskill The name fo the skill the defender is using
 * @param modifier The percentage modifier
 * @param off_tm_type This should be one of the standard definitions in
 * /include/tasks.h and is for the attacker
 * @param def_tm_type This should be one of the standard definitions in
 * /include/tasks.h and is for the defender
 *
 * @example
 *
 * switch(TASKER->compare_skills(attacker, "fighting.combat.melee.sharp",
 *                               defender, "fighting.combat.parry.unarmed",
 *                               25, TM_COMMAND, TM_FREE) {
 * case OFFAWARD:
 *   tell_object(attacker, "%^YELLOW%^You manage to grasp one of the "
 *                         "principles of slicing people up more firmly."
 *                         "%^RESET%^\n");
 * case OFFWIN:
 *   say(defender->one_short() + " loses an arm!\n");
 *   break;
 * case DEFAWARD:
 *   tell_object(defender, "%^YELLOW%^You feel better able to keep your arms "
 *                         "attached when parrying unarmed.%^RESET%^\n");
 * case DEFWIN:
 *   say(defender->one_short() + " keeps " + defender->query_possesive() +
 *       " arm attached.\n");
 *   break;
 * }
 *
 * @see perform_task
 *
 */
int compare_skills( object offob, string offskill, object defob,
                    string defskill, int modifier, int off_tm_type,
                    int def_tm_type ) {
    int offbonus, defbonus, perc, chance;

    offbonus = offob->query_skill_bonus(offskill);
    defbonus = defob->query_skill_bonus(defskill);

    perc = (offbonus * 100) / (offbonus + defbonus);
    perc += modifier; // the difficulty may be weighted.

    chance = random(100);
    if( chance < perc ) {
        // winner is 1, now do a TM check.
        switch( perform_task( offob, offskill, defbonus-modifier, off_tm_type ) ) {
        case AWARD:
            return OFFAWARD;
        default:
            return OFFWIN;
        }
    } else {
        // winner is 2
        switch( perform_task( defob, defskill, offbonus-modifier, def_tm_type ) ) {
        case AWARD:
            return DEFAWARD;
        default:
            return DEFWIN;
        }
    }
} /* compare_skills() */

/**
 * This function will attempt a task.  It handles all the stuff about
 * looking up the skill, and giving the tm advance, but doesn't give
 * any messages to the player, you'll have to do that.
 *
 * @param person the one attempting the task (could be any living thing)
 * @param skill the skill tested against
 * @param difficulty the lowest bonus where the attempt can succeed
 * @param tm_type This should use one of the standard definitions in
 *        /include/tasks.h
 *        They are:
 *        TM_FIXED - for use where the difficulty is a fixed value
 *        TM_FREE  - for use when the tm attempt doesn't cost anything.
 *        TM_CONTINUOUS - for use in continuous actions eg. combat or sneak
 *        TM_COMMAND - for use with guild commands
 *        TM_RITUAL - when the action is a ritual
 *        TM_SPELL - when the action is a spell
 * @param use_class This is 0 when you don't want a class returned,
 * and 1 when you do.
 *
 * @return BARF if something screwed up, AWARD if the task succeeded, and
 * should give an advance, SUCCEED if it succeeded, FAIL if it failed.
 *
 * @example
 *
 * switch(TASKER->perform_task(person,"covert.manipulation.stealing", TM_COMMAND)) {
 *  case AWARD :
 *    tell_object( person, "%^YELLOW%^You manage to grasp the principles "
 *                "of stealing more firmly.%^RESET%^\n");
 *    // Note, no break;
 *  case SUCCEED :
 *    // Whatever happens when it succeeds
 *    break;
 *  default :
 *    // Whatever happens when it fails
 * }
 */
varargs mixed perform_task( object person, string skill, int difficulty,
                            int tm_type, int use_class ) {
    int bonus, upper, extra;
    mixed result;

    bonus = person->query_skill_bonus( skill );

    if( difficulty )
        extra = 6 * sqrt(difficulty);

    switch( tm_type ) {
    case TM_FIXED:
        result = attempt_task( difficulty, bonus, 100, extra, use_class );
    break;
    case TM_FREE:
        result = attempt_task( difficulty, bonus, 25, extra, use_class );
    break;
    case TM_CONTINUOUS:
        result = attempt_task( difficulty, bonus, 50, extra, use_class );
    break;
    case TM_COMMAND:
        if( explode(skill, ".")[0] == "covert" )
            result = attempt_task_e( difficulty, bonus, 50, 30, use_class );
        else
            result = attempt_task( difficulty, bonus, 60, extra, use_class );
    break;
    case TM_RITUAL:
//        result = attempt_task_e( difficulty, bonus, 30, 40, use_class );
                 // approximate old behaviour - Sandoz.
        result = attempt_task( difficulty, bonus, 18, 100 + to_int( 5 *
                     sqrt( to_float( difficulty < 0 ? 0 : difficulty ) ) ),
                     use_class );
    break;
    case TM_SPELL:
        result = attempt_task_e( difficulty, bonus, 30, 40, use_class );
    break;
    case TM_NONE:
        result = attempt_task_e( difficulty, bonus, 1, extra, use_class );
        if( result == AWARD )
            result = SUCCEED;
    break;
    default: // this is for backward compatibility
#ifdef LOGGING
        if( file_name(PO)[0..2] != "/w/" )
            log_file("ATTEMPT_TASK",
                "%s Object %s using old perform_task [%d, %d]\n",
                ctime(time()), base_name(PO), tm_type, use_class );
#endif
        upper = tm_type;
        extra = use_class;
        if( !upper )
            upper = 100;
        result = attempt_task( difficulty, bonus, upper, extra );
    }

    if( classp(result) ) {
        if( result->result == AWARD ) {
            if( !( random(100) < TM_RATES[explode(skill, ".")[0]] ) ||
                !is_valid_tm( person, skill ) ||
                !person->add_skill_level( skill, 1, PO ) ) {
                result->result = SUCCEED; // No advance.
            }
        }
    } else {
        if( result == AWARD ) {
            if( !( random(100) < TM_RATES[explode(skill, ".")[0]] ) ||
                !is_valid_tm( person, skill ) ||
                !person->add_skill_level( skill, 1, PO ) ) {
                result = SUCCEED; // No advance.
            }
        }
    }

    return result;

} /* perform_task() */

/**
 * This function will attempt to figure out if a tm should be given
 * in the points skill used.  It handles all the stuff about looking
 * up the skill, giving the tm advance, and reducing the guild points.
 * It will also give a generic tm message to the player.
 *
 * @param person the one attempting the points task (could be any living thing)
 * @param type the type (covert, magic etc.) of points skill to check against
 * @param amount the amount of guild points to check against
 * @return 1 if player has enough available guild points of the type, 0 if not
 *
 * @example
 *
 * if( !TASKER->point_tasker( person, "covert", 80 ) ) {
 *     add_failed_mess("You can't concentrate enough to hide.");
 *     return 0;
 * }
 */
int point_tasker( object person, string type, int amount ) {
    string skill;
    int m, level, bonus;
    float b;

    if( ( amount < 1 ) || ( person->query_specific_gp( type ) < amount ) )
        return 0;

    person->adjust_gp( -amount );

    skill = type+".points";

    level = (int)person->query_skill( skill );
    bonus = (int)person->query_skill_bonus( skill );

    b = to_float( ( bonus < 50 ? 50 : bonus ) + ( level < 50 ? 50 : level ) );

    m = (int)person->stat_modify( 10, skill, 1 );

    if( random(1000) < to_int( 25 * sqrt( to_float( 20 + amount ) ) * ( m / b ) ) &&
        is_valid_tm( person, type ) && person->add_skill_level( skill, 1, PO ) )
        tell_object( person, "%^YELLOW%^You feel more able to concentrate "
            "on this task than you thought.%^RESET%^\n");

    return 1;

} /* point_tasker() */

/*
 * This function will attempt a task and return whether it succeedd.<br>
 * <br>
 * Chance of      /|\<br>
 * success  100% __|            ______..<br>
 *                 |           /<br>
 *                 |          /<br>
 *                 |         /<br>
 *            0% __| .._____/<br>
 *                 |_____________________\   Player's bonus<br>
 *                          |   |        /<br>
 *                 difficulty   difficulty + margin<br>
 * <br>
 * Chance of       /|\<br>
 * gaining  upper __|        .<br>
 *                  |        |\<br>
 *                  |        | \<br>
 *                  |        |  \<br>
 *             0% __| .._____|   \_____..<br>
 *                  |_____________________\  Player's bonus<br>
 *                           |   |        /   <br>
 *                  difficulty   difficulty + margin<br>
 * <br>
 * @param difficulty the lowest bonus where the attempt can succeed
 * @param bonus the bonus the player has in the relevant skill
 * @param upper the maximum chance of getting an advance
 * @param extra the margin control. If it is an int, the margin is set
 * to extra. It it's 0 the margin will be calculated automatically from
 * the difficulty as 3*sqrt(difficulty), if it's an array, it'll be
 * calculated as extra[0]+extra[1]*sqrt(difficulty)
 * @return BARF if something screwed up, AWARD if the task succeeded, and
 * should give an advance, SUCCEED if it succeeded, FAIL if it failed.
 *
 * Do not use this function. Use perform_task instead.
 * @see perform_task
 */
mixed attempt_task( int difficulty, int bonus, int upper, mixed extra,
                    int use_class ) {
    int margin, result;
    float tmp;

    class task_class_result return_class;

#ifdef LOGGING
    if( call_stack(2)[1] != "perform_task" &&
        base_name(PO) != "/std/effects/fighting/combat" &&
        file_name(PO)[0..2] != "/w/") {
        log_file("ATTEMPT_TASK", "%s Object %s is using old attempt_task "
                 "[%O]\n", ctime(time()), base_name(PO), call_stack(2) );
    }
#endif

#if LOG_STATS == 2
    if( pointerp(control) && control[1] ) {
        skill_checked( control[1], control[0]->query_skill(control[1]) );
    }
#endif

    /* If the bonus is below the difficulty, they fail. */
    if( bonus < difficulty ) {
        if( use_class ) {
            return_class = new( class task_class_result );
            return_class->result = FAIL;
            // Compatible with degree of failure,and not always critical.
            return_class->degree = ( 0 - random(100) );
            return return_class;
        }
        else {
            return FAIL;
        }
    }

    /* Work out the margin between total failure and total success. */
    if( !extra )
        if( !difficulty ) margin = 6;
        else margin = 6 * sqrt( difficulty );
    else {
        if( intp( extra ) )
            margin = extra;
        if( pointerp( extra ) )
            margin = extra[ 0 ] + extra[ 1 ] * sqrt( difficulty );
    }
    if( !margin )
        return BARF;
    else
        /* If the bonus is above the margin, they succeed. */
    if( bonus > difficulty + margin ) {
#ifdef DEBUG
        if( pointerp(control ) && find_player(DEBUG) &&
            WATCH_PLAYER || control[0]->query_name() == WATCH_PLAYER )
            TCRE( DEBUG, sprintf("%^RED%^TM: %s Skill: %s [%d] [%d] [%d] "
                  "Succeeded%^RESET%^", control[0]->query_name(),
                  control[1], bonus, difficulty, margin ) );
#endif
        if( use_class ) {
            return_class = new( class task_class_result );
            return_class->result = SUCCEED;
            //Compatible with degree of success, and not always critical.
            return_class->degree = (random(100));
            return return_class;
        } else {
            return SUCCEED;
        }
    }

    /* In the margin, they might fail. */
    if( !margin )
        margin = 1;

    result = ( -99 + random(100) + ( ( bonus - difficulty ) + 100 / margin ) );
    if( result < 1 ) result -= 1;
    if( use_class ) {
        return_class = new( class task_class_result );
        return_class->degree = result;
    }

    if( result < 0 ) {
#ifdef DEBUG
        if( pointerp(control) && find_player(DEBUG) &&
            control[0]->query_name() == WATCH_PLAYER )
            TCRE( DEBUG, sprintf("%^RED%^TM: %s Skill: %s [%d] [%d] "
                  "Failed (2)%^RESET%^", control[0]->query_name(),
                  control[1], bonus, difficulty ) );
#endif
        if( use_class ) {
            return_class->result = FAIL;
            return return_class;
        } else {
            return FAIL;
        }
    }

    /* If information is available, adjust the chance to award based on stats.*/
    if( pointerp( control ) ) {
#ifdef DEBUG
        if( find_player(DEBUG) && bonus >= MIN_LEVEL )
            tell_creator( DEBUG, "%^RED%^TM: %s Skill: %s [%d,%d] "
                  "OU: %d ", control[0]->query_name(), control[1],
                  bonus, control[0]->query_skill(control[1]), upper );
#endif

        // Reduce the upper dependant on their stats.
        upper = (int)control[ 0 ]->stat_modify( upper, control[ 1 ] );

#ifdef DEBUG
        if( find_player(DEBUG) && bonus >= MIN_LEVEL)
            tell_creator( DEBUG, "SU: %d ", upper );
#endif

        // Reduce the upper dependant on their level.
        tmp = exp( ( control[0]->query_skill(control[1]) - BASE ) / DECAY );
        upper = to_int( upper / tmp ) - MODIFIER;

#ifdef DEBUG
        if( find_player(DEBUG) && bonus >= MIN_LEVEL )
            tell_creator( DEBUG, "LU: %d ", upper );
#endif

        // prevent upper going negative
        if( upper < 0 )
            upper = 0;

#ifdef DEBUG
        if( find_player(DEBUG) && bonus >= MIN_LEVEL )
            tell_creator( DEBUG, "FU: %d\n%^RESET%^", upper );
#endif
    }
#ifdef DEBUG
    else
    if( find_player(DEBUG) )
        TCRE( DEBUG, sprintf("No control: %O\n", previous_object(-1) ) );
#endif
    /* If they succeed, they might be awarded a level. */
    if( !margin )
        margin = 1;

#ifdef DEBUG
    TCRE( DEBUG, sprintf("Tm variable %d.",
        ( upper * ( difficulty + margin - bonus + 5) ) / margin ) );
#endif
    if( random( 100 ) < ( upper * ( difficulty + margin - bonus + 5 ) ) / margin ) {
        if( use_class) {
            return_class->result = AWARD;
            return return_class;
        } else {
            return AWARD;
        }
    }

    if( use_class ) {
        return_class->result = SUCCEED;
        return return_class;
    } else {
        return SUCCEED;
    }
} /* attempt_task() */

/*
 * This one acts like the other one except that the failure chance
 * starts at 100% at the difficulty and is halved every (half) bonus levels.
 * This function should never be called directly, use perform_task instead.
 *
 * @param difficulty the lowest bonus where the attempt can succeed
 * @param bonus the bonus the player has in the relevant skill
 * @param upper the maximum chance of getting an advance
 * @param half every time the bonus rise by half, the failure
 * chance is halved
 *
 * @return BARF is something screwed up, AWARD if the task succeeded, and
 * should give an advance, SUCCEED if it succeeded, FAIL if it failed.
 *
 * @see perform_task
 */
/**
 * Do not use this function. Use perform_task instead.
 */
varargs mixed attempt_task_e( int difficulty, int bonus, int upper, int half,
                              int use_class ) {
    float fail_chance, tmp;

    class task_class_result return_class;

#ifdef DEBUG
    TCRE( DEBUG, sprintf("D[%d], B[%d], U[%d], H[%d]",
                 difficulty, bonus, upper, half ) );
#endif

#ifdef LOGGING
    if( call_stack(2)[1] != "perform_task" &&
        base_name(PO) != "/std/effects/fighting/combat" &&
        file_name(PO)[0..2] != "/w/" ) {
        log_file("ATTEMPT_TASK", "%s Object %s is using old attempt_task "
            "[%O]\n", ctime(time()), base_name(PO), call_stack(2) );
    }
#endif

#if LOG_STATS == 2
    if( pointerp(control) && control[1] ) {
        skill_checked( control[1], control[0]->query_skill(control[1]) );
    }
#endif

    /* If the bonus is below the difficulty, they fail. */
    if( bonus < difficulty ) {
        if( use_class ) {
            return_class = new( class task_class_result );
            return_class->result = FAIL;
            // Compatible with degree of failure,and not always critical.
            return_class->degree = ( 0 - random(100) );
            return return_class;
        } else {
            return FAIL;
        }
    }

    if( !half ) /* Approximate old behaviour. */
        half = 6 * sqrt( difficulty );

    fail_chance = exp( ( -0.693 * ( bonus - difficulty ) ) / half );

    //   if ( random( 1000 ) < ( 1000 * fail_chance ) ) {
    //      return FAIL;
    //   }

    tmp = - 99 + random(100) + ( 100 * ( 1 - fail_chance ) );
    //   tmp = ( ( ( 1000 * fail_chance) - random(1000) - 500 ) / 5 );
    if( tmp == 100 ) tmp = 99; // So incredibly unlikely, but possible.
    if( use_class ) {
        return_class = new( class task_class_result );
        return_class->degree = tmp;
    }
    if( tmp < 0 ) {
        if( use_class ) {
            return_class->result = FAIL;
            return return_class;
        } else {
            return FAIL;
        }
    }

    /* If information is available, adjust the chance to award based on stats.*/
    if( pointerp( control ) ) {

#ifdef DEBUG
        if( find_player(DEBUG) && bonus >= MIN_LEVEL )
            tell_creator( DEBUG, "%^RED%^TM: %s Skill: %s [%d,%d] OU: %d ",
                control[0]->query_name(), control[1], bonus,
                control[0]->query_skill(control[1]), upper );
#endif

        // Reduce the upper dependaont on their stats.
        upper = (int)control[ 0 ]->stat_modify( upper, control[ 1 ] );

#ifdef DEBUG
        if( find_player(DEBUG) && bonus >= MIN_LEVEL )
            tell_creator( DEBUG, "SU: %d ", upper );
#endif

        // Reduce the upper dependant on their level.
        tmp = exp( (control[0]->query_skill(control[1]) - BASE ) / DECAY );
        upper = to_int( upper / tmp ) - E_MODIFIER;

#ifdef DEBUG
        if( find_player(DEBUG) && bonus >= MIN_LEVEL )
            tell_creator( DEBUG, "LU: %d ", upper );
#endif

        // prevent upper going negative and round down to zero from 10
        if( upper < 0 )
            upper = 0;
#ifdef DEBUG
        if( find_player(DEBUG) && bonus >= MIN_LEVEL )
            tell_creator( DEBUG, "FU: %d\n%^RESET%^", upper );
#endif

    }

    /* If they succeed, they might be awarded a level. */
#ifdef DEBUG
    TCRE( DEBUG, sprintf("TM chance part 1, %O\nTm chance part 2 %O < part 3 %O",
        ( upper * fail_chance * 10 ), bonus, difficulty + ( half * 5 ) ) );
#endif
    if( random( 1100 ) < ( upper * fail_chance * 10 ) &&
        bonus < difficulty + ( half * 5 ) ) {
#ifdef DEBUG
        TCRE( DEBUG, sprintf("TM given out, difficulty was %O, upper was %O, "
              "bonus was %O.", difficulty, upper, bonus ) );
#endif
        if( use_class ) {
            return_class->result = AWARD;
            return return_class;
        } else {
            return AWARD;
        }
    }

    if( use_class ) {
        return_class->result = SUCCEED;
        return return_class;
    } else {
        return SUCCEED;
    }
} /* attempt_task_e() */