;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;   Modular Simulator Controller System - Pitstop Information Retrieval   ;;;
;;;                                                                         ;;;
;;;   Author:     Oliver Juwig (TheBigO)                                    ;;;
;;;   License:    (2026) Creative Commons - BY-NC-SA                        ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;-------------------------------------------------------------------------;;;
;;;                            Private Rules Section                        ;;;
;;;-------------------------------------------------------------------------;;;

tyrePosition(FL, FrontLeft) <= !Session.Settings.Tyre.Service = Wheel
tyrePosition(FL, Front) <= !Session.Settings.Tyre.Service = Axle
tyrePosition(FR, FrontRight) <= !Session.Settings.Tyre.Service = Wheel
tyrePosition(FR, Front) <= !Session.Settings.Tyre.Service = Axle
tyrePosition(RL, RearLeft) <= !Session.Settings.Tyre.Service = Wheel
tyrePosition(RL, Rear) <= !Session.Settings.Tyre.Service = Axle
tyrePosition(RR, RearRight) <= !Session.Settings.Tyre.Service = Wheel
tyrePosition(RR, Rear) <= !Session.Settings.Tyre.Service = Axle


;;;-------------------------------------------------------------------------;;;
;;;                            Public Rules Section                         ;;;
;;;-------------------------------------------------------------------------;;;

lastPitstop(?nr) <= Get(Pitstop.Last, ?nr)

lastTyreChange(?nr) <= lastTyreChange(ALL, ?nr)

lastTyreChange(?tyre, ?nr) <= lastTyreChange(?tyre, !Pitstop.Last, ?nr)

lastTyreChange(?, 0, ?) <= !, fail

lastTyreChange(ALL, ?nr, ?result) <= Get(Pitstop, ?nr, Tyre.Compound, ?tyreCompound), ?tyreCompound = false, !,
									 ?next = ?nr - 1, lastTyreChange(ALL, ?next, ?result)
lastTyreChange(ALL, ?nr, ?nr) <= Get(Pitstop, ?nr, Tyre.Compound, ?), !
lastTyreChange(ALL, ?, ?) <= !, fail

lastTyreChange(ANY, ?nr, ?result) <= tyrePosition(?, ?position), lastTyreChange(?position, ?nr, ?result), !
lastTyreChange(ANY, ?, ?) <= !, fail

lastTyreChange(?tyre, ?nr, ?result) <= tyrePosition(?tyre, ?position), lastTyreChange(?position, ?nr, ?result), !

lastTyreChange(?position, ?nr, ?result) <= Get(Pitstop, ?nr, Tyre.Compound, ?position, ?tyreCompound), ?tyreCompound = false, !,
										   ?next = ?nr - 1, lastTyreChange(?position, ?next, ?result)
lastTyreChange(?position, ?nr, ?nr) <= Get(Pitstop, ?nr, Tyre.Compound, ?position, ?), !

lastTyreChange(?tyre, ?nr, ?result) <= ?next = ?nr - 1, lastTyreChange(?tyre, ?next, ?result)

lastTyreChange(?, ?nr, ?result) <= lastTyreChange(ALL, ?nr, ?result)

pitstopLap(?lap) <= lastPitstopLap(?lap)

lastPitstopLap(?lap) <= lastPitstop(?nr), Get(Pitstop, ?nr, Lap, ?lap)

recentPitstopLap(?lap) <= lastPitstopLap(?lap)
recentPitstopLap(?lap) <= lastPitstopLap(?lastLap), ?lap = ?lastLap + 1

lastPitstopTyreSet(?tyreSet) <= lastTyreChange(ALL, ?nr), Get(Pitstop, ?nr, Tyre.Set, ?tyreSet)

lastPitstopTyreCompound(?tyreCompound) <= lastTyreChange(ALL, ?nr), Get(Pitstop, ?nr, Tyre.Compound, ?tyreCompound)

lastPitstopTyreCompound(?position, ?tyreCompound) <= lastTyreChange(?position, ?nr),
													 Get(Pitstop, ?nr, Tyre.Compound, ?position, ?tyreCompound), !
lastPitstopTyreCompound(?tyre, ?tyreCompound) <= tyrePosition(?tyre, ?position), lastPitstopTyreCompound(?position, ?tyreCompound)
lastPitstopTyreCompound(?, ?tyreCompound) <= lastPitstopTyreCompound(?tyreCompound)

lastPitstopTyreCompound(?position, ?tyreCompound, ?tyreCompoundColor) <=
		lastTyreChange(?position, ?nr), Get(Pitstop, ?nr, Tyre.Compound, ?position, ?tyreCompound),
		Get(Pitstop, ?nr, Tyre.Compound.Color, ?position, ?tyreCompoundColor), !
lastPitstopTyreCompound(?tyre, ?tyreCompound, ?tyreCompoundColor) <= tyrePosition(?tyre, ?position),
																	 lastPitstopTyreCompound(?position, ?tyreCompound, ?tyreCompoundColor)
lastPitstopTyreCompound(?, ?tyreCompound, ?tyreCompoundColor) <=
		lastTyreChange(?nr), Get(Pitstop, ?nr, Tyre.Compound, ?tyreCompound),
		Get(Pitstop, ?nr, Tyre.Compound.Color, ?tyreCompoundColor)

lastPitstopTyrePressure(?compound, ?tyre, ?pressure) <= lastPitstopTyreCompound(?tyre, ?compound), lastTyreChange(?tyre, ?nr),
														Get(Pitstop, ?nr, Tyre.Pressure, ?tyre, ?pressure)

lastPitstopTyrePressures(?compound, [], [])
lastPitstopTyrePressures(?compound, [?tyre | ?tyreTypes], [?pressure | ?pressures]) <=
		lastPitstopTyrePressure(?compound, ?tyre, ?pressure), lastPitstopTyrePressures(?compound, ?tyreTypes, ?pressures)

lastPitstopDamageRepair(Suspension, ?repair) <= lastPitstop(?nr), Get(Pitstop, ?nr, Repair.Suspension, ?repair)
lastPitstopDamageRepair(Bodywork, ?repair) <= lastPitstop(?nr), Get(Pitstop, ?nr, Repair.Bodywork, ?repair)
lastPitstopDamageRepair(Engine, ?repair) <= lastPitstop(?nr), Get(Pitstop, ?nr, Repair.Engine, ?repair)

pitstopTyreCompound(?compound, ?color) <= lastPitstop(?nr), pitstopTyreCompound(?nr, ?compound, ?color), !
pitstopTyreCompound(?compound, ?color) <= tyrePosition(?, ?position), lastPitstopTyreCompound(?position, ?compound, ?color)

pitstopTyreCompound(0, ?, ?) <= !, fail
pitstopTyreCompound(?nr, ?compound, ?color) <= Get(Pitstop, ?nr, Tyre.Compound, ?compound), ?compound = false, !,
											   ?next = ?nr - 1, pitstopTyreCompound(?next, ?compound, ?color)
pitstopTyreCompound(?nr, ?compound, ?color) <= Get(Pitstop, ?nr, Tyre.Compound, ?compound), Get(Pitstop, ?nr, Tyre.Compound.Color, ?color)
pitstopTyreCompound(?nr, ?compound, ?color) <= ?next = ?nr - 1, pitstopTyreCompound(?next, ?compound, ?color)