Debian Codegraph @

types/README

raw
1 This directory is for the object-type dependent code, in addition to also
2 storing some generic methods for objects which an object-type may want to
3 override.
4
5 ==Organization==
6 -Place object-type specific code in this directory either as "foobar.c" or in a
7 directory, as "foobar/*.c" with logical and clear individual file names. Use
8 your discretion on if the code for the type should be split across multiple
9 C files or not
10 -If multiple type numbers are the same in behavior (i.e. armor types, though
11 those should be fixed later), it is fine to put them in the same grouping of
12 code.
13 -Code used by multiple types, that is specific to an action controlled by this
14 subsystem is put in "common/*.c" under a logical filename. This includes
15 generic code for dropping, picking up, etc.
16 -Code for an action controlled by this system, but not yet split in to
17 type-specific segments may be stored in "legacy/*.c" for the moment. This will
18 obviously be removed after refactoring is complete.
19
20 ==API==
21 -The api for this system is in "server/ob_types.c" and "server/ob_methods.c" at
22 the top level. Read the comments in those files for more complete
23 documentation.
24 -The following types are initialized and defined in init_ob_methods(), which
25 you should edit to modify.
26 -The base_type is for defining default actions for all object types. It
27 inherits from legacy_type.
28 -legacy_type is for references to code in "legacy/*.c", and does not have a
29 fallback. It will be removed when the refactoring is complete.
30 -Functions:
31 -The function, init_ob_method_struct(ob_method *methods, ob_methods *fallback)
32 initializes an ob_method struct and sets it's fallback to fallback.
33 -All functions in the form of ob_foobar(object *ob, ...) are for calling object
34 methods. They search though fallbacks when the object's type.
35 -All functions named register_foobar(int ob_type, ...) are for registering a
36 callback with the array storing object methods for different types. Use this
37 to register object_type_specific callbacks
38 -Defined types:
39 -Always make sure your callback functions match the typedefs such as
40 apply_func, as defined in ob_methods.h
41
42 ==Adding new object methods==
43 As a quick reference, here is a checklist for adding a new object method for use
44 in this section of the code.
45 1) Define "foobar_func" in ob_methods.h
46 2) Add "foobar" to the "ob_methods" struct in ob_methods.h
47 3) Add a line to set it to NULL in init_ob_method_struct() in ob_methods.c
48 4) Add the boring handler function, "ob_foobar(object *ob, ...)" in
49 ob_methods.c
50 5) Add the boring "register_foobar(int ob_type, foobar_func *methodptr)"
51 function in ob_types.c
52 6) Add handler functions for base_type and/or legacy_type if applicable.
53 Reference to in init_ob_methods() in ob_methods.c
54 7) Add type-specific methods and register them in an init function for the
55 type, using register_foobar(). Call this init function in init_ob_types() in
56 ob_types.c
57
58 ==Notes on refactoring into here==
59 -Always make a note in the ChangeLog file in this directory, but don't
60 neglect the top level ChangeLog either when making changes here.
61 -Try to refactor one whole object type at a time, adding whatever object
62 methods are required.
63 -Commit often, in fact, as often as you can so long as things don't break
64 -Try not to change in-game behavior here; unless it's a really obvious bug, try
65 to leave that for separate commits either before or after moving the code.
66 -When moving code here, always review it entirely, clean up the comments, and
67 code style.
68
69 ==Example==
70
71 /** @file example.c
72 * An example of how to define a type for the object 'method' system. This
73 * example is for a simple food type with no special behavoir except for when it
74 * is applied it gives the food value to the player. Do not use this for real code,
75 and just consider it an example of how to
76 */
77
78 /**
79 * Initialize the food object type. Call this from init_ob_types in
80 * server/ob_types.c
81 */
82 void init_type_food() {
83 register_apply(FOOD, food_type_apply);
84 register_apply(FLESH, food_type_apply);
85 register_apply(DRINK, food_type_apply);
86 }
87
88 /**
89 * ob_method handler for FOOD, FLESH and DRINK object types.
90 * @todo Handle cursed food
91 * @todo Add hook for dragon resistance gaining
92 * @todo Give special messages when full
93 * @note Remember this is just an example ;-)
94 */
95 method_ret food_type_apply(ob_methods *context, object *ob, object *pl) {
96 method_ret can_apply;
97
98 /*
99 * Call the 'can_apply' method for the player to check if the player can
100 * indeed apply it (checking if the player can reach it, etc).
101 */
102 can_apply = ob_can_apply(pl, ob);
103 if (can_apply == METHOD_OK) {
104 char name[MAX_BUF];
105 query_name(ob, name, MAX_BUF);
106 pl->stats.food += ob->stats.food;
107 if (pl->stats.food > MAX_FOOD)
108 pl->stats.food = MAX_FOOD;
109 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
110 "You eat the %s!",
111 "You eat the %s!",
112 );
113 decrease_ob(ob);
114 }
115 return METHOD_OK;
116 }