Skip to content

Commit 9b93ca6

Browse files
committed
update AI engine
1 parent ea5ecfa commit 9b93ca6

File tree

4 files changed

+41
-32
lines changed

4 files changed

+41
-32
lines changed

ai-loader.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ const getShaUrl = login =>
44
const getAIUrl = (login, sha, ai) =>
55
`https://rawgit.com/${login}/tron/${sha}/ai/${ai || login}.js`
66

7+
const toBlob = r => r.blob()
8+
const toUrlObject = b => URL.createObjectURL(b, { "type" : "text/javascript" })
9+
const fetchBlob = url => fetch(url).then(toBlob).then(toUrlObject)
710
const getUrl = async (login, sha) => {
8-
if (!location.hostname.endsWith('.github.io')) {
9-
return `${location.origin}/ai/${login}.js`
10-
}
11+
if (!location.hostname.endsWith('.github.io')) return `/ai/${login}.js`
1112

12-
if (sha) return getAIUrl(login, sha)
13+
if (sha) return fetchBlob(getAIUrl(login, sha))
1314

1415
const res = await fetch(getShaUrl(login))
15-
if (res.ok) return getAIUrl(login, (await res.json()).sha)
16+
if (res.ok) return fetchBlob(getAIUrl(login, (await res.json()).sha))
1617

1718
sha = (await (await fetch(getShaUrl('nan-academy'))).json()).sha
18-
return getAIUrl('nan-academy', sha, login)
19+
return fetchBlob(getAIUrl('nan-academy', sha, login))
1920
}
2021

