Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html lang="us">
- <head>
- <meta charset="utf-8">
- <title>MAP GENERATOR</title>
- <link href="jquery-ui-1.12.1/jquery-ui.css" rel="stylesheet">
- <style>
- #petaku{
- border:1px solid green;
- background-color:lighblue;
- }
- .p_0{
- background-color:#79deff;
- }
- .p_1{
- background-color:#74dcfe;
- }
- .p_2{
- background-color:#bfb5b8;
- }
- .p_5{
- background-color:#a0ed6b;
- }
- .p_7{
- background-color:#42900d;
- }
- .p_9{
- background-color:#486fde;
- }
- .p_11{
- background-color:#833142;
- }
- .p_101{
- background-color:#f2e70e;
- }
- .p_13{
- background-color:#aeab6c;
- }
- .p_21{
- background-color:#5f25a7;
- }
- #petaku td{
- width:15px;height:15px;
- }
- /*
- arDice = [4,6,8,10,100,12,20];
- #petaku tr:nth-child(odd){
- background-color:lightblue;
- }
- #petaku tr:nth-child(even){
- background-color:beige;
- }
- */
- </style>
- </head>
- <body>
- example can be seen on : https://jsfiddle.net/cfyktsa3/
- <input type='button' value='Run' onclick='jalankan()' />
- <table border=0 id='petaku' cellpadding=2 cellspacing=0>
- </table>
- <script src="jquery-ui-1.12.1/external/jquery/jquery.js"></script>
- <script src="jquery-ui-1.12.1/jquery-ui.js"></script>
- <script>
- function jalankan(){
- param={}
- param['height']=25;
- param['width']=25;
- param['water_ratio']=60; //dibawah 100
- param['dice_ratio']=15; //dibawah 10-20
- logs('start');
- logs(param);
- param['base'] = param_base_map(param);
- map_base = generate_base_map(param);
- map_base = smooth_map(map_base);
- j1=map_base.length;
- j2=map_base[0].length;
- $("#petaku").empty();
- for(i1=-2;i1<j1+2;i1++){
- txt="<tr>";
- for(i2=-2;i2<j2+2;i2++){
- if(typeof map_base[i1] !== 'undefined'){
- if(typeof map_base[i1][i2] !== 'undefined'){
- id_class = Math.floor( map_base[i1][i2] /100 ) + 1;
- }
- else{
- id_class=0;
- }
- }
- else{
- id_class=0;
- }
- txt=txt+"<td class='p_"+id_class+"'> </td>";
- if(id_class!=1&&id_class!=2) logs([id_class,i1,i2] );
- }
- txt=txt+"</tr>";
- $("#petaku").append(txt);
- }
- logs('end');
- }
- function smooth_map(map0){
- maps=map0;
- j1=maps.length;
- j2=maps[0].length;
- for(i1=0;i1<j1;i1++){
- for(i2=0;i2<j2;i2++){
- code = maps[i1][i2];
- if(code!=0&&code!=101){ //bukan air/laut
- //atas
- i3=i1;
- i4=i2-1;
- if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
- if(maps[i3][i4]==0)
- maps[i3][i4]=101;
- }
- //bawah
- i3=i1;
- i4=i2+1;
- if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
- if(maps[i3][i4]==0)
- maps[i3][i4]=101;
- }
- //kiri
- i3=i1-1;
- i4=i2;
- if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
- if(maps[i3][i4]==0)
- maps[i3][i4]=101;
- }
- //kanan
- i3=i1+1;
- i4=i2;
- if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
- if(maps[i3][i4]==0)
- maps[i3][i4]=101;
- }
- //===========diagonal
- //atas
- i3=i1-1;
- i4=i2-1;
- if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
- if(maps[i3][i4]==0)
- maps[i3][i4]=101;
- }
- //bawah
- i3=i1+1;
- i4=i2+1;
- if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
- if(maps[i3][i4]==0)
- maps[i3][i4]=101;
- }
- //kiri
- i3=i1-1;
- i4=i2+1;
- if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
- if(maps[i3][i4]==0)
- maps[i3][i4]=101;
- }
- //kanan
- i3=i1+1;
- i4=i2-1;
- if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
- if(maps[i3][i4]==0)
- maps[i3][i4]=101;
- }
- }else{}
- }
- }
- return maps;
- }
- function generate_base_map(param){
- base = param['base'];
- maps=[ ]; //0: water
- maps_tmp=[];
- //=====create water
- for(i=0;i<param['height'];i++){
- map=[];
- for(j=0;j<param['width'];j++){
- map[j]=0;
- n=i*param['height'] + j;
- maps_tmp[n]=0;
- }
- maps[ i ]=map;
- }
- total= base['size'];
- total_land=base['total']['allow_land']; //
- start=0;
- //=====angka terkecil dari lebar dan tinggi
- low_number = param['height'];
- if(param['height']>param['width'])
- low_number=param['width'];
- //total_land=2;
- n=0;
- land_dice=[];
- arDice = dice_set();
- for(i=0;i<total_land;i++){
- n = (Math.ceil( Math.random() * low_number*2432) + 7 ) % arDice.length;
- dice = arDice[n];
- roll = (Math.ceil( Math.random() * low_number*4343) % dice )+1;
- num_dice = dice*100+roll;
- land_dice[i]=num_dice;
- logs(['input dice',i,n,dice,roll,num_dice ]);
- // land_dice[i]=101; //asumsikan bahwa land tersebut tidak digulirkan dadu
- }
- logs('total dice :'+arDice.length);
- logs(['land_dice', land_dice]);
- j=0;j2= base['total']['land']*4;input_dice=0;
- arDice = dice_set();
- total_land=j3=base['total']['allow_land'];
- ar=[];nPos=0;
- /*
- while(j3 > 0 )
- {
- //dice => ada 7
- //4,6,8,10,100,12,20
- j++;
- move=Math.ceil( Math.random() * low_number * 5)+1;
- n0=nPos;
- nPos=(n0+move)% total_land;
- logs(['land_dice0',n0,move,nPos,total_land, land_dice[n]]);
- if(land_dice[nPos]==101){
- j3--;
- //=========
- n = (Math.ceil( Math.random() * low_number*2432) + 7 ) % arDice.length;
- dice = arDice[n];
- roll = (Math.ceil( Math.random() * low_number*4343) % dice )+1;
- num_dice = dice*100+roll;
- logs(["num dice",n,dice,roll,num_dice]);
- ar[nPos]=num_dice;
- land_dice[nPos]=num_dice;
- input_dice++;
- }
- else{
- num_dice=0;
- }
- logs(["land_dice1",j,j2,n,move,j3,num_dice,land_dice[n] ]);
- if(j> j2 ){
- j3=-1;
- logs('kelebihan?!! '+j2);
- }
- }
- */
- // logs('input random(1):'+input_dice+"/"+land_dice.length);
- logs(ar);
- total_land=base['total']['allow_land'];//['land'];
- n=0;j=0;input_dice=0;
- ar_input=[];
- while(total_land > 0 )
- {
- move=Math.ceil( Math.random() * low_number)+1;
- n=(n+move)% total;
- if(maps_tmp[n]==0){
- j = j % land_dice.length;
- code_land = land_dice[j];
- maps_tmp[n]=code_land;
- total_land--;
- j++;
- if(code_land!=101){
- ar_input[n]=code_land;
- input_dice++;
- }
- }
- else{
- code_land=101;
- }
- // logs(['create land',n,move,total_land,code_land]);
- }
- logs(land_dice);
- logs('input random(2):'+input_dice+"/"+j);
- logs(ar_input);
- for(i=0;i<maps_tmp.length;i++){
- x=Math.floor( i/param['height']);
- y=i % param['width'];
- maps[x][y] = maps_tmp[i];
- }
- logs(base);
- logs(maps);
- return maps;
- }
- function dice_set(){
- arDice = [4,6,8,10,100,12,20];
- return arDice;
- }
- //===========Membuat peta dasar
- function param_base_map(param){
- total = param['height'] * param['width'];
- res={ 'raw':param};
- res['size']=total;
- logs('total grid:'+total);
- total_water = Math.round( total * param['water_ratio'] / 100);
- total_land = total - total_water;
- total_allow = Math.round( total_land * param['dice_ratio'] / 100);
- res['total']= objTotal = {
- 'water':total_water,
- 'land':total_land,
- 'allow_land':total_allow
- }
- logs(objTotal);
- logs(res);
- return res;
- }
- //if (a[i] === undef || a[i] === null)
- function logs(txt){
- console.log(Date(),txt);
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement