clan.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "clan.hpp"
  4. #include <cstring> //memset
  5. #include <common/cbasetypes.hpp>
  6. #include <common/malloc.hpp>
  7. #include <common/mmo.hpp>
  8. #include <common/nullpo.hpp>
  9. #include <common/showmsg.hpp>
  10. #include "battle.hpp"
  11. #include "clif.hpp"
  12. #include "instance.hpp"
  13. #include "intif.hpp"
  14. #include "log.hpp"
  15. #include "pc.hpp"
  16. #include "script.hpp"
  17. #include "status.hpp"
  18. static DBMap* clan_db; // int32 clan_id -> struct clan*
  19. void do_init_clan(){
  20. clan_db = idb_alloc(DB_OPT_RELEASE_DATA);
  21. }
  22. void do_final_clan(){
  23. db_destroy(clan_db);
  24. }
  25. void clan_load_clandata( int32 count, struct clan* clans ){
  26. int32 i,j;
  27. nullpo_retv( clans );
  28. for( i = 0, j = 0; i < count; i++, clans++ ){
  29. struct clan* clan = clans;
  30. struct clan* clanCopy;
  31. clanCopy = (struct clan*)aMalloc( sizeof( struct clan ) );
  32. if( clanCopy == nullptr ){
  33. ShowError("Memory could not be allocated for a clan.\n");
  34. break;
  35. }
  36. memcpy( clanCopy, clan, sizeof( struct clan ) );
  37. memset( clanCopy->members, 0, sizeof( clanCopy->members ) );
  38. idb_put( clan_db, clanCopy->id, clanCopy );
  39. j++;
  40. }
  41. ShowStatus( "Received '" CL_WHITE "%d" CL_RESET "' clans from char-server.\n", j );
  42. }
  43. struct clan* clan_search( int32 id ){
  44. return (struct clan*)idb_get(clan_db,id);
  45. }
  46. struct clan* clan_searchname( const char* name ){
  47. struct clan* c;
  48. DBIterator *iter = db_iterator(clan_db);
  49. for( c = (struct clan*)dbi_first(iter); dbi_exists(iter); c = (struct clan*)dbi_next(iter) ){
  50. if( strncmpi( c->name, name, NAME_LENGTH ) == 0 ){
  51. break;
  52. }
  53. }
  54. dbi_destroy(iter);
  55. return c;
  56. }
  57. map_session_data* clan_getavailablesd( struct clan& clan ){
  58. int32 i;
  59. ARR_FIND( 0, clan.max_member, i, clan.members[i] != nullptr );
  60. return ( i < clan.max_member ) ? clan.members[i] : nullptr;
  61. }
  62. int32 clan_getMemberIndex( struct clan* clan, uint32 account_id ){
  63. int32 i;
  64. nullpo_retr(-1,clan);
  65. ARR_FIND( 0, clan->max_member, i, clan->members[i] != nullptr && clan->members[i]->status.account_id == account_id );
  66. if( i == clan->max_member ){
  67. return -1;
  68. }else{
  69. return i;
  70. }
  71. }
  72. int32 clan_getNextFreeMemberIndex( struct clan* clan ){
  73. int32 i;
  74. nullpo_retr(-1,clan);
  75. ARR_FIND( 0, clan->max_member, i, clan->members[i] == nullptr );
  76. if( i == clan->max_member ){
  77. return -1;
  78. }else{
  79. return i;
  80. }
  81. }
  82. void clan_member_joined( map_session_data& sd ){
  83. if( sd.clan != nullptr ){
  84. clif_clan_basicinfo( sd );
  85. clif_clan_onlinecount( *sd.clan );
  86. return;
  87. }
  88. struct clan* clan = clan_search( sd.status.clan_id );
  89. if( clan == nullptr ){
  90. return;
  91. }
  92. int32 index = clan_getNextFreeMemberIndex( clan );
  93. if( index >= 0 ){
  94. sd.clan = clan;
  95. clan->members[index] = &sd;
  96. clan->connect_member++;
  97. clif_clan_basicinfo(sd);
  98. intif_clan_member_joined(clan->id);
  99. clif_clan_onlinecount( *clan );
  100. }
  101. }
  102. void clan_member_left( map_session_data& sd ){
  103. struct clan* clan = sd.clan;
  104. if( clan == nullptr ){
  105. return;
  106. }
  107. int32 index = clan_getMemberIndex( clan, sd.status.account_id );
  108. if( index >= 0 ){
  109. clan->members[index] = nullptr;
  110. clan->connect_member--;
  111. intif_clan_member_left(clan->id);
  112. clif_clan_onlinecount( *clan );
  113. }
  114. }
  115. bool clan_member_join( map_session_data& sd, int32 clan_id, uint32 account_id, uint32 char_id ){
  116. struct clan *clan = clan_search( clan_id );
  117. if( clan == nullptr ){
  118. return false;
  119. }
  120. if( sd.status.account_id != account_id || sd.status.char_id != char_id || sd.status.clan_id != 0 ){
  121. return false;
  122. }
  123. if( clan->instance_id > 0 && battle_config.instance_block_invite ){
  124. return false;
  125. }
  126. sd.status.clan_id = clan->id;
  127. clan_member_joined(sd);
  128. return true;
  129. }
  130. bool clan_member_leave( map_session_data& sd, int32 clan_id, uint32 account_id, uint32 char_id ){
  131. if( sd.status.account_id != account_id || sd.status.char_id != char_id || sd.status.clan_id != clan_id ){
  132. return false;
  133. }
  134. struct clan* clan = sd.clan;
  135. if( clan == nullptr ){
  136. return false;
  137. }
  138. if( clan->instance_id > 0 && battle_config.instance_block_leave ){
  139. return false;
  140. }
  141. clan_member_left(sd);
  142. sd.clan = nullptr;
  143. sd.status.clan_id = 0;
  144. clif_clan_leave(sd);
  145. return true;
  146. }
  147. void clan_recv_message( int32 clan_id, uint32 account_id, const char *mes, size_t len ){
  148. struct clan *clan = clan_search( clan_id );
  149. if( clan == nullptr ){
  150. return;
  151. }
  152. clif_clan_message( *clan, mes, len );
  153. }
  154. void clan_send_message( map_session_data& sd, const char *mes, size_t len ){
  155. if( sd.clan == nullptr ){
  156. return;
  157. }
  158. intif_clan_message( sd.status.clan_id, sd.status.account_id, mes, len );
  159. clan_recv_message( sd.status.clan_id, sd.status.account_id, mes, len );
  160. log_chat( LOG_CHAT_CLAN, sd.status.clan_id, sd.status.char_id, sd.status.account_id, mapindex_id2name( sd.mapindex ), sd.bl.x, sd.bl.y, nullptr, mes );
  161. }
  162. int32 clan_get_alliance_count( struct clan& clan, int32 flag ){
  163. int32 count = 0;
  164. for( int32 i = 0; i < MAX_CLANALLIANCE; i++ ){
  165. if( clan.alliance[i].clan_id > 0 && clan.alliance[i].opposition == flag ){
  166. count++;
  167. }
  168. }
  169. return count;
  170. }