Guest User

Unnamed chinsouki userscript

a guest
Jan 3rd, 2024
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Unnamed chinsouki userscript
  3. // @namespace   Violentmonkey Scripts
  4. // @match       https://cgi.heyuri.net/chinsouki/main.cgi*
  5. // @grant       none
  6. // @version     1.0
  7. // @author      -
  8. // @description Displays information about purchasable items and where to buy them
  9. // ==/UserScript==
  10.  
  11. (()=>{
  12.  
  13. let shopsGroupedByItem = []
  14. let positions = []
  15.  
  16. let container = document.createElement('div')
  17. let select = document.createElement('select')
  18. let info = document.createElement('div')
  19. let pathInfo = document.createElement('div')
  20. let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
  21. let pathBlocked = false
  22.  
  23. function getItemNum(itemCode) {
  24.   let itemNum = itemCode.charCodeAt()
  25.   if (itemNum > 48 && itemNum < 58) {
  26.     itemNum -= 48
  27.   } else if (itemNum >= 97) {
  28.     itemNum -= 87
  29.   } else {
  30.     itemNum -= 29
  31.   }
  32.   return itemNum
  33. }
  34.  
  35. function init() {
  36.   for (let id in graph) {
  37.     let tile = graph[id]
  38.     if (tile[4] === 'c' || tile[4] === 'i') {
  39.       for (let itemCode of tile[5]) {
  40.         let itemNum = getItemNum(itemCode)
  41.         if (!shopsGroupedByItem[itemNum]) {
  42.           shopsGroupedByItem[itemNum] = []
  43.         }
  44.         shopsGroupedByItem[itemNum].push(id)
  45.       }
  46.     }
  47.  
  48.     positions[`${tile[1]}_${tile[2]}`] = id
  49.   }
  50.   select.innerHTML = shopsGroupedByItem.map((item, itemNum) => {
  51.     let name = itemdata[itemNum][0]
  52.     return `<option value="${itemNum}">${name}</option>`
  53.   }).join('\n')
  54.   let hr = document.createElement('hr')
  55.   container.append(pathInfo, hr, select, info)
  56.   container.style = 'text-align: left; width: 360px;'
  57. }
  58.  
  59. function insertHTML() {
  60.   mainframe.document.getElementsByTagName('table')[0].insertAdjacentElement('afterend', container)
  61.   select.addEventListener('change', () => displayItemDetails())
  62. }
  63.  
  64. function displayItemDetails(itemNum) {
  65.   let html = ''
  66.   itemNum = itemNum || parseInt(select.value)
  67.   let item = itemdata[itemNum]
  68.   html += `<b>Name:</b> ${item[0]}<br>`
  69.   html += `<b>Price:</b> ${(item[1] * 10000).toLocaleString()} yen<br>`
  70.   if (itemNum >= 10) {
  71.     html += `<b>Description:</b> ${item[2]}<br>`
  72.   } else {
  73.     html += `Speed: ${item[2]}, Capacity: ${item[3]}, Flags: ${item[4]}<br>`
  74.   }
  75.   html += '<b>Purchasable at:</b><br>'
  76.   shopsGroupedByItem[itemNum].forEach(id => {
  77.     let shop = graph[id]
  78.     let x = shop[1], y = shop[2]
  79.     let pref = prfname[shop[3]]
  80.     let type = pexp[shop[4]]
  81.     html += `${type} at ${pref} <a href="javascript:parent.putMain(${x},${y},0)">(${x}, ${y})</a><br>`
  82.   })
  83.   info.innerHTML = html
  84. }
  85.  
  86. function displayShopItems(mx, my, mp) {
  87.   let id = positions[`${mx}_${my}`]
  88.   if (id && graph[id][4].match(/[ci]/)) {
  89.     let shop = graph[id]
  90.     let pref = prfname[shop[3]]
  91.     let type = pexp[shop[4]]
  92.     let html = `<b>${type} in ${pref} (${mx}, ${my})</b><br>`
  93.     for (let itemCode of shop[5]) {
  94.       let itemNum = getItemNum(itemCode)
  95.       let item = itemdata[itemNum]
  96.       html += `<a href="javascript:parent.displayItemDetails(${itemNum})">${item[0]}</a> - ${(item[1]*10000).toLocaleString()} yen<br>`
  97.     }
  98.     info.innerHTML = html
  99.   }
  100. }
  101.  
  102. function printItemInfo() {
  103.   shopsGroupedByItem.forEach((shops, itemNum) => {
  104.     let item = itemdata[itemNum]
  105.     console.log(`name: ${item[0]}`)
  106.     console.log(`price: ${item[1]}`)
  107.     if (itemNum >= 10) {
  108.       console.log(`description: ${item[2]}`)
  109.     } else {
  110.       console.log(`speed: ${item[2]}, capacity: ${item[3]}, flags: ${item[4]}`)
  111.     }
  112.     console.log('where to buy:')
  113.     shops.forEach(id => {
  114.       let shop = graph[id]
  115.       let x = shop[1], y = shop[2]
  116.       let pref = prfname[shop[3]]
  117.       let type = pexp[shop[4]]
  118.  
  119.       console.log(`${type} at ${pref} (${x}, ${y})`)
  120.     })
  121.   })
  122. }
  123.  
  124. function bfs(start, end, blockIgnore) {
  125.   let queue = []
  126.   let explored = new Set()
  127.   let blocked = []
  128.   blockIgnore ||= (user[num].status + itemdata[user[num].car][4]).match(/t/)
  129.  
  130.   // Road constructions and road closures
  131.   blocked.push(...user_special[6], ...user_special[7])
  132.   // Players with drunk driving or no overtaking status
  133.   blocked.push(...user.filter(u => (u.status + itemdata[u.car][4])
  134.     .match(/[gy]/))
  135.     .map(u => parseInt(u.position)))
  136.   blocked = new Set(blocked)
  137.  
  138.   explored.add(start)
  139.   queue.push([start, [], []])
  140.  
  141.   while (queue.length > 0) {
  142.     let [v, path, dir] = queue.shift()
  143.     if (v === end) {
  144.       return { path, dir}
  145.     }
  146.     for (let i = 0; i < 4; i++) {
  147.       let w = graph[v][6+i]
  148.       if (!w || (!blockIgnore && blocked.has(w))) {
  149.         continue
  150.       }
  151.       if (!explored.has(w)) {
  152.         explored.add(w)
  153.         queue.push([w, path.concat(w), dir.concat(i)])
  154.       }
  155.     }
  156.   }
  157. }
  158.  
  159. function drawPath(svg, path) {
  160.   const radius = 4, offset = 16, colorOK = '#2f8', colorBlocked = '#FF7F7F'
  161.   let color = pathBlocked ? colorBlocked : colorOK
  162.  
  163.   let pathCmd = []
  164.   pathCmd.push(`M ${offset+path[0].x*32},${offset+path[0].y*32}`)
  165.   path.forEach(r => {
  166.     pathCmd.push(`L ${offset+r.x*32},${offset+r.y*32}`)
  167.   })
  168.   svg.innerHTML = `<path d="${pathCmd.join(' ')}" fill="none" stroke="${color}" stroke-dasharray="0 8" stroke-linecap="round" stroke-width="4" />`
  169. }
  170.  
  171. function updatePan(mx, my, mp) {
  172.   let mapElem = mainframe.document.querySelector('table > tbody > tr > td > center > div')
  173.   mapElem.appendChild(svg)
  174.   svg.setAttribute('viewBox', `${(mx-(mapsize-1)/2)*32} ${(my-(mapsize-1)/2)*32} ${mapsize*32} ${mapsize*32}`)
  175.   svg.setAttribute('width', mapsize*32)
  176.   svg.setAttribute('height', mapsize*32)
  177. }
  178.  
  179. function initPath() {
  180.   let start = parseInt(user[num].position)
  181.   let end = parseInt(user_goal)
  182.   let sp = bfs(start, end)
  183.   let spi = bfs(start, end, true)
  184.   let path = []
  185.   svg.setAttribute('style', 'position:absolute; top:0px; left:4px; pointer-events: none')
  186.  
  187.   let html = `Destination: <b>${graph[end][0]}</b><br>`
  188.   if (!sp) {
  189.     if (start === end) {
  190.       html += 'You\'re already there<br>'
  191.     } else {
  192.       pathBlocked = true
  193.       path = path.concat(start, spi.path).map(n => ({
  194.         x: graph[n][1],
  195.         y: graph[n][2]
  196.       }))
  197.       html += '<a style="color: #FF7F7F">Blocked, unreachable</a><br>'
  198.       html += `Disregarding obstacles, the shortest path(${spi.path.length}) is:<br>${spi.dir.map(n => 'ENWS'[n]).join(', ')}`
  199.     }
  200.   } else {
  201.     path = path.concat(start, sp.path).map(n => ({
  202.       x: graph[n][1],
  203.       y: graph[n][2]
  204.     }))
  205.     // print directions
  206.     html += `Shortest path(${sp.path.length}):<br>${sp.dir.map(n => 'ENWS'[n]).join(', ')}`
  207.   }
  208.   pathInfo.innerHTML = html
  209.   drawPath(svg, path)
  210. }
  211.  
  212. let putMainOrig = window.putMain
  213.  
  214. window.putMain = function putMain(mx, my, mp) {
  215.   putMainOrig(mx, my, mp)
  216.   if(mp > 0) {
  217.     mx = graph[user[mp].position][1]
  218.     my = graph[user[mp].position][2]
  219.   } else if (mp == -1) {
  220.     mx = graph[user_goal][1]
  221.     my = graph[user_goal][2]
  222.   }
  223.   insertHTML()
  224.   displayShopItems(mx, my, mp)
  225.   updatePan(mx, my, mp)
  226. }
  227.  
  228. window.displayItemDetails = displayItemDetails
  229. // window.mapsize = 21
  230.  
  231. init()
  232. initPath()
  233.  
  234. })()
  235.  
Advertisement
Add Comment
Please, Sign In to add comment