mercenary.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/malloc.h"
  5. #include "../common/socket.h"
  6. #include "../common/timer.h"
  7. #include "../common/nullpo.h"
  8. #include "../common/mmo.h"
  9. #include "../common/random.h"
  10. #include "../common/showmsg.h"
  11. #include "../common/strlib.h"
  12. #include "../common/utils.h"
  13. #include "log.h"
  14. #include "clif.h"
  15. #include "chrif.h"
  16. #include "intif.h"
  17. #include "itemdb.h"
  18. #include "map.h"
  19. #include "pc.h"
  20. #include "status.h"
  21. #include "skill.h"
  22. #include "mob.h"
  23. #include "pet.h"
  24. #include "battle.h"
  25. #include "party.h"
  26. #include "guild.h"
  27. #include "atcommand.h"
  28. #include "script.h"
  29. #include "npc.h"
  30. #include "trade.h"
  31. #include "unit.h"
  32. #include "mercenary.h"
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <math.h>
  37. struct s_mercenary_db mercenary_db[MAX_MERCENARY_CLASS]; // Mercenary Database
  38. int merc_search_index(int class_)
  39. {
  40. int i;
  41. ARR_FIND(0, MAX_MERCENARY_CLASS, i, mercenary_db[i].class_ == class_);
  42. return (i == MAX_MERCENARY_CLASS)?-1:i;
  43. }
  44. bool merc_class(int class_)
  45. {
  46. return (bool)(merc_search_index(class_) > -1);
  47. }
  48. struct view_data * merc_get_viewdata(int class_)
  49. {
  50. int i = merc_search_index(class_);
  51. if( i < 0 )
  52. return 0;
  53. return &mercenary_db[i].vd;
  54. }
  55. int merc_create(struct map_session_data *sd, int class_, unsigned int lifetime)
  56. {
  57. struct s_mercenary merc;
  58. struct s_mercenary_db *db;
  59. int i;
  60. nullpo_retr(0,sd);
  61. if( (i = merc_search_index(class_)) < 0 )
  62. return 0;
  63. db = &mercenary_db[i];
  64. memset(&merc,0,sizeof(struct s_mercenary));
  65. merc.char_id = sd->status.char_id;
  66. merc.class_ = class_;
  67. merc.hp = db->status.max_hp;
  68. merc.sp = db->status.max_sp;
  69. merc.life_time = lifetime;
  70. // Request Char Server to create this mercenary
  71. intif_mercenary_create(&merc);
  72. return 1;
  73. }
  74. int mercenary_get_lifetime(struct mercenary_data *md)
  75. {
  76. const struct TimerData * td;
  77. if( md == NULL || md->contract_timer == INVALID_TIMER )
  78. return 0;
  79. td = get_timer(md->contract_timer);
  80. return (td != NULL) ? DIFF_TICK(td->tick, gettick()) : 0;
  81. }
  82. int mercenary_get_guild(struct mercenary_data *md)
  83. {
  84. int class_;
  85. if( md == NULL || md->db == NULL )
  86. return -1;
  87. class_ = md->db->class_;
  88. if( class_ >= 6017 && class_ <= 6026 )
  89. return ARCH_MERC_GUILD;
  90. if( class_ >= 6027 && class_ <= 6036 )
  91. return SPEAR_MERC_GUILD;
  92. if( class_ >= 6037 && class_ <= 6046 )
  93. return SWORD_MERC_GUILD;
  94. return -1;
  95. }
  96. int mercenary_get_faith(struct mercenary_data *md)
  97. {
  98. struct map_session_data *sd;
  99. int class_;
  100. if( md == NULL || md->db == NULL || (sd = md->master) == NULL )
  101. return 0;
  102. class_ = md->db->class_;
  103. if( class_ >= 6017 && class_ <= 6026 )
  104. return sd->status.arch_faith;
  105. if( class_ >= 6027 && class_ <= 6036 )
  106. return sd->status.spear_faith;
  107. if( class_ >= 6037 && class_ <= 6046 )
  108. return sd->status.sword_faith;
  109. return 0;
  110. }
  111. int mercenary_set_faith(struct mercenary_data *md, int value)
  112. {
  113. struct map_session_data *sd;
  114. int class_, *faith;
  115. if( md == NULL || md->db == NULL || (sd = md->master) == NULL )
  116. return 0;
  117. class_ = md->db->class_;
  118. if( class_ >= 6017 && class_ <= 6026 )
  119. faith = &sd->status.arch_faith;
  120. else if( class_ >= 6027 && class_ <= 6036 )
  121. faith = &sd->status.spear_faith;
  122. else if( class_ >= 6037 && class_ <= 6046 )
  123. faith = &sd->status.sword_faith;
  124. else
  125. return 0;
  126. *faith += value;
  127. *faith = cap_value(*faith, 0, SHRT_MAX);
  128. clif_mercenary_updatestatus(sd, SP_MERCFAITH);
  129. return 0;
  130. }
  131. int mercenary_get_calls(struct mercenary_data *md)
  132. {
  133. struct map_session_data *sd;
  134. int class_;
  135. if( md == NULL || md->db == NULL || (sd = md->master) == NULL )
  136. return 0;
  137. class_ = md->db->class_;
  138. if( class_ >= 6017 && class_ <= 6026 )
  139. return sd->status.arch_calls;
  140. if( class_ >= 6027 && class_ <= 6036 )
  141. return sd->status.spear_calls;
  142. if( class_ >= 6037 && class_ <= 6046 )
  143. return sd->status.sword_calls;
  144. return 0;
  145. }
  146. int mercenary_set_calls(struct mercenary_data *md, int value)
  147. {
  148. struct map_session_data *sd;
  149. int class_, *calls;
  150. if( md == NULL || md->db == NULL || (sd = md->master) == NULL )
  151. return 0;
  152. class_ = md->db->class_;
  153. if( class_ >= 6017 && class_ <= 6026 )
  154. calls = &sd->status.arch_calls;
  155. else if( class_ >= 6027 && class_ <= 6036 )
  156. calls = &sd->status.spear_calls;
  157. else if( class_ >= 6037 && class_ <= 6046 )
  158. calls = &sd->status.sword_calls;
  159. else
  160. return 0;
  161. *calls += value;
  162. *calls = cap_value(*calls, 0, INT_MAX);
  163. return 0;
  164. }
  165. int mercenary_save(struct mercenary_data *md)
  166. {
  167. md->mercenary.hp = md->battle_status.hp;
  168. md->mercenary.sp = md->battle_status.sp;
  169. md->mercenary.life_time = mercenary_get_lifetime(md);
  170. intif_mercenary_save(&md->mercenary);
  171. return 1;
  172. }
  173. static int merc_contract_end(int tid, unsigned int tick, int id, intptr_t data)
  174. {
  175. struct map_session_data *sd;
  176. struct mercenary_data *md;
  177. if( (sd = map_id2sd(id)) == NULL )
  178. return 1;
  179. if( (md = sd->md) == NULL )
  180. return 1;
  181. if( md->contract_timer != tid )
  182. {
  183. ShowError("merc_contract_end %d != %d.\n", md->contract_timer, tid);
  184. return 0;
  185. }
  186. md->contract_timer = INVALID_TIMER;
  187. merc_delete(md, 0); // Mercenary soldier's duty hour is over.
  188. return 0;
  189. }
  190. int merc_delete(struct mercenary_data *md, int reply)
  191. {
  192. struct map_session_data *sd = md->master;
  193. md->mercenary.life_time = 0;
  194. merc_contract_stop(md);
  195. if( !sd )
  196. return unit_free(&md->bl, CLR_OUTSIGHT);
  197. if( md->devotion_flag )
  198. {
  199. md->devotion_flag = 0;
  200. status_change_end(&sd->bl, SC_DEVOTION, INVALID_TIMER);
  201. }
  202. switch( reply )
  203. {
  204. case 0: mercenary_set_faith(md, 1); break; // +1 Loyalty on Contract ends.
  205. case 1: mercenary_set_faith(md, -1); break; // -1 Loyalty on Mercenary killed
  206. }
  207. clif_mercenary_message(sd, reply);
  208. return unit_remove_map(&md->bl, CLR_OUTSIGHT);
  209. }
  210. void merc_contract_stop(struct mercenary_data *md)
  211. {
  212. nullpo_retv(md);
  213. if( md->contract_timer != INVALID_TIMER )
  214. delete_timer(md->contract_timer, merc_contract_end);
  215. md->contract_timer = INVALID_TIMER;
  216. }
  217. void merc_contract_init(struct mercenary_data *md)
  218. {
  219. if( md->contract_timer == INVALID_TIMER )
  220. md->contract_timer = add_timer(gettick() + md->mercenary.life_time, merc_contract_end, md->master->bl.id, 0);
  221. md->regen.state.block = 0;
  222. }
  223. int merc_data_received(struct s_mercenary *merc, bool flag)
  224. {
  225. struct map_session_data *sd;
  226. struct mercenary_data *md;
  227. struct s_mercenary_db *db;
  228. int i = merc_search_index(merc->class_);
  229. if( (sd = map_charid2sd(merc->char_id)) == NULL )
  230. return 0;
  231. if( !flag || i < 0 )
  232. { // Not created - loaded - DB info
  233. sd->status.mer_id = 0;
  234. return 0;
  235. }
  236. db = &mercenary_db[i];
  237. if( !sd->md )
  238. {
  239. sd->md = md = (struct mercenary_data*)aCalloc(1,sizeof(struct mercenary_data));
  240. md->bl.type = BL_MER;
  241. md->bl.id = npc_get_new_npc_id();
  242. md->devotion_flag = 0;
  243. md->master = sd;
  244. md->db = db;
  245. memcpy(&md->mercenary, merc, sizeof(struct s_mercenary));
  246. status_set_viewdata(&md->bl, md->mercenary.class_);
  247. status_change_init(&md->bl);
  248. unit_dataset(&md->bl);
  249. md->ud.dir = sd->ud.dir;
  250. md->bl.m = sd->bl.m;
  251. md->bl.x = sd->bl.x;
  252. md->bl.y = sd->bl.y;
  253. unit_calc_pos(&md->bl, sd->bl.x, sd->bl.y, sd->ud.dir);
  254. md->bl.x = md->ud.to_x;
  255. md->bl.y = md->ud.to_y;
  256. map_addiddb(&md->bl);
  257. status_calc_mercenary(md,1);
  258. md->contract_timer = INVALID_TIMER;
  259. merc_contract_init(md);
  260. }
  261. else
  262. {
  263. memcpy(&sd->md->mercenary, merc, sizeof(struct s_mercenary));
  264. md = sd->md;
  265. }
  266. if( sd->status.mer_id == 0 )
  267. mercenary_set_calls(md, 1);
  268. sd->status.mer_id = merc->mercenary_id;
  269. if( md && md->bl.prev == NULL && sd->bl.prev != NULL )
  270. {
  271. map_addblock(&md->bl);
  272. clif_spawn(&md->bl);
  273. clif_mercenary_info(sd);
  274. clif_mercenary_skillblock(sd);
  275. }
  276. return 1;
  277. }
  278. void mercenary_heal(struct mercenary_data *md, int hp, int sp)
  279. {
  280. if( hp )
  281. clif_mercenary_updatestatus(md->master, SP_HP);
  282. if( sp )
  283. clif_mercenary_updatestatus(md->master, SP_SP);
  284. }
  285. int mercenary_dead(struct mercenary_data *md)
  286. {
  287. merc_delete(md, 1);
  288. return 0;
  289. }
  290. int mercenary_killbonus(struct mercenary_data *md)
  291. {
  292. const enum sc_type scs[] = { SC_MERC_FLEEUP, SC_MERC_ATKUP, SC_MERC_HPUP, SC_MERC_SPUP, SC_MERC_HITUP };
  293. int index = rnd() % ARRAYLENGTH(scs);
  294. sc_start(&md->bl,&md->bl, scs[index], 100, rnd() % 5, 600000);
  295. return 0;
  296. }
  297. int mercenary_kills(struct mercenary_data *md)
  298. {
  299. md->mercenary.kill_count++;
  300. md->mercenary.kill_count = cap_value(md->mercenary.kill_count, 0, INT_MAX);
  301. if( (md->mercenary.kill_count % 50) == 0 )
  302. {
  303. mercenary_set_faith(md, 1);
  304. mercenary_killbonus(md);
  305. }
  306. if( md->master )
  307. clif_mercenary_updatestatus(md->master, SP_MERCKILLS);
  308. return 0;
  309. }
  310. int mercenary_checkskill(struct mercenary_data *md, uint16 skill_id)
  311. {
  312. int i = skill_id - MC_SKILLBASE;
  313. if( !md || !md->db )
  314. return 0;
  315. if( md->db->skill[i].id == skill_id )
  316. return md->db->skill[i].lv;
  317. return 0;
  318. }
  319. static bool read_mercenarydb_sub(char* str[], int columns, int current)
  320. {
  321. int ele;
  322. struct s_mercenary_db *db;
  323. struct status_data *status;
  324. db = &mercenary_db[current];
  325. db->class_ = atoi(str[0]);
  326. safestrncpy(db->sprite, str[1], NAME_LENGTH);
  327. safestrncpy(db->name, str[2], NAME_LENGTH);
  328. db->lv = atoi(str[3]);
  329. status = &db->status;
  330. db->vd.class_ = db->class_;
  331. status->max_hp = atoi(str[4]);
  332. status->max_sp = atoi(str[5]);
  333. status->rhw.range = atoi(str[6]);
  334. status->rhw.atk = atoi(str[7]);
  335. status->rhw.atk2 = status->rhw.atk + atoi(str[8]);
  336. status->def = atoi(str[9]);
  337. status->mdef = atoi(str[10]);
  338. status->str = atoi(str[11]);
  339. status->agi = atoi(str[12]);
  340. status->vit = atoi(str[13]);
  341. status->int_ = atoi(str[14]);
  342. status->dex = atoi(str[15]);
  343. status->luk = atoi(str[16]);
  344. db->range2 = atoi(str[17]);
  345. db->range3 = atoi(str[18]);
  346. status->size = atoi(str[19]);
  347. status->race = atoi(str[20]);
  348. ele = atoi(str[21]);
  349. status->def_ele = ele%10;
  350. status->ele_lv = ele/20;
  351. if( status->def_ele >= ELE_MAX )
  352. {
  353. ShowWarning("Mercenary %d has invalid element type %d (max element is %d)\n", db->class_, status->def_ele, ELE_MAX - 1);
  354. status->def_ele = ELE_NEUTRAL;
  355. }
  356. if( status->ele_lv < 1 || status->ele_lv > 4 )
  357. {
  358. ShowWarning("Mercenary %d has invalid element level %d (max is 4)\n", db->class_, status->ele_lv);
  359. status->ele_lv = 1;
  360. }
  361. status->aspd_rate = 1000;
  362. status->speed = atoi(str[22]);
  363. status->adelay = atoi(str[23]);
  364. status->amotion = atoi(str[24]);
  365. status->dmotion = atoi(str[25]);
  366. return true;
  367. }
  368. int read_mercenarydb(void)
  369. {
  370. memset(mercenary_db,0,sizeof(mercenary_db));
  371. sv_readdb(db_path, "mercenary_db.txt", ',', 26, 26, MAX_MERCENARY_CLASS, &read_mercenarydb_sub);
  372. return 0;
  373. }
  374. static bool read_mercenary_skilldb_sub(char* str[], int columns, int current)
  375. {// <merc id>,<skill id>,<skill level>
  376. struct s_mercenary_db *db;
  377. int i, class_;
  378. uint16 skill_id, skill_lv;
  379. class_ = atoi(str[0]);
  380. ARR_FIND(0, MAX_MERCENARY_CLASS, i, class_ == mercenary_db[i].class_);
  381. if( i == MAX_MERCENARY_CLASS )
  382. {
  383. ShowError("read_mercenary_skilldb : Class %d not found in mercenary_db for skill entry.\n", class_);
  384. return false;
  385. }
  386. skill_id = atoi(str[1]);
  387. if( skill_id < MC_SKILLBASE || skill_id >= MC_SKILLBASE + MAX_MERCSKILL )
  388. {
  389. ShowError("read_mercenary_skilldb : Skill %d out of range.\n", skill_id);
  390. return false;
  391. }
  392. db = &mercenary_db[i];
  393. skill_lv = atoi(str[2]);
  394. i = skill_id - MC_SKILLBASE;
  395. db->skill[i].id = skill_id;
  396. db->skill[i].lv = skill_lv;
  397. return true;
  398. }
  399. int read_mercenary_skilldb(void)
  400. {
  401. sv_readdb(db_path, "mercenary_skill_db.txt", ',', 3, 3, -1, &read_mercenary_skilldb_sub);
  402. return 0;
  403. }
  404. int do_init_mercenary(void)
  405. {
  406. read_mercenarydb();
  407. read_mercenary_skilldb();
  408. //add_timer_func_list(mercenary_contract, "mercenary_contract");
  409. return 0;
  410. }
  411. int do_final_mercenary(void);