Added logic of usage in car.
[toni-reis.git] / src / simulator-car.adb
blobce70e8ddfeeaa2bf1d8615ee2dd5f726ac18f61c
1 with Ada.Text_IO; use Ada.Text_IO;
2 with Ada.Float_Text_IO; use Ada.Float_Text_IO;
4 with Simulator.Controller;
5 with Simulator.Track;
7 with Simulator.Car.Gui; use Simulator.Car.Gui;
9 with Glib; use Glib;
10 with Gtk.Label; use Gtk.Label;
11 with Gtk.Button; use Gtk.Button;
12 with Gtk.Scale; use Gtk.Scale;
14 package body Simulator.Car is
15 package DurationIO is new Ada.Text_IO.Fixed_Io(Duration);
16 use Simulator;
18 -- Oggetto che iidentifica l'auto. Non può essere locale alle funzioni.
19 Own_Car : aliased Car_Type;
21 --Task da eseguire solo quando Own_Car è inizializzato completamente
22 task type Racer;
23 type Racer_Access is access Racer;
24 Racer_Instance : Racer_Access;
26 task body Racer is
27 begin
28 Simulator.Track.PutOnPitLane(Own_Car.CarProperties);
29 Print("Il task Racer terminato. Simulazione conclusa");
30 end Racer;
32 function Init(Tires: Tires_T;
33 CarWeight: CarWeight_T;
34 CarFuel: CarFuel_T;
35 CarPerformance: CarPerformance_T) return Boolean is
36 use Simulator.Controller;
37 Success : Boolean;
38 Registered : Boolean := False;
39 id : CarId_T;
40 begin
41 --iscrivo l'auto alla gara e ottengo l'id dell'auto
42 Print("LOG: Registro l'auto alla corsa.");
43 Simulator.Controller.GetRace.CarRegister(id, Success);
44 if (Success = True) then
45 --creo l'istanza di CarProperties_T
46 Own_Car.CarProperties := (id,Tires,0.0,CarFuel,CarPerformance);
47 Registered := Simulator.Controller.RegisterCar (Own_Car'Access, id);
48 -- aggiorno GUI con il nuovo id
49 Set_Text(ValId, Own_Car.CarProperties.CarId'Img);
50 Print("LOG: Auto inizializzata. Parametri: " & Own_Car.CarProperties.CarId'Img &","& Own_Car.CarProperties.Tires'Img&","&Own_Car.CarProperties.CarPerformance'Img&","&Own_Car.CarProperties.CarFuel'Img&"." );
51 Racer_Instance := new Racer;
52 SetWinTitle("Car "&id'Img);
53 return True;
54 else
55 return False;
56 end if;
57 end Init;
59 --PROCEDURE DI SERVIZIO
61 -- chiamata a procedura di circuito che avverte di fermarsi
62 -- ai box appena possibile
63 procedure CallForPitStop(CarFuel :CarFuel_T; Tires :Tires_T) is
64 begin
65 Simulator.Track.CallForPitStop(Own_Car.CarProperties.CarId,CarFuel, Tires);
66 end CallForPitStop;
68 -- chiamata a procedura di gara che permette di ritirare
69 -- un auto dalla corsa
70 CurrentLap : Integer := 0;
71 FuelLevel : Integer :=0;
72 TyresLevel : Float := 0.0;
73 PitStopRequest : Boolean := False;
75 procedure Kill is
76 begin
77 Simulator.Controller.GetRace.Kill(Own_Car.CarProperties.CarId);
78 end Kill;
80 procedure UpdateProperties(This : access Car_Type;
81 CarId: CarId_T;
82 Tires: Tires_T;
83 TiresConsumption: TiresConsumption_T;
84 CarFuel: CarFuel_T;
85 CarPerformance: CarPerformance_T;
86 Lap: Integer;
87 Sector : Natural;
88 Speed : Float;
89 SectorDuration : Duration) is
90 str : String := "aaaaaaaaaa";
91 time : String := "bbbbbbbbb";
92 begin
93 This.CarProperties.CarId := CarId;
94 This.CarProperties.Tires := Tires;
95 This.CarProperties.TiresConsumption := TiresConsumption;
96 This.CarProperties.CarFuel := CarFuel;
97 This.CarProperties.CarPerformance := CarPerformance;
99 if (Lap /= CurrentLap or else Lap =0) then
100 if (Lap > 0) then
101 SetUsage(FuelLevel - Integer(Get_Value(ValFuel)),Float(Get_Value(ValTyresConsum)) - TyresLevel) ;
102 end if;
103 FuelLevel := Integer(Own_Car.CarProperties.CarFuel);
104 TyresLevel :=Float(Own_Car.CarProperties.TiresConsumption);
105 CurrentLap := Lap;
106 end if;
109 Set_Value(ValFuel, GDouble(Own_Car.CarProperties.CarFuel));
110 Set_Value(ValTyresConsum, GDouble(Own_Car.CarProperties.TiresConsumption));
112 if ((Get_Value(ValFuel) <= Get_Value(ValFuelLimit) or else Get_Value(ValTyresConsum) >= Get_Value(ValTyresConsumLimit)) and then PitStopRequest = False) then
113 Clicked(ButtBox);
114 PitStopRequest := True;
115 end if;
118 Put(str, Float(Speed*3.6), Aft => 3, Exp => 0);
119 DurationIO.Put(time, SectorDuration, Aft => 3);
121 if (Sector = 500) then
122 -- settore box
123 Print("LOG: Tratto: "&"Box"&","&time&"s,"&str&"km/h.");
124 PitStopRequest := False;
125 else
126 -- settore normale
127 Print("LOG: Tratto: "&Sector'Img&","&time&"s,"&str&"km/h.");
128 end if;
129 end UpdateProperties;
132 end Simulator.Car;