source_doc.txt 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. //===== rAthena Documentation ================================
  2. //= Source Documentation
  3. //===== By: ==================================================
  4. //= rAthena Dev Team
  5. //===== Last Updated: ========================================
  6. //= 20140218
  7. //===== Description: =========================================
  8. //= Explanation of source behaviours and structures.
  9. //============================================================
  10. This file provides basic information about rAthena's source code.
  11. The format of this file is as follows:
  12. 1. Glossary
  13. 2. Intro & Emulation
  14. 3. Interface and Communications
  15. 4. Databases and Independence
  16. 5. Package and Module Purposes
  17. 6. Nomenclature
  18. 7. Variable Notes
  19. 8. Building
  20. 9. Atcommands & Script Commands
  21. ===============
  22. | 1. Glossary |
  23. ===============
  24. The following terms will be frequently used throughout this file, so it is
  25. important to have a thorough understanding of what they are to avoid confusion.
  26. Term Description
  27. ---- -----------
  28. serv a program/daemon that runs indefinitely offering a service
  29. host a machine that has one or more servs running
  30. command a request of an action on the server or client
  31. (atcommand, script_command, packet_request)
  32. interface a class/module that offers a list of commands
  33. ========================
  34. | 2. Intro & Emulation |
  35. ========================
  36. rAthena is an emulation of Ragnarok Online, which runs on software known as AEGIS.
  37. AEGIS is separated into 4 servs:
  38. Serv Description
  39. ---- -----------
  40. account handles player account information and logins.
  41. char handles character data (persistent information).
  42. inter handles broadcasting across map-serv. [merged into rAthena's char-serv]
  43. map handles all player runtime actions.
  44. These servs are an aggregation of each other:
  45. login-serv => 1 - * char-serv, 1 - * map-serv
  46. In this case, * is 30. This means that 1 login-serv is able to manage up to
  47. 30 char-serv, which itself can manage up to 30 map-serv. Note that due to these
  48. aggregations, the login-serv and map-serv never directly communicate with each other.
  49. ===================================
  50. | 3. Interface and Communications |
  51. ===================================
  52. We have 3 types of communication:
  53. 1. serv <=> serv (AH,HA,HZ,ZH)
  54. This type of server-to-server communication is referred to as "inter-serv" communication.
  55. 2. serv <=> client (AC,CA,HC,CH,ZC,CZ)
  56. This is what our servs send or receive to a player client.
  57. 3. serv <=> console/terminal
  58. This is the only kind of communication which doesn't use packets (currently).
  59. It's only done in localhost from console to servs (a way to input args in servs runtime).
  60. The packet notation and structure are well defined in 'doc/packet_struct_notation.txt'.
  61. Note that scripts and atcommands are another kind of interface, as they allow
  62. users to input data into the serv.
  63. =================================
  64. | 4. Databases and Independence |
  65. =================================
  66. Each server can theoretically be set in a different host with its own databases
  67. associated (although this is currently broken due to years without documentation).
  68. In other words, you shouldn't expect to find char-serv data on a map-serv host
  69. and access it directly, but rather ask the char-serv to fetch it.
  70. The list below details the association of database tables with the servs.
  71. For real table names, see 'conf/inter_athena.conf'.
  72. ==============
  73. | Login-serv |
  74. ==============
  75. Table Contents
  76. ----- --------
  77. login_db all account-related information
  78. reg_db permanent account variables (ex. #CASHPOINTS)
  79. =============
  80. | Char-serv |
  81. =============
  82. Table Contents
  83. ----- --------
  84. char_db all char-related information
  85. hotkey_db hotkeys set for each character
  86. scdata_db character status at disconnection
  87. cart_db list of items in each character's cart
  88. inventory_db list of items in each character's inventory
  89. charlog_db char-serv logs
  90. storage_db list of items in each character's storage (Kafra)
  91. reg_db permanent character variables (ex. ADVJOB)
  92. skill_db character learned skill database
  93. interlog_db inter-serv logs
  94. memo_db character Memo_point database
  95. guild_db guild record (name, master, lv, exp, emblem, etc.)
  96. guild_alliance_db guild relations database (allies, enemies)
  97. guild_castle_db guild owned castle database
  98. guild_expulsion_db guild expulsion logs
  99. guild_member_db guild current member titles and positions
  100. guild_skill_db guild learned skills database
  101. guild_position_db guild positions configuration (names, taxes, rights)
  102. guild_storage_db guild item storage
  103. party_db party record (name, leader, shared_exp, shared_item)
  104. pet_db saved pet objects database
  105. friend_db character friends database
  106. mail_db mail database
  107. auction_db auction database
  108. quest_db character quest realisation database
  109. homunculus_db saved homunculus objects database
  110. skill_homunculus_db homunculus learned skills database
  111. mercenary_db saved mercenary objects database (HP, SP, level, etc.)
  112. mercenary_owner_db character proprietary link to mercenary object save and use stats
  113. elemental_db saved Elemental objects database (HP, SP, FLEE, etc.)
  114. ragsrvinfo_db map-serv rate record (similar to 'conf/battle/drop.conf', possibly a leftover?)
  115. skillcooldown_db character skill cooldowns at disconnection
  116. bonus_script_db character bonus_script at disconnection
  117. ============
  118. | Map-serv |
  119. ============
  120. Table Contents
  121. ----- --------
  122. mapreg_db permanent map-serv global variables (ex. $agit_result_timer)
  123. buyingstore_db live buyers database (map_pos, aid, shop title, etc.)
  124. buyingstore_items_db items currently being purchased by live buyers
  125. vending_db live vendors database (map_pos, aid, shop title, etc.)
  126. vending_items_db items currently being sold by live vendors
  127. The tables below are optional alternatives to TXT databases located in 'db/*.txt'.
  128. item_db item database (Pre-Renewal)
  129. item_db_re item database (Renewal)
  130. item_db2 item database import (Pre-Renewal)
  131. item_db2_re item database import (Renewal)
  132. item_cash_db cash shop database
  133. item_cash_db2 cash shop database (import)
  134. mob_db monster database (Pre-Renewal)
  135. mob_db_re monster database (Renewal)
  136. mob_db2 monster database import (Pre-Renewal)
  137. mob_db2_re monster database import (Renewal)
  138. mob_skill_db monster skill database (Pre-Renewal)
  139. mob_skill_db_re monster skill database (Renewal)
  140. mob_skill_db2 monster skill database import (Pre-Renewal)
  141. mob_skill_db2_re monster skill database import (Renewal)
  142. ==================================
  143. | 5. Package and Module Purposes |
  144. ==================================
  145. The following list describes each module and its purpose.
  146. ============
  147. | 3rdparty |
  148. ============
  149. The '3rdparty/' folder contains libraries used by the project but are not maintained by us.
  150. ==========
  151. | Common |
  152. ==========
  153. The 'src/common' folder contains all the modules which are used by more then 1 serv.
  154. Module Description
  155. ------ -----------
  156. cbasetypes adapter to OS and arch specification (function name, bit representation)
  157. cli console Line Interface handling (get arguments from terminal at beginning and runtime)
  158. conf facade of libconfig api
  159. core MAIN program entry (initialization of each serv starts here)
  160. db database module (create, parse, and destroy databases)
  161. des Data Encryption Standard algorithm modified for rAthena
  162. ers Entry Reusage System to help memory allocation
  163. grfio handles *.grf files (searches for files in them and decodes them)
  164. malloc handles runtime memory allocation (so that memory manager could check for leaks)
  165. mapindex handles the processing and reading of the mapcache.dat
  166. md5calc offers md5 encryption
  167. mmo.hpp common structures and defines across serv
  168. msg_conf handles msg in src from configuration
  169. nullpo checks and dumps info for debug mode
  170. random generation of random numbers
  171. showmsg display messages in console with a certain color
  172. socket handling of sockets (listening, close, open, etc.)
  173. sql.cpp MySQL database proxy
  174. strlib.cpp string handling
  175. timer.cpp timer-related functions
  176. utils.cpp misc functions
  177. winapi.hpp Windows redefine and include
  178. ==============
  179. | Login-serv |
  180. ==============
  181. Module Description
  182. ------ -----------
  183. account persistence for account data
  184. ipban offers IP banishment
  185. login main module of login-serv
  186. loginclif client <=> login-serv connections interface (send and receive packets to/from client)
  187. loginchrif char-serv <=> login-serv connections interface (send and receive packets to char-serv)
  188. logincsnlif console <=> login-serv connections interface (send and receive packets to/from console (internal buffer))
  189. loginlog records all operations into log for login-serv
  190. =============
  191. | Char-serv |
  192. =============
  193. The char-serv is responsible for persistence (save/load data permanently) and
  194. also serves as a controller that handles all associated map-servs. Further, it
  195. is responsible for ensuring that there are no duplicate party names among the
  196. map-servs (which could create conflicts if a party transfers map-servs).
  197. Module Description
  198. ------ -----------
  199. char currently holds all the char-serv (EA) process
  200. -- char_clif client <=> char-serv connections interface (send and receive packets to/from client)
  201. -- char_csnlif console <=> char-serv connections interface (send and receive packets to/from console (internal buffer))
  202. -- char_mapif map-serv <=> char-serv connections interface (send and receive packets to map-serv)
  203. -- char_logif login-serv <=> char-serv connections interface (send and receive packets to login-serv)
  204. inter main entry to inter-serv; delegates packet handling to submodules
  205. -- int_auction handles auction request and saving
  206. -- int_elemental handles elemental data (BL_ELE => Sorcerer mob)
  207. -- int_guild handles guild data (creation, destruction, add member, etc.)
  208. -- int_homun handles homunculus data (BL_HOM => Alchemist mob)
  209. -- int_mail handles mail data
  210. -- int_mercenary handles mercenary data (BL_MER => All class mob)
  211. -- int_party handles party data (creation, destruction, add member, etc.)
  212. -- int_pet handles pet data (BL_PET => All class mob)
  213. -- int_quest handles quest data
  214. -- int_storage handles storage data (save storage, load storage, etc.)
  215. ============
  216. | Map-serv |
  217. ============
  218. Module Description
  219. ------ -----------
  220. atcommand handles GM commands (ex. @who)
  221. battle handles damage calculation where target is enemy and battle configuration settings
  222. battleground functions for Battleground system (create, destroy, messaging, join, etc.)
  223. buyingstore functions for player Buying Stores (create, search, sell)
  224. cashshop functions to set up the server cashshop (from cashshop_db), and contains function to buy items from cashshop
  225. channel functions for the channel system (create, delete, join/auto-join, leave, broadcast, alter options)
  226. chat functions for the chatroom system (create, delete, trigger chatroom_event, change owner, etc.)
  227. chrif char-serv <=> map-serv connections interface (send and receive packets to char-serv)
  228. clif client <=> map-serv connections interface (send and receive packets to/from client)
  229. date functions for time
  230. duel functions for the duel system
  231. elemental functions for Sorcerer Elementals processing (create, delete, etc.)
  232. guild functions for the guild system
  233. homunculus functions for Alchemist Homunculi processing (create, delete, get stats, death, etc.)
  234. instance functions for instance system
  235. intif map-serv <=> inter-serv interface (meant to communicate with 'char/inter.cpp' or its submodules)
  236. itemdb functions for the item database
  237. log functions for server log system
  238. mail functions for mail system
  239. map map-serv main module, and a representation of a map object
  240. adds or removes other objects into map (blocklist) and provides iterators (ex. map_foreachpc)
  241. mapreg functions to save or read variables in mapreg_db (global variables for all map-serv)
  242. mercenary functions for Mercenary system (create, search, get stats, dead)
  243. mob functions for mob data, structures, and mob routines
  244. npc functions for NPC data (create, delete, calling NPCs)
  245. npc_chat functions for PCRE and NPC interaction
  246. party functions for the party system (create, join, delete, alter options, etc.)
  247. path functions for path finding (check_distance, search path unit will use)
  248. pc functions for player processing (loot/drop/delete items, player bonus handling, player dead, etc.)
  249. pc_groups functions for players groups system (manage player permissions and atcommand access)
  250. pet functions for the pet system (similar to mercenary, homunculus, player, etc.)
  251. quest functions for the quest log system (add, complete, remove, etc.)
  252. script handles script language logic (used in NPC scripts), script commands, and mapflags
  253. searchstore functions for the Vendor Shop Search feature
  254. skill functions for skills (skill_casttime calculation, skill behaviours, skill_chk_cast, requirement checks, 'db/skill_*.txt' processing)
  255. status functions for statuses on a bl (add, remove, calculation of effects as a temporary bonus)
  256. status is a struct available by most units as common attributes (bl_type only attribute are dealt in bl specific files, like 'pc.cpp' or 'mob.cpp')
  257. storage functions for the storage system: Kafra, cart, guild, inventory (add, transfer, remove items between containers)
  258. also ensures container mutex (e.g. guild_storage) and preparation for save requests
  259. trade functions to perform a trade (request, accept, add items/Zeny, checks, complete trade)
  260. unit functions for controlling player/mob/NPC actions (walk, follow, skill use)
  261. vending functions for Merchant Vending (create, purchase)
  262. ===================
  263. | 6. Nomenclature |
  264. ===================
  265. The following are standard naming conventions used by rAthena.
  266. Type Prefix Example
  267. ---- ------ -------
  268. function module_ pc_addspiritball -> located in pc.cpp file
  269. structure s_ s_quest_db
  270. enum e_ e_race
  271. status SC_ SC_INTOABYSS
  272. skill classmid_ AL_TELEPORT -> AL = Acolyte
  273. bonus SP_ SP_ATK_RATE
  274. NOTES:
  275. - If a status name conflicts with a skill name, another '_' is added (e.g. SC__WEAKNESS).
  276. - All constants should be written in all caps.
  277. - battle_config vs. #define macro:
  278. battle_config can be changed during runtime (ex. @setbattleflag), but this requires
  279. more processing and could render the server less stable than a macro would.
  280. =====================
  281. | 7. Variable Notes |
  282. =====================
  283. The following variables are commonly used in the source code.
  284. Variable Full Name Description
  285. -------- --------- -----------
  286. sd session data represents the session of a client into a serv (login, char, or map)
  287. tsd target sd same as sd, but for a target
  288. pl_sd usually in an iteration loop, the current sd of index
  289. it_sd a variant of pl_sd (for iter_sd)
  290. fd file descriptor a link to an I/O like a socket or file
  291. md mob data represents monster information; also used to represent mercenary information
  292. hd homunculus data represents homunculus information
  293. nd NPC data represents NPC information
  294. ed elemental data represents elemental information
  295. pd pet data represents pet information
  296. sc status change a structure containing all the possible status applied to a character
  297. tsc target sc same as sc, but for a target
  298. sce status change entry represents data of a specific inflicted status
  299. bl blocklist common data of one object (a skill, pet, player, etc); also represents a 2-way chain-link
  300. tbl target bl same as bl, but for a target
  301. st script stack the stack of an NPC
  302. aid account id a player account ID
  303. gid game id the general unique ID of a Unit, which is the aid for players
  304. (since a single character per account can be connected at one time)
  305. cid character id a player character ID
  306. rid character id a variant of cid
  307. su skill unit a skill with a unit that remains on the ground
  308. ===============
  309. | 8. Building |
  310. ===============
  311. When adding a new src file or library (new.cpp and its header, new.hpp), you'll also
  312. need to update the following files to fully integrate it into the project so that
  313. users can compile it.
  314. There are 3 ways to compile the project:
  315. > configure + makefile (requires POSIX environment + C compiler)
  316. This flow is mainly used by Linux users, but can be done in any POSIX environment (ex. Windows + Cygwin).
  317. - Configure.in: Template file to generate the configure script by autoconf.
  318. - Makefile.in: Template makefile to generate the real makefile according to configure. Each subfolder needs its own makefile.
  319. - Makefile: File filled with rules for gcc to compile folder.
  320. The sequence is as follows:
  321. 1) configure.in => configure by autoconf ('autoconf configure.in > configure')
  322. 2) configure => Makefile by Makefile.in
  323. 3) Makefile => binary by 'make all' or alternative
  324. > cmake (requires C compiler + cmake)
  325. - CmakeList: Comparable to Makefile, but in a more cross-OS way.
  326. The sequence is as follows:
  327. 1) Define which toolchain to use, acting like a configure ('cmake -G "Unix Makefiles"' or 'cmake -G "Visual Studio 10"')
  328. 2) Enter the build folder where the Makefiles are generated and launch 'make install' to produce binaries from them
  329. > sln (requires Visual Studio)
  330. - *.sln: Solution project for Visual Studio (Windows).
  331. See https://github.com/rathena/rathena/wiki/compiling for more detailed compilation instructions.
  332. ===================================
  333. | 9. Atcommands & Script Commands |
  334. ===================================
  335. To implement an atcommand or script command, you must define a function and
  336. add its reference to the appropriate array. See the files in 'src/custom/'
  337. for examples.
  338. Atcommands
  339. ----------
  340. ACMD_FUNC(name)
  341. {
  342. <code>
  343. }
  344. ACMD_DEF(name) - OR - ACMD_DEFR(name,restriction)
  345. - OR -
  346. ACMD_DEF2("alias",name) - OR - ACMD_DEF2R("alias",name,restriction)
  347. Restriction Description
  348. ----------- -----------
  349. 1 restrict usage in console
  350. 2 restrict usage in script_command
  351. Script Commands
  352. ---------------
  353. BUILDIN_FUNC(name)
  354. {
  355. <code>
  356. }
  357. BUILDIN_DEF(name,"arguments")
  358. - OR -
  359. BUILDIN_DEF2(name,"alias","arguments")
  360. Argument Description
  361. -------- -----------
  362. i integer
  363. s string
  364. v variable
  365. l label
  366. r reference (of a variable)
  367. ? optional parameter (one)
  368. * optional parameter (unknown count)
  369. null (no arguments)
  370. Useful functions:
  371. script_hasdata(st,i); // Returns if the stack contains data at the target index
  372. script_getdata(st,i); // Returns the script_data at the target index (data is a glob type)
  373. script_getnum(st,val); // Returns the int at the target index
  374. script_getstr(st,val); // Returns the string at the target index
  375. script_getref(st,val); // Returns the reference of a variable at the target index (useful for arrays, ex. 'checkweight2')
  376. script_getfuncname(st); // Returns the current function name (useful for function variants, ex. 'sc_start')
  377. script_pushint(st,val); // Pushes an int into the stack
  378. script_pushstr(st,val); // Pushes a string into the stack
  379. script_isstring(st,i); // Returns if the data at at the target index is a string
  380. script_isint(st,i); // Returns if the data at at the target index is an int