IcarusLives

Sheep Dog Game Engine COMMENTED

Jul 10th, 2017
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 15.52 KB | None | 0 0
  1. @echo off & setlocal enableDelayedExpansion
  2.  
  3. %multithread_dispatcher%
  4.     if not exist %temp%\Canvas_3.txt call :map
  5.     call :updateCanvas /f 3
  6.     call :mathMacros
  7.     set /a "dx=18", "dy=14"
  8.     set /a "totalSheep=2", "sheepMoveSpeed=0", "maxSheepRadarDistance=8"
  9.     for /l %%a in (0,1,%totalSheep%) do set /a "sheep_x[%%a]=18 - (%%a + 2)", "sheep_y[%%a]=19 - %%a"
  10. call :multiThread GAME_ENGINE "wasd"
  11. goto :eof
  12.  
  13. :GAME_ENGINE
  14.  
  15.     (   for /l %%# in () do (       set /a "frame+=1", "sheepMoveChance=frame %% (sheepMoveSpeed + 1)", "currentSheep=frame %% (totalSheep + 1)"
  16.            
  17.             REM Collect input from user and set coordinates
  18.             %controller% & %move% w a s d . . . . dx dy
  19.            
  20.             REM Draw all sheep
  21.             for /l %%0 in (0,1,%totalSheep%) do %plot% sheep_x[%%0] sheep_y[%%0] !c[%%0]!
  22.            
  23.             REM Begin CURRENT NPC com-munication
  24.             for /f "tokens=1" %%0 in ("!currentSheep!") do ( set "com=nul"
  25.                
  26.                 REM Reset the totalRaffles for sheep movement, and DirectionIDs
  27.                 set /a "dirID[%%0]*=0", "totalRaffles[%%0]*=0"
  28.                
  29.                 REM To allow sheep to stop/move every    "sheepMoveChance=frame %% (sheepMoveSpeed + 1)"    frames
  30.                 if !sheepMoveChance! equ 0 (
  31.                
  32.                     REM Calculate nearby coordinates for (currentSheep %%0)
  33.                     for %%b in ("-1 -1","0 -1","1 -1","1 0","1 1","0 1","-1 1","-1 0") do (
  34.                         for /f "tokens=1,2" %%c in (%%b) do set /a "sheep[%%0][!dirID[%%0]!]X=%%c + sheep_x[%%0]", "sheep[%%0][!dirID[%%0]!]Y=%%d + sheep_y[%%0]"
  35.                         set /a "dirID[%%0]+=1"
  36.                     )
  37.  
  38.                     for /l %%b in (0,1,7) do (
  39.                         for /l %%c in (0,1,!totalSheep!) do (
  40.                            
  41.                             REM Sheep Heard
  42.                             if not %%c == %%0 (
  43.                                 set /a "t1[%%0]=sheep[%%0][%%b]X - sheep_x[%%c]", "t2[%%0]=sheep[%%0][%%b]Y - sheep_y[%%c]", "distance=%Max%"
  44.                                            if !distance! leq 3 ( set /a "sheep[%%0][%%b]+=2000"
  45.                                     ) else if !distance! equ 4 ( set /a "sheep[%%0][%%b]+=1500"
  46.                                     ) else if !distance! equ 5 ( set /a "sheep[%%0][%%b]+=1400"
  47.                                     ) else if !distance! equ 6 ( set /a "sheep[%%0][%%b]+=1300"
  48.                                     ) else if !distance! equ 7 ( set /a "sheep[%%0][%%b]+=1200"
  49.                                     ) else if !distance! geq 8 ( set /a "sheep[%%0][%%b]+=1100"
  50.                                 )
  51.                             )
  52.                            
  53.                             REM Sheep awareness of DOG
  54.                             set /a "t1[%%0]=dx - sheep_x[%%0]", "t2[%%0]=dy - sheep_y[%%0]", "t3[%%0]=t1[%%0] * t1[%%0]", "distance_D=%Max%"
  55.                             if !distance_D! geq %maxSheepRadarDistance% (
  56.                                 set "c[%%0]=0"
  57.                                            if !distance_D! leq 3 ( set /a "sheep[%%0][%%b]-=3000"
  58.                                     ) else if !distance_D! equ 4 ( set /a "sheep[%%0][%%b]-=2000"
  59.                                     ) else if !distance_D! equ 5 ( set /a "sheep[%%0][%%b]-=1600"
  60.                                     ) else if !distance_D! equ 6 ( set /a "sheep[%%0][%%b]-=250"
  61.                                     ) else if !distance_D! equ 7 ( set /a "sheep[%%0][%%b]-=100"
  62.                                     ) else if !distance_D! geq 8 ( set /a "sheep[%%0][%%b]-=50"
  63.                                 )
  64.                             ) else ( set "c[%%0]=1" )
  65.  
  66.                         )
  67.                        
  68.                         set /a "totalRaffles[%%0]+=!sheep[%%0][%%b]!"
  69.                     )
  70.                    
  71.                     set /a "selectedRaffle=!random! %% !totalRaffles[%%0]!"
  72.                     for /l %%b in (0,1,7) do (
  73.                         set /a "selectedRaffle-=!sheep[%%0][%%b]!"
  74.                         if !selectedRaffle! LEQ 7 if "!com!" equ "nul" set "com=%%b" & set /a "sheep[%%0][%%b]=1"
  75.                     )
  76.                    
  77.                 )
  78.                
  79.                 %move% 1 7 5 3 4 6 2 0 sheep_x[%%0] sheep_y[%%0]
  80.             )
  81.  
  82.             %adjustCamera% dx dy
  83.             call:showCanvas !dx! !dy! D
  84.             call:updateCanvas /v 3
  85.     )   )
  86. exit
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. :canvas
  113. set ^"LF=^
  114.  
  115. ^" Above empty line is required - do not remove
  116. set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
  117.         call:macros
  118.         call:cursorpos 2>nul
  119.         if "%~2" neq "" ( set /a "width=%~1 - 1", "height=%~2 - 1" ) else ( goto :eof )
  120.         if "%~3" neq "" ( set "canvasChar=%~3" ) else ( set "canvasChar=#" )
  121.         if "%~4" neq "" ( set "title=%~4" ) else ( set "title=CANVAS" )
  122.         if "%~7" neq "" ( if /i "%~5" equ "/c" (
  123.                 set "CAM=/c"
  124.                 set /a "CAM_Y=0", "CAM_X=0", "maxX=%~6", "winMaxY=%~7","cols=maxX + 5", "rows=winMaxY + 8"
  125.                 if not defined outerBuffer ( for /l %%a in (-1,1,!maxX!) do (
  126.                     set "outerBuffer=!outerBuffer!%canvasChar%") )
  127.                 if !maxX!    geq %width%  set /a "maxX=width"
  128.                 if !winMaxY! geq %height% set /a "winMaxY=height"
  129.             )
  130.         ) else (
  131.             set /a "cols=width + 5", "rows=height + 8"
  132.             if not defined outerBuffer ( for /l %%a in (-2,1,%width%) do (
  133.                 set "outerBuffer=!outerBuffer!%canvasChar%") )
  134.         )
  135.         if not defined Controller ( set "Controller=set "com="^^& set /p "com="" )
  136.         if not defined multithread_dispatcher ( set "multithread_dispatcher=if "%%~1" neq "" call :%%~1 "%%~2"" )
  137.         for /l %%a in (0,1,%width%)  do set "widthBuffer=!widthBuffer! "
  138.         call:updateCanvas
  139.         if exist "%temp%\cursorpos.exe" (
  140.             set "cls="%temp%\cursorpos.exe" 0 1" ) else ( set "cls=cls" )
  141.         mode con: cols=%cols% lines=%rows%
  142.         title %title%
  143.     goto :eof
  144.    
  145.     :saveCanvas
  146.         if "%~2" neq "" ( if /i "%~1" equ "/f" (
  147.                 if exist "%tmp%\Canvas_%~2.txt" ( del /f /q "%tmp%\Canvas_%~2.txt" 2>nul )
  148.                 for /l %%a in (0,1,%height%) do ( echo="!_[%%a]!">>"%tmp%\Canvas_%~2.txt")
  149.                 <nul set /p ".=:: "%width%+1" "%height%+1" "%canvasChar%" "%title%" "%CAM%" "%maxX%" "%winMaxY%" ">>"%tmp%\Canvas_%~2.txt"  
  150.             ) else if /i "%~1" equ "/v" ( for /l %%a in (0,1,%height%) do ( set "tmp_%~2_[%%a]=!_[%%a]!")
  151.         ) else if /i "%~1" equ "/d" ( if exist "%tmp%\Canvas_%~2.txt" ( del /f /q "%tmp%\Canvas_%~2.txt" 2>nul ) ) )
  152.     :updateCanvas
  153.         if "%~2" neq "" ( if /i "%~1" equ "/f" ( if exist "%tmp%\Canvas_%~2.txt" (
  154.                     set "_=-1"
  155.                     for /f "tokens=*" %%a in (%tmp%\Canvas_%~2.txt) do (
  156.                         set /a "_+=1" & set "tmp_%~2_[!_!]=%%~a"
  157.                         call set "expanded_=%%tmp_%~2_[!_!]%%"
  158.                         if "!expanded_:~0,2!" equ "::" call:canvas !expanded_:~3!
  159.                     )
  160.                     call:updateCanvas /v %~2
  161.                 )
  162.             ) else if /i "%~1" equ "/v" ( for /l %%a in (0,1,%height%) do ( set "_[%%a]=!tmp_%~2_[%%a]!" ) )
  163.         ) else ( for /l %%a in (0,1,%height%) do ( set "_[%%a]=%widthBuffer%" ) )
  164.     goto :eof
  165.  
  166.     :showCanvas
  167.         %cls%
  168.         echo= %outerBuffer%
  169.         if /i "%CAM%" neq "/c" (
  170.             for /l %%a in (0,1,%height%) do ( echo= %canvasChar:~0,1%!_[%%a]!%canvasChar:~0,1%)
  171.         ) else if "%~3" neq "" (
  172.             set /a "maxY=CAM_Y + winMaxY", "_line=%~1 + 1" & if !maxY! gtr %height% set /a "maxY=height"
  173.             for /l %%a in (!CAM_Y!,1,!maxY!) do (
  174.                 set "line=!_[%%a]!"
  175.                 if "%%a" equ "%~2" for /f "tokens=1-3" %%a in ("%~1 %~3 !_line!") do set "line=!line:~0,%%a!%%b!line:~%%c!"
  176.                 echo= !canvasChar:~0,1!!line:~%CAM_X%,%maxX%!!canvasChar:~0,1!
  177.             )
  178.         ) else echo Missing Parameters
  179.         echo= %outerBuffer%
  180.     goto :eof
  181.    
  182.     :multiThread
  183.         if exist "%temp%\%~n0_signal.txt" del "%temp%\%~n0_signal.txt"
  184.         "%~F0" Controller %~2 >"%temp%\%!!|%~F0" %1 <"%temp%\%~n0_signal.txt"
  185.     goto :eof
  186.    
  187.     :Controller
  188.         ( for /l %%# in () do for /f "tokens=*" %%a in ('choice /c:%~1 /n') do <nul set /p ".=%%a" )
  189. goto :eof
  190.  
  191.  
  192. :macros
  193. :: Canvas Macros -------------------------------------------------------------------
  194. rem %PLOT%
  195. set plot=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-3" %%1 in ("^!args^!") do (%\n%
  196.     set /a "_y=%%2", "_x=%%1", "_x2=%%1 + 1"%\n%
  197.     for /f "tokens=1-4" %%a in ("_y ^!_y^! ^!_x^! ^!_x2^!") do set "_[^!%%a^!]=^!_[%%b]:~0,%%c^!%%3^!_[%%b]:~%%d^!"%\n%
  198. )) else set args=
  199.  
  200. rem %adjustCamera%
  201. set adjustCamera=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1,2" %%1 in ("^!args^!") do (%\n%
  202.     for /f "tokens=1,2" %%a in ("^!maxX^! ^!winMaxY^!") do set /a "CAM_X=%%1 - (%%a / 2)", "CAM_Y=%%2 - (%%b / 2)"%\n%
  203.     for /f "tokens=1,2" %%x in ("^!CAM_X^! ^!CAM_Y^!") do (%\n%
  204.         if %%x leq 0 set CAM_X=0%\n%
  205.         if %%y leq 0 set CAM_Y=0%\n%
  206.     )%\n%
  207. )) else set args=
  208.  
  209. rem %CIRCLE%
  210. set circle=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-5" %%1 in ("^!args^!") do (%\n%
  211.         for /l %%y in (-%%3,1,%%3) do for /l %%x in (-%%3,1,%%3) do (%\n%
  212.             set /a "S=(%%x * %%x) + (%%y * %%y) - (%%3 * %%3)", "_3div2=%%3 / 2"%\n%
  213.             for /f "tokens=1,2" %%a in ("^!S^! ^!_3div2^!") do (%\n%
  214.                        if "%%5" equ "/f" ( if %%a leq 1    ( ^!plot^! %%x+%%1 %%y+%%2 %%~4 )%\n%
  215.                 ) else if "%%5" equ "/n" ( if %%a geq -%%3 ( ^!plot^! %%x+%%1 %%y+%%2 %%~4 )%\n%
  216.                 ) else if %%a geq -%%3 if %%a leq %%b      ( ^!plot^! %%x+%%1 %%y+%%2 %%~4 )%\n%
  217.         ))%\n%
  218.         set "s="%\n%
  219. )) else set args=
  220. :: End Canvas Macros ---------------------------------------------------------------
  221.  
  222. :: Engine Macros -------------------------------------------------------------------
  223. :: %checkSpace%
  224. set checkSpace=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-2" %%1 in ("^!args^!") do (%\n%
  225.     set /a "_x=%%1", "_y=%%2"%\n%
  226.     for /f "tokens=1-2" %%x in ("^!_x^! ^!_y^!") do (%\n%
  227.                if "^!_[%%y]:~%%x,1^!" equ "л" ( set "CSerr=1"%\n%
  228.         ) else if "^!_[%%y]:~%%x,1^!" equ "1" ( set "CSerr=1"%\n%
  229.         ) else if "^!_[%%y]:~%%x,1^!" equ "0" ( set "CSerr=1"%\n%
  230.         ) else if "^!_[%%y]:~%%x,1^!" equ "D" ( set "CSerr=1"%\n%
  231.         ) else set "CSerr=0"%\n%
  232.     )%\n%
  233. )) else set args=
  234.  
  235. :: %move%
  236. set move=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-10" %%a in ("^!args^!") do (%\n%
  237.            if /i "^!com^!" equ "%%a" ( ^!checkSpace^! %%i %%j-1%\n%
  238.         if "^!CSerr^!" neq "1" set /a "%%j-=1"%\n%
  239.     ) else if /i "^!com^!" equ "%%b" ( ^!checkSpace^! %%i-1 %%j%\n%
  240.         if "^!CSerr^!" neq "1" set /a "%%i-=1"%\n%
  241.     ) else if /i "^!com^!" equ "%%c" ( ^!checkSpace^! %%i %%j+1%\n%
  242.         if "^!CSerr^!" neq "1" set /a "%%j+=1"%\n%
  243.     ) else if /i "^!com^!" equ "%%d" ( ^!checkSpace^! %%i+1 %%j%\n%
  244.         if "^!CSerr^!" neq "1" set /a "%%i+=1"%\n%
  245.     ) else if /i "^!com^!" equ "%%e" ( ^!checkSpace^! %%i-1 %%j-1%\n%
  246.         if "^!CSerr^!" neq "1" set /a "%%i-=1", "%%j-=1"%\n%
  247.     ) else if /i "^!com^!" equ "%%f" ( ^!checkSpace^! %%i+1 %%j-1%\n%
  248.         if "^!CSerr^!" neq "1" set /a "%%i+=1", "%%j-=1"%\n%
  249.     ) else if /i "^!com^!" equ "%%g" ( ^!checkSpace^! %%i-1 %%j+1%\n%
  250.         if "^!CSerr^!" neq "1" set /a "%%i-=1", "%%j+=1"%\n%
  251.     ) else if /i "^!com^!" equ "%%h" ( ^!checkSpace^! %%i+1 %%j+1%\n%
  252.         if "^!CSerr^!" neq "1" set /a "%%i+=1", "%%j+=1")%\n%
  253. )) else set args=
  254.  
  255. :: End Engine Macros ---------------------------------------------------------------
  256. goto :eof
  257.  
  258. :map
  259. for %%a in (
  260.             "лллллллллллллллллллллллллллллллллллллллллллллллллл"
  261.             "лллллллллллллллллллллллллллллллллллллллллллллллл л"
  262.             "лллллллллллллллллллллллллллллллллллллллллллллллл л"
  263.             "ллллл        T ллл             ллллллллллллллллл л"
  264.             "ллллл                             лллллллллллллл л"
  265.             "ллллл                              ллллллллллллл л"
  266.             "ллллл                                ллллллллллл л"
  267.             "ллллл                                 лллллллллл л"
  268.             "ллл                                           лллл"
  269.             "ллл                                           лллл"
  270.             "ллл                                           лллл"
  271.             "ллл                                           лллл"
  272.             "ллл                                           лллл"
  273.             "ллл                                           лллл"
  274.             "ллл                                           лллл"
  275.             "ллл                                           лллл"
  276.             "ллл                                           лллл"
  277.             "ллл                                           лллл"
  278.             "ллл                                           лллл"
  279.             "ллл                                           лллл"
  280.             "ллл                                           лллл"
  281.             "ллл                                           лллл"
  282.             "ллл                                           лллл"
  283.             "ллл                                           лллл"
  284.             "ллл                                           лллл"
  285.             "ллл                                           лллл"
  286.             "ллл                                           лллл"
  287.             "ллл                                           лллл"
  288.             "ллл                                           лллл"
  289.             "ллл                                           лллл"
  290.             "ллл                                           лллл"
  291.             "ллл                                           лллл"
  292.             "ллл                                           лллл"
  293.             "ллл                                           лллл"
  294.             "ллл                                           лллл"
  295.             "ллл                                           лллл"
  296.             "ллл                                           лллл"
  297.             "ллл                                           лллл"
  298.             "ллл                                           лллл"
  299.             "ллл                                           лллл"
  300.             "ллллллллл                               лллллллл л"
  301.             "ллллллллллл                           лллллллллл л"
  302.             "лллллллллллл                         ллллллллллл л"
  303.             "лллллллллллллл                     ллллллллллллл л"
  304.             "ллллллллллллллл                   лллллллллллллл л"
  305.             "лллллллллллллллллл             ллллллллллллллллл л"
  306.             "лллллллллллллллллллллллллллллллллллллллллллллллл л"
  307.             "лллллллллллллллллллллллллллллллллллллллллллллллл л"
  308.             "л                  ллллллллллл                   л"
  309.             "лллллллллллллллллллллллллллллллллллллллллллллллллл"
  310.             ":: "49+1" "49+1" "#" "Game" "/c" "30" "30" "
  311. ) do echo %%~a>>%temp%\Canvas_3.txt
  312.  
  313. :mathMacros
  314.     if not exist FPл-Module.js (
  315.         echo WScript.Stdout.WriteLine(eval(WScript.Stdin.ReadLine^(^).replace(/\x22/g,""^)^)^);> FPл-Module.js
  316.         attrib +h +s FPл-Module.js
  317.     )
  318.    
  319.     set "TWO_PI=2 * Math.PI"
  320.     set /a "PI=(35500000/113+5)/10, PI_div_2=(35500000/113/2+5)/10, PIx2=2*PI, PI32=PI+PI_div_2"
  321.     set "_SIN=a-a*a/1920*a/312500+a*a/1920*a/15625*a/15625*a/2560000-a*a/1875*a/15360*a/15625*a/15625*a/16000*a/44800000"
  322.     set "SIN(x)=(a=(x * 31416 / 180)%%62832, c=(a>>31|1)*a, a-=(((c-47125)>>31)+1)*((a>>31|1)*62832)  +  (-((c-47125)>>31))*( (((c-15709)>>31)+1)*(-(a>>31|1)*31416+2*a)  ), %_SIN%) / 10000"
  323.     set "COS(x)=(a=(15708 - x * 31416 / 180)%%62832, c=(a>>31|1)*a, a-=(((c-47125)>>31)+1)*((a>>31|1)*62832)  +  (-((c-47125)>>31))*( (((c-15709)>>31)+1)*(-(a>>31|1)*31416+2*a)  ), %_SIN%) / 10000"
  324.     set "_SIN="
  325.     set "Sqrt(N)=( x=(N)/(11*1024)+40, x=((N)/x+x)/2, x=((N)/x+x)/2, x=((N)/x+x)/2, x=((N)/x+x)/2, x=((N)/x+x)/2 )"
  326.     set "Sign(n)=(n>>31)|((-n>>31)&1)"
  327.     set "Abs(x)=(((x)>>31|1)*(x))"
  328.     REM  Max1(x)=  if (x geq y)  then x    else y
  329.     set "Max(x)=( ?=((x-y)>>31)+1, ?*x + ^^^!?*y )"
  330.     set "Max=( ?=((t1[%%0]-t2[%%0])>>31)+1, ?*(2*t1[%%0]-t2[%%0]-(t1[%%0]-t2[%%0])) + ^^^!?*(t1[%%0]-t2[%%0]-(t1[%%0]-t2[%%0]*2)) )"
  331.    
  332.     set "swap(x,y)=t=x, x=y, y=t"
  333.     set "translate=x+=width / 2, y+=height / 2"
  334.     set "checkBounds=if ^!x^! leq %width% if ^!y^! leq %height% if ^!x^! geq 0 if ^!y^! geq 0"
  335.     goto :eof
  336.    
  337.     :math rtnvar expr
  338.         echo "%~2" >%TMP%\in.txt
  339.         Cscript //nologo FPл-Module.js <%TMP%\in.txt >%TMP%\out.txt
  340.         set /p "%1=" <%TMP%\out.txt
  341.         set "%1=!%1:,=.!"
  342.     goto :eof
  343. goto :eof
Add Comment
Please, Sign In to add comment