View Single Post
  #2  
Old 06-07-2012, 10:57 AM
FG28_Kodiak FG28_Kodiak is offline
Approved Member
 
Join Date: Dec 2009
Location: Swabia->Bavaria->Germany
Posts: 884
Default

Don't know if this script working 100% at Dedi Server (theoretical it should ), have not the time to test it properly so it's on you :
Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using maddox.GP;
using maddox.game;
using maddox.game.world;
using part;

public class Mission : AMission
{

    /// <summary>
    /// This Dictionary contains the InternalTypeName of the limited actor and the number of available actors 
    /// </summary>
    /// <remarks>Actors which are not in List are not limited</remarks>
    internal Dictionary<string, int> AircraftLimitations = new Dictionary<string, int>
    {
        //{"bob:Aircraft.Bf-109E-3",10},                         
        //{"bob:Aircraft.Bf-109E-3B",10},
        //{"bob:Aircraft.Bf-109E-1",10},
        {"bob:Aircraft.Bf-109E-4",5},
        {"bob:Aircraft.Bf-109E-4B",5},
        //{"bob:Aircraft.Bf-110C-4",10},
        //{"bob:Aircraft.Bf-110C-7",10},
        //{"bob:Aircraft.Ju-87B-2",10},
        //{"bob:Aircraft.Ju-88A-1",10},
        //{"bob:Aircraft.He-111H-2",10},
        //{"bob:Aircraft.He-111P-2",10},
        //{"bob:Aircraft.DH82A",10},
        //{"bob:Aircraft.HurricaneMkI",10},
        //{"bob:Aircraft.HurricaneMkI_dH5-20",10},
        //{"bob:Aircraft.BlenheimMkIV",10},
        //{"bob:Aircraft.SpitfireMkI",10},
        {"bob:Aircraft.SpitfireMkIa",5},
        {"bob:Aircraft.SpitfireMkIIa",5},
        //{"bob:Aircraft.SpitfireMkI_Heartbreaker",10},
        //{"bob:Aircraft.G50",10},
        //{"bob:Aircraft.BR-20M",10}
    };


    private bool IsLimitReached(AiActor actor)
    {
        bool limitReached = false;
        AiCart cart = actor as AiCart;

        if (cart != null)
            if (AircraftLimitations.ContainsKey(cart.InternalTypeName()))
                if (AircraftLimitations[cart.InternalTypeName()] < 1)
                    limitReached = true;

        return limitReached;
    }


    private void CheckActorOut(AiActor actor)
    {
        AiCart cart = actor as AiCart;

        if (cart != null)
            if (AircraftLimitations.ContainsKey(cart.InternalTypeName()))
                AircraftLimitations[cart.InternalTypeName()]--;
    }


    private void CheckActorIn(AiActor actor)
    {
        AiCart cart = actor as AiCart;

        if (cart != null)
            if (AircraftLimitations.ContainsKey(cart.InternalTypeName()))
                AircraftLimitations[cart.InternalTypeName()]++;
    }


    private void DebugPrintNumberOfAvailablePlanes()
    {
        foreach (KeyValuePair<string, int> current in AircraftLimitations)
        {
            GamePlay.gpLogServer(new Player[] { GamePlay.gpPlayer()},"InternalTypeName: {0}, Available: {1}", new object[]{current.Key, current.Value.ToString(CultureInfo.InvariantCulture)});
        }
    }


    private int NumberPlayerInActor(AiActor actor)
    {
        int number = 0;

        AiCart cart = actor as AiCart;

        if(cart != null)
            for (int i = 0; i < cart.Places(); i++)
                if (cart.Player(i) != null)
                    number++;

        return number;
    }


