Global_Functions.txt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. //===== rAthena Script =======================================
  2. //= Global Functions
  3. //===== By: ==================================================
  4. //= Lupus, kobra_k88
  5. //===== Current Version: =====================================
  6. //= 2.23
  7. //===== Compatible With: =====================================
  8. //= rAthena Project
  9. //===== Description: =========================================
  10. //= General script functions.
  11. //===== Additional Comments: =================================
  12. //= Added F_ClearJobVar - on getting a new job it clears all Job Quest variables
  13. //= Removed individual job check functions as they were redundant [kobra_k88]
  14. //= 1.3 Added Job Change Function for Baby/Normal Classes
  15. //= 1.3b a fix, due to const.txt change [Lupus]
  16. //= 1.4 Added Is_Taekwon_Class: Taekwondo, Star Knight(Gladiator),Soul Linker [Lupus]
  17. //= 1.5 Added clear of Bard Quest variables. 1.5a updated [Lupus]
  18. //= 1.6 Added F_RandMes, F_SexMes, F_Hi, F_Bye functions [Lupus]
  19. //= Moved here useful function 'getJobName'
  20. //= 1.7 Added "F_ItemName" function, added a great sample for "F_ItemName"
  21. //= 1.8 Added Job Vars clear for Extended classes: TK,SL,SG [Lupus]
  22. //= 1.9 Added "F_ClearGarbage" func that clears outdated, wasted variables [Lupus]
  23. //= 1.91 Added "F_SaveQuestSkills","F_Load?Skills" functions [Lupus]
  24. //= 2.0 Fixed F_Save/LoadQuestSkills functions. Had to split in into 2 vars [Lupus]
  25. //= 2.01 Added $talk to F_ClearGarbage [Evera]
  26. //= 2.02 Added $dtsglobalelig and $dts|(1<<6) to F_ClearGarbage [Evera]
  27. //= 2.03 Added Gunslinger vars to F_ClearJobVar [Lupus]
  28. //= 2.04 Added dtseligible 2 (becomes MISC_QUEST|128) to F_ClearGarbage [Evera]
  29. //= 2.05 Book of Devil -> MISC_QUEST|256, clear other Niflheim garbage vars [Lupus]
  30. //= +more garbage clean
  31. //= 2.06 Pass to Bio-Lab -> MISC_QUEST|512 [Lupus]
  32. //= 2.07 Added TK_Q variable clear. 2.08 Added NINJ_Q variable clear [Lupus]
  33. //= 2.09 Cleared F_ClearGarbage [Lupus]
  34. //= 2.10 Added F_CheckMaxCount to check count of carrid items. [L0ne_w0lf]
  35. //= 2.10a Removed function F_CheckMaxCount. Not needed. [L0ne_W0lf]
  36. //= 2.11 Updated function Job_Change to jobchange based on Upper value. [Paradox924X]
  37. //= 2.12 Added more unused vars to F_ClearGarbage [Lupus]
  38. //= 2.13 Added more unused vars to F_ClearGarbage [Lupus]
  39. //= 2.14 Added old novice variables to F_ClearGarbage. [L0ne_W0lf]
  40. //= 2.15 Added no unused DTS variables to F_ClearGarbage. [L0ne_W0lf]
  41. //= 2.16 Added F_GetWeaponType, and F_GetArmorType. [L0ne_W0lf]
  42. //= 2.17 Renamed 'F_RandMes' to 'F_Rand'. [Euphy]
  43. //= 2.18 Removed useless 'getJobName' function. [Euphy]
  44. //= 2.2 Added "F_GetPlural", "F_InsertPlural", "F_InsertArticle", "F_InsertComma", "F_GetNumSuffix". [Euphy]
  45. //= Standardized descriptions, updated 'F_GetArmorType'.
  46. //= 2.21 Added format string to "F_InsertPlural" and more checks to "F_GetPlural". [Euphy]
  47. //= 2.22 Further improvements to "F_GetPlural". [Euphy]
  48. //= 2.23 Completed article function and added "F_GetArticle". [Euphy]
  49. //============================================================
  50. //////////////////////////////////////////////////////////////////////////////////
  51. // Clears job quest variables.
  52. // -- callfunc "F_ClearJobVar"
  53. //////////////////////////////////////////////////////////////////////////////////
  54. function script F_ClearJobVar {
  55. // Misc ---------------------------------
  56. set JBLVL,0;
  57. set FIRSTAID,0;
  58. set PLAYDEAD,0;
  59. set got_bandage,0;
  60. set got_novnametag,0;
  61. // First Class Jobs ---------------------
  62. set job_acolyte_q,0; set job_acolyte_q2,0;
  63. set job_archer_q,0;
  64. set job_magician_q,0;
  65. set job_merchant_q,0; set job_merchant_q2,0; set job_merchant_q3,0;
  66. set job_sword_q,0; set SWTEST, 0;
  67. set job_thief_q,0;
  68. // Super Novice
  69. set SUPNOV_Q,0;
  70. // 2-1 Jobs ------------------------------
  71. set ASSIN_Q,0; set ASSIN_Q2,0; set ASSIN_Q3,0;
  72. set BSMITH_Q,0; set BSMITH_Q2,0;
  73. set HNTR_Q,0; set HNTR_Q2,0;
  74. set KNIGHT_Q,0; set KNIGHT_Q2,0;
  75. set PRIEST_Q,0; set PRIEST_Q2,0; set PRIEST_Q3,0;
  76. set WIZ_Q,0; set WIZ_Q2,0;
  77. // 2-2 Jobs ------------------------------
  78. set ROGUE_Q,0; set ROGUE_Q2,0;
  79. set ALCH_Q,0; set ALCH_Q2,0;
  80. set CRUS_Q,0;
  81. set MONK_Q,0; set JOB_MONK_C,0;
  82. set SAGE_Q,0; set SAGE_Q2,0;
  83. set DANC_Q,0;
  84. set BARD_Q,0;
  85. // Extended Classes
  86. set TAEK_Q,0; set TK_Q,0;
  87. set STGL_Q,0;
  88. set SOUL_Q,0;
  89. set GUNS_Q,0;
  90. set NINJ_Q,0;
  91. return;
  92. }
  93. //////////////////////////////////////////////////////////////////////////////////
  94. // Clears other unused variables.
  95. // -- callfunc "F_ClearGarbage"
  96. //////////////////////////////////////////////////////////////////////////////////
  97. function script F_ClearGarbage {
  98. // Clear outdated global VARS
  99. //Clear TURTLE var if the Turtle Islans Quest is already done
  100. if(TURTLE==20) set MISC_QUEST,MISC_QUEST | 65536;
  101. if(MISC_QUEST & 65536) set TURTLE,0; //now clear the var if the quest is over
  102. //Clear previously saved Quest Skills (now it isn't used)
  103. set ADV_QSK,0;
  104. set ADV_QSK2,0;
  105. //Old Reset Skill Event used to use RES_SKILL var
  106. set RES_SKILL,0;
  107. //Here you put outdated variables from your outdated EVENTS
  108. //e.g. Is XMAS done? Add the EVENT var clearing code here.
  109. //due to bugs in BS quest:
  110. set wizard_m2,0;
  111. // Old Novice Ground Variables.
  112. set NEW_MES_FLAG0,0;
  113. set NEW_MES_FLAG1,0;
  114. set NEW_MES_FLAG2,0;
  115. set NEW_MES_FLAG3,0;
  116. set NEW_MES_FLAG4,0;
  117. set NEW_MES_FLAG5,0;
  118. set NEW_LVUP0,0;
  119. set NEW_LVUP1,0;
  120. set NEW_JOBLVUP,0;
  121. // Old DTS variables that are no longer used.
  122. set dtseligible,0;
  123. set MISC_QUEST,MISC_QUEST & ~128;
  124. return;
  125. }
  126. //////////////////////////////////////////////////////////////////////////////////
  127. // Job change based on Upper value (normal, advanced, baby).
  128. // -- callfunc "Job_Change",<Class value>;
  129. // See Job_* constants in 'db/const.txt' for Class values.
  130. //////////////////////////////////////////////////////////////////////////////////
  131. function script Job_Change {
  132. jobchange getarg(0),Upper;
  133. logmes "CLASS CHANGE: " +strcharinfo(0)+ " become a "+jobname(Class);
  134. return;
  135. }
  136. //////////////////////////////////////////////////////////////////////////////////
  137. // Functions used to spiff up dialoges [Lupus]
  138. //////////////////////////////////////////////////////////////////////////////////
  139. //////////////////////////////////////////////////////////////////////////////////
  140. // Returns a random argument.
  141. // -- callfunc "F_Rand",arg0,arg1,...
  142. // Example:
  143. // // You can use it to pick a random number from a list:
  144. // set @itemIDfromList, callfunc("F_Rand",1129,1222,1163,1357,1360,1522,1811,1410);
  145. //////////////////////////////////////////////////////////////////////////////////
  146. function script F_Rand {
  147. return getarg(rand(getargcount()));
  148. }
  149. //////////////////////////////////////////////////////////////////////////////////
  150. // Returns 1st argument if female, 2nd argument if male.
  151. // -- callfunc "F_Sex",argFemale,argMale
  152. // Example:
  153. // mes callfunc("F_Sex","What a beautiful lady!","What a handsome man!");
  154. //////////////////////////////////////////////////////////////////////////////////
  155. function script F_SexMes {
  156. return getarg(Sex);
  157. }
  158. //////////////////////////////////////////////////////////////////////////////////
  159. // Returns random "hello" and "goodbye" messages.
  160. // -- callfunc "F_Hi"
  161. // -- callfunc "F_Bye"
  162. //////////////////////////////////////////////////////////////////////////////////
  163. function script F_Hi {
  164. return callfunc("F_Rand","Hi!","Hello!","Good day!","How are you?","Hello there.");
  165. }
  166. function script F_Bye {
  167. return callfunc("F_Rand","Bye. See you again.","Later.","Goodbye.","Good luck!","Have a nice day!","Byebye!!!");
  168. }
  169. //////////////////////////////////////////////////////////////////////////////////
  170. // Returns expanded item name string.
  171. // -- callfunc "F_ItemName",<item ID>,{<element>{,<VVS meter 0..3>{,<refine rate>}}}
  172. // Elements: 0=none, 1=Ice, 2=Earth, 3=Fire, 4=Wind
  173. // Example:
  174. // // Displays "Show me your ^000090+5 VVS Fire Knife^000000..."
  175. // mes "Show me your "+callfunc("F_ItemName",1201,1,2,5)+"...";
  176. //////////////////////////////////////////////////////////////////////////////////
  177. function script F_ItemName {
  178. if (getarg(3,0))
  179. set .@t$,"+"+getarg(3)+" ";
  180. switch (getarg(2,0)) {
  181. case 0: break;
  182. case 1: set .@t$,.@t$+"VS "; break;
  183. case 2: set .@t$,.@t$+"VVS "; break;
  184. case 3: set .@t$,.@t$+"VVVS "; break;
  185. default: set .@t$,.@t$+getarg(2)+"xVS "; break;
  186. }
  187. switch (getarg(1,0)) {
  188. case 0: break;
  189. case 1: set .@t$,.@t$+"Ice "; break;
  190. case 2: set .@t$,.@t$+"Earth "; break;
  191. case 3: set .@t$,.@t$+"Fire "; break;
  192. case 4: set .@t$,.@t$+"Wind "; break;
  193. default: set .@t$,.@t$+"Strange "; break;
  194. }
  195. return "^000090"+.@t$+getitemname(getarg(0))+"^000000";
  196. }
  197. //////////////////////////////////////////////////////////////////////////////////
  198. // Stores learnt quest skills as variables ADV_QSK and ADV_QSK2.
  199. // -- callfunc "F_SaveQuestSkills"
  200. //
  201. // Restores learnt 1st class quest skills.
  202. // -- callfunc "F_Load1Skills"
  203. //
  204. // Restores learnt 2nd class quest skills.
  205. // -- callfunc "F_Load2Skills"
  206. //////////////////////////////////////////////////////////////////////////////////
  207. function script F_SaveQuestSkills {
  208. set ADV_QSK,0; set ADV_QSK2,0;
  209. //1st classes quest skills
  210. for(set .@i, 0; .@i < 14; set .@i, .@i + 1){
  211. if(getskilllv(144+.@i)) set ADV_QSK,ADV_QSK|pow(2,.@i);
  212. }
  213. //2nd classes quest skills
  214. for(set .@i, 0; .@i < 19; set .@i, .@i + 1){
  215. if(getskilllv(1001+.@i)) set ADV_QSK2,ADV_QSK2|pow(2,.@i);
  216. }
  217. return;
  218. }
  219. function script F_Load1Skills {
  220. for(set .@i, 0; .@i < 14; set .@i, .@i + 1){
  221. if(ADV_QSK|pow(2,.@i) == ADV_QSK) skill 144+.@i,1,0;
  222. }
  223. set ADV_QSK,0; //Clear var
  224. return;
  225. }
  226. function script F_Load2Skills {
  227. for(set .@i, 0; .@i < 19; set .@i, .@i + 1){
  228. if(ADV_QSK2|pow(2,.@i) == ADV_QSK2) skill 1001+.@i,1,0;
  229. }
  230. set ADV_QSK2,0; //Clear var
  231. return;
  232. }
  233. //////////////////////////////////////////////////////////////////////////////////
  234. // Returns weapon type, based on view.
  235. // -- callfunc "F_GetWeaponType",<weapon ID>
  236. //
  237. // Returns equipment type, based on equip location.
  238. // -- callfunc "F_GetArmorType",<item ID>
  239. //////////////////////////////////////////////////////////////////////////////////
  240. function script F_GetWeaponType {
  241. switch(getiteminfo(getarg(0),11)) {
  242. case 1: return "Dagger"; break;
  243. case 2: return "One-handed Sword"; break;
  244. case 3: return "Two-handed Sword"; break;
  245. case 4: return "One-handed Spear"; break;
  246. case 5: return "Two-handed Spear"; break;
  247. case 6: return "One-handed Axe"; break;
  248. case 7: return "Two-handed Axe"; break;
  249. case 8: return "Mace"; break;
  250. //case 9: return "Unused"; break;
  251. case 10: return "Staff"; break;
  252. case 11: return "Bow"; break;
  253. case 12: return "Knuckle"; break;
  254. case 13: return "Instrument"; break;
  255. case 14: return "Whip"; break;
  256. case 15: return "Book"; break;
  257. case 16: return "Katar"; break;
  258. case 17: return "Revolver"; break;
  259. case 18: return "Rifle"; break;
  260. case 19: return "Gatling gun"; break;
  261. case 20: return "Shotgun"; break;
  262. case 21: return "Grenade Launcher"; break;
  263. case 22: return "Shuriken"; break;
  264. default: return "Unknown Weapon"; break;
  265. }
  266. end;
  267. }
  268. function script F_GetArmorType {
  269. switch(getiteminfo(getarg(0),5)) {
  270. case 001: return "Lower Headgear"; break;
  271. case 2: return callfunc("F_GetWeaponType",getarg(0)); break;
  272. case 4: return "Garment"; break;
  273. case 8: return "Accessory"; break;
  274. case 16: return "Armor"; break;
  275. case 32: return "Shield"; break;
  276. case 64: return "Shoes"; break;
  277. case 128: return "Accessory"; break;
  278. case 136: return "Accessory"; break; //8+128
  279. case 256: return "Upper Headgear"; break;
  280. case 512: return "Middle Headgear"; break;
  281. case 1024: return "Costume Upper Headgear"; break;
  282. case 2048: return "Costume Midle Headgear"; break;
  283. case 4096: return "Costume Lower Headgear"; break;
  284. case 8192: return "Costume Garment"; break;
  285. case 32768: return "Ammo"; break;
  286. case 65536: return "Shadow Armor"; break;
  287. case 131072: return "Shadow Weapon"; break;
  288. case 262144: return "Shadow Shield"; break;
  289. case 524288: return "Shadow Shoes"; break;
  290. case 1048576: return "Shadow Accessory"; break;
  291. case 2097152: return "Shadow Accessory"; break;
  292. case 3145728: return "Shadow Accessory"; break; //1048576+2097152
  293. default: return "Unknown Equip"; break;
  294. }
  295. end;
  296. }
  297. //////////////////////////////////////////////////////////////////////////////////
  298. // Returns time remaining in a readable format.
  299. // The argument is subtracted from UNIX epoch time, or 'gettimetick(2)'.
  300. // -- callfunc "Time2Str",<tick in UNIX epoch time>
  301. // Example:
  302. // // Displays "Time left: 1 day, 1 hour, 8 minutes, 20 seconds"
  303. // set .@Timer, gettimetick(2) + 90500;
  304. // mes "Time left: " + callfunc("Time2Str",.@Timer);
  305. //////////////////////////////////////////////////////////////////////////////////
  306. function script Time2Str {
  307. set .@Time_Left, getarg(0) - gettimetick(2);
  308. set .@Days, .@Time_Left / 86400;
  309. set .@Time_Left, .@Time_Left - (.@Days * 86400);
  310. set .@Hours, .@Time_Left / 3600;
  311. set .@Time_Left, .@Time_Left - (.@Hours * 3600);
  312. set .@Minutes, .@Time_Left / 60;
  313. set .@Time_Left, .@Time_Left - (.@Minutes * 60);
  314. set .@Time$, "";
  315. if( .@Days > 1 )
  316. set .@Time$, .@Time$ + .@Days + " days, ";
  317. else if( .@Days > 0 )
  318. set .@Time$, .@Time$ + .@Days + " day, ";
  319. if( .@Hours > 1 )
  320. set .@Time$, .@Time$ + .@Hours + " hours, ";
  321. else if( .@Hours > 0 )
  322. set .@Time$, .@Time$ + .@Hours + " hour, ";
  323. if( .@Minutes > 1 )
  324. set .@Time$, .@Time$ + .@Minutes + " minutes, ";
  325. else if( .@Minutes > 0 )
  326. set .@Time$, .@Time$ + .@Minutes + " minute, ";
  327. if( .@Time_Left > 1 || .@Time_Left == 0 )
  328. set .@Time$, .@Time$ + .@Time_Left + " seconds";
  329. else if( .@Time_Left == 1 )
  330. set .@Time$, .@Time$ + .@Time_Left + " second";
  331. return .@Time$;
  332. }
  333. //////////////////////////////////////////////////////////////////////////////////
  334. // Functions for text manipulation [Euphy]
  335. //////////////////////////////////////////////////////////////////////////////////
  336. //////////////////////////////////////////////////////////////////////////////////
  337. // Returns the plural of a noun - works in most cases, but not for everything!
  338. // -- callfunc "F_GetPlural","<noun>"{,<0:normal/1:uppercase>}
  339. // Examples:
  340. // callfunc("F_GetPlural","dog") // returns "dogs"
  341. // callfunc("F_GetPlural","fox",1) // returns "FOXES"
  342. // callfunc("F_GetPlural","knife") // returns "knives"
  343. // callfunc("F_GetPlural","Piece of Cake") // returns "Pieces of Cake"
  344. //
  345. // Returns the plural of a noun if the given number is not 1.
  346. // -- callfunc "F_InsertPlural",<number>,"<noun>"{,<0:normal/1:uppercase>{,"<format string>"}}
  347. // Format string uses sprintf(), and MUST contain %d (arg0) and %s (arg1), in that order.
  348. // Examples:
  349. // callfunc("F_InsertPlural",1,"dog") // returns "1 dog"
  350. // callfunc("F_InsertPlural",3,"fox",1) // returns "3 FOXES"
  351. // // returns "^FF00005^000000 knives"
  352. // callfunc("F_InsertPlural",5,"knife",0,"^FF0000%d^000000 %s")
  353. //////////////////////////////////////////////////////////////////////////////////
  354. function script F_GetPlural {
  355. set .@str$, getarg(0);
  356. if (countstr(.@str$," ")) { // multiple words
  357. explode(.@tmp$,.@str$," ");
  358. set .@size, getarraysize(.@tmp$);
  359. // if format is "... of|in|on ...", pluralize preceding word; else pluralize last word
  360. if (compare(.@str$," of ") || compare(.@str$," in ") || compare(.@str$," on ")) {
  361. for (set .@i,1; .@i<.@size; set .@i,.@i+1) {
  362. if (getstrlen(.@tmp$[.@i]) == 2 && compare("of|in|on",.@tmp$[.@i]))
  363. break;
  364. set .@index, .@index + 1;
  365. }
  366. } else
  367. set .@index, .@size - 1;
  368. set .@str$, .@tmp$[.@index];
  369. set .@tmp$[.@index],"%s";
  370. set .@format$, implode(.@tmp$," ");
  371. } else
  372. set .@format$, "%s";
  373. set .@strlen, getstrlen(.@str$);
  374. if (.@strlen < 3) // prevent errors
  375. return ((getarg(1,0)) ? strtoupper(sprintf(.@format$,.@str$)) : sprintf(.@format$,.@str$));
  376. setarray .@suffix$[0], charat(.@str$,.@strlen - 1), substr(.@str$,.@strlen - 2,.@strlen - 1);
  377. if (!compare("abcdefghijklmnopqrstuvwxyz",.@suffix$[0])) { // last character is not a letter
  378. set .@result$, .@str$;
  379. }
  380. // common exceptions --> singular form == plural form
  381. else if (compare("fish|glasses|sunglasses|clothes|boots|shoes|greaves|sandals|wings|ears",.@str$)) {
  382. set .@result$, .@str$;
  383. }
  384. // ends in -s, -x, -z, -ch, -sh --> add -es
  385. else if (.@suffix$[0] == "s" || .@suffix$[0] == "x" || .@suffix$[0] == "z" ||
  386. .@suffix$[1] == "ch" || .@suffix$[1] == "sh") {
  387. set .@result$, .@str$ + "es";
  388. }
  389. // ends in -f, -fe --> remove -f, -fe --> add -ves
  390. else if ((.@suffix$[0] == "f" || .@suffix$[1] == "fe") && .@suffix$[1] != "ff") {
  391. if (compare("belief|cliff|chief|dwarf|grief|gulf|proof|roof",.@str$))
  392. set .@result$, .@str$ + "s"; // exceptions --> add -s
  393. else
  394. set .@result$, substr(.@str$,0,.@strlen - 2 - (.@suffix$[1] == "fe")) + "ves";
  395. }
  396. // ends in consonant + -y --> remove -y --> add -ies
  397. else if (.@suffix$[0] == "y" && !compare("aeiou",charat(.@suffix$[1],0))) {
  398. set .@result$, delchar(.@str$,.@strlen - 1) + "ies";
  399. }
  400. // ends in -o --> exceptions --> add -es
  401. else if (.@suffix$[0] == "o" &&
  402. compare("buffalo|domino|echo|grotto|halo|hero|mango|mosquito|potato|tomato|tornado|torpedo|veto|volcano",.@str$)) {
  403. set .@result$, .@str$ + "es";
  404. }
  405. // default --> add -s
  406. else {
  407. set .@result$, .@str$ + "s";
  408. }
  409. return ((getarg(1,0)) ? strtoupper(sprintf(.@format$,.@result$)) : sprintf(.@format$,.@result$));
  410. }
  411. function script F_InsertPlural {
  412. return sprintf(getarg(3,"%d %s"), getarg(0), ((getarg(0) == 1) ? getarg(1) : callfunc("F_GetPlural",getarg(1),getarg(2,0))));
  413. }
  414. //////////////////////////////////////////////////////////////////////////////////
  415. // Returns 'a' or 'an' based on a word.
  416. // -- callfunc "F_GetArticle","<noun>";
  417. // Examples:
  418. // callfunc("F_GetArticle","Apple") // returns "an"
  419. // callfunc("F_GetArticle","dog") // returns "a"
  420. //
  421. // Returns 'a' or 'an' based on a word, followed by the word.
  422. // -- callfunc "F_InsertArticle","<word>"{,<0:lowercase a/1:uppercase A>}
  423. // Examples:
  424. // callfunc("F_InsertArticle","Apple") // returns "an Apple"
  425. // callfunc("F_InsertArticle","dog",1) // returns "A dog"
  426. //////////////////////////////////////////////////////////////////////////////////
  427. function script F_GetArticle {
  428. set .@str$, strtolower(getarg(0));
  429. // not a word
  430. if (!charisalpha(.@str$,0))
  431. return "a";
  432. // 1-letter words
  433. if (getstrlen(.@str$) == 1) {
  434. if (strpos("aefhilmnorsx",.@str$) > -1)
  435. return "an";
  436. else
  437. return "a";
  438. }
  439. // special cases
  440. if (preg_match("(euler|hour(?!i)|heir|honest|hono)",.@str$))
  441. return "an";
  442. // consonants
  443. if (preg_match("^[^aeiouy]",.@str$))
  444. return "a";
  445. // special vowel forms
  446. if (preg_match("^e[uw]",.@str$) || preg_match("^onc?e\b",.@str$) || preg_match("^uni([^nmd]|mo)",.@str$) || preg_match("^u[bcfhjkqrst][aeiou]",.@str$))
  447. return "a";
  448. if (preg_match("^ut[th]",.@str$))
  449. return "an";
  450. // special capitals (rare)
  451. //if (preg_match("^U[NK][AIEO]?",getarg(0)))
  452. // return "a";
  453. // vowels
  454. if (preg_match("^[aeiou]",.@str$))
  455. return "an";
  456. // y... (rare)
  457. //if (preg_match("^(y(b[lor]|cl[ea]|fere|gg|p[ios]|rou|tt))",.@str$))
  458. // return "an";
  459. return "a";
  460. }
  461. function script F_InsertArticle {
  462. set .@article$, callfunc("F_GetArticle",getarg(0));
  463. return sprintf("%s %s", ((getarg(1,0)) ? replacestr(.@article$,"a","A") : .@article$), getarg(0));
  464. }
  465. //////////////////////////////////////////////////////////////////////////////////
  466. // Returns a number with commas between every three digits.
  467. // -- callfunc "F_InsertComma",<number>
  468. // Examples:
  469. // callfunc("F_InsertComma",7777777) // returns "7,777,777"
  470. //////////////////////////////////////////////////////////////////////////////////
  471. function script F_InsertComma {
  472. set .@str$, getarg(0);
  473. for (set .@i,getstrlen(.@str$)-3; .@i>0; set .@i,.@i-3)
  474. set .@str$, insertchar(.@str$,",",.@i);
  475. return .@str$;
  476. }
  477. //////////////////////////////////////////////////////////////////////////////////
  478. // Returns a number with a '-st', '-nd', '-rd', or '-th' suffix.
  479. // -- callfunc "F_GetNumSuffix",<number>
  480. // Examples:
  481. // callfunc("F_GetNumSuffix",1) // returns "1st"
  482. // callfunc("F_GetNumSuffix",11) // returns "11th"
  483. // callfunc("F_GetNumSuffix",32) // returns "32nd"
  484. //////////////////////////////////////////////////////////////////////////////////
  485. function script F_GetNumSuffix {
  486. set .@n, getarg(0);
  487. set .@mod, .@n % 10;
  488. if (.@mod == 1 && .@n != 11) return .@n+"st";
  489. else if (.@mod == 2 && .@n != 12) return .@n+"nd";
  490. else if (.@mod == 3 && .@n != 13) return .@n+"rd";
  491. else return .@n+"th";
  492. }