%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     离线托管-狂战领域
%%% @end
%%%-------------------------------------------------------------------
-module(offline_hosting_madness_land).
-author("liushl").

-include("def_module.hrl").
-include("server.hrl").
-include("scene.hrl").
-include("common.hrl").
-include("attr.hrl").
-include("role.hrl").
-include("figure.hrl").
-include("battle.hrl").
-include("madness_land.hrl").
%% API
-export([
    check_hosting/0,
    check_enter/1,
    end_hosting/0,
    enter_scene/1,
    host_behavior/3,
    when_lost_target/2
]).

%% 活动开始
check_hosting() ->
    offline_hosting:take_all_part_in(?MODULE, ?MOD_MADNESS_LAND, [], 5000).

check_enter(#ets_role_show{figure = #figure{lv = Lv} }) ->
    NeedLv = lib_module:get_open_lv(?MOD_MADNESS_LAND),
    Lv >= NeedLv.

%% 托管结束
end_hosting() ->
    offline_hosting:notice_logout(?MOD_MADNESS_LAND),
    ok.

%% 狂战玩家进入场景
enter_scene(Player) ->
    case offline_hosting_api:is_offline_hosting(Player) of
        true  ->
            {ok, Player1} = pp_scene:handle(12002, Player, []),
            {ok, Player2} = offline_hosting:host_behavior_delay(Player1, go_to_battle, [], 10000),
            Player2;
        false ->
            Player
    end.

%% 托管上线事件->进入场景后会回调
host_behavior(login, Player, _) ->
    madness_land:apply_info({ask_enter, Player#player_status.id, true}),
    {ok, Player};

%% 前往战斗
host_behavior(go_to_battle, Player, _) ->
    %% 前往战斗
    {ok, Player1} = go_to_battle(Player),
    {ok, Player1};
%% 战斗
host_behavior(battle, Player, _) ->
    {ok, Player1} = offline_hosting:host_behavior_battle(Player, 3),
    {ok, Player1};
%% 死亡后安全区复活-15秒
host_behavior(die, Player, _) ->
    {ok, Player1} = offline_hosting:host_behavior_delay(Player, revive, [], 15000),
    {ok, Player1};
%% 复活->前往战斗
host_behavior(revive, Player, _) ->
    %% 通知离开场景
    lib_scene:leave_scene(Player),
    [X, Y] = lib_scene:get_born_xy(Player#player_status.scene),
    Player0 = full_hp(Player),
    Player1 = Player0#player_status{x = X, y = Y},
    %% 加载场景
    {ok, Player2} = pp_scene:handle(12002, Player1, []),
    %% 前往战斗
    {ok, Player3} = go_to_battle(Player2),
    {ok, Player3};
host_behavior(_Behavior, Player, _Any) ->
    {ok, Player}.

%% 前往战斗
go_to_battle(Player) ->
    case data_scene:get(Player#player_status.scene) of
        #ets_scene{mon = [[_, X, Y, _, _]|_]} ->
            DestinationBehavior = battle,
            DestinationArg = [],
            {ok, Player1} = offline_hosting:host_behavior_move(Player, X, Y, DestinationBehavior, DestinationArg, 0),
            {ok, Player1};
        _ ->
            ?ERR("xy error:~p", [Player#player_status.scene]),
            {ok, Player}
    end.

%% 丢失了攻击目标
when_lost_target(EtsScene, User) ->
    #ets_scene_user{
        copy_id = CopyId, id = RoleId,
        x = X, y = Y, scene = SceneId,
        battle_attr = #battle_attr{group = Group}
    } = User,
    case madness_land_api:is_madness_land_scene(SceneId) of
        true  ->
            case lib_scene_calc:get_all_scene_trace(CopyId, X, Y, {Group, ?BATTLE_SIGN_PLAYER, RoleId, 0, 0}, undefined) of
                {Sign, Id, _, _, _} ->
                    offline_hosting_battle:user_target_battle(EtsScene, User, Sign, Id, 3);
                _ ->
                    ok
            end;
        false ->
            ok
    end.

full_hp(Player) ->
    #player_status{battle_attr = #battle_attr{hp_lim = Hp} = BA} = Player,
    Player#player_status{battle_attr = BA#battle_attr{hp = Hp}}.

