用户:
23332808查看:5 回复:2 评论:5 创建时间:2024-06-24T15:17:50
function club(sx, sy, sz, xsize, ysize, zsize,vox){
var xend = sx+xsize;
var yend = sy+ysize;
var zend = sz+zsize;
for(var x=sx;x<xend;x++){
for(var y=sy;y<yend;y++){
for(var z=sz;z<zend;z++){
voxels.setVoxel(x,y,z,vox)
}}}
}
class my_function{
c喵tructor(p1,p2){
this.b=p1.x
this.h=p1.y
this.a=(p2.y-this.h)/(p2.x-this.b)
}
get_number(x){
return this.a*(x-this.b)+this.h
}
}
class block{
c喵tructor(p1,p2,p3,p4,size){
this.func1=new my_function({x:p1.x,y:p1.y},{x:p2.x,y:p2.y})
this.func2=new my_function({x:p3.x,y:p3.y},{x:p4.x,y:p4.y})
this.func_list=[]
for(this.i of range(p1.x,p2.x,size)){
this.func_list.push(new my_function({x:p1.z,y:this.func1.get_number(this.i)},{x:p3.z,y:this.func2.get_number(this.i)}))
}
this.start_x=p1.x
this.size=size
}
get_number(x1,z1){
return this.func_list[Math.floor((x1-this.start_x)/this.size)].get_number(z1)
}
}
class new_world{
c喵tructor(y1,y2,y3,y4,random_number){
this.random_number=random_number
this.height_list=[
[y1,y2,0],
[y3,y4,0],
[0,0,0]
]
this.size=128
while (this.size>8){
this.block_list=[]
for(this.z=0;this.z<this.height_list.length-1;this.z++){
this.block_list2=[]
for(this.x=0;this.x<this.height_list[0].length-1;this.x++){
this.block_list2.push(new block({x:this.x*this.size,y:this.height_list[this.z][this.x],z:this.z*this.size},
{x:(this.x+1)*this.size,y:this.height_list[this.z][this.x+1],z:this.z*this.size},
{x:this.x*this.size,y:this.height_list[this.z+1][this.x],z:(this.z+1)*this.size},
{x:(this.x+1)*this.size,y:this.height_list[this.z+1][this.x+1],z:(this.z+1)*this.size},this.size/2))
}
this.block_list.push(this.block_list2)
}
this.height_list=[]
for(this.z=0;this.z<this.block_list.length;this.z++){
this.height_list21=[]
this.height_list22=[]
for(this.x=0;this.x<this.block_list[0].length;this.x++){
this.height_block=this.block_list[this.z][this.x]
this.height_list21.push(this.height_block.get_number(this.x*this.size,this.z*this.size))
this.height_list21.push(this.height_block.get_number(this.x*this.size+this.size/2,this.z*this.size))
this.height_list22.push(this.height_block.get_number(this.x*this.size,this.z*this.size+this.size/2))
this.height_list22.push(this.height_block.get_number(this.x*this.size+this.size/2,this.z*this.size+this.size/2))
}
this.height_list.push(this.height_list21)
this.height_list.push(this.height_list22)
}
this.size/=2
this.random_number/=1.5
this.random()
}
this.block_list=[]
for(this.z=0;this.z<this.height_list.length-1;this.z++){
this.block_list2=[]
for(this.x=0;this.x<this.height_list[0].length-1;this.x++){
this.block_list2.push(new block({x:this.x*this.size,y:this.height_list[this.z][this.x],z:this.z*this.size},
{x:(this.x+1)*this.size,y:this.height_list[this.z][this.x+1],z:this.z*this.size},
{x:this.x*this.size,y:this.height_list[this.z+1][this.x],z:(this.z+1)*this.size},
{x:(this.x+1)*this.size,y:this.height_list[this.z+1][this.x+1],z:(this.z+1)*this.size},1))
}
this.block_list.push(this.block_list2)
}
}
random(){
for(this.z in this.height_list){
for(this.x in this.height_list[this.z]){
this.height_list[this.z][this.x]+=(Math.random()-0.5)*2*this.random_number
}
}
}
async build(func){
for(this.z=0;this.z<this.height_list.length-1;this.z++){
for(this.x=0;this.x<this.height_list[0].length-1;this.x++){
this.block=new block({x:this.x*this.size,y:this.height_list[this.z][this.x],z:this.z*this.size},
{x:(this.x+1)*this.size,y:this.height_list[this.z][this.x+1],z:this.z*this.size},
{x:this.x*this.size,y:this.height_list[this.z+1][this.x],z:(this.z+1)*this.size},
{x:(this.x+1)*this.size,y:this.height_list[this.z+1][this.x+1],z:(this.z+1)*this.size},1)
for(this.x1=this.x*this.size;this.x1<(this.x+1)*this.size;this.x1++){
for(this.z1=this.z*this.size;this.z1<(this.z+1)*this.size;this.z1++){
func(this.x1,this.z1,this.block.get_number(this.x1,this.z1))
}
}
await sleep(30)
}
}
}
}
function sphere( cx, cy, cz, radius,vox){
let xend = cx+radius
let yend = cy+radius
let zend = cz+radius
for(let x=cx-radius;x<=xend;x++){
for(let y=cy-radius;y<=yend;y++){
for(let z=cz-radius;z<=zend;z++){
let dx = x-cx;
let dy = y-cy;
let dz = z-cz;
if(Math.round(Math.sqrt(dx*dx+dy*dy+dz*dz)) <= radius){
voxels.setVoxel(x,y,z,vox)
}
}}}
}
class cave{
constructor(x,y,z,up,corner,size,length,change_size=1,change_size1=3,change_up=0.25,change_corner=0.25){
this.cave_x=x
this.cave_y=y
this.cave_z=z
this.cave_corner=corner
this.cave_up=up
this.cave_size=size
this.cave_length=length
for(this.i=0;this.i<this.cave_length;this.i++){
this.cave_step=randint(1,this.cave_size-1)
this.cave_y+=Math.round(Math.sin(this.cave_up)*this.cave_step)
this.cave_step2=Math.cos(this.cave_up)*this.cave_step
this.cave_x+=Math.round(Math.sin(this.cave_corner)*this.cave_step2)
this.cave_z+=Math.round(Math.cos(this.cave_corner)*this.cave_step2)
sphere(this.cave_x,this.cave_y,this.cave_z,this.cave_size,"air")/*记得换成air*/
this.cave_size+=randint(0,change_size*2)-change_size
this.cave_up+=Math.random()*change_up-0.5*change_up
if(this.cave_size<(1)||this.cave_size>(4))this.cave_size=size
this.cave_corner+=(Math.random()*change_corner-0.5*change_corner)
if(this.cave_x<0||this.cave_x>256||this.cave_y<0||this.cave_y>128||this.cave_z<0||this.cave_z>256)return
}}}
class new_plain extends new_world{
random(){
for(this.z in this.height_list){
for(this.x in this.height_list[this.z]){
this.height_list[this.z][this.x]+=(Math.random()-0.5)*2*this.random_number
}
}
if(this.size==32){
this.height_list[randint(0,4)][randint(0,4)]=80
}
}
}
class new_sea extends new_world{
random(){
for(this.z in this.height_list){
for(this.x in this.height_list[this.z]){
this.height_list[this.z][this.x]+=(Math.random()-0.5)*2*this.random_number
}
}
if(this.size==32){
for(this.i of range(4)){this.height_list[randint(0,4)][randint(0,4)]=100}
}
}
}
function wood_tree(x, z) {
for (var y = 100; y > 84; y--) {
if (voxels.name(voxels.getVoxel(x, y, z)) == "grass" && voxels.getVoxel(x, y + 1, z) == 0) {
if (voxels.getVoxel(x - 2, y + 3, z) == 0 && voxels.getVoxel(x + 2, y + 3, z) == 0 && voxels.getVoxel(x, y + 3, z + 2) == 0 && voxels.getVoxel(x, y + 3, z - 2) == 0) {
club(x - 2, y + 3, z - 2, 5, 2, 5, "green_leaf")
club(x - 1, y + 5, z - 1, 3, 1, 3, "green_leaf")
club(x - 1, y + 6, z, 3, 1, 1, 'green_leaf')
club(x, y + 6, z - 1, 1, 1, 3, 'green_leaf')
club(x, y + 1, z, 1, 5, 1, "wood")
break
} else {
break
}
}
}
}
class plain{
async setup(){
this.water_level=82
this.new_world=new new_plain(randint(80,90),randint(80,90),randint(80,90),randint(80,90),3)
await this.new_world.build((x,z,h)=>{
club(x,0,z,1,h-4,1,"stone")
club(x,h-4,z,1,3,1,"dirt")
if(h<=this.water_level+1){
club(x,h-2,z,1,2,1,"sand")
club(x,h,z,1,this.water_level-h,1,"water")
}else{
club(x,h-1,z,1,1,1,"grass")
}
})
for(this.i=0;this.i<500;this.i++){
wood_tree(randint(0,128),randint(0,128))
}
}
}
function cylinder( cx, cy, cz, radius, height,vox) {
let xend = cx + radius
let yend = cy + height
let zend = cz + radius
for (let x = cx - radius; x <= xend; x++) {
for (let z = cz - radius; z <= zend; z++) {
let dx = x - cx;
let dz = z - cz;
if (Math.round(Math.sqrt(dx * dx + dz * dz)) <= radius) {
for (let y = cy; y < yend; y++) {
voxels.setVoxel(x, y, z, vox)
}
}
}
}
}
class woods{
async setup(){
this.water_level=84
this.new_world=new new_world(randint(85,95),randint(85,95),randint(85,95),randint(85,95),10)
await this.new_world.build((x,z,h)=>{
club(x,0,z,1,h-4,1,"stone")
club(x,h-4,z,1,3,1,"dirt")
if(h<=this.water_level+1){
club(x,h-2,z,1,2,1,"sand")
club(x,h,z,1,this.water_level-h,1,"water")
}else{
club(x,h-1,z,1,1,1,"grass")
}
})
for(this.i=0;this.i<200;this.i++){
wood_tree(randint(0,128),randint(0,128))
}
}
}
class sea{
async setup(){
this.water_level=90
this.new_world=new new_sea(randint(80,85),randint(80,85),randint(80,85),randint(80,85),5)
await this.new_world.build((x,z,h)=>{
club(x,0,z,1,h-4,1,"stone")
club(x,h-4,z,1,3,1,"dirt")
if(h<=this.water_level+1){
club(x,h-2,z,1,2,1,"sand")
club(x,h,z,1,this.water_level-h,1,"water")
}else{
club(x,h-1,z,1,1,1,"grass")
}
})
for(this.i=0;this.i<200;this.i++){
wood_tree(randint(0,128),randint(0,128))
}
}
}
class desert{
async setup(){
this.new_world=new new_world(randint(80,95),randint(80,95),randint(80,95),randint(80,95),10)
await this.new_world.build((x,z,h)=>{
club(x,0,z,1,h-4,1,"stone")
club(x,h-4,z,1,4,1,"sand")
})
world.fogColor=new GameRGBColor(232,139,0)
world.fogStartDistance=0
world.fogHeightOffset=128
world.fogUnifor喵ensity=0.5
world.fogHeightFalloff=0.1
world.maxFog=0.5
}
}
class mountain{
async setup(){
this.new_world=new new_plain(randint(80,100),randint(80,100),randint(80,100),randint(80,90),30)
await this.new_world.build((x,z,h)=>{
club(x,0,z,1,h,1,"stone")
})
}
}
class snow{
async setup(){
this.water_level=83
this.new_world=new new_plain(randint(85,90),randint(85,90),randint(85,90),randint(85,90),20)
await this.new_world.build((x,z,h)=>{
club(x,0,z,1,h-4,1,"stone")
club(x,h-4,z,1,3,1,"dirt")
if(h<=this.water_level+1){
club(x,h-2,z,1,2,1,"sand")
club(x,h,z,1,this.water_level-h,1,"water")
if(h<=this.water_level){
club(x,this.water_level,z,1,1,1,"ice")
}else{
club(x,h,z,1,1,1,"snow_grass")
}
}else{
club(x,h-1,z,1,1,1,"snow_grass")
}
})
for(this.i=0;this.i<100;this.i++){
this.tree(randint(0,128),randint(0,128))
}
}
tree(x,z){
for (this.height = 120; this.height > 84; this.height--) {
if (voxels.name(voxels.getVoxel(x, this.height, z)) == "snow_grass" && voxels.getVoxel(x, this.height + 1, z) == 0) {
cylinder(x,this.height+3,z,4,1,"green_leaf")
cylinder(x,this.height+4,z,3,1,"green_leaf")
cylinder(x,this.height+6,z,3,1,"green_leaf")
cylinder(x,this.height+7,z,2,1,"green_leaf")
club(x-1,this.height+9,z,3,1,1,"green_leaf")
club(x,this.height+9,z-1,1,1,3,"green_leaf")
club(x+0.5,this.height+10,z+0.5,1,1,1,"green_leaf")
club(x,this.height,z,1,9,1,"wood")
}
}
}
}
function get_heigth(x,z){
for(y=128;y>70;y--){
if(voxels.getVoxel(x,y,z)){
return new GameVector3(x,y,z)
}
}
}
function random_select(list,number){
let random_choice_number=0
let random_choice_random=Math.random()
for(i=0;i<number.length;i++){
random_choice_number+=number[i]
if(random_choice_random<=random_choice_number){
return list[i]
}
}
warn("out of range(random_choice)")
}
function in_stone(hight_place_y, hight_size_x, hight_size_y, hight_size_z, material, hight_time, low_time) {
for (var hard = 0; hard < randint(hight_time, low_time); hard++) {
club(randint(0, 127), randint(0, hight_place_y - hight_size_y), randint(0, 127 - hight_size_z), randint(1, hight_size_x), randint(1, hight_size_y), randint(1, hight_size_z), material)
}
}
async function set_world(){
await (new (random_select([plain,sea,woods,snow,mountain,desert],[0.3,0.1,0.3,0.2,0.05,0.05]))).setup()
in_stone(75, 3, 3, 3, 'black', 2000, 3000)
in_stone(65, 3, 2, 3, 'orange', 2000, 3000)
in_stone(45, 2, 2, 2, 'medium_yellow', 400, 800)
in_stone(60, 2, 2, 2, 'red', 500, 1200)
in_stone(14, 2, 2, 2, 'medium_spring_green', 400, 600)
in_stone(10, 2, 2, 2, 'medium_green', 2, 5)
in_stone(60, 3, 3, 3, "strawberry_juice", 150, 260)
for (i = 0; i < randint(5, 8); i++) {
await(new cave(randint(0,128),randint(0,70),randint(0,128),Math.random()-0.5,Math.PI*(Math.random()-0.5),
randint(1,3),randint(30,50)))
}
for (x_grass = 0; x_grass < 129; x_grass++) {
await sleep(1)
for (z_grass = 0; z_grass < 129; z_grass++) {
club(x_grass, 0, z_grass, 1, randint(1, 6), 1, "dark_gray")
}
}
world.onTick(({ tick }) => {
if(tick%2560==0){
world.sunPhase+=0.01
if(world.sunPhase>1){
world.sunPhase-=1
}
}
});
world.onTick(({ tick }) => {
if(tick%16==0){
if(world.sunPhase>0.5){
while(monsters.length<50){
random_select([set_zombie,set_skeleton,set_creeper,set_shadow,set_spider],[0.3,0.2,0.1,0.3,0.1])(get_heigth(randint(3,125),randint(3,125)))
}
}else{
while(animals.length<10){
try{
random_select([set_chicken,set_sheep,set_cow,set_pig],[0.5,0.1,0.1,0.1,0.2])(get_heigth(randint(3,125),randint(3,125)))
}catch(e){
}
}
}
}
})
leavesInit()
world.querySelectorAll("player").forEach(e=>[
e.reset_spawnpoint()
])
}
set_world()
class item_block{
constructor(item,able_list){
this.item=item
this.item.bag=this
this.able_list=able_list
this.free=true
this.options_list=["取出","放入"]
}
show(){
return this.item.show()
}
give(item){
if(this.item===Empty_item){
this.item=set_item(item,this)
return true
}else{
if(this.item.give(item)){
return true
}else{
return false
}
}
}
take(item){
if(this.item.have(item)){
this.item.take(item)
return true
}else{
return false
}
}
splice(){
this.item=Empty_item
}
async give_dialog(e){
this.give_select=e.bag.select((item)=>{
if(this.item===Empty_item){
if(this.able_list.includes(item.name)){
return true
}else{
return false
}
}else{
if(item.name==this.item.name){
return true
}else{
return false
}
}
})
this.give_dialog1=await e.player.select_dialog({
title:"放入",
options:show(this.give_select)
})
if(!this.give_dialog1)return
this.give_item=this.give_select[this.give_dialog1.index]
if(this.give_item.type.includes("uncountable_item")){
if(e.bag.list.includes(this.give_item)){
if(this.give(this.give_item)){
e.bag.splice(this.give_item)
}
}else{
e.player.select_dialog({
content:"数量不足",
options:["确认"],
title:"放入"
})
}
}else{
this.give_dialog2=await e.player.dialog({
type:"input",
title:"放入",
content:"选择放入数量"
})
if(!this.give_dialog2)return
this.give_number=Number(this.give_dialog2)
if(!(this.give_number>0))return
this.give_number=Math.floor(this.give_number)
this.accurate_give_item={name:this.give_item.name,number:this.give_number}
if(this.give_number<=喵&&e.bag.take(this.accurate_give_item)){
if(!this.give(this.accurate_give_item)){
await sleep(0)
e.bag.give(this.accurate_give_item)
}
}else{
e.player.select_dialog({
content:"数量不足",
options:["确认"],
title:"放入"
})
}
}
}
async take_dialog(e){
if(this.item==Empty_item){
e.player.select_dialog({
content:"没有物品",
title:"取出",
options:["确认"]
})
return
}
if(this.item.type.includes("uncountable_item")){
e.bag.give(this.item)
this.item=Empty_item
}else{
this.take_dialog1=await e.player.dialog({
title:"取出",
content:"取出数量",
type:"input"
})
if(!this.take_dialog1)return
this.take_number=Number(this.take_dialog1)
if(!(this.take_number>0))return
this.take_number=Math.floor(this.take_number)
this.take_item={name:this.item.name,number:this.take_number}
if(this.take(this.take_item)){
e.bag.give(this.take_item)
}else{
e.player.select_dialog({
content:"数量不足",
options:["确认"],
title:"取出"
})
}
}
}
async open(e){
if(this.free){
this.free=false
this.open_dialog=await e.player.select_dialog({
title:"空格",
content:this.show(),
options:this.options_list
})
if(!this.open_dialog){
this.free=true
return
}
if(this.open_dialog.value=="取出"){
await this.take_dialog(e)
}else if (this.open_dialog.value=="放入"){
await this.give_dialog(e)
}
this.free=true
}else{
e.player.select_dialog({
content:"占线中",
options:["确认"],
title:"空格"
})
}
}
}
world.onTick(({tick})=>{
if(tick%16==0){
world.querySelectorAll(".oven").filter((e)=>{
if(!(e.喵elter.item===Empty_item)){
if(e.fuel_time<=0){
if(!(e.fuel.item===Empty_item)){
e.fuel_name=e.fuel.item.name
if((e.product.item===Empty_item||e.product.item.name==Smelters[e.喵elter.item.name].name)&&e.fuel.take({name:e.fuel.item.name,number:1})){
e.fuel_time=Fuels[e.fuel_name]
}else{
return
}
}else{
return
}
}
e.fuel_time-=1
e.burn_time+=1
if(e.burn_time>=10){
e.burn_time=0
if(e.喵elter.item.number>0){
if(e.product.item===Empty_item||e.product.item.name==Smelters[e.喵elter.item.name].name){
name=Smelters[e.喵elter.item.name].name
if(e.喵elter.take({name:e.喵elter.item.name,number:1})){
e.product.give({name:name,number:1})
}
}
}
}
}else{
e.fuel_time=0
e.burn_time=0
}
})
}
})
function searchB(p, r, b) {
var ans = false;
for (let i = p.x - r; i <= p.x + r; i++) {
for (let j = p.z - r; j <= p.z + r; j++) {
for (let k = p.y - r; k <= p.y + r; k++) {
if (voxels.getVoxel(i, k, j) == voxels.id(b)) {
ans = true;
}
}
}
}
return ans
}
world.onTick(async ({ tick }) => {
if (tick % 128 == 0) {
for (let i = 0; i < world.leaves.length; i++) {
if (!searchB(world.leaves[i], 2, "wood") && Math.random() < 0.3) {
if (voxels.getVoxel(world.leaves[i].x, world.leaves[i].y, world.leaves[i].z) == 131) {
voxels.setVoxel(world.leaves[i].x, world.leaves[i].y, world.leaves[i].z, 0)
if (Math.random() < 0.03) { dropper( {name:"木棍", number:1}, [world.leaves[i].x, world.leaves[i].y, world.leaves[i].z]) }
if (Math.random() < 0.01) { dropper( {name:"苹果", number:1}, world.leaves[i].x, world.leaves[i].y, world.leaves[i].z) }
world.leaves.splice(i, 1);
} else { world.leaves.splice(i, 1) }
}
if (i % 100 == 0)
await sleep(20)
}
}
})
world.leaves = [];
async function leavesInit() {
world.leaves = []
for (let i = 0; i < 127; i++) {
for (let j = 0; j < 127; j++) {
for (let k = 20; k < 127; k++) {
if (voxels.getVoxel(i, k, j) == 131) world.leaves.push(new GameVector3(i, k, j))
}
}
await sleep(20);
}
}