1-
1+ /*
2+ * Package: Attributes
3+ * Summary: An entity's attributes, such as strength, intelligence, etc.
4+ *
5+ * If the entity is a character (living object), then the attributes will be used to
6+ * calculate the character's abilities, such as attack, defense, magic power, etc.
7+ * Character's attributes can be modified by various means, such as conditions, equipments,
8+ * magical effects, etc.
9+ *
10+ * If the entity is not a character, the attributes will be used for calculation when
11+ * a character interacts with it.
12+ */
213
314#include <dbase.h>
415#include <attribute.h>
@@ -7,56 +18,47 @@ mapping attribute = ([]);
718
819mapping query_attribute () { return attribute ; }
920
10- // query_attr()
11- //
12- // returns the value of a character's attribute.
13-
14- varargs int
15- query_attr (string what , int raw )
16- {
21+ varargs int query_attr (string what , int raw ) {
1722 int a ;
23+ if (!mapp (attribute ) || undefinedp (a = attribute [what ]))
24+ return 0 ;
1825
19- if ( !mapp (attribute ) || undefinedp (a = attribute [what ]) ) return 0 ;
20-
21- if ( raw ) return a ;
26+ if (raw )
27+ return a ;
2228
23- return a + (int )query_temp ("apply/" + what );
29+ return a + (int )query_temp ("apply/" + what );
2430}
2531
26- int
27- set_attr (string what , int value )
28- {
29- if ( !mapp (attribute ) ) return 0 ;
32+ int set_attr (string what , int value ) {
33+ if (!mapp (attribute ))
34+ attribute = ( []) ;
3035
31- if ( userp ( this_object ())
32- && ( undefinedp ( attribute [ what ])
33- || value < ATTRVAL_MIN
34- || value > ATTRVAL_MAX ) )
35- return 0 ;
36+ // For user characters, the attribute must be initialized in init_attribute() and
37+ // the value must be between ATTRVAL_MIN and ATTRVAL_MAX.
38+ if ( userp ( this_object ())
39+ && ( undefinedp ( attribute [ what ]) || value < ATTRVAL_MIN || value > ATTRVAL_MAX ) )
40+ return 0 ;
3641
3742 return (attribute [what ] = value );
3843}
3944
40- varargs void
41- init_attribute (mapping base )
42- {
45+ varargs void init_attribute (mapping base ) {
4346 mapping attr ;
4447 string name ;
4548 int value ;
4649
47-
48- if ( mapp (attr = query ("attribute" )) ) {
49- attribute = attr ;
50- delete ("attribute" );
50+ if (mapp (attr = query ("attribute" ))) {
51+ attribute = attr ;
52+ delete ("attribute" );
5153 }
5254
53- if ( !mapp (attribute ) ) attribute = allocate_mapping (NUM_ATTRIBUTES );
54-
55- if ( !mapp (base ) || !sizeof (base ) ) return ;
55+ if (!mapp (attribute ))
56+ attribute = allocate_mapping (NUM_ATTRIBUTES );
5657
58+ if (!mapp (base ) || !sizeof (base ))
59+ return ;
5760
58- foreach (name , value in base )
59- if ( undefinedp (attribute [name ]) )
61+ foreach (name , value in base )
62+ if ( undefinedp (attribute [name ]))
6063 attribute [name ] = value ;
6164}
62-
0 commit comments