void Main()
{
//////////////////////////////////////////////////////////////////////////////////////////////
//******************************************************************************************//
int id_Drone = 0; //<==== Ne peux pas etre = a 0 et doit etre unique !!!!!//
//******************************************************************************************//
//////////////////////////////////////////////////////////////////////////////////////////////
string debug = "";
string info_Grid = ""; // info de la grille du vaissaux
bool is_connected = false; // vrais si le vaissaux est connecter a un autre.
if (id_Drone == 0)
throw new Exception("l'ID du drone ne peux etre 0. merci de mettre une ID unique");
//on cherche le prog de prog pour recuperer l id de la grid.
List<IMyTerminalBlock> liste_blocks = Get_Block_Programmable(id_Drone);
if (!(liste_blocks.Count == 1 && liste_blocks[0].CustomName == "IA Docking [N°" + id_Drone + "]"))
{
throw new Exception("Block IA non trouver,executez le script sans etre connecter un vaissaux");
}
else
{
// on a trouver le block de prog. on recuperer les information de grid pour ne toucher que les equipement du vaissaux.
info_Grid = liste_blocks[0].CubeGrid + "";
// recherche de l antenne pour savoir si elle est connecter.
IMyRadioAntenna ant = null;
liste_blocks = Get_Liste_Antenne();
for (int cpt = 0; cpt < liste_blocks.Count; cpt++)
{
if (liste_blocks[cpt].CubeGrid + "" == info_Grid)
{
ant = liste_blocks[cpt] as IMyRadioAntenna;
}
}
if (!ant.IsWorking) // si l antenne n est pas allumer on l active.
{
ant.GetActionWithName("OnOff_On").Apply(ant);
}
else // si l antenne est allumer on regarde pour executer une maneuvre de docking
{
// recherche du connecteur
liste_blocks = Get_Liste_Connecteur();
IMyShipConnector connec = null;
bool trouver = false;
for (int cpt = 0; cpt < liste_blocks.Count; cpt++)
{
if (liste_blocks[cpt].CubeGrid + "" == info_Grid)
{
trouver = true;
connec = liste_blocks[cpt] as IMyShipConnector;
}
else
{
is_connected = true;
if (trouver)
{
break;
}
}
}
if (connec == null) // si le connecteur n'a pas ete trouver
{
throw new Exception("le connecteur du vaissaux n a pas ete trouver");
}
else
{
if (connec.IsWorking && is_connected) // si le block est allimenter et connecter a un autre vaissaux
{
// activation des propulsseur
this.Activation_Propulsseur(info_Grid);
// activation de la camera
this.Activation_Camera_Avant(info_Grid);
// deconnection du vaissaux et extinction du connecteur
connec.GetActionWithName("SwitchLock").Apply(connec);
connec.GetActionWithName("OnOff_Off").Apply(connec);
}
else if (connec.IsWorking && connec.IsLocked && !is_connected) // si le drone peut ce connecter mais ne l ai pas encore
{
// deactivation de l antenne
this.Deactivation_Antenne(info_Grid);
// desactivation des propulsseur
this.Deactivation_Propulsseur(info_Grid);
// connection aux vaissaux
connec.GetActionWithName("SwitchLock").Apply(connec);
}
else if (!connec.IsWorking) // si le connecteur n est pas allimenter
{
connec.GetActionWithName("OnOff_On").Apply(connec);
}
else // si le connecteur est allimenter mais qu on ne peux pas ce connecter
{
connec.GetActionWithName("OnOff_Off").Apply(connec);
}
}
}
}
if (debug != "")
Set_debug(debug);
}
void Set_Block_Prog(int Id) // initialisation du vaissaux par changement de nom du block de prog.
{
List<IMyTerminalBlock> liste_Conn = Get_Liste_Block_Programmable();
if (liste_Conn.Count == 1)
{
Renam_Block(liste_Conn[0], "IA Docking [N°" + Id + "]");
}
}
void Activation_Antenne(string Info_Grid)
{
IMyRadioAntenna ant = null;
List<IMyTerminalBlock> liste_blocks = Get_Liste_Antenne();
for (int cpt = 0; cpt < liste_blocks.Count; cpt++)
{
if (liste_blocks[cpt].CubeGrid + "" == Info_Grid)
{
ant = liste_blocks[cpt] as IMyRadioAntenna;
ant.GetActionWithName("OnOff_On").Apply(ant);
}
}
}
void Deactivation_Antenne(string Info_Grid)
{
List<IMyTerminalBlock> liste_blocks = Get_Liste_Antenne();
IMyRadioAntenna ant = null;
for (int cpt = 0; cpt < liste_blocks.Count; cpt++)
{
if (liste_blocks[cpt].CubeGrid + "" == Info_Grid)
{
ant = liste_blocks[cpt] as IMyRadioAntenna;
ant.GetActionWithName("OnOff_Off").Apply(ant);
}
}
}
void Activation_Camera_Avant(string Info_Grid) // desactiver pour bug
{
//IMyCameraBlock cam = null;
//List<IMyTerminalBlock> liste_blocks = Get_Liste_Camera();
//for (int cpt = 0; cpt < liste_blocks.Count; cpt++)
//{
// if (liste_blocks[cpt].CubeGrid + "" == Info_Grid)
// {
// cam = liste_blocks[cpt] as IMyCameraBlock;
// if (cam.CustomName.Contains("AV"))
// {
// cam.GetActionWithName("View").Apply(cam);
// break;
// }
// }
//}
}
void Activation_Propulsseur(string Info_Grid)
{
IMyThrust propu = null;
List<IMyTerminalBlock> liste_blocks = Get_Liste_Propulsseur();
for (int cpt = 0; cpt < liste_blocks.Count; cpt++)
{
if (liste_blocks[cpt].CubeGrid + "" == Info_Grid)
{
propu = liste_blocks[cpt] as IMyThrust;
propu.GetActionWithName("OnOff_On").Apply(propu);
}
}
}
void Deactivation_Propulsseur(string Info_Grid)
{
IMyThrust propu = null;
List<IMyTerminalBlock> liste_blocks = Get_Liste_Propulsseur();
for (int cpt = 0; cpt < liste_blocks.Count; cpt++)
{
if (liste_blocks[cpt].CubeGrid + "" == Info_Grid)
{
propu = liste_blocks[cpt] as IMyThrust;
propu.GetActionWithName("OnOff_Off").Apply(propu);
}
}
}
List<IMyTerminalBlock> Get_Liste_Connecteur()
{
List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyShipConnector>(blocks);
return blocks;
}
List<IMyTerminalBlock> Get_Liste_Antenne()
{
List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyRadioAntenna>(blocks);
return blocks;
}
List<IMyTerminalBlock> Get_Liste_Propulsseur()
{
List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyThrust>(blocks);
return blocks;
}
List<IMyTerminalBlock> Get_Liste_Camera()
{
List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyCameraBlock>(blocks);
return blocks;
}
List<IMyTerminalBlock> Get_Liste_Block_Programmable()
{
List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyProgrammableBlock>(blocks);
return blocks;
}
List<IMyTerminalBlock> Get_Block_Programmable(int id)
{
List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
GridTerminalSystem.SearchBlocksOfName("IA Docking [N°" + id + "]", blocks); // recherche du block de prog
if (blocks.Count == 0) // si le block de prog n a pas etre trouver on tante d inisatiliser le vaissaux.
{
this.Set_Block_Prog(id);
GridTerminalSystem.SearchBlocksOfName("IA Docking [N°" + id + "]", blocks); // on cherche si un block de prog a ete ini
}
return blocks;
}
void Set_debug(string texte)
{
List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyRadioAntenna>(blocks);
Renam_Block(blocks[0], "IA debug " + texte);
}
void Renam_Block(IMyTerminalBlock Block, string name)
{
Block.SetCustomName(name);
}