-
Notifications
You must be signed in to change notification settings - Fork 1k
Coding style
OpenKore is written in Perl, a scripting language. Although Perl is a fine language, due to the language's extreme flexibility, many developers (including Kura, the original author of Kore, and many modders) make coding mistakes. The goal of this document is to inform developers about how to properly write code for (Open)Kore.
- Use Tabs, not spaces.
- Put the bracket immediately after a statement or subroutine definition.
- Do not put a space between a function name and the argument list.
- Do not capitalize the first character of a name. Like:
$helloWorld
, not$HelloWorld
.
- Correct example for the above points
if ($canProceed) { bar(1); } else { bar(0); } }
- Wrong example
sub foo() { my $CanProceed = $_[0]; if ($CanProceed) { bar(1); } else { bar(0); } }
Avoid global variables as much as possible. They are not automatically destroyed when not needed anymore, so they hog memory. A lot of memory leaks in Kore are caused by the use of global variables that really should have been local.
(!!) If the variable is only used temporary (only within one function or statement), make the variable local by defining it with "my". Example:
$msg = "01"; $switch = "ABCD"; if ($switch eq "ABCD") { my $level = unpack("S1", substr($msg, 0, 2)); print "/me uses Dummy Skill lv $level\n"; }
(!!) Within the AI() function, you may want to use shared variables between two blocks. Even so, try to avoid using global variables. Instead, use the $ai_v{'temp'} hash. This hash is destroyed every time you change a map or re-login, so it's still better than using global variables. You shouldn't forget to manually destroy your variable when no longer needed.
- Example:
# Now we really don't need this variable anymore undef $ai_v{'temp'}{'escape_chance'}; } else { # Calculate the chance to run away $ai_v{'temp'}{'escape_chance'} = rand(1); unshift @ai_seq, "escape"; } } if ($ai_seq[0] eq "escape") { if ($ai_v{'temp'}{'escape_chance'} < 0.5) { print "Running away!\n"; runAway(); # We don't need it anymore, so destroy it undef $ai_v{'temp'}{'escape_chance'}; } # The chance to run away is too low. The current value of escape_chance is used next time "if ($mobbed)" is called, so we don't destroy it. }
Comments are important and often underestimated. In the long run, good comments will help you and potential contributors.
- Write a brief description for complex or confusing blocks of code.
- Example:
... (hundreds of lines follow) ...
- You should put a comment before every function definition, describing what the function does, what parameters it accepts, and the function's return value. If the function's complex, add an example.
- Examples:
# configModify(key, val) # key: a key name. # val: the new value. # # Changes the value of configuration key $key to $val. Both $config and config.txt are modified. sub configModify { ...
# distance(r_hash1, r_hash2) # r_hash1, r_hash2: references to position hash tables. # Returns: the distance as integer. # # Calculates the distance between r_hash1 and r_hash2. # # Example: # my $dist = distance(\%{$chars[$config{'char'}]{'pos_to'}}, # \%{$monsters{$ID}{'pos_to'}}); sub distance { ...