Advertisement
kura2yamato

map generator

May 6th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 6.73 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="us">
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>MAP GENERATOR</title>
  6.     <link href="jquery-ui-1.12.1/jquery-ui.css" rel="stylesheet">
  7. <style>
  8. #petaku{
  9. border:1px solid green;
  10. background-color:lighblue;
  11. }
  12.  
  13. .p_0{
  14.     background-color:#79deff;
  15. }
  16. .p_1{
  17.     background-color:#74dcfe;
  18. }
  19. .p_2{
  20.     background-color:#bfb5b8;
  21. }
  22. .p_5{
  23.     background-color:#a0ed6b;
  24. }
  25. .p_7{
  26.     background-color:#42900d;
  27. }
  28. .p_9{
  29.     background-color:#486fde;
  30. }
  31. .p_11{
  32.     background-color:#833142;
  33. }
  34. .p_101{
  35.     background-color:#f2e70e;
  36. }
  37. .p_13{
  38.     background-color:#aeab6c;
  39. }
  40. .p_21{
  41.     background-color:#5f25a7;
  42. }
  43.  
  44. #petaku td{
  45. width:15px;height:15px;
  46. }
  47. /*
  48.  arDice = [4,6,8,10,100,12,20];
  49. #petaku tr:nth-child(odd){
  50. background-color:lightblue;
  51.  
  52. }
  53. #petaku tr:nth-child(even){
  54. background-color:beige;
  55.  
  56. }
  57. */
  58. </style>
  59. </head>
  60. <body>
  61. example can be seen on : https://jsfiddle.net/cfyktsa3/
  62. <input type='button' value='Run' onclick='jalankan()' />
  63.  
  64. <table border=0 id='petaku' cellpadding=2 cellspacing=0>
  65.  
  66. </table>
  67. <script src="jquery-ui-1.12.1/external/jquery/jquery.js"></script>
  68. <script src="jquery-ui-1.12.1/jquery-ui.js"></script>
  69. <script>
  70. function jalankan(){
  71.     param={}
  72.     param['height']=25;
  73.     param['width']=25;
  74.     param['water_ratio']=60; //dibawah 100
  75.     param['dice_ratio']=15; //dibawah 10-20
  76.     logs('start');
  77.     logs(param);
  78.     param['base'] = param_base_map(param);
  79.     map_base = generate_base_map(param);
  80.     map_base = smooth_map(map_base);
  81.    
  82.     j1=map_base.length;
  83.     j2=map_base[0].length;
  84.     $("#petaku").empty();
  85.     for(i1=-2;i1<j1+2;i1++){
  86.         txt="<tr>";
  87.         for(i2=-2;i2<j2+2;i2++){
  88.             if(typeof map_base[i1] !== 'undefined'){
  89.                 if(typeof map_base[i1][i2] !== 'undefined'){
  90.                     id_class = Math.floor( map_base[i1][i2] /100 ) + 1;
  91.                 }
  92.                 else{
  93.                     id_class=0;
  94.                 }
  95.             }
  96.             else{
  97.               id_class=0;
  98.             }
  99.             txt=txt+"<td class='p_"+id_class+"'>&nbsp;</td>";
  100.             if(id_class!=1&&id_class!=2) logs([id_class,i1,i2] );
  101.         }
  102.         txt=txt+"</tr>";
  103.         $("#petaku").append(txt);
  104.     }
  105.    
  106.     logs('end');
  107. }
  108.  
  109. function smooth_map(map0){
  110. maps=map0;
  111.     j1=maps.length;
  112.     j2=maps[0].length;
  113.     for(i1=0;i1<j1;i1++){
  114.         for(i2=0;i2<j2;i2++){
  115.             code = maps[i1][i2];
  116.             if(code!=0&&code!=101){ //bukan air/laut
  117.         //atas
  118.                 i3=i1;
  119.                 i4=i2-1;
  120.                 if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
  121.                     if(maps[i3][i4]==0)
  122.                         maps[i3][i4]=101;
  123.                 }
  124.  
  125.         //bawah
  126.                 i3=i1;
  127.                 i4=i2+1;
  128.                 if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
  129.                     if(maps[i3][i4]==0)
  130.                         maps[i3][i4]=101;
  131.                 }
  132.  
  133.         //kiri
  134.                 i3=i1-1;
  135.                 i4=i2;
  136.                 if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
  137.                     if(maps[i3][i4]==0)
  138.                         maps[i3][i4]=101;
  139.                 }
  140.  
  141.         //kanan
  142.                 i3=i1+1;
  143.                 i4=i2;
  144.                 if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
  145.                     if(maps[i3][i4]==0)
  146.                         maps[i3][i4]=101;
  147.                 }
  148. //===========diagonal
  149.         //atas
  150.                 i3=i1-1;
  151.                 i4=i2-1;
  152.                 if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
  153.                     if(maps[i3][i4]==0)
  154.                         maps[i3][i4]=101;
  155.                 }
  156.  
  157.         //bawah
  158.                 i3=i1+1;
  159.                 i4=i2+1;
  160.                 if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
  161.                     if(maps[i3][i4]==0)
  162.                         maps[i3][i4]=101;
  163.                 }
  164.  
  165.         //kiri
  166.                 i3=i1-1;
  167.                 i4=i2+1;
  168.                 if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
  169.                     if(maps[i3][i4]==0)
  170.                         maps[i3][i4]=101;
  171.                 }
  172.  
  173.         //kanan
  174.                 i3=i1+1;
  175.                 i4=i2-1;
  176.                 if(i3>=0&&i4>=0&&i3<j1&&i4<j2){
  177.                     if(maps[i3][i4]==0)
  178.                         maps[i3][i4]=101;
  179.                 }
  180.  
  181.             }else{}
  182.         }
  183.  
  184.     }
  185. return maps;
  186. }
  187.  
  188. function generate_base_map(param){
  189. base = param['base'];
  190. maps=[ ]; //0: water
  191. maps_tmp=[];
  192. //=====create water
  193.     for(i=0;i<param['height'];i++){
  194.     map=[];
  195.         for(j=0;j<param['width'];j++){
  196.             map[j]=0;
  197.             n=i*param['height'] + j;
  198.             maps_tmp[n]=0;
  199.         }
  200.         maps[ i ]=map;
  201.     }
  202.    
  203.     total= base['size'];   
  204.     total_land=base['total']['allow_land']; //
  205.     start=0;
  206.     //=====angka terkecil dari lebar dan tinggi
  207.     low_number = param['height'];
  208.     if(param['height']>param['width'])
  209.         low_number=param['width'];
  210.     //total_land=2;
  211.     n=0;
  212.     land_dice=[];
  213.     arDice = dice_set();
  214.     for(i=0;i<total_land;i++){
  215.         n = (Math.ceil( Math.random() * low_number*2432) + 7 ) % arDice.length;
  216.         dice = arDice[n];
  217.         roll = (Math.ceil( Math.random() * low_number*4343) % dice )+1;
  218.         num_dice = dice*100+roll;
  219.         land_dice[i]=num_dice;
  220.         logs(['input dice',i,n,dice,roll,num_dice ]);
  221. //      land_dice[i]=101; //asumsikan bahwa land tersebut tidak digulirkan dadu
  222.     }
  223.    
  224.     logs('total dice :'+arDice.length);
  225.     logs(['land_dice', land_dice]);
  226.     j=0;j2= base['total']['land']*4;input_dice=0;
  227.     arDice = dice_set();
  228.    
  229.     total_land=j3=base['total']['allow_land'];
  230.     ar=[];nPos=0;
  231. /
  232.     while(j3 > 0 )
  233.     {
  234.     //dice => ada 7
  235.     //4,6,8,10,100,12,20
  236.         j++;
  237.         move=Math.ceil( Math.random() * low_number * 5)+1;
  238.         n0=nPos;
  239.         nPos=(n0+move)% total_land;
  240.         logs(['land_dice0',n0,move,nPos,total_land, land_dice[n]]);
  241.         if(land_dice[nPos]==101){
  242.             j3--;
  243. //=========
  244.             n = (Math.ceil( Math.random() * low_number*2432) + 7 ) % arDice.length;
  245.             dice = arDice[n];
  246.             roll = (Math.ceil( Math.random() * low_number*4343) % dice )+1;
  247.             num_dice = dice*100+roll;
  248.             logs(["num dice",n,dice,roll,num_dice]);
  249.             ar[nPos]=num_dice;
  250.             land_dice[nPos]=num_dice;
  251.             input_dice++;
  252.         }
  253.         else{
  254.             num_dice=0;
  255.         }
  256.        
  257.         logs(["land_dice1",j,j2,n,move,j3,num_dice,land_dice[n] ]);
  258.         if(j> j2 ){
  259.             j3=-1;
  260.             logs('kelebihan?!! '+j2);
  261.         }
  262.     }
  263. */
  264. //  logs('input random(1):'+input_dice+"/"+land_dice.length);
  265.     logs(ar);
  266.     total_land=base['total']['allow_land'];//['land'];
  267.     n=0;j=0;input_dice=0;
  268.     ar_input=[];
  269.     while(total_land > 0 )
  270.     {
  271.         move=Math.ceil( Math.random() * low_number)+1;
  272.         n=(n+move)% total;
  273.        
  274.         if(maps_tmp[n]==0){
  275.             j = j % land_dice.length;
  276.             code_land = land_dice[j];
  277.             maps_tmp[n]=code_land;
  278.             total_land--;
  279.             j++;
  280.            
  281.             if(code_land!=101){
  282.                 ar_input[n]=code_land;
  283.                 input_dice++;
  284.             }
  285.         }
  286.         else{
  287.             code_land=101;
  288.         }
  289.     //  logs(['create land',n,move,total_land,code_land]);
  290.     }
  291.     logs(land_dice);
  292.     logs('input random(2):'+input_dice+"/"+j);
  293.     logs(ar_input);
  294.     for(i=0;i<maps_tmp.length;i++){
  295.         x=Math.floor( i/param['height']);
  296.         y=i % param['width'];
  297.         maps[x][y] = maps_tmp[i];
  298.     }
  299.  
  300.  
  301. logs(base);
  302. logs(maps);
  303.     return maps;
  304. }
  305.  
  306. function dice_set(){
  307.  arDice = [4,6,8,10,100,12,20];
  308.  return arDice;
  309. }
  310. //===========Membuat peta dasar
  311. function param_base_map(param){
  312.     total = param['height'] * param['width'];
  313.     res={ 'raw':param};
  314.     res['size']=total;
  315.     logs('total grid:'+total);
  316.    
  317.    
  318.     total_water = Math.round( total * param['water_ratio'] / 100);
  319.     total_land = total - total_water;
  320.     total_allow = Math.round( total_land * param['dice_ratio'] / 100);
  321.     res['total']= objTotal = {
  322.         'water':total_water,
  323.         'land':total_land,
  324.         'allow_land':total_allow
  325.     }
  326.     logs(objTotal);
  327.     logs(res);
  328.     return res;
  329. }
  330.  
  331.  
  332. //if (a[i] === undef || a[i] === null)
  333. function logs(txt){
  334.     console.log(Date(),txt);
  335. }
  336. </script>
  337.  
  338. </body>
  339. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement