READ_DES.C
LIB3/STD/BASIC/READ_DES.C · 4.5 KB
From the archive nm4dos.zip
Download the archive « Back to the archive contents
#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/" });
}