/
LIB3/
LIB3/D/ADMIN/
LIB3/D/ADMIN/OBJ/
LIB3/D/ADMIN/ROOM/W/
LIB3/D/HOME/
LIB3/D/HOME/CITY/ARENA/
LIB3/D/HOME/CITY/ITEMS/
LIB3/D/HOME/CITY/POSTOFFI/
LIB3/DOC/
LIB3/GLOBAL/SPECIAL/
LIB3/GLOBAL/VIRTUAL/
LIB3/NET/
LIB3/NET/CONFIG/
LIB3/NET/DAEMON/CHARS/
LIB3/NET/GOPHER/
LIB3/NET/INHERIT/
LIB3/NET/OBJ/
LIB3/NET/SAVE/
LIB3/NET/VIRTUAL/
LIB3/OBJ/B_DAY/
LIB3/OBJ/HANDLERS/TERM_TYP/
LIB3/PLAYERS/B/
LIB3/PLAYERS/N/
LIB3/ROOM/
LIB3/SAVE/
LIB3/SAVE/BOARDS/
LIB3/SAVE/ENVIRON/
LIB3/SAVE/POST/
LIB3/STD/COMMANDS/SHADOWS/
LIB3/STD/CREATOR/
LIB3/STD/DOM/
LIB3/STD/EFFECTS/
LIB3/STD/EFFECTS/HEALING/
LIB3/STD/EFFECTS/OTHER/
LIB3/STD/EFFECTS/POISONS/
LIB3/STD/ENVIRON/
LIB3/STD/GUILDS/
LIB3/STD/LIQUIDS/
LIB3/STD/ROOM/
LIB3/STD/TRIGGER/SHADOW/
LIB3/W/
LIB3/W/BANNOR/
LIB3/W/NEWSTYLE/
#pragma save_binary
inherit "/std/basic/desc";
#include "language.h"

static mixed *read_mess;
static int max_size = 100, cur_size;

void    add_read_mess( string str, string type, string lang, int size );

void    set_max_size( int siz )
{
    max_size = siz;
}
int     query_max_size()
{
    return max_size;
}

void    set_cur_size( int siz )
{
    cur_size = siz;
}
int     query_cur_size()
{
    return cur_size;
}

void    add_read_this( object ob )
{
    ob->add_command( "read", this_object() );
}

/* This also erases all previous writing */
varargs void set_read_mess( mixed str, string lang, int size )
{
    if( pointerp( str ) )
    {
	read_mess = str;
	return;
    }
    if( !read_mess && str && environment() )
    {
	filter_array( all_inventory( environment() ), "add_read_this", this_object() );
	environment()->add_command( "read", this_object() );
    }
    if( !lang )
	lang = "common";
    if( !size )
	size = 1;
    if( str )
	read_mess = ({ ({ str, 0, lang, size }) });
    else
	read_mess = 0;
}

string *query_read_mess()
{
    return read_mess;
}

mixed   add_read_mess( mixed str, string type, string lang, int size )
{
    int     de_size;

    if( cur_size >= max_size )
	return "";
    if( !size )
	size = 1;
    if( !read_mess && environment() )
    {
	filter_array( all_inventory( environment() ), "add_read_this", this_object() );
	environment()->add_command( "read", this_object() );
    }
    de_size = size * ((int)LANGUAGE_HAND->query_language_size( lang, str ));
    if( cur_size + de_size > max_size )
    {
	str = (string)LANGUAGE_HAND->squidge_text( lang, str,
						   (max_size - cur_size) / size );
	if( !strlen( str ) )
	    return;
	cur_size += size * ((int)LANGUAGE_HAND->query_language_size( lang, str ));
    }
    if( !read_mess )
	read_mess = ({ ({ str, type, lang, size }) });
    else
	read_mess += ({ ({ str, type, lang, size }) });
    return str;
}

/* This function works best given several parameters */
int     remove_read_mess( string str, string type, string lang )
{
    int     i;

    for( i = 0; i < sizeof( read_mess ); i++ )
    {
	if( str && read_mess[ i ][ READ_STR ] != str )
	    continue;
	if( type && read_mess[ i ][ READ_TYPE ] != type )
	    continue;
	if( lang && read_mess[ i ][ READ_LANG ] != lang )
	    continue;
	cur_size -= read_mess[ i ][ READ_SIZE ]
	    * ((int)LANGUAGE_HAND->query_language_size( lang,
							read_mess[ i ][ READ_STR ] ));
	read_mess = delete( read_mess, i, 1 );
	return 1;
    }
    return 0;
}

void    init()
{
    if( read_mess || sizeof( labels ) )
	this_player()->add_command( "read", this_object() );
}

/* Used by do_read */
string  create_read_array( object ob )
{
    mixed * bing;
    int     i;
    string  ret;

    bing = (mixed *)ob->query_read_mess();
    ret = "";
    if( !bing || !sizeof( bing ) )
	return "There is nothing written on " + ob->short() + ".\n";

    for( i = 0; i < sizeof( bing ); i++ )
	ret += (string)this_player()->read_message( bing[ i ][ READ_STR ],
						    bing[ i ][ READ_TYPE ],
						    bing[ i ][ READ_LANG ],
						    bing[ i ][ READ_SIZE ] );
    return ret;
}

/* Called when an object is read */
mixed   do_read()
{
    string  str, s1, s2, s3;
    int     i;

    if( !read_mess && !sizeof( labels ) )
	return 0;
    if( read_mess )
    {
	for( i = 0; i < sizeof( read_mess ); i++ )
	{
	    str = read_mess[ i ][ READ_STR ];
	    if( stringp( str ) )
	    {
		s3 = "";
		while( sscanf( str, "%s$$%s$$%s", s1, s2, str ) == 3 )
		    s3 += s1 + read_file( s2 );
		write( (string)this_player()->read_message( s3 + str,
							    read_mess[ i ][ READ_TYPE ],
							    read_mess[ i ][ READ_LANG ],
							    read_mess[ i ][ READ_SIZE ] ) );
	    }
	    else
	    {
		write( (string)this_player()->read_message( str,
							    read_mess[ i ][ READ_TYPE ],
							    read_mess[ i ][ READ_LANG ],
							    read_mess[ i ][ READ_SIZE ] ) );
	    }
	}
    }
    labels = labels - ({ 0 });
    if( sizeof( labels ) )
    {
	str = implode( map_array( labels, "create_read_array", this_object() ), "" );
	write( str );
	if( !read_mess )
	    if( (s1 = (string)this_object()->query_property( "read id" )) )
		return query_multiple_short( labels ) + " on " + s1;
	    else
		return query_multiple_short( labels ) + " on " + short( 0 );
    }
    if( (s1 = (string)this_object()->query_property( "read id" )) )
	return s1;
    return 1;
}

string long( string str, int dark )
{
    if( read_mess )
	return ::long( str ) + "There is some writing on it.\n";
    return ::long( str, dark );
}

mixed * query_init_data()
{
    return ::query_init_data() +
	({ "read_mess", read_mess, "set_read_mess/P0/P2/" });
}