    private AiAirport GetNearestAirfield(AiActor actor)
    {
        if (actor == null) return null;
        
        AiAirport nearestAirfield = null;
        AiAirport[] airports = GamePlay.gpAirports();

        Point3d actorPos = actor.Pos();

        if (airports != null)
        {
            foreach (AiAirport airport in airports)
            {
                if (nearestAirfield != null)
                {
                    if (nearestAirfield.Pos().distance(ref actorPos) > airport.Pos().distance(ref actorPos))
                        nearestAirfield = airport;
                }
                else nearestAirfield = airport;
            }
        }
        return nearestAirfield;
    }


    private bool LandedOnAirfield(AiActor actor, AiAirport airport, double maxdistance)
    {
        if (actor == null || airport == null || !IsActorGrounded(actor)) return false;
        
        Point3d ActorPos = actor.Pos();

        if (airport.Pos().distance(ref ActorPos) < maxdistance)
            return true;
        return false;
    }


    private bool IsActorGrounded(AiActor actor)
    {
        bool onGround = false;
        AiAircraft aircraft = actor as AiAircraft;
        
        if (aircraft != null)
            if (aircraft.getParameter(ParameterTypes.Z_AltitudeAGL, -1) <= 2.0 
               || aircraft.getParameter(ParameterTypes.Z_VelocityTAS, -1) <= 1.0)
            onGround = true;
       
        return onGround;
    }


    private bool IsActorDamaged(AiActor actor)
    {
        foreach (AiActor ac in Battle.GetDamageVictims())
            if (ac == actor)
                return true;
        
        return false;
    }


    private string ParseTypeName(string typeName)
    {
        string[] tempString = null;
        string parsedName = "";
        tempString = typeName.Split('.');

        parsedName = tempString[1].Replace("_", " ");

        return parsedName;
    }


    public void CheckActorAvailibility(Player player, AiActor actor, int placeIndex)
    {
        if (actor != null)
        {
            if (NumberPlayerInActor(actor) == 1)
            {
                if (!IsLimitReached(actor))
                    CheckActorOut(actor);
                else
                {
                    AiCart cart = actor as AiCart;

                    if (cart != null)
                    {
                        GamePlay.gpHUDLogCenter(new Player[] { player }, "Planetype: {0} no longer available", new object[] { ParseTypeName(cart.InternalTypeName()) });
                    
                        if (AircraftLimitations.ContainsKey(cart.InternalTypeName()))
                            AircraftLimitations[cart.InternalTypeName()] = -1; // after leaving the plane +1 is added via CheckActorIn
                        
                        player.PlaceLeave(placeIndex); // does not work on Dedi correctly
                        Timeout(2, cart.Destroy);      // so destroy the plane
                    }
                }
            }
        }
    }


    private bool OverEnemyTeritory(AiActor actor)
    {
        if (actor == null) return false;
        if (!GamePlay.gpFrontExist()) return false;

        if (GamePlay.gpFrontArmy(actor.Pos().x, actor.Pos().y) != actor.Army())
            return true;
        return false;
    }


    public override void OnPlaceEnter(Player player, AiActor actor, int placeIndex)
    {
        base.OnPlaceEnter(player, actor, placeIndex);

        CheckActorAvailibility(player, actor, placeIndex);

        DebugPrintNumberOfAvailablePlanes(); // for testing
    }
    

    public override void OnPlaceLeave(Player player, AiActor actor, int placeIndex)
    {
        base.OnPlaceLeave(player, actor, placeIndex);

        if (actor != null)
        {
            if (NumberPlayerInActor(actor) == 0 && LandedOnAirfield(actor, GetNearestAirfield(actor), 2000.0) && !OverEnemyTeritory(actor) /*&& !IsActorDamaged(actor)*/)
            {
                CheckActorIn(actor);
            }

            if (NumberPlayerInActor(actor) == 0)
                if (actor is AiCart)
                    Timeout(5, ()=>
                        {
                            if (actor as AiCart != null)
                                (actor as AiCart).Destroy();
                        });
        }
    }

}

Last edited by FG28_Kodiak; 06-07-2012 at 11:11 AM.
Reply With Quote