garage44.eu/jeroen/frits

logclone: git clone https://garage44.eu/jeroen/frits.git

.. /crates/bench/scripts/football.frits

raw

// The football in the Euroborg: a physical prim that referees itself.
//
// Cartopolis's `content/football.lua`, written in Frits. The physics is the
// simulator's; the script notices the ball crossing a goal line between the
// posts, keeps the score, says so, and puts the ball back on the centre spot.

script / Prim, Chat, Timer, Sound

let NORTH = { lat: 53.20661975, lng: 6.5916658 }
let SOUTH = { lat: 53.20566315, lng: 6.5916427 }

let GOAL_HALF_WIDTH = 3.66 // metres either side of the middle of the line
let GOAL_HEIGHT = 2.44
let BALL_RADIUS = 0.11
// Further than this outside the lines and the ball is past the advertising boards.
let OUT_OF_PLAY = 4.5
// Half of the pitch's 68 m width; the two goal points carry only its length.
let HALF_WIDTH = 34.0

let CENTRE = { lat: (NORTH.lat + SOUTH.lat) / 2, lng: (NORTH.lng + SOUTH.lng) / 2 }
let M = geo.metres_per_degree(CENTRE.lat)

// The pitch's own axes in metres: `along` points at the north goal, `across`
// is square to it, pointing roughly east.
let AXIS = {
    let e = (NORTH.lng - SOUTH.lng) * M.lng
    let n = (NORTH.lat - SOUTH.lat) * M.lat
    let length = math.sqrt(e * e + n * n)
    { e: e / length, n: n / length, half_length: length / 2 }
}

var north_score = 0
var south_score = 0
// Seconds until the ball goes back to the centre spot, when it is out of play.
var restart_in: Option<Float> = None

fn on_pitch() -> { along: Float, across: Float } / Prim {
    let g = Prim.geo()
    let e = (g.lng - CENTRE.lng) * M.lng
    let n = (g.lat - CENTRE.lat) * M.lat
    { along: e * AXIS.e + n * AXIS.n, across: e * AXIS.n - n * AXIS.e }
}

fn score_line() -> Str {
    "North {north_score} – {south_score} South"
}

fn place_on_centre_spot() / Prim {
    Prim.set_geo(CENTRE)
    let p = Prim.pos()
    // Dropped from a little height, so a restart reads as one.
    Prim.set_pos({ ..p, y: 1.0 })
    Prim.set_vel({ x: 0, y: 0, z: 0 })
}

on start() {
    Prim.name("Football — run into it to dribble, kick emote (5) to shoot")
    Prim.color({ r: 1.0, g: 1.0, b: 1.0 })
    Prim.physical(true)
    Timer.every(0.1)
}

on touch(id: Int, name: Str) {
    Chat.say("{name} puts the ball on the centre spot. {score_line()}")
    restart_in = None
    place_on_centre_spot()
}

on timer() {
    if let Some(t) = restart_in {
        if t - 0.1 <= 0 {
            restart_in = None
            place_on_centre_spot()
        } else {
            restart_in = Some(t - 0.1)
        }
        return
    }

    let p = on_pitch()
    let y = Prim.pos().y
    let between_posts = math.abs(p.across) < GOAL_HALF_WIDTH - BALL_RADIUS && y < GOAL_HEIGHT - BALL_RADIUS

    // Wholly over the line, between the posts and under the bar.
    if between_posts && p.along > AXIS.half_length + BALL_RADIUS {
        south_score += 1
        Chat.shout("GOAL at the north end! {score_line()}")
        Sound.play("bell")
        restart_in = Some(3)
    } else if between_posts && p.along < -(AXIS.half_length + BALL_RADIUS) {
        north_score += 1
        Chat.shout("GOAL at the south end! {score_line()}")
        Sound.play("bell")
        restart_in = Some(3)
    } else if math.abs(p.along) > AXIS.half_length + OUT_OF_PLAY || math.abs(p.across) > HALF_WIDTH + OUT_OF_PLAY {
        Chat.say("The ball went over the boards; back to the centre spot.")
        restart_in = Some(1)
    }
}