source_doc.txt 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. //===== rAthena Documentation ================================
  2. //= Source Documentation
  3. //===== By: ==================================================
  4. //= rAthena Dev Team
  5. //===== Last Updated: ========================================
  6. //= 20140215
  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. Except for mapreg_db, vending_db, and vending_items_db, the other map-serv
  121. tables are optional alternatives to TXT databases located in 'db/*.txt'.
  122. Table Contents
  123. ----- --------
  124. mapreg_db permanent map-serv global variables (ex. $agit_result_timer)
  125. vending_db live vendors database (map_pos, aid, shop title, etc.)
  126. vending_items_db items currently being sold by live vendors
  127. item_db item database (Pre-Renewal)
  128. item_db_re item database (Renewal)
  129. item_db2 item database import (Pre-Renewal)
  130. item_db2_re item database import (Renewal)
  131. item_cash_db cash shop database
  132. item_cash_db2 cash shop database (import)
  133. mob_db monster database (Pre-Renewal)
  134. mob_db_re monster database (Renewal)
  135. mob_db2 monster database import (Pre-Renewal)
  136. mob_db2_re monster database import (Renewal)
  137. mob_skill_db monster skill database (Pre-Renewal)
  138. mob_skill_db_re monster skill database (Renewal)
  139. mob_skill_db2 monster skill database import (Pre-Renewal)
  140. mob_skill_db2_re monster skill database import (Renewal)
  141. ==================================
  142. | 5. Package and Module Purposes |
  143. ==================================
  144. The following list describes each module and its purpose.
  145. ============
  146. | 3rdparty |
  147. ============
  148. The '3rdparty/' folder contains libraries used by the project but are not maintained by us.
  149. ==========
  150. | Common |
  151. ==========
  152. The 'src/common' folder contains all the modules which are used by more then 1 serv.
  153. Module Description
  154. ------ -----------
  155. atomic adapter to atomic operations for Windows API
  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. evdp handles events
  164. grfio handles *.grf files (searches for files in them and decodes them)
  165. malloc handles runtime memory allocation (so that memory manager could check for leaks)
  166. mapindex handles the processing and reading of the mapcache.dat
  167. md5calc offers md5 encryption
  168. mempool handles shared memory pool for thread support
  169. mmo.h common structures and defines across serv
  170. msg_conf handles msg in src from configuration
  171. mutex handles Mutual exclusion (Mutex) for thread support
  172. netbuffer iobuffer initialisation for thread support
  173. network updated socket module, with ipv6 and thread support
  174. nullpo checks and dumps info for debug mode
  175. raconf processes the conf files
  176. random generation of random numbers
  177. showmsg display messages in console with a certain color
  178. socket handling of sockets (listening, close, open, etc.)
  179. spinlock handles synchronization (waiting loop) for thread support
  180. sql.c MySQL database proxy
  181. strlib.c string handling
  182. thread.c thread creation and destruction
  183. timer.c timer-related functions
  184. utils.c misc functions
  185. winapi.h Windows redefine and include
  186. ==============
  187. | Login-serv |
  188. ==============
  189. Module Description
  190. ------ -----------
  191. account persistence for account data
  192. ipban offers IP banishment
  193. login main module of login-serv
  194. loginlog records all operations into log for login-serv
  195. =============
  196. | Char-serv |
  197. =============
  198. The char-serv is responsible for persistence (save/load data permanently) and
  199. also serves as a controller that handles all associated map-servs. Further, it
  200. is responsible for ensuring that there are no duplicate party names among the
  201. map-servs (which could create conflicts if a party transfers map-servs).
  202. Module Description
  203. ------ -----------
  204. char currently holds all the char-serv (EA) process
  205. inter main entry to inter-serv; delegates packet handling to submodules
  206. -- int_auction handles auction request and saving
  207. -- int_elemental handles elemental data (BL_ELE => Sorcerer mob)
  208. -- int_guild handles guild data (creation, destruction, add member, etc.)
  209. -- int_homun handles homunculus data (BL_HOM => Alchemist mob)
  210. -- int_mail handles mail data
  211. -- int_mercenary handles mercenary data (BL_MER => All class mob)
  212. -- int_party handles party data (creation, destruction, add member, etc.)
  213. -- int_pet handles pet data (BL_PET => All class mob)
  214. -- int_quest handles quest data
  215. -- int_storage handles storage data (save storage, load storage, etc.)
  216. ============
  217. | Map-serv |
  218. ============
  219. Module Description
  220. ------ -----------
  221. atcommand handles GM commands (ex. @who)
  222. battle handles damage calculation where target is enemy and battle configuration settings
  223. battleground functions for Battleground system (create, destroy, messaging, join, etc.)
  224. buyingstore functions for player Buying Stores (create, search, sell)
  225. cashshop functions to set up the server cashshop (from cashshop_db), and contains function to buy items from cashshop
  226. channel functions for the channel system (create, delete, join/auto-join, leave, broadcast, alter options)
  227. chat functions for the chatroom system (create, delete, trigger chatroom_event, change owner, etc.)
  228. chrif char-serv <=> map-serv connections interface (send and receive packets to char-serv)
  229. clif client <=> map-serv connections interface (send and receive packets to/from client)
  230. date functions for time
  231. duel functions for the duel system
  232. elemental functions for Sorcerer Elementals processing (create, delete, etc.)
  233. guild functions for the guild system
  234. homunculus functions for Alchemist Homunculi processing (create, delete, get stats, death, etc.)
  235. instance functions for instance system
  236. intif map-serv <=> inter-serv interface (meant to communicate with 'char/inter.c' or its submodules)
  237. itemdb functions for the item database
  238. log functions for server log system
  239. mail functions for mail system
  240. map map-serv main module, and a representation of a map object
  241. adds or removes other objects into map (blocklist) and provides iterators (ex. map_foreachpc)
  242. mapreg functions to save or read variables in mapreg_db (global variables for all map-serv)
  243. mercenary functions for Mercenary system (create, search, get stats, dead)
  244. mob functions for mob data, structures, and mob routines
  245. npc functions for NPC data (create, delete, calling NPCs)
  246. npc_chat functions for PCRE and NPC interaction
  247. party functions for the party system (create, join, delete, alter options, etc.)
  248. path functions for path finding (check_distance, search path unit will use)
  249. pc functions for player processing (loot/drop/delete items, player bonus handling, player dead, etc.)
  250. pc_groups functions for players groups system (manage player permissions and atcommand access)
  251. pet functions for the pet system (similar to mercenary, homunculus, player, etc.)
  252. quest functions for the quest log system (add, complete, remove, etc.)
  253. script handles script language logic (used in NPC scripts), script commands, and mapflags
  254. searchstore functions for the Vendor Shop Search feature
  255. skill functions for skills (skill_casttime calculation, skill behaviours, skill_chk_cast, requirement checks, 'db/skill_*.txt' processing)
  256. status functions for statuses on a bl (add, remove, calculation of effects as a temporary bonus)
  257. status is a struct available by most units as common attributes (bl_type only attribute are dealt in bl specific files, like 'pc.c' or 'mob.c')
  258. storage functions for the storage system: Kafra, cart, guild, inventory (add, transfer, remove items between containers)
  259. also ensures container mutex (e.g. guild_storage) and preparation for save requests
  260. trade functions to perform a trade (request, accept, add items/Zeny, checks, complete trade)
  261. unit functions for controlling player/mob/NPC actions (walk, follow, skill use)
  262. vending functions for Merchant Vending (create, purchase)
  263. ===================
  264. | 6. Nomenclature |
  265. ===================
  266. The following are standard naming conventions used by rAthena.
  267. Type Prefix Example
  268. ---- ------ -------
  269. function module_ pc_addspiritball -> located in pc.c file
  270. structure s_ s_quest_db
  271. enum e_ e_race
  272. status SC_ SC_INTOABYSS
  273. skill classmid_ AL_TELEPORT -> AL = Acolyte
  274. bonus SP_ SP_ATK_RATE
  275. NOTES:
  276. - If a status name conflicts with a skill name, another '_' is added (e.g. SC__WEAKNESS).
  277. - All constants should be written in all caps.
  278. - battle_config vs. #define macro:
  279. battle_config can be changed during runtime (ex. @setbattleflag), but this requires
  280. more processing and could render the server less stable than a macro would.
  281. =====================
  282. | 7. Variable Notes |
  283. =====================
  284. The following variables are commonly used in the source code.
  285. Variable Full Name Description
  286. -------- --------- -----------
  287. sd session data represents the session of a client into a serv (login, char, or map)
  288. tsd target sd same as sd, but for a target
  289. pl_sd usually in an iteration loop, the current sd of index
  290. it_sd a variant of pl_sd (for iter_sd)
  291. fd file descriptor a link to an I/O like a socket or file
  292. md mob data represents monster information; also used to represent mercenary information
  293. hd homunculus data represents homunculus information
  294. nd NPC data represents NPC information
  295. ed elemental data represents elemental information
  296. pd pet data represents pet information
  297. sc status change a structure containing all the possible status applied to a character
  298. tsc target sc same as sc, but for a target
  299. sce status change entry represents data of a specific inflicted status
  300. bl blocklist common data of one object (a skill, pet, player, etc); also represents a 2-way chain-link
  301. tbl target bl same as bl, but for a target
  302. st script stack the stack of an NPC
  303. aid account id a player account ID
  304. gid game id the general unique ID of a Unit, which is the aid for players
  305. (since a single character per account can be connected at one time)
  306. cid character id a player character ID
  307. rid character id a variant of cid
  308. su skill unit a skill with a unit that remains on the ground
  309. ===============
  310. | 8. Building |
  311. ===============
  312. When adding a new src file or library (new.c and its header, new.h), you'll also
  313. need to update the following files to fully integrate it into the project so that
  314. users can compile it.
  315. There are 3 ways to compile the project:
  316. > configure + makefile (requires POSIX environment + C compiler)
  317. This flow is mainly used by Linux users, but can be done in any POSIX environment (ex. Windows + Cygwin).
  318. - Configure.in: Template file to generate the configure script by autoconf.
  319. - Makefile.in: Template makefile to generate the real makefile according to configure. Each subfolder needs its own makefile.
  320. - Makefile: File filled with rules for gcc to compile folder.
  321. The sequence is as follows:
  322. 1) configure.in => configure by autoconf ('autoconf configure.in > configure')
  323. 2) configure => Makefile by Makefile.in
  324. 3) Makefile => binary by 'make all' or alternative
  325. > cmake (requires C compiler + cmake)
  326. - CmakeList: Comparable to Makefile, but in a more cross-OS way.
  327. The sequence is as follows:
  328. 1) Define which toolchain to use, acting like a configure ('cmake -G "Unix Makefiles"' or 'cmake -G "Visual Studio 10"')
  329. 2) Enter the build folder where the Makefiles are generated and launch 'make install' to produce binaries from them
  330. > sln (requires Visual Studio)
  331. - *.sln: Solution project for Visual Studio (Windows).
  332. See http://rathena.org/wiki/Compiling for more detailed compilation instructions.
  333. ===================================
  334. | 9. Atcommands & Script Commands |
  335. ===================================
  336. To implement an atcommand or script command, you must define a function and
  337. add its reference to the appropriate array. See the files in 'src/custom/'
  338. for examples.
  339. Atcommands
  340. ----------
  341. ACMD_FUNC(name)
  342. {
  343. <code>
  344. }
  345. ACMD_DEF(name) - OR - ACMD_DEFR(name,restriction)
  346. - OR -
  347. ACMD_DEF2("alias",name) - OR - ACMD_DEF2R("alias",name,restriction)
  348. Restriction Description
  349. ----------- -----------
  350. 1 restrict usage in console
  351. 2 restrict usage in script_command
  352. Script Commands
  353. ---------------
  354. BUILDIN_FUNC(name)
  355. {
  356. <code>
  357. }
  358. BUILDIN_DEF(name,"arguments")
  359. - OR -
  360. BUILDIN_DEF2(name,"alias","arguments")
  361. Argument Description
  362. -------- -----------
  363. i integer
  364. s string
  365. v variable
  366. l label
  367. r reference (of a variable)
  368. ? optional parameter (one)
  369. * optional parameter (unknown count)
  370. null (no arguments)
  371. Useful functions:
  372. script_hasdata(st,i); // Returns if the stack contains data at the target index
  373. script_getdata(st,i); // Returns the script_data at the target index (data is a glob type)
  374. script_getnum(st,val); // Returns the int at the target index
  375. script_getstr(st,val); // Returns the string at the target index
  376. script_getref(st,val); // Returns the reference of a variable at the target index (useful for arrays, ex. 'checkweight2')
  377. script_getfuncname(st); // Returns the current function name (useful for function variants, ex. 'sc_start')
  378. script_pushint(st,val); // Pushes an int into the stack
  379. script_pushstr(st,val); // Pushes a string into the stack
  380. script_isstring(st,i); // Returns if the data at at the target index is a string
  381. script_isint(st,i); // Returns if the data at at the target index is an int