Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Unnamed chinsouki userscript
- // @namespace Violentmonkey Scripts
- // @match https://cgi.heyuri.net/chinsouki/main.cgi*
- // @grant none
- // @version 1.0
- // @author -
- // @description Displays information about purchasable items and where to buy them
- // ==/UserScript==
- (()=>{
- let shopsGroupedByItem = []
- let positions = []
- let container = document.createElement('div')
- let select = document.createElement('select')
- let info = document.createElement('div')
- let pathInfo = document.createElement('div')
- let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
- let pathBlocked = false
- function getItemNum(itemCode) {
- let itemNum = itemCode.charCodeAt()
- if (itemNum > 48 && itemNum < 58) {
- itemNum -= 48
- } else if (itemNum >= 97) {
- itemNum -= 87
- } else {
- itemNum -= 29
- }
- return itemNum
- }
- function init() {
- for (let id in graph) {
- let tile = graph[id]
- if (tile[4] === 'c' || tile[4] === 'i') {
- for (let itemCode of tile[5]) {
- let itemNum = getItemNum(itemCode)
- if (!shopsGroupedByItem[itemNum]) {
- shopsGroupedByItem[itemNum] = []
- }
- shopsGroupedByItem[itemNum].push(id)
- }
- }
- positions[`${tile[1]}_${tile[2]}`] = id
- }
- select.innerHTML = shopsGroupedByItem.map((item, itemNum) => {
- let name = itemdata[itemNum][0]
- return `<option value="${itemNum}">${name}</option>`
- }).join('\n')
- let hr = document.createElement('hr')
- container.append(pathInfo, hr, select, info)
- container.style = 'text-align: left; width: 360px;'
- }
- function insertHTML() {
- mainframe.document.getElementsByTagName('table')[0].insertAdjacentElement('afterend', container)
- select.addEventListener('change', () => displayItemDetails())
- }
- function displayItemDetails(itemNum) {
- let html = ''
- itemNum = itemNum || parseInt(select.value)
- let item = itemdata[itemNum]
- html += `<b>Name:</b> ${item[0]}<br>`
- html += `<b>Price:</b> ${(item[1] * 10000).toLocaleString()} yen<br>`
- if (itemNum >= 10) {
- html += `<b>Description:</b> ${item[2]}<br>`
- } else {
- html += `Speed: ${item[2]}, Capacity: ${item[3]}, Flags: ${item[4]}<br>`
- }
- html += '<b>Purchasable at:</b><br>'
- shopsGroupedByItem[itemNum].forEach(id => {
- let shop = graph[id]
- let x = shop[1], y = shop[2]
- let pref = prfname[shop[3]]
- let type = pexp[shop[4]]
- html += `${type} at ${pref} <a href="javascript:parent.putMain(${x},${y},0)">(${x}, ${y})</a><br>`
- })
- info.innerHTML = html
- }
- function displayShopItems(mx, my, mp) {
- let id = positions[`${mx}_${my}`]
- if (id && graph[id][4].match(/[ci]/)) {
- let shop = graph[id]
- let pref = prfname[shop[3]]
- let type = pexp[shop[4]]
- let html = `<b>${type} in ${pref} (${mx}, ${my})</b><br>`
- for (let itemCode of shop[5]) {
- let itemNum = getItemNum(itemCode)
- let item = itemdata[itemNum]
- html += `<a href="javascript:parent.displayItemDetails(${itemNum})">${item[0]}</a> - ${(item[1]*10000).toLocaleString()} yen<br>`
- }
- info.innerHTML = html
- }
- }
- function printItemInfo() {
- shopsGroupedByItem.forEach((shops, itemNum) => {
- let item = itemdata[itemNum]
- console.log(`name: ${item[0]}`)
- console.log(`price: ${item[1]}`)
- if (itemNum >= 10) {
- console.log(`description: ${item[2]}`)
- } else {
- console.log(`speed: ${item[2]}, capacity: ${item[3]}, flags: ${item[4]}`)
- }
- console.log('where to buy:')
- shops.forEach(id => {
- let shop = graph[id]
- let x = shop[1], y = shop[2]
- let pref = prfname[shop[3]]
- let type = pexp[shop[4]]
- console.log(`${type} at ${pref} (${x}, ${y})`)
- })
- })
- }
- function bfs(start, end, blockIgnore) {
- let queue = []
- let explored = new Set()
- let blocked = []
- blockIgnore ||= (user[num].status + itemdata[user[num].car][4]).match(/t/)
- // Road constructions and road closures
- blocked.push(...user_special[6], ...user_special[7])
- // Players with drunk driving or no overtaking status
- blocked.push(...user.filter(u => (u.status + itemdata[u.car][4])
- .match(/[gy]/))
- .map(u => parseInt(u.position)))
- blocked = new Set(blocked)
- explored.add(start)
- queue.push([start, [], []])
- while (queue.length > 0) {
- let [v, path, dir] = queue.shift()
- if (v === end) {
- return { path, dir}
- }
- for (let i = 0; i < 4; i++) {
- let w = graph[v][6+i]
- if (!w || (!blockIgnore && blocked.has(w))) {
- continue
- }
- if (!explored.has(w)) {
- explored.add(w)
- queue.push([w, path.concat(w), dir.concat(i)])
- }
- }
- }
- }
- function drawPath(svg, path) {
- const radius = 4, offset = 16, colorOK = '#2f8', colorBlocked = '#FF7F7F'
- let color = pathBlocked ? colorBlocked : colorOK
- let pathCmd = []
- pathCmd.push(`M ${offset+path[0].x*32},${offset+path[0].y*32}`)
- path.forEach(r => {
- pathCmd.push(`L ${offset+r.x*32},${offset+r.y*32}`)
- })
- svg.innerHTML = `<path d="${pathCmd.join(' ')}" fill="none" stroke="${color}" stroke-dasharray="0 8" stroke-linecap="round" stroke-width="4" />`
- }
- function updatePan(mx, my, mp) {
- let mapElem = mainframe.document.querySelector('table > tbody > tr > td > center > div')
- mapElem.appendChild(svg)
- svg.setAttribute('viewBox', `${(mx-(mapsize-1)/2)*32} ${(my-(mapsize-1)/2)*32} ${mapsize*32} ${mapsize*32}`)
- svg.setAttribute('width', mapsize*32)
- svg.setAttribute('height', mapsize*32)
- }
- function initPath() {
- let start = parseInt(user[num].position)
- let end = parseInt(user_goal)
- let sp = bfs(start, end)
- let spi = bfs(start, end, true)
- let path = []
- svg.setAttribute('style', 'position:absolute; top:0px; left:4px; pointer-events: none')
- let html = `Destination: <b>${graph[end][0]}</b><br>`
- if (!sp) {
- if (start === end) {
- html += 'You\'re already there<br>'
- } else {
- pathBlocked = true
- path = path.concat(start, spi.path).map(n => ({
- x: graph[n][1],
- y: graph[n][2]
- }))
- html += '<a style="color: #FF7F7F">Blocked, unreachable</a><br>'
- html += `Disregarding obstacles, the shortest path(${spi.path.length}) is:<br>${spi.dir.map(n => 'ENWS'[n]).join(', ')}`
- }
- } else {
- path = path.concat(start, sp.path).map(n => ({
- x: graph[n][1],
- y: graph[n][2]
- }))
- // print directions
- html += `Shortest path(${sp.path.length}):<br>${sp.dir.map(n => 'ENWS'[n]).join(', ')}`
- }
- pathInfo.innerHTML = html
- drawPath(svg, path)
- }
- let putMainOrig = window.putMain
- window.putMain = function putMain(mx, my, mp) {
- putMainOrig(mx, my, mp)
- if(mp > 0) {
- mx = graph[user[mp].position][1]
- my = graph[user[mp].position][2]
- } else if (mp == -1) {
- mx = graph[user_goal][1]
- my = graph[user_goal][2]
- }
- insertHTML()
- displayShopItems(mx, my, mp)
- updatePan(mx, my, mp)
- }
- window.displayItemDetails = displayItemDetails
- // window.mapsize = 21
- init()
- initPath()
- })()
Advertisement
Add Comment
Please, Sign In to add comment