%% ---------------------------------------------------------------------------
%% @doc 机器人定制
%% @author hek
%% @since  2016-09-14
%% @deprecated 本模块用于定制机器人特殊逻辑
%% ---------------------------------------------------------------------------
-module(lib_robot).
-export([
        is_robot/1                  %% 是否机器人
        ,trans_skill_id/2           %% 技能Id替换
        ,get_battle_object/2        %% 获取战斗对象
        ,get_collect_object/2       %% 获取采集对象
        ,task_send_equip/2          %% 帮角色穿上任务送的装备
    ]).
-include("scene.hrl").
-include("battle.hrl").
-include("common.hrl").
-include("figure.hrl").
-include("skill.hrl").
-include("goods.hrl").
-include("server.hrl").
-include("task.hrl").
-include("predefine.hrl").
-include("def_goods.hrl").
-define(ROBOT_SOURCE, <<"robot">>).

%%----------------------------------------------------------------------------
%% 是否机器人
%%----------------------------------------------------------------------------
-ifdef(DEV_SERVER).

is_robot(#player_status{source = ?ROBOT_SOURCE}) -> true;
is_robot(#ets_scene_user{source = ?ROBOT_SOURCE}) -> true;
is_robot(#task_args{source = ?ROBOT_SOURCE}) -> true;
is_robot(_) -> false.
-else.
is_robot(_) -> false.
-endif.

%%----------------------------------------------------------------------------
%% 技能Id替换
%%----------------------------------------------------------------------------
trans_skill_id(PS, SkillId) ->
    #player_status{figure = #figure{career = Career}} = PS,
    case lib_robot:is_robot(PS) of
        true -> trans_skill_id_core(Career, SkillId);
        _ -> SkillId
    end.

%% 剑士
trans_skill_id_core(?SWORDSMAN, SkillId) -> SkillId;
%% 剑士普攻 - 骑士普攻
trans_skill_id_core(?KNIGHT, 101001) -> 102001;
trans_skill_id_core(?KNIGHT, 101002) -> 102002;
trans_skill_id_core(?KNIGHT, 101003) -> 102003;
trans_skill_id_core(?KNIGHT, 101004) -> 102004;
%% 剑士2技能 - 骑士4技能
trans_skill_id_core(?KNIGHT, 101011) -> 102031;
trans_skill_id_core(?KNIGHT, 101012) -> 102032;
trans_skill_id_core(?KNIGHT, 101013) -> 102033;
%% 剑士3技能 - 骑士5技能
trans_skill_id_core(?KNIGHT, 101021) -> 102041;
trans_skill_id_core(?KNIGHT, 101022) -> 102042;
trans_skill_id_core(?KNIGHT, 101023) -> 102042;
%% 剑士4技能 - 骑士2技能
trans_skill_id_core(?KNIGHT, 101031) -> 102011;
%% 剑士5技能 - 骑士3技能
trans_skill_id_core(?KNIGHT, 101041) -> 102021;
trans_skill_id_core(?KNIGHT, 101042) -> 102022;
trans_skill_id_core(?KNIGHT, 101043) -> 102023;
trans_skill_id_core(?KNIGHT, 101044) -> 102024;

%% 剑士普攻 - 炮手普攻
trans_skill_id_core(?GUNNER, 101001) -> 104001;
trans_skill_id_core(?GUNNER, 101002) -> 104002;
trans_skill_id_core(?GUNNER, 101003) -> 104003;
trans_skill_id_core(?GUNNER, 101004) -> 104004;
%% 剑士2技能 - 炮手2技能
trans_skill_id_core(?GUNNER, 101011) -> 104011;
trans_skill_id_core(?GUNNER, 101012) -> 104012;
trans_skill_id_core(?GUNNER, 101013) -> 104013;
%% 剑士3技能 - 骑士5技能
trans_skill_id_core(?GUNNER, 101021) -> 104041;
trans_skill_id_core(?GUNNER, 101022) -> 104042;
trans_skill_id_core(?GUNNER, 101023) -> 104043;
%% 剑士4技能 - 骑士3技能
trans_skill_id_core(?GUNNER, 101031) -> 104021;
%% 剑士5技能 - 骑士4技能
trans_skill_id_core(?GUNNER, 101041) -> 104031;
trans_skill_id_core(?GUNNER, 101042) -> 104032;
trans_skill_id_core(?GUNNER, 101043) -> 104033;
trans_skill_id_core(?GUNNER, 101044) -> 104034;
trans_skill_id_core(_, SkillId) -> SkillId.


%%----------------------------------------------------------------------------
%% 获取战斗对象
%%----------------------------------------------------------------------------
get_battle_object(AttKey, BattleArgs) ->
    #battle_args{mon_list = NMonList, player_list = NPlayerList} = BattleArgs,
    AttUser = lib_scene_agent:get_user(AttKey),
    case lib_robot:is_robot(AttUser) of
        true ->
            get_battle_object_core(AttUser, BattleArgs);
        _ -> {NMonList, NPlayerList}
    end.

get_battle_object_core(AttUser, BattleArgs) ->
    #ets_scene_user{copy_id = CopyId, x = X, y = Y} = AttUser,
    #battle_args{skill_id=SkillId, skill_lv=SkillLv, player_list = PlayerList} = BattleArgs,
    AllObject = get_all_area_object(AttUser),
    MonList = [ObjectId||#scene_object{id = ObjectId, is_be_atted = IsBeAtted} <-AllObject, IsBeAtted==1],
    case PlayerList =/= [] of
        true ->
            UserList = lib_scene_agent:get_scene_user(CopyId, X, Y, 300),
            NewPlayerList = [PlayerId||#ets_scene_user{id = PlayerId} <-UserList];
        false -> NewPlayerList = []
    end,
    SR = data_skill:get(SkillId, SkillLv),
    select_att_num_der(AttUser, SR, ?BATTLE_SIGN_PLAYER, MonList, NewPlayerList).

%% 先计算战斗人数
select_att_num_der(_User, SR, ?BATTLE_SIGN_PLAYER, MonList, PlayerList)->
    #skill{lv_data=#skill_lv{num = [PNum, MNum]}} = SR,
    {
        lists:sublist(MonList, min(MNum, ?MAX_ATTACK_NUM)),
        lists:sublist(PlayerList, min(PNum, ?MAX_ATTACK_NUM))
    };
select_att_num_der(_User, _SR, _Sign, MonList, PlayerList) ->
    if
        PlayerList =/= [] -> [PId|_] = PlayerList, {[], [PId]};
        MonList =/= [] -> [MId|_] = MonList, {[MId], []};
        true -> {[], []}
    end.

%%----------------------------------------------------------------------------
%% 获取采集对象
%%----------------------------------------------------------------------------
get_collect_object(AttKey, MonId) ->
    AttUser = lib_scene_agent:get_user(AttKey),
    case {is_record(AttUser, ets_scene_user), lib_robot:is_robot(AttUser)} of
        {true, true} ->
            AllObject = get_all_area_object(AttUser),
            MonList = [ObjectId|| #scene_object{id = ObjectId, mon = #scene_mon{kind = Kind}}<-AllObject,
                Kind == ?MON_KIND_COLLECT orelse Kind == ?MON_KIND_TASK_COLLECT],
            case MonList of
                [ObjectId|_] -> ObjectId;
                _ -> MonId
            end;
        _ -> MonId
    end.

get_all_area_object(AttUser) ->
    #ets_scene_user{copy_id = CopyId, x = X, y = Y} = AttUser,
    AllArea = lib_scene_calc:get_the_area(X, Y),
    lib_scene_object_agent:get_all_area_object(AllArea, CopyId).

%%----------------------------------------------------------------------------
%% 帮角色穿上任务送的装备
%%----------------------------------------------------------------------------
task_send_equip(PS, TaskAwards) ->
    case lib_robot:is_robot(PS) of
        true ->
            EquipIdList = find_equip(TaskAwards),
            case EquipIdList of
                [] -> PS;
                _ -> put_on_equip_list(PS, EquipIdList)
            end;
        _ -> PS
    end.

%% 检查任务奖励里面是否有装备
find_equip(TaskAwards) ->
    find_equip(TaskAwards, []).

find_equip([], EquipIdList) -> EquipIdList;
find_equip([{Type, GoodsId, _Num} | T], EquipIdList)
when Type == ?TYPE_GOODS orelse Type == ?TYPE_BIND_GOODS->
    case data_goods_type:get(GoodsId) of
        #ets_goods_type{goods_id = GoodsId, type = ?GOODS_TYPE_EQUIP} ->
            find_equip(T, [GoodsId | EquipIdList]);
        _ ->
            find_equip(T, EquipIdList)
    end;
find_equip([_OtherAward | T], EquipIdList) ->
    find_equip(T, EquipIdList).

%% 任务穿戴装备
put_on_equip_list(PS, []) -> PS;
put_on_equip_list(PS, [EquipId | T]) ->
    NewPS = put_on_equip(PS, EquipId),
    put_on_equip_list(NewPS, T).

put_on_equip(PS, EquipTypeId) ->
    GoodsStatus = lib_goods_do:get_goods_status(),
    [GoodsInfo | _] = lib_goods_util:get_type_goods_list(
        PS#player_status.id, EquipTypeId, ?GOODS_LOC_BAG, GoodsStatus#goods_status.dict, PS#player_status.figure#figure.lv),
    #goods{id = EquipId} = GoodsInfo,
    case pp_equip:handle(15201, PS, [EquipId]) of
        {ok, equip, NewPS} ->
            mod_scene_agent:update(NewPS, [{battle_attr, NewPS#player_status.battle_attr}]),
            NewPS;
        _ -> PS
    end.

