Skip to content

Commit

Permalink
Fix Fatal bug for can not read buildings in game
Browse files Browse the repository at this point in the history
  • Loading branch information
Linnest2020 committed Jun 7, 2023
1 parent a893671 commit 40064f0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 45 deletions.
9 changes: 5 additions & 4 deletions src/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const preprocess = (map,header) => {
for (let state=Object.keys(data),i=state.length;i--;){
let name = state[i]
for (let state_region=Object.keys(data[name]),j=state_region.length;j--;){
if (name == "if") continue
let full_name = name +"." +state_region[j].replace(":",":c:")
if (!full_data.statepointmap[full_name]){
console.log("delete empty scope",header,full_name)
Expand Down Expand Up @@ -63,13 +64,13 @@ const default_upload = async function(e) {

let buildings_map_write = jomini.write(
(writer) => {
justwrite(writer,buildings_map,["create_building"],["building","activate_production_methods"])
justwrite(writer,buildings_map,["create_building","if"],["building","activate_production_methods"])
}
)

let pops_map_write = jomini.write(
(writer) => {
justwrite(writer,pops_map,["create_pop"])
justwrite(writer,pops_map,["create_pop","if"])
}
)

Expand Down Expand Up @@ -193,7 +194,7 @@ const mod_upload = async function(e) {
buildings_tree[key] = new TextDecoder().decode(
jomini.write((writer) => {
justwrite(writer, buildings_tree[key],
["create_building"], ["building", "activate_production_methods"])})
["create_building","if"], ["building", "activate_production_methods"])})
).replaceAll(" ", "\t").replaceAll("=", " = ")
}

Expand All @@ -202,7 +203,7 @@ const mod_upload = async function(e) {
pops_tree[key] = new TextDecoder().decode(
jomini.write((writer) => {
justwrite(writer, pops_tree[key],
["create_pop"])})
["create_pop","if"])})
).replaceAll(" ", "\t").replaceAll("=", " = ")

}
Expand Down
53 changes: 16 additions & 37 deletions src/canvas_selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,8 @@ const select_prov_pure = (imgdata,reset_data,label,sindex,provs_name,e) => {
}




const select_provs = (imgdata,state_data,label,sindex,provs_name,e) => {


full_data.ctx.putImageData(state_data, 0, 0)
// full_data.ctx.putImageData(state_data, 0, 0)
if (provs_name.has(label)){
provs_name.delete(label)
} else if(e.ctrlKey) {
Expand Down Expand Up @@ -107,56 +103,39 @@ const select_provs = (imgdata,state_data,label,sindex,provs_name,e) => {
select_provs_color(prov,imgdata)
}
full_data.ctx.putImageData(imgdata, 0, 0);
do_draw()
// try{
// let select_area = get_select_area(provs_name)

// full_data.ctx.beginPath()
// full_data.ctx.rect(select_area[0],select_area[1],select_area[2],select_area[3],)
// full_data.ctx.stroke()
// full_data.ctx.closePath()
// full_data.ctx.strokeRect(select_area[0],select_area[1],select_area[2],select_area[3],)
// }

// catch{

// }

} else {
full_data.ctx.putImageData(full_data.state_data, 0, 0)
}


do_draw()
console.log(provs_name)
}

