/*
        ====================================================================
        Set the deploy mask for this unit. If 'init', use the initial deploy
        mask (or a default one). If not, set the valid deploy centers. In a
        second run, remove any tile blocked by an own unit if 'unit' is set.
        ====================================================================
        */
        public void map_get_deploy_mask(Player player, Unit unit, bool init)
        {
            int x, y;

            map_clear_mask (MAP_MASK.F_DEPLOY);
            if (init)
                map_set_initial_deploy_mask (player);
            else
                map_add_deploy_centers_to_deploy_mask (player, unit);
            if (unit != null) {
                for (x = 0; x < map_w; x++)
                    for (y = 0; y < map_h; y++)
                        if ((unit.sel_prop.flags & UnitFlags.FLYING) == UnitFlags.FLYING) {
                            if (map [x, y].a_unit != null)
                                mask [x, y].deploy = false;
                        } else {
                            if (map [x, y].g_unit != null && (!init || !map_check_unit_embark (unit, x, y, UnitEmbarkTypes.EMBARK_AIR, true)))
                                mask [x, y].deploy = false;
                        }
            }
        }
 public MountCtx(Player pl, int x, int y, bool u)
 {
     player = pl;
     unit_x = x;
     unit_y = y;
     unsafe_ = u;
 }
 /*
 ====================================================================
 Check whether (mx, my) can serve as a deploy center for the given
 unit (assuming it is close to it, which is not checked). If no unit
 is given, check whether it is any
 ====================================================================
 */
 bool map_check_deploy_center(Player player, Unit unit, int mx, int my)
 {
     if (map [mx, my].deploy_center != 1 || map [mx, my].player != null)
         return false;
     if (!Player.player_is_ally (map [mx, my].player, player))
         return false;
     if (unit != null) {
         if (Terrain.GetMovementCost (map [mx, my].terrain, unit.sel_prop.mov_type, Scenario.cur_weather) == 0)
             return false;
         if ((unit.sel_prop.flags & UnitFlags.FLYING) == UnitFlags.FLYING)
         if ((map [mx, my].terrain.flags [Scenario.cur_weather] & Terrain_flags.SUPPLY_AIR) != Terrain_flags.SUPPLY_AIR)
             return false;
         if (map [mx, my].nation != unit.nation)
             return false;
     }
     return true;
 }
        /*
        ====================================================================
        Set deploy mask by player's field list. If first entry is (-1,-1),
        create a default mask, using the initial layout of spotting and
        units.
        ====================================================================
        */
        void map_set_initial_deploy_mask(Player player)
        {
            int i = Player.player_get_index (player);
            List<MapCoord> field_list;

            if (deploy_fields == null)
                return;
            /*
            list_reset(deploy_fields);
            while ((field_list = list_next(deploy_fields)) && i--) ;
            */
            field_list = deploy_fields [i];
            if (field_list == null)
                return;

            int j = 0;
            while (j < field_list.Count) {
                MapCoord pt = field_list [j];
                Mask_Tile tile = map_mask_tile (pt.x, pt.y);
                if (tile == null) {
                    field_list.Remove (pt);
                    j--;
                    map_add_default_deploy_fields (player, field_list);
                } else {
                    tile.deploy = true;
                    tile.spot = true;
                }
                j++;
            }
        }
 void map_add_default_deploy_fields(Player player, List<MapCoord> fields)
 {
     int i, j, next_x, next_y;
     bool okay;
     foreach (Unit unit in Scenario.units) {
         if (unit.player == player && unit.SupportsDeploy ()) {
             for (i = 0; i < 6; i++)
                 if (Misc.get_close_hex_pos (unit.x, unit.y, i, out next_x, out next_y)) {
                     okay = true;
                     int x, y;
                     for (j = 0; j < 6; j++)
                         if (Misc.get_close_hex_pos (next_x, next_y, j, out x, out y))
                         if (!mask [x, y].spot ||
                                 (map [x, y].a_unit != null && !Player.player_is_ally (player, map [x, y].a_unit.player)) ||
                                 (map [x, y].g_unit != null && !Player.player_is_ally (player, map [x, y].g_unit.player))) {
                             okay = false;
                             break;
                         }
                     if ((map [next_x, next_y].terrain.flags [Scenario.cur_weather] & Terrain_flags.RIVER) == Terrain_flags.RIVER)
                         okay = false;
                     mask [next_x, next_y].deploy = okay;
                 }
         }
     }
     foreach (Unit unit in Scenario.units) {
         /* make sure all units can be re-deployed */
         if (unit.player == player && unit.SupportsDeploy ())
             mask [unit.x, unit.y].deploy = true;
     }
     map_add_deploy_centers_to_deploy_mask (player, null);
     for (short x = 0; x < map_w; x++)
         for (short y = 0; y < map_h; y++)
             if (mask [x, y].deploy)
                 fields.Add (new MapCoord (x, y));
 }
 /*
 ====================================================================
 Add any deploy center and its surrounding to the deploy mask, if it
 can supply 'unit'. If 'unit' is not set, add any deploy center.
 ====================================================================
 */
 void map_add_deploy_centers_to_deploy_mask(Player player, Unit unit)
 {
     int x, y, i, next_x, next_y;
     for (x = 0; x < map_w; x++)
         for (y = 0; y < map_h; y++)
             if (map_check_deploy_center (player, unit, x, y)) {
                 mask [x, y].deploy = true;
                 for (i = 0; i < 6; i++)
                     if (Misc.get_close_hex_pos (x, y, i, out next_x, out next_y))
                         mask [next_x, next_y].deploy = true;
             }
 }
        /*
        ====================================================================
        Begin turn of next player. Therefore select next player or use
        'forced_player' if not NULL (then the next is the one after
        'forced_player').
        If 'skip_unit_prep' is set scen_prep_unit() is not called.
        ====================================================================
        */
        public static void engine_begin_turn(Player forced_player, bool skip_unit_prep)
        {
            char[] text = new char[400];
            int new_turn = 0;
            Player player = null;
            #if TODO
            /* clear various stuff that may be still set from last turn */
            group_set_active( gui.confirm, ID_OK, 1 );
            engine_hide_unit_menu();
            engine_hide_game_menu();
            #endif
            /* clear undo */
            engine_clear_backup();
            /* clear hideous clicks */
            #if TODO
            if ( !deploy_turn && cur_ctrl == PLAYERCONTROL.PLAYER_CTRL_HUMAN )
                event_wait_until_no_input();
            #endif

            /* get player */
            if (forced_player == null)
            {
                /* next player and turn */
                player = Player.players_get_next(out new_turn);
                if (new_turn != 0)
                {
                    Scenario.turn++;
                }
                if (Scenario.turn == Scenario.scen_info.turn_limit)
                {
                    /* use else condition as Scenario result */
                    /* and take a final look */
                    Scenario.scen_check_result(true);
                    blind_cpu_turn = false;
                    engine_show_final_message();
                    draw_map = true;
            #if TODO
                    image_hide(gui.cursors, 0);
                    gui_set_cursor(CURSOR_STD);
            #endif
                    engine_select_player(null, skip_unit_prep);
                    engine_set_status(STATUS.STATUS_NONE);
                    phase = PHASE.PHASE_NONE;
                }
                else
                {
                    Scenario.cur_weather = Scenario.scen_get_weather();
                    engine_select_player(player, skip_unit_prep);
                }
            }

            else
            {
                engine_select_player(forced_player, skip_unit_prep);
                Player.players_set_current(Player.player_get_index(forced_player));
            }
            #if DEBUG_CAMPAIGN
            if ( scen_check_result(0) ) {
                blind_cpu_turn = 0;
                engine_show_final_message();
                draw_map = 1;
                image_hide( gui.cursors, 0 );
                gui_set_cursor( CURSOR_STD );
                engine_select_player( 0, skip_unit_prep );
                engine_set_status( STATUS_NONE );
                phase = PHASE_NONE;
                end_scen = 1;
                return;
            }
            #endif

            /* init ai turn if any */
            if (cur_player != null && cur_player.ctrl == PLAYERCONTROL.PLAYER_CTRL_CPU)
                cur_player.ai_init();
            engine_set_status(deploy_turn ? STATUS.STATUS_DEPLOY : STATUS.STATUS_NONE);
            phase = PHASE.PHASE_NONE;
            /* update screen */
            if (cur_ctrl != PLAYERCONTROL.PLAYER_CTRL_CPU || Config.show_cpu_turn)
            {
            #if TODO
                if (cur_ctrl == PLAYERCONTROL.PLAYER_CTRL_CPU)
                    engine_update_info(0, 0, 0);
                else
                {
                    image_hide(gui.cursors, 0);
                }
                engine_draw_map();
            #endif
                blind_cpu_turn = false;
            }
            else
            {
            #if TODO
                engine_update_info(0, 0, 0);
                draw_map = false;
                FULL_DEST(sdl.screen);
                fill_surf(0x0);
                gui.font_turn_info.align = ALIGN_X_CENTER | ALIGN_Y_CENTER;
                sprintf(text, tr("CPU thinks..."));
                write_text(gui.font_turn_info, sdl.screen, sdl.screen.w >> 1, sdl.screen.h >> 1, text, OPAQUE);
                sprintf(text, tr("( Enable option 'Show Cpu Turn' if you want to see what it is doing. )"));
                write_text(gui.font_turn_info, sdl.screen, sdl.screen.w >> 1, (sdl.screen.h >> 1) + 20, text, OPAQUE);
                refresh_screen(0, 0, 0, 0);
            #endif
                blind_cpu_turn = true;
            }
            if (cur_ctrl == PLAYERCONTROL.PLAYER_CTRL_CPU)
            {
                while (!cur_player.ai_run()) ;
            }
        }
 /*
 ====================================================================
 Set the fog to players spot mask by using mask::aux (not mask::spot)
 ====================================================================
 */
 public void map_set_fog_by_player(Player player)
 {
     int next_x, next_y;
     Unit unit;
     map_clear_mask (MAP_MASK.F_AUX); /* buffer here first */
     /* units */
     for (int i = 0; i < Scenario.units.Count; i++) {
         unit = Scenario.units [i];
         if (unit.killed != 0)
             continue;
         if (Player.player_is_ally (player, unit.player)) /* it's your unit or at least it's allied... */
             map_add_unit_spot_mask (unit);
     }
     /* check all flags; if flag belongs to you or any of your partners you see the surrounding, too */
     for (int x = 0; x < map_w; x++)
         for (int y = 0; y < map_h; y++)
             if (Engine.map.map [x, y].player != null)
             if (Player.player_is_ally (player, map [x, y].player)) {
                 mask [x, y].aux = 1;
                 for (int i = 0; i < 6; i++)
                     if (Misc.get_close_hex_pos (x, y, i, out next_x, out next_y))
                         mask [next_x, next_y].aux = 1;
             }
     /* convert aux to fog */
     for (int x = 0; x < map_w; x++)
         for (int y = 0; y < map_h; y++)
             if (mask [x, y].aux != 0 || !Config.fog_of_war)
                 mask [x, y].fog = false;
             else
                 mask [x, y].fog = true;
 }
 public void scenTOscen_data(string nationdbname, string unitdbname)
 {
     this.scen_info = Scenario.scen_info;
     this.nation_db_name = nationdbname;
     this.unit_db_name = unitdbname;
     this.unitsToAdd = Scenario.unitsToAdd;
     this.map_fname = Scenario.map_fname;
     if (string.IsNullOrEmpty(this.map_fname)){
         this.map = Scenario.mapScen;
     }
     this.weather = Scenario.weather;
     this.list_flags = Scenario.list_flags;
     this.list_players = Player.players;
     this.vcond_check_type = Scenario.vcond_check_type;
     this.vcond_count = Scenario.vcond_count;
     this.vconds = Scenario.vconds;
     this.player = Scenario.player;
     this.units = Scenario.units;
      foreach (Unit ubase in this.units){
         ubase.name = Unit.DeleteOrdinal(ubase.name);
     }
 }
 /*
 ====================================================================
 Check if these two players are allies.
 ====================================================================
 */
 public static bool player_is_ally(Player player, Player second)
 {
     if (player == second || player == null) return true;
     foreach (Player ally in player.allies)
         if (ally == second)
             return true;
     return false;
 }
 /*
 ====================================================================
 Get player index in list
 ====================================================================
 */
 public static int player_get_index(Player player)
 {
     int i = 0;
     foreach (Player entry in players)
     {
         if (player == entry)
             return i;
         i++;
     }
     return 0;
 }
 /*
 ====================================================================
 Delete player entry.
 ====================================================================
 */
 public static void player_delete(Player player)
 {
 }
 /*
 ====================================================================
 Add player to player list.
 ====================================================================
 */
 public static void player_add(Player player)
 {
     if (players == null)
         players = new List<Player>();
     players.Add(player);
 }