2122
export default async ({ name, id, sha, seed }) => {
22-
const w = new Worker(await getUrl(name, sha), { type: 'module' })
23+
const w = new Worker(await getUrl(name, sha), { type: 'module', name })
2324

2425
await new Promise((s, f) => {
2526
w.onmessage = e => e.data === 'loaded' ? s() : f(Error(e.data))

ai/random.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const SIZE = 100
2+
const MAP = new Int8Array(SIZE * SIZE) // L'etat de la carte
3+
const isFree = ({ x, y }) => MAP[y * SIZE + x] === 0 // 0 = case pleine
4+
const isOccupied = ({ x, y }) => MAP[y * SIZE + x] === 1 // 1 = case pleine
25

36
// `inBounds` regarde si notre coordonée (n) n'est pas hors de notre carte
47
const inBounds = n => n < SIZE && n >= 0
@@ -56,10 +59,7 @@ const update = state => {
5659
addEventListener('message', self.init = initEvent => {
5760
const { seed, id } = JSON.parse(initEvent.data)
5861
const isOwnPlayer = p => p.name === id
59-
const MAP = new Int8Array(SIZE * SIZE)
60-
const addToMap = ({ x, y }) => MAP[x * SIZE + y] = 1
61-
const isFree = ({ x, y }) => MAP[x * SIZE + y] === 0
62-
const isOccupied = ({ x, y }) => MAP[x * SIZE + y] === 1
62+
const addToMap = ({ x, y }) => MAP[y * SIZE + x] = 1
6363

6464
let _seed = seed // On utilise une seed pour pouvoir replay les games
6565
const _m = 0x80000000
@@ -68,7 +68,8 @@ addEventListener('message', self.init = initEvent => {
6868
addEventListener('message', ({ data }) => {
6969
const players = JSON.parse(data)
7070
const player = players.find(isOwnPlayer)
71-
players.forEach(addToMap)
71+
players.forEach(addToMap) // J'ajoute toutes les nouvelles positions
72+
// des joueurs dans notre état de la carte `MAP`
7273

7374
postMessage(JSON.stringify(update({ isFree, isOccupied, players, player })))
7475
})

app.js

+24-19
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ const isStuck = ({ x, y }) =>
5959
const computePlayer = ({ x, y, name, coords, score, cardinal, direction }) =>
6060
({ x, y, name, coords, score, cardinal, direction })
6161

62+
const calculateCoords = player => {
63+
const { x, y } = player
64+
const { cardinal, direction } = player.coords
65+
.find(coord => coord.x === x && coord.y === y)
66+
67+
player.coords = [
68+
{ x, y: y - 1, cardinal: 0, direction: (4 - cardinal) % 4 },
69+
{ x: x + 1, y, cardinal: 1, direction: (5 - cardinal) % 4 },
70+
{ x, y: y + 1, cardinal: 2, direction: (6 - cardinal) % 4 },
71+
{ x: x - 1, y, cardinal: 3, direction: (7 - cardinal) % 4 },
72+
]
73+
74+
player.cardinal = cardinal
75+
player.direction = direction
76+
}
77+
6278
const update = async forced => {
6379
await update.lock
6480
clearTimeout(update.timeout)
@@ -129,22 +145,7 @@ const update = async forced => {
129145

130146
playersLeft
131147
.filter(isAlive)
132-
.forEach(player => {
133-
console.log(player.name, 'is not stuck at', player.x, player.y)
134-
const { x, y } = player
135-
const { cardinal, direction } = player.coords
136-
.find(coord => coord.x === x && coord.y === y)
137-
138-
player.coords = [
139-
{ x, y: y - 1, cardinal: 0, direction: (4 - cardinal) % 4 },
140-
{ x: x + 1, y, cardinal: 1, direction: (5 - cardinal) % 4 },
141-
{ x, y: y + 1, cardinal: 2, direction: (6 - cardinal) % 4 },
142-
{ x: x - 1, y, cardinal: 3, direction: (7 - cardinal) % 4 },
143-
]
144-
145-
player.cardinal = cardinal
146-
player.direction = direction
147-
})
148+
.forEach(calculateCoords)
148149

149150
graphic.update(players)
150151
const diff = performance.now() - startTime
@@ -169,7 +170,7 @@ state.users.forEach(addPlayer)
169170
Promise.all(players.map(p => p.load)).then(() => {
170171
Math.random = rseed.float
171172

172-
shuffle(players.sort((a, b) => a.name - b.name))
173+
players.sort((a, b) => a.name - b.name)
173174

174175
state.map = Array(SIZE * SIZE).fill(emptyTile)
175176

@@ -189,15 +190,19 @@ Promise.all(players.map(p => p.load)).then(() => {
189190
const max1 = max(1)
190191
const max2PI = max(Math.PI * 2)
191192
const angle = (Math.PI * 2) / players.length
192-
const rate = (100 / players.length / 100)
193+
const rate = (SIZE / players.length / SIZE)
193194
const shift = angle * rseed.float()
194195
const h = SIZE / 2
195196
const m = h * 0.8
196197

197198
players.forEach((player, i) => {
198-
player.color = hslToRgb(max1(i * rate + 0.25), 1, 0.4)
199199
player.cardinal = 0
200200
player.direction = 0
201+
player.color = hslToRgb(max1(i * rate + 0.25), 1, 0.5)
202+
})
203+
204+
// Shuffle players before calculating coords
205+
shuffle(players).forEach((player, i) => {
201206
const x = player.x = Math.round(max2PI(Math.cos(angle * i + shift)) * m + h)
202207
const y = player.y = Math.round(max2PI(Math.sin(angle * i + shift)) * m + h)
203208
player.coords = [

state.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import map from './izi/map.js'
33
import rseed from './rseed.js'
44
import qs from './izi/query-string.js'
55

6+
const rnd = Math.random
7+
68
const setRoute = obj => {
79
const newQuery = '?'+ Object.keys(obj)
810
.sort()
@@ -54,7 +56,7 @@ export default {
5456
incSpeed,
5557
decSpeed,
5658
reset: () => setRoute({
57-
seed: Math.floor(Math.random() * 0x80000000),
59+
seed: Math.floor(rnd() * 0x80000000),
5860
users: urlParams.users.sort(),
5961
}),
6062
reload: () => location.reload(),

0 commit comments

Comments
 (0)