| 1 |
This document has 3 sections - section 1 is current programming style/hints for |
| 2 |
developers to make things easier. Section 2 is a programming guide for new |
| 3 |
addition. Section 3 contains notes for making patches. |
| 4 |
|
| 5 |
------------------------------------------------------------------------------- |
| 6 |
Section 1 - currently used conventions/hints for new code writers: |
| 7 |
|
| 8 |
1) Variable abbreviations - op is short for object pointer, ob is for object, |
| 9 |
and pl is for player. |
| 10 |
|
| 11 |
2) Some functions are named using the conventions above - the naming reflects |
| 12 |
what options they take (insert_ob_in_ob takes 2 object structures) |
| 13 |
|
| 14 |
3) Identation is either 2 spaces or 4 spaces. This can be a pain to read, but |
| 15 |
most functions should be consistent through the function. |
| 16 |
|
| 17 |
4) Some structure elements should never be accessed directly - rather, there |
| 18 |
are other functions to use the values. |
| 19 |
|
| 20 |
object->owner: This contains the owner id for this object. Use |
| 21 |
set_owner and get_owner instead. Directly using object->owner |
| 22 |
is likely to get unpredictable results. |
| 23 |
|
| 24 |
object->nrof: This contains the number of an object. |
| 25 |
Since changing this will change the weight of an object, direct |
| 26 |
access should also be avoided. Use decrease_ob_nr, split_ob, |
| 27 |
and insert_ob_in_... - the later will merge the objects if |
| 28 |
applicable. |
| 29 |
|
| 30 |
5) If using insert_ob_in_map and plan to do further actions with the object, |
| 31 |
check and make sure the object still exists after insertion - it is |
| 32 |
possible that the object gets destroyed while being inserted. |
| 33 |
|
| 34 |
------------------------------------------------------------------------------- |
| 35 |
Section 2 - Style guide for new additions: |
| 36 |
|
| 37 |
1) Use descriptive variable names. op and pl should only be used for |
| 38 |
temporary variables (cycling through the list or the like). For variables |
| 39 |
well defined, use an accurate name (ie, hitter, sack, etc). |
| 40 |
|
| 41 |
2) Only add name options with #ifdef's to the config file if the behaviour |
| 42 |
seriously changes the game. Adding a new spell does not warrant an #ifdef. |
| 43 |
There are already too many options in the config.h file. |
| 44 |
|
| 45 |
3) Log errors/diagnostics with the LOG function. When doing so, please |
| 46 |
include the function name - this is especially true for errors. |
| 47 |
|
| 48 |
4) If you want to add special debug code for certain compiles, generate a |
| 49 |
unique #define for it - don't use the global DEBUG. For example, |
| 50 |
NEWCS_DEBUG. |
| 51 |
|
| 52 |
5) Try to use the [s/u]int[8/16/32] whenever possible. Use the one of |
| 53 |
appropriate size/type. If not sure, go for the next size up. Do not ever |
| 54 |
write code assuming that any of those will have an exact number of bits - |
| 55 |
those types only mean that you will get at least that many bits - you may |
| 56 |
get more. |
| 57 |
|
| 58 |
6) The exception to #5 above is strings. Continue to use 'char', since the |
| 59 |
signedness of functions that take string options can differ system to |
| 60 |
system, and generate excessive warnings if the wrong sign is used. |
| 61 |
|
| 62 |
7) When adding new function, include a comment of what the function is |
| 63 |
supposed to do, what options it takes, and what if any value it returns. |
| 64 |
This makes debugging of such functions easier, and also makes it better |
| 65 |
known to other developers if that function might be useful to them. |
| 66 |
|
| 67 |
8) Try to keep lines to less than 80 columns when possible. This is not a |
| 68 |
strict requirement - don't break up some complex comparison because the |
| 69 |
line would otherwise be 83 characters long. Xterms can be resized to most |
| 70 |
any width. However, use your judgement on whether breaking up a long line |
| 71 |
would make something more or less readable. |
| 72 |
|
| 73 |
9) Assume all names use one namespace. For example, if there is a struct |
| 74 |
called spell, don't make the name of an optional parameter spell. This |
| 75 |
will break on C compilers that follow the spec strictly |
| 76 |
|
| 77 |
10) As a followup on 9 above, don't use nonstandard gcc extensions (// for |
| 78 |
comment lines, ability to nest functions, declare arrays with variable |
| 79 |
bounds, etc.) Likewise, don't use special system functions - don't assume |
| 80 |
the target system will be bsd or svr4 - if using a potentially non standard |
| 81 |
function, add checks in the autoconf script and include a version of the |
| 82 |
function in case it is not on that system. They key word here is |
| 83 |
portability - don't assume everyone else has the same system as you do. |
| 84 |
|
| 85 |
11) Write code that can easily be maintained in the future, not code that is |
| 86 |
easiest to write at that second. This basically means don't do the quick |
| 87 |
and ugly hack, but instead fix it properly. |
| 88 |
|
| 89 |
12) Use 4 space indentation. While a lot of old code may have 2 space indents, |
| 90 |
a move to 4 space will improve readability in the future. |
| 91 |
|
| 92 |
13) /* |
| 93 |
* do block |
| 94 |
* comment like |
| 95 |
* this |
| 96 |
*/ |
| 97 |
|
| 98 |
/* |
| 99 |
and not |
| 100 |
like this |
| 101 |
*/ |
| 102 |
|
| 103 |
/* If you are doing a single line comment, this method is fine */ |
| 104 |
|
| 105 |
It is much easier to spot the block comments if they all start with *, |
| 106 |
and these comments tend to be worth noticing. |
| 107 |
|
| 108 |
14) /** |
| 109 |
* Functions should be commented like this. |
| 110 |
* |
| 111 |
* @param bla |
| 112 |
* this is a parameter |
| 113 |
* @return |
| 114 |
* returns NULL |
| 115 |
*/ |
| 116 |
|
| 117 |
This lets doxygen generate nice documentation. |
| 118 |
|
| 119 |
15) As discussed on IRC, the preferred style for expressions is like this: |
| 120 |
|
| 121 |
if (expression) { |
| 122 |
statement; |
| 123 |
statement; |
| 124 |
} |
| 125 |
|
| 126 |
If <space> (expression), the space between the if and expression is |
| 127 |
required. |
| 128 |
|
| 129 |
NOT like this: |
| 130 |
|
| 131 |
if (expression) |
| 132 |
{ |
| 133 |
statement; |
| 134 |
statement; |
| 135 |
} |
| 136 |
|
| 137 |
16) The preferred style of formal parameters: |
| 138 |
|
| 139 |
void myFooFunction(param1, param2, param3) { |
| 140 |
statement; |
| 141 |
statement; |
| 142 |
} |
| 143 |
|
| 144 |
There is no space after the left paren, no space before the right paren, |
| 145 |
comma right after the formal param, and space right after the comma. |
| 146 |
|
| 147 |
17) Local variable names. Just a rules of thumb. |
| 148 |
|
| 149 |
These are ok: |
| 150 |
|
| 151 |
int mylongvarname; |
| 152 |
int my_long_var_name; |
| 153 |
|
| 154 |
Please do NOT use caps except for typedefs, enums and defines. |
| 155 |
|
| 156 |
------------------------------------------------------------------------------- |
| 157 |
Section 3 - Submitting Patches: |
| 158 |
|
| 159 |
1) Please send patches on a bug fix or feature enhancement basis individually, |
| 160 |
and not make mega patches. A diff that changes 10 things is first more |
| 161 |
difficult for me to look over and understand as unrelated changes might be |
| 162 |
going on. It is also harder for me to reject part of a patch (feature X is |
| 163 |
nice, but Y doesn't work). |
| 164 |
|
| 165 |
2) Please state in the message included with the patch what it fixes/changes. |
| 166 |
Too often, I get patches which is just a bunch of source code, and I have |
| 167 |
no idea if I want to incorporate it, or even if the bug is still there. |
| 168 |
Please also state what version of crossfire the diff is for. |
| 169 |
|
| 170 |
3) I will assume any patches mailed directly to me are to be included. If |
| 171 |
posting a patch on the mailing list (either source or ftp location), please |
| 172 |
explicity state whether or not you want that patch incorporated into the |
| 173 |
master source. Many times, a patch may be made available on an |
| 174 |
expiremental basis which is not ready for widespread distribution. |
| 175 |
|
| 176 |
4) When making patches, please make context diffs. Please also include the |
| 177 |
directory that the file is in (run the diff in the top level directory). |
| 178 |
Please make 5 line context diffs - large line context diffs are fine if you |
| 179 |
think that may make it easier. |
| 180 |
|
| 181 |
Example: |
| 182 |
|
| 183 |
'diff -c5 (oldfile) (newfile)' |
| 184 |
|
| 185 |
You can also do diffs of entire directories. Do do this, type: |
| 186 |
|
| 187 |
'diff -c5 -r (old_directory) (new_directory)' |
| 188 |
|
| 189 |
An example: |
| 190 |
|
| 191 |
'diff -c5 -r crossfire-0.90.1 crossfire-0.90.2' |
| 192 |
|
| 193 |
5) Gnu diff will include files that did not exist before. Other diff programs |
| 194 |
may not do this. |
| 195 |
|
| 196 |
6) If your diff looks excessively long and you made a lot of formatting |
| 197 |
changes, you can add -w to the diff options to have it ignore whitespace. |
| 198 |
Note that this will then mean that those formatting changes will then be |
| 199 |
lost. |
| 200 |
|
| 201 |
7) There is no need to make a seperate diff file for each file different (ie, |
| 202 |
treasure.diff, player.diff, etc). Assuming you follow steps 1-6, all the |
| 203 |
diffs can be contained in one file, and patch will deal with it just fine. |
| 204 |
|
| 205 |
8) If you need to send a map, new archetypes, or other new files where a diff |
| 206 |
doesn't make since, a uuencoded tar file will work just fine. |
| 207 |
|
| 208 |
9) Submit all patches to the Sourceforge patch tracker on Sourceforge: |
| 209 |
|
| 210 |
http://sourceforge.net/projects/crossfire |
| 211 |
|
| 212 |
After posting the patch, mail an announcement to: |
| 213 |
|
| 214 |
Crossfire Discussion Mailing List <[email protected]> |
| 215 |
|