const get_select_area = (provs_name) => {
let area = []
let y_min_area = []
let y_max_area = []
let x_min_area = []
let x_max_area = []
for (let prov of provs_name){
y_min_area.push( Math.floor(Math.min.apply(null,full_data.colormap[prov]) / (4*full_data.width)) )
y_max_area.push( Math.floor(Math.max.apply(null,full_data.colormap[prov]) / (4*full_data.width)) )
y_min_area.push( Math.floor(Math.min(...full_data.colormap[prov]) / (4*full_data.width)) )
y_max_area.push( Math.floor(Math.max(...full_data.colormap[prov]) / (4*full_data.width)) )
let color_x_area = full_data.colormap[prov].map(sindex => sindex / 4 % full_data.width)
x_min_area.push( Math.min.apply(null,color_x_area) )
x_max_area.push( Math.max.apply(null,color_x_area) )
x_min_area.push( Math.min(...color_x_area) )
x_max_area.push( Math.max(...color_x_area) )
}
// area = area.map(sindex => {
// let y = Math.floor((sindex/4)/full_data.width)
// let x = (sindex/4) - (full_data.width*y)
// return [sindex,full_data.width,x,y]
// })
// console.log(area)
// console.log(Math.min.apply(null,area))
// let start_y = Math.floor(Math.min.apply(null,area) / (4*full_data.width))
// let select_height = Math.floor(Math.max.apply(null,area) / (4*full_data.width)) - start_y

// area = area.map(sindex => sindex / 4 % full_data.width)
// console.log(area)
// let res = [Math.min.apply(null,area),start_y,Math.max.apply(null,area) - Math.min.apply(null,area),select_height]

// return res
let start_x = Math.min.apply(null,x_min_area)
let start_y = Math.min.apply(null,y_min_area)
let width = Math.max.apply(null,x_max_area) - start_x
let height = Math.max.apply(null,y_max_area) - start_y

let start_x = Math.min(...x_min_area)
let start_y = Math.min(...y_min_area)
let width = Math.max(...x_max_area) - start_x
let height = Math.max(...y_max_area) - start_y
let res = [start_x,start_y,width,height]
console.log(res)
return res
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ import { do_draw } from './drawing_little.js';
import { select_provs ,select_states,select_prov_pure } from './canvas_selection.js';

const canvas_select = function(e){
let imgdata = ctx.getImageData(0,0,canvas.width,canvas.height)
let imgdata = ctx.createImageData(canvas.width,canvas.height)
let label = ""
let x = e.pageX - this.offsetLeft
label += " "
Expand Down
10 changes: 8 additions & 2 deletions src/write.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const blockwrite = (writer,key,value,ambiuious=[],quote=[]) => {
if (value instanceof Array && ambiuious.indexOf(key) >= 0 ){
if (typeof(value[0]) == 'string' || typeof(value[0]) =='number'){
if (typeof(value[0]) == 'string' || typeof(value[0]) =='number' || typeof(value[0]) === "boolean"){
for (let i= 0;i<value.length;i++){
let item = value[i]
writer.write_unquoted(key)
Expand All @@ -10,6 +10,8 @@ const blockwrite = (writer,key,value,ambiuious=[],quote=[]) => {
} else {
writer.write_integer(item)
}
} else if ( typeof(item) == 'boolean' ) {
writer.write_bool(item)
} else {
if (quote.indexOf(key)>-1){
writer.write_quoted(item)
Expand Down Expand Up @@ -39,6 +41,8 @@ const blockwrite = (writer,key,value,ambiuious=[],quote=[]) => {
if (typeof(item) == 'number'){
if (String(item).indexOf(".")>-1) writer.write_f32(item)
else writer.write_integer(item)
} else if ( typeof(item) == 'boolean' ) {
writer.write_bool(item)
} else if (typeof(item) == 'string') {
if (quote.indexOf(key)>-1)writer.write_quoted(item)
else writer.write_unquoted(item)
Expand All @@ -51,14 +55,16 @@ const blockwrite = (writer,key,value,ambiuious=[],quote=[]) => {
}
}
writer.write_end();
} else if( typeof(value) == 'string' || typeof(value) =='number' ) {
} else if( typeof(value) == 'string' || typeof(value) =='number' || typeof(value) === "boolean") {
writer.write_unquoted(key)
if (typeof(value) == 'number'){
if (String(value).indexOf(".")>-1){
writer.write_f32(value)
} else {
writer.write_integer(value)
}
} else if ( typeof(value) == 'boolean' ) {
writer.write_bool(value)
} else {
if (quote.indexOf(key)>-1) {
writer.write_quoted(value)
Expand Down
Binary file modified start.exe
Binary file not shown.
4 changes: 3 additions & 1 deletion start.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ func handle_upload(rw http.ResponseWriter, r *http.Request) {
}
log.Printf("dump to %s ", src)
for key, value := range data {
err := ioutil.WriteFile(src+"/"+key, []byte(value), 0666)
byte_to_write := []byte{0xef, 0xbb, 0xbf}
byte_to_write = append(byte_to_write, []byte(value)...)
err := ioutil.WriteFile(src+"/"+key, byte_to_write, 0666)
if err != nil {
log.Println(err)
}
Expand Down

0 comments on commit 40064f0

Please sign in to comment.