<commit_before>module Scuffle {
	export class Instance {
		game : ServerGame
		id : number
		map : Map
		clients : { [k : number] : Client }
		bullets : { [k : number] : Bullet }

		constructor(game : ServerGame, id : number) {
			this.game = game
			this.id = id
			this.clients = {}
			this.bullets = {}
		}

		firstAvailablePlayerID() {
			for(var id=0; this.clients[id]!==undefined; ++id) {}
			return id
		}

		firstAvailableBulletID() {
			for(var id=0; this.bullets[id]!==undefined; ++id) {}
			return id
		}

		forEachClient(fn : Function) {
			for(var k in this.clients)
				if(fn(this.clients[k], k, this.clients) === false)
					return false
			return true
		}

		forEachPlayer(fn : Function) {
			for(var k in this.clients)
				if(fn(this.clients[k].player, k, this.clients) === false)
					return false
			return true
		}

		forEachBullet(fn : Function) {
			for(var k in this.bullets)
				if(fn(this.bullets[k], k, this.bullets) === false)
					return false
			return true
		}

		newPlayer(client : Client) {
			var id = this.firstAvailablePlayerID()
			this.clients[id] = client
			this.clients[id].player = new Player(id)
			var colors = [
				0xff0000,
				0xff8800,
				0xffff00,
				0x00ff00,
				0x55aaff,
				0xff00ff,
				0x5500ff
			]
			this.clients[id].player.color = colors[Math.floor(Math.random() * colors.length)]
			return this.clients[id].player
		}

		removePlayer(id : number) {
			delete this.clients[id]
		}

		newBullet(owner : number) {
			var id = this.firstAvailableBulletID()
			return (this.bullets[id] = new Bullet(id, owner))
		}

		removeBullet(id : number) {
			this.game.io.sockets.in(this.id).emit('instance$bullet$remove', id)
			delete this.bullets[id]
		}

		spawn(id : number) {
			if(this.clients[id]) {
				var spawnIndex = Math.floor(Math.random() * this.map.spawns.length)
				this.clients[id].player.pos = this.map.spawns[spawnIndex]
				this.clients[id].player.health = this.clients[id].player.baseHealth
				this.game.io.sockets.in(this.id).emit('instance$player$spawn', this.clients[id].player)
			}
		}

		kill(id : number, idKiller : number) {
			this.game.io.sockets.in(this.id).emit('instance$player$kill', id, idKiller)
		}

		respawn(id : number, idKiller : number) {
			this.kill(id, idKiller)
			setTimeout(() => { this.spawn(id) }, 2000)
		}

		tick(time : number) {
			this.forEachClient((client : Client) => {
				client.tick(time)
			})
			this.forEachBullet((bullet : Bullet, id : number) => {
				var newPos = bullet.velocity.addedToPoint(bullet.pos)
				var hitsWall = this.map.lines.some((ln : Line) => {
					return Line.prototype.intersectsLineOf.call(ln, bullet.pos, newPos)
				})
				if(hitsWall)
					this.removeBullet(id)
				else {
					var ln = new Line(bullet.pos, newPos)
					var hitsPlayer = false
					for(var idPl in this.clients) {
						var pl = this.clients[idPl].player
						// != used to coerce string and number
						if(idPl != bullet.owner && pl.isAlive())
							if(circlesIntersect(newPos, bullet.radius, pl.pos, pl.radius)) {
								pl.health -= bullet.damage
								if(pl.isAlive())
									this.game.io.sockets.in(this.id).emit('instance$player$hurt', idPl, pl.health)
								else
									this.respawn(idPl, bullet.owner)
								this.removeBullet(id)
								hitsPlayer = true
								break
							}
					}

					if(!hitsPlayer) {
						bullet.pos = newPos
						this.game.io.sockets.in(this.id).volatile.emit('instance$bullet$move', id, bullet.pos)<commit_msg>Use line-circle intersection for bullet-wall collision<commit_after>module Scuffle {
	export class Instance {
		game : ServerGame
		id : number
		map : Map
		clients : { [k : number] : Client }
		bullets : { [k : number] : Bullet }

		constructor(game : ServerGame, id : number) {
			this.game = game
			this.id = id
			this.clients = {}
			this.bullets = {}
		}

		firstAvailablePlayerID() {
			for(var id=0; this.clients[id]!==undefined; ++id) {}
			return id
		}

		firstAvailableBulletID() {
			for(var id=0; this.bullets[id]!==undefined; ++id) {}
			return id
		}

		forEachClient(fn : Function) {
			for(var k in this.clients)
				if(fn(this.clients[k], k, this.clients) === false)
					return false
			return true
		}

		forEachPlayer(fn : Function) {
			for(var k in this.clients)
				if(fn(this.clients[k].player, k, this.clients) === false)
					return false
			return true
		}

		forEachBullet(fn : Function) {
			for(var k in this.bullets)
				if(fn(this.bullets[k], k, this.bullets) === false)
					return false
			return true
		}

		newPlayer(client : Client) {
			var id = this.firstAvailablePlayerID()
			this.clients[id] = client
			this.clients[id].player = new Player(id)
			var colors = [
				0xff0000,
				0xff8800,
				0xffff00,
				0x00ff00,
				0x55aaff,
				0xff00ff,
				0x5500ff
			]
			this.clients[id].player.color = colors[Math.floor(Math.random() * colors.length)]
			return this.clients[id].player
		}

		removePlayer(id : number) {
			delete this.clients[id]
		}

		newBullet(owner : number) {
			var id = this.firstAvailableBulletID()
			return (this.bullets[id] = new Bullet(id, owner))
		}

		removeBullet(id : number) {
			this.game.io.sockets.in(this.id).emit('instance$bullet$remove', id)
			delete this.bullets[id]
		}

		spawn(id : number) {
			if(this.clients[id]) {
				var spawnIndex = Math.floor(Math.random() * this.map.spawns.length)
				this.clients[id].player.pos = this.map.spawns[spawnIndex]
				this.clients[id].player.health = this.clients[id].player.baseHealth
				this.game.io.sockets.in(this.id).emit('instance$player$spawn', this.clients[id].player)
			}
		}

		kill(id : number, idKiller : number) {
			this.game.io.sockets.in(this.id).emit('instance$player$kill', id, idKiller)
		}

		respawn(id : number, idKiller : number) {
			this.kill(id, idKiller)
			setTimeout(() => { this.spawn(id) }, 2000)
		}

		tick(time : number) {
			this.forEachClient((client : Client) => {
				client.tick(time)
			})
			this.forEachBullet((bullet : Bullet, id : number) => {
				var newPos = bullet.velocity.addedToPoint(bullet.pos)
				var hitsWall = this.map.lines.some((ln : Line) => {
					return Line.prototype.intersectsCircleOf.call(ln, bullet.pos, bullet.radius)
				})
				if(hitsWall)
					this.removeBullet(id)
				else {
					var hitsPlayer = false
					for(var idPl in this.clients) {
						var pl = this.clients[idPl].player
						// != used to coerce string and number
						if(idPl != bullet.owner && pl.isAlive())
							if(circlesIntersect(newPos, bullet.radius, pl.pos, pl.radius)) {
								pl.health -= bullet.damage
								if(pl.isAlive())
									this.game.io.sockets.in(this.id).emit('instance$player$hurt', idPl, pl.health)
								else
									this.respawn(idPl, bullet.owner)
								this.removeBullet(id)
								hitsPlayer = true
								break
							}
					}

					if(!hitsPlayer) {
						bullet.pos = newPos
						this.game.io.sockets.in(this.id).volatile.emit('instance$bullet$move', id, bullet.pos)
{'fraction_non_alphanumeric': 0.1467837098863146, 'fraction_numerical': 0.010073391854943157, 'mean_word_length': 2.9332201471420487, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '465384', 'n_tokens_mistral': 3156, 'n_tokens_neox': 2493, 'n_words': 455}