Advertisement
Guest User

RandoTK 1.05

a guest
May 18th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 31.29 KB | None | 0 0
  1. # -*- encoding:Latin-1 -*-
  2. #THIS PROGRAM CREATES RANDOM SCENARIOS FOR ROTK11
  3. #RandOTK11 v1.05
  4. #
  5. #
  6. # IMPORTING WIDGETS AND DEFINING VARIABLES
  7. #
  8. import os
  9. from random import *
  10. from tkinter import *
  11. from tkinter import messagebox
  12. from tkinter.filedialog import *
  13. chk = b'KOEI%SAN11'
  14. authorized_filesizes = [167559, 169822, 179109]
  15. helptext = "RandOTK11 v1.05\n\n Information : This program randomizes Romance of the Three Kingdoms XI Scenario Files (.S11 extension) and has been developed in Python by Zetta on the forums of http://the-scholars.com/"
  16. #
  17. # DEFINE FUNCTIONS
  18. #
  19. #
  20. # DEFINE HELP / INFO FUNCTION
  21. #
  22. def info():
  23.   global helptext
  24.   messagebox.showinfo("Help", helptext)
  25. #
  26. # DEFINE EXIT FUNCTION
  27. #
  28. def exitrandotk():
  29.   global window
  30.   try:
  31.     Scen.close()
  32.   except:
  33.     pass
  34.   window.destroy()
  35. #
  36. # DEFINE OPEN FILE FUNCTION
  37. #
  38. def openSCEN():
  39.   global Scen,File,Filesize
  40.   File = askopenfilename(defaultextension = ".S11", filetypes = [("S11","*.S11")])
  41.   try:
  42.     Scen = open(File, "rb+")
  43.     Scen.seek(8,0)
  44.     f = Scen.read(10)
  45.     Filesize = os.path.getsize(File)
  46.     if chk == f and Filesize in authorized_filesizes:
  47.       randbtn.config(state = ACTIVE)
  48.     if chk != f or Filesize not in authorized_filesizes:
  49.       print("ERROR: chosen file is not a ROTK11 scenario file")
  50.       Scen.close()
  51.       randbtn.config(state = DISABLED)
  52.   except:
  53.     print("No File selected")
  54.     randbtn.config(state = DISABLED)
  55. #
  56. #
  57. # DEFINE RANDOMIZE FUNCTION
  58. #
  59. #
  60. def Randomize():
  61.   global a,b,c,d,e,f,x,y,IA,equi,res,HistVar,rdoff,fnum,LifeVar,CityVar,AllyVar,Filesize
  62.   randbtn.config(state = DISABLED)
  63.   leaders = [9, 16, 17, 18, 20, 50, 65, 91, 101, 105, 109, 120, 129, 149, 160, 180, 182, 184, 190, 225, 263, 288, 328, 343, 347, 349, 350, 358, 362, 365, 367, 368, 370, 383, 415, 431, 442, 465, 484, 493, 515, 517, 568, 598, 614, 618, 624, 629, 631, 632, 635, 636, 660]
  64.   colors = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41]
  65.   cities = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41]
  66.   nghb = [[1], [0,2,3], [1,3,5], [2,4], [3,6,7,11], [2,6], [4,5,11,12], [4,8,9], [7,9], [7,8,10,11,12], [9,14,25], [4,6,7,9,12], [6,9,11,13,15,16], [12,14,16,28], [10,13,28], [12,16,17], [13,15,17,18,28], [15,16,19,36], [16,29,36], [17,20,21], [17,19,21,36], [19,20], [23,25], [22,24], [23], [10,22,26,27], [25,27,30,31], [25,26,28,30], [13,14,16,27,29], [18,28,30,35], [26,27,29,31,32,35], [26,30,32,33], [30,31,34], [31,34], [32,33], [29,30,38], [18,20,37], [36,39], [35,39,40], [37,38], [38,41], [40]]
  67.   gates = [[10], [], [], [], [11], [0,12], [13], [14,15], [16,17], [], [18], [19], [20], [], [], [1,21,22], [], [23,2,3,4], [25], [], [], [], [26,27], [28], [29], [30], [31,32,33,34], [35], [36], [37], [38,39,40], [41], [42,43], [], [], [44], [5], [6,7], [], [8,9], [], []]
  68.   forces = []
  69. #
  70. # PREPARING A BLANK SCENARIO BY DEFAULT
  71. #
  72. # SCENARIO NAME AND DESCRIPTION
  73. #
  74.   if Filesize ==179109:
  75.     sname = b'Random\x20Scenario'
  76.     Scen.seek(95,0)
  77.     Scen.write(sname)
  78.     Scen.write(bytes(5))
  79.     Scen.seek(121,0)
  80.     Scen.write(bytes(450))
  81. #
  82. # BLANK MAP PREVIEW
  83. #
  84.   if Filesize==179109:
  85.     Scen.seek(722,0)
  86.   if Filesize==169822:
  87.     Scen.seek(475,0)
  88.   if Filesize==167559:
  89.     Scen.seek(418,0)
  90.   Scen.write(42*bytes([255]))
  91. #
  92. # BLANK FORCES DESCRIPTION
  93. #
  94.   if Filesize==179109:
  95.     x = 0
  96.     while x<42:
  97.       Scen.seek(932+x*607,0)
  98.       Scen.write(bytes([0,20]))
  99.       Scen.write(bytes(605))
  100.       x+=1
  101. #
  102. # OFFICERS BLANK
  103. #
  104.   x = 0
  105.   while x<670:
  106.     if Filesize==179109:
  107.       Scen.seek(28098+x*152, 0)
  108.     if Filesize==169822:
  109.       Scen.seek(17855+x*152, 0)
  110.     if Filesize==167559:
  111.       Scen.seek(15404+x*152, 0)
  112.     Scen.write(bytes([255]))
  113.     a = [randrange(0,42),0]
  114.     Scen.write(bytes(a))
  115.     Scen.write(bytes(a))
  116.     y = choice([4,7,7,7])
  117.     Scen.write(bytes([y]))
  118.     Scen.write(b"\x50")
  119.     if Filesize==179109:
  120.       Scen.seek(28107+x*152, 0)
  121.     if Filesize==169822:
  122.       Scen.seek(17864+x*152, 0)
  123.     if Filesize==167559:
  124.       Scen.seek(15413+x*152, 0)
  125.     Scen.write(bytes(3))
  126.     x+=1
  127. #
  128. # FORCE SLOTS BLANK
  129. #
  130.   y = 0
  131.   while y<42:
  132.     L = [255,255,255,255]
  133.     L.extend([50]*y)
  134.     L.extend([100])
  135.     L.extend((41-y)*[50])
  136.     if Filesize==179109:
  137.       Scen.seek(160603+y*68, 0)
  138.     if Filesize==169822:
  139.       Scen.seek(152660+y*68, 0)
  140.     if Filesize==167559:
  141.       Scen.seek(150209+y*72, 0)
  142.     Scen.write(bytes(L))
  143.     Scen.write(bytes(5))
  144.     Scen.write(bytes([9, 255, y, 255, 255]))
  145.     Scen.write(bytes(12))
  146.     y+=1
  147. #
  148. # DISTRICTS BLANK
  149. #
  150.   y = 0
  151.   while y<42:
  152.     L = [255,00,255,255,255,255,255,255]
  153.     if Filesize==179109:
  154.       Scen.seek(163799+y*8, 0)
  155.     if Filesize==169822:
  156.       Scen.seek(155856+y*8, 0)
  157.     if Filesize==167559:
  158.       Scen.seek(153593+y*8, 0)
  159.     Scen.write(bytes(L))
  160.     y+=1
  161. #
  162. # CITIES BLANK
  163. #
  164.   y = 42
  165.   while y:
  166.     y-=1
  167.     if Filesize==179109:
  168.       Scen.seek(164175+y*81, 0)
  169.     if Filesize==169822:
  170.       Scen.seek(156232+y*81, 0)
  171.     if Filesize==167559:
  172.       Scen.seek(153969+y*81, 0)
  173.     Scen.write(b"\xff")
  174. #
  175.     if Filesize==179109:
  176.       Scen.seek(164179+y*81, 0)
  177.     if Filesize==169822:
  178.       Scen.seek(156236+y*81, 0)
  179.     if Filesize==167559:
  180.       Scen.seek(153973+y*81, 0)
  181.     Scen.write(bytes(61))
  182. #
  183.     if Filesize==179109:
  184.       Scen.seek(164248+y*81, 0)
  185.     if Filesize==169822:
  186.       Scen.seek(156305+y*81, 0)
  187.     if Filesize==167559:
  188.       Scen.seek(154042+y*81, 0)
  189.     Scen.write(b"\x00"+b"\x32")
  190. #
  191. #GATES AND PORTS BLANK
  192. #
  193.   y = 45
  194.   while y:
  195.     y-=1
  196.     if Filesize==179109:
  197.       Scen.seek(167577+y*64, 0)
  198.     if Filesize==169822:
  199.       Scen.seek(159634+y*64, 0)
  200.     if Filesize==167559:
  201.       Scen.seek(157371+y*64, 0)
  202.     Scen.write(b"\xff")
  203.     Scen.write(bytes(61))
  204.   print("Scenario has been made blank for preparation.")
  205. #
  206. #
  207. # THIS IS THE STARTING PART OF THE RANDOMIZER
  208. # SETUP STARTING DATE SELECTION
  209. #
  210. #
  211.   try:
  212.     sdate = int(SdateEntry.get())
  213.     if sdate<0 or sdate >300:
  214.       sdate = randrange(160,300)
  215.   except:
  216.     sdate = randrange(160,300)
  217.   w = [sdate%256, sdate//256, randrange(0,13)]
  218.   Scen.seek(91, 0)
  219.   Scen.write(bytes(w))
  220.   if Filesize==179109:
  221.     Scen.seek(26427, 0)
  222.   if Filesize==169822:
  223.     Scen.seek(16184, 0)
  224.   if Filesize==167559:
  225.     Scen.seek(13733, 0)
  226.   Scen.write(bytes(w))
  227. #
  228. # OFFICERS UPDATE AND CREATION OF A LIST OF FREE OFFICERS
  229. #
  230.   x = 0
  231.   offi = []
  232.   while x<670:
  233.     if Filesize==179109:
  234.       Scen.seek(28059+x*152, 0)
  235.     if Filesize==169822:
  236.       Scen.seek(17816+x*152, 0)
  237.     if Filesize==167559:
  238.       Scen.seek(15365+x*152, 0)
  239.     avail = ord(Scen.read(1)) + 256*ord(Scen.read(1))
  240.     birth = ord(Scen.read(1)) + 256*ord(Scen.read(1))
  241. #
  242.     if LifeVar.get()==1:
  243.       deathd = birth + randrange(30,100)
  244.       Scen.write(bytes([deathd%256, deathd//256]))
  245.       if Filesize==179109:
  246.         Scen.seek(28059+x*152, 0)
  247.       if Filesize==169822:
  248.         Scen.seek(17816+x*152, 0)
  249.       if Filesize==167559:
  250.         Scen.seek(15365+x*152, 0)
  251.       avail = birth + randrange(15,31)
  252.       Scen.write(bytes([avail%256, avail//256]))
  253.     if LifeVar.get()==0:
  254.       deathd = ord(Scen.read(1)) + 256*ord(Scen.read(1))
  255. #
  256.     if Filesize==179109:
  257.       Scen.seek(28103+x*152, 0)
  258.     if Filesize==169822:
  259.       Scen.seek(17860+x*152, 0)
  260.     if Filesize==167559:
  261.       Scen.seek(15409+x*152, 0)
  262.     if sdate > deathd:
  263.       Scen.write(b"\x08")
  264.       if x in leaders:
  265.         leaders.remove(x)
  266.     if sdate < avail:
  267.       Scen.write(b"\x06")
  268.       if x in leaders:
  269.         leaders.remove(x)
  270.     if avail<=sdate<deathd:
  271.       offi.append(x)
  272.     x+=1
  273.   totaloff = len(offi)
  274. #
  275. # EMPEROR UPDATE
  276. #
  277.   if sdate>189:
  278.     if Filesize==179109:
  279.       Scen.seek(26431,0)
  280.       Scen.write(bytes([190,2]))
  281.       Scen.seek(134503, 0)
  282.       Scen.write(bytes([8]))
  283.       Scen.seek(134503+152, 0)
  284.       Scen.write(bytes([8]))
  285.       Scen.seek(134503+2*152, 0)
  286.       Scen.write(bytes([255]))
  287.     if Filesize==169822:
  288.       Scen.seek(16188,0)
  289.       Scen.write(bytes([190,2]))
  290.       Scen.seek(124260, 0)
  291.       Scen.write(bytes([8]))
  292.       Scen.seek(124260+152, 0)
  293.       Scen.write(bytes([8]))
  294.       Scen.seek(124260+2*152, 0)
  295.       Scen.write(bytes([255]))
  296.     if Filesize==167559:
  297.       Scen.seek(13737,0)
  298.       Scen.write(bytes([190,2]))
  299.       Scen.seek(121809, 0)
  300.       Scen.write(bytes([8]))
  301.       Scen.seek(121809+152, 0)
  302.       Scen.write(bytes([8]))
  303.       Scen.seek(121809+2*152, 0)
  304.       Scen.write(bytes([255]))
  305.   if sdate<=189:
  306.     if Filesize==179109:
  307.       Scen.seek(26431,0)
  308.       Scen.write(bytes([188,2]))
  309.       Scen.seek(134503, 0)
  310.       Scen.write(bytes([255]))
  311.       Scen.seek(134503+152, 0)
  312.       Scen.write(bytes([8]))
  313.       Scen.seek(134503+2*152, 0)
  314.       Scen.write(bytes([255]))
  315.     if Filesize==169822:
  316.       Scen.seek(16188,0)
  317.       Scen.write(bytes([188,2]))
  318.       Scen.seek(124260, 0)
  319.       Scen.write(bytes([255]))
  320.       Scen.seek(124260+152, 0)
  321.       Scen.write(bytes([8]))
  322.       Scen.seek(124260+2*152, 0)
  323.       Scen.write(bytes([255]))
  324.     if Filesize==167559:
  325.       Scen.seek(13737,0)
  326.       Scen.write(bytes([188,2]))
  327.       Scen.seek(121809, 0)
  328.       Scen.write(bytes([255]))
  329.       Scen.seek(121809+152, 0)
  330.       Scen.write(bytes([8]))
  331.       Scen.seek(121809+2*152, 0)
  332.       Scen.write(bytes([255]))
  333. #
  334. # OFFICER RANDOMIZER
  335. #
  336.   if rdoff.get()==1:
  337.     x = 0
  338.     while x<670:
  339.       if Filesize==179109:
  340.         Scen.seek(28110+x*152,0)
  341.       if Filesize==169822:
  342.         Scen.seek(17867+x*152,0)
  343.       if Filesize==167559:
  344.         Scen.seek(15416+x*152,0)
  345.       f1 = randrange(1,6)
  346. # defining 4 types of random officers : 1 is hero, 2 is strategist, 3 is general, 4 is standard and 5 is random
  347.       if f1==1:
  348.         f = [randrange(1,4), randrange(1,4), randrange(1,4),randrange(1,4),randrange(0,4), randrange(1,4), randrange(65,101), randrange(65,101), randrange(65,96), randrange(65,96), randrange(65,96)]
  349.       if f1==2:
  350.         f = [randrange(0,3), randrange(0,3), randrange(1,4),randrange(0,4),randrange(1,4), randrange(1,4), randrange(50,81), randrange(50,81), randrange(75,101), randrange(75,101), randrange(75,101)]
  351.       if f1==3:
  352.         f = [randrange(0,4), randrange(0,4), randrange(0,4),randrange(0,4),randrange(0,4), randrange(0,4), randrange(80,98), randrange(80,97), randrange(40,81), randrange(40,81), randrange(80,94)]
  353.       if f1==4:
  354.         f = [randrange(0,3), randrange(0,3), randrange(0,3),randrange(0,3),randrange(0,3), randrange(0,3), randrange(20,61), randrange(20,61), randrange(20,61), randrange(20,61), randrange(20,61)]
  355.       if f1==5:
  356.         f = [randrange(0,4), randrange(0,4), randrange(0,4),randrange(0,4),randrange(0,4), randrange(0,4), randrange(2,80), randrange(1,80), randrange(3,80), randrange(1,80), randrange(5,80)]
  357.       Scen.write(bytes(f))
  358.       if Filesize==179109:
  359.         Scen.seek(28127+x*152,0)
  360.       if Filesize==169822:
  361.         Scen.seek(17884+x*152,0)
  362.       if Filesize==167559:
  363.         Scen.seek(15433+x*152,0)
  364.       f2 = 255
  365.       if f[7]>=75:
  366.         f2 = choice([255, randrange(10,53)])
  367.       if f[8]>=70:
  368.         f2 = choice([255, randrange(53,79)])
  369.       if f[9]>=60 or f[10]>60:
  370.         f2 = choice([255, randrange(79,100)])
  371.       if f[6]>=70:
  372.         f2 = choice([255, randrange(0,10)])
  373.       if f[6]>=85 and f[0]==3:
  374.         f2 = choice([255,34,42])
  375.       if f[6]>=85 and f[1]==3:
  376.         f2 = choice([255,35,43])
  377.       if f[6]>=85 and f[2]==3:
  378.         f2 = choice([255,36,44])
  379.       if f[6]>=85 and f[3]==3:
  380.         f2 = choice([255,25,37,45,49])
  381.       if f[6]>=85 and f[3]==5:
  382.         f2 = choice([255,38,47])
  383.       Scen.write(bytes([f2]))
  384.       x+=1
  385. #
  386. # SETUP FOR NUMBER OF FORCES
  387. #
  388.   try:
  389.     fnum = int(FnumEntry.get())
  390.   except:
  391.     fnum = randrange(2,43)
  392.   if fnum>42 or fnum<2:
  393.     fnum = randrange(2,43)
  394.   print("There is " + str(len(offi)) + " officers available at year " + str(sdate) + " in this scenario.")
  395. #
  396. # REMOVE LEADERS FROM OFFICERS LIST
  397. #
  398.   if HistVar.get()==1:
  399.     for n in leaders:
  400.       try:
  401.         offi.remove(n)
  402.       except:
  403.         print(" \n ERROR : Could not remove officer from list \n")
  404. #
  405. # REMOVE FIXED NUMBER OF FREE CITIES
  406. #
  407.   try:
  408.     freecities = int(FcityEntry.get())
  409.     if freecities>40 or freecities+fnum>42:
  410.       freecities = 42 - fnum
  411.   except:
  412.     if fnum<41:
  413.       freecities = randrange(0,41-fnum)
  414.     if fnum==41:
  415.       freecities = 0
  416.   while freecities>0:
  417.     cities.remove(choice(cities))
  418.     freecities-=1
  419.   avcity = len(cities)
  420. #
  421. # CODE FOR CREATION OF EACH RANDOM FORCE
  422. #
  423. # NOTE : b is force number being created, a is leader, d is capital and c is force color
  424. # e is list of owned cities and f and n are integers for various uses
  425. #
  426. #
  427. # CHOOSE A RULER FROM HISTORICAL OR OFFICERS LIST
  428. #
  429.   leaderslist = []
  430.   b=0
  431.   while b<fnum and offi and len(cities):
  432.     if HistVar.get()!=1 or not len(leaders):
  433.       a = offi[randrange(0,len(offi))]
  434.       offi.remove(a)
  435.     if HistVar.get()==1 and len(leaders):
  436.       a = leaders[randrange(0,len(leaders))]
  437.       leaders.remove(a)
  438.     leaderslist.append(a)
  439. #
  440. # MODIFY LEADER ATTRIBUTES
  441. #
  442.     if Filesize==179109:
  443.       Scen.seek(28098+a*152, 0)
  444.     if Filesize==169822:
  445.       Scen.seek(17855+a*152, 0)
  446.     if Filesize==167559:
  447.       Scen.seek(15404+a*152, 0)
  448.     Scen.write(bytes([b]))
  449.     e = []
  450.     d = choice(cities)
  451.     e.append(d)
  452.     cities.remove(d)
  453.     Scen.write(bytes([d]))
  454.     Scen.write(bytes(1))
  455.     Scen.write(bytes([d]))
  456.     Scen.write(bytes(2))
  457.     if Filesize==179109:
  458.       Scen.seek(28107+a*152, 0)
  459.     if Filesize==169822:
  460.       Scen.seek(17864+a*152, 0)
  461.     if Filesize==167559:
  462.       Scen.seek(15413+a*152, 0)
  463.     Scen.write(bytes([100]))
  464. #
  465. # CREATE FORCE
  466. #
  467.     c = colors[randrange(0,len(colors))]
  468.     forces.append(c)
  469.     colors.remove(c)
  470.     if Filesize==179109:
  471.       Scen.seek(160603+c*68, 0)
  472.     if Filesize==169822:
  473.       Scen.seek(152660+c*68, 0)
  474.     if Filesize==167559:
  475.       Scen.seek(150209+c*72, 0)
  476.     Scen.write(bytes([a%256]))
  477.     Scen.write(bytes([a//256]))
  478. #
  479. # FORCE IA BEHAVIOR
  480. #
  481.     if Filesize==179109:
  482.       Scen.seek(160657+68*c,0)
  483.     if Filesize==169822:
  484.       Scen.seek(152714+68*c,0)
  485.     if Filesize==167559:
  486.       Scen.seek(150263+72*c,0)
  487.     f = 0
  488.     if IA.get()==1:
  489.       f = 0
  490.     if IA.get()==2:
  491.       f = randrange(1,3)
  492.     if IA.get()==3:
  493.       f = randrange(0,6)
  494.     Scen.write(bytes([f]))
  495.     if f==1:
  496.       Scen.write(bytes([randrange(0,6)]))
  497.     if f==2:
  498.       Scen.write(bytes([randrange(0,12)]))
  499.     if f==0 or f==3 or f==4 or f==5:
  500.       Scen.write(bytes([255]))
  501. #
  502. # CREATE FORCE DISTRICT
  503. #
  504.     if Filesize==179109:
  505.       Scen.seek(163799+b*8, 0)
  506.     if Filesize==169822:
  507.       Scen.seek(155856+b*8, 0)
  508.     if Filesize==167559:
  509.       Scen.seek(153593+b*8, 0)
  510.     Scen.write(bytes([c]))
  511.     Scen.write(bytes([1]))
  512.     Scen.write(bytes([a%256]))
  513.     Scen.write(bytes([a//256]))
  514.     obk = choice([1,2])
  515.     if obk ==1:
  516.       Scen.write(bytes([1]))
  517.       Scen.write(bytes([randrange(0,6)]))
  518.     if obk ==2:
  519.       Scen.write(bytes([2]))
  520.       Scen.write(bytes([randrange(0,12)]))
  521. #
  522. # CITY ASSIGNMENT
  523. #
  524.     if Filesize==179109:
  525.       Scen.seek(164175+d*81, 0)
  526.     if Filesize==169822:
  527.       Scen.seek(156232+d*81, 0)
  528.     if Filesize==167559:
  529.       Scen.seek(153969+d*81, 0)
  530.     Scen.write(bytes([b]))
  531.     if equi.get()==4:
  532.       equi.set(randrange(1,4))
  533.     if equi.get()==1:
  534.       f = int(len(cities)/fnum)
  535.     if equi.get()==2:
  536.       try:
  537.         f = randrange(0,len(cities)-fnum+b)
  538.       except:
  539.         f = 0
  540.     if equi.get()==3:
  541.       f = 0
  542.     while f>0:
  543.       if CityVar.get()==1:
  544.         for q in e:
  545.           for r in nghb[q]:
  546.             if r in cities and f>0:
  547.               e.append(r)
  548.               cities.remove(r)
  549.               f-=1
  550.             else:
  551.               f-=1
  552.       if CityVar.get()==0:
  553.         try:
  554.           r = choice(cities)
  555.           e.append(r)
  556.           cities.remove(r)
  557.         except:
  558.           print("Error : could not distribute more cities in non neighbour cities setup")
  559.         f-=1
  560. #
  561. # EQUIP CITY TROOPS GOLD AND FOOD
  562. #
  563.     for n in e:
  564.       if Filesize==179109:
  565.         Scen.seek(164175+n*81, 0)
  566.       if Filesize==169822:
  567.         Scen.seek(156232+n*81, 0)
  568.       if Filesize==167559:
  569.         Scen.seek(153969+n*81, 0)
  570.       Scen.write(bytes([b]))
  571. #
  572.       if Filesize==179109:
  573.         Scen.seek(164180+n*81, 0)
  574.       if Filesize==169822:
  575.         Scen.seek(156237+n*81, 0)
  576.       if Filesize==167559:
  577.         Scen.seek(153974+n*81, 0)
  578.       f = 500*randrange(30,91)
  579.       Scen.write(bytes([f%256]))
  580.       Scen.write(bytes([f//256]))
  581. #
  582.       if Filesize==179109:
  583.         Scen.seek(164184+n*81, 0)
  584.       if Filesize==169822:
  585.         Scen.seek(156241+n*81, 0)
  586.       if Filesize==167559:
  587.         Scen.seek(153978+n*81, 0)
  588.       f = 500*randrange(4,15)
  589.       Scen.write(bytes([f%256]))
  590.       Scen.write(bytes([f//256]))
  591. #
  592.       if Filesize==179109:
  593.         Scen.seek(164188+n*81, 0)
  594.       if Filesize==169822:
  595.         Scen.seek(156245+n*81, 0)
  596.       if Filesize==167559:
  597.         Scen.seek(153982+n*81, 0)
  598.       f = 1000*randrange(30,66)
  599.       Scen.write(bytes([f%256]))
  600.       Scen.write(bytes([f//256]))
  601. #
  602. # EQUIP CITY WEAPONS
  603. #
  604.       if Filesize==179109:
  605.         Scen.seek(164196+n*81, 0)
  606.       if Filesize==169822:
  607.         Scen.seek(156253+n*81, 0)
  608.       if Filesize==167559:
  609.         Scen.seek(153990+n*81, 0)
  610.       f = 1000*randrange(0,6)
  611.       Scen.write(bytes([f%256]))
  612.       Scen.write(bytes([f//256]))
  613. #
  614.       if Filesize==179109:
  615.         Scen.seek(164200+n*81, 0)
  616.       if Filesize==169822:
  617.         Scen.seek(156257+n*81, 0)
  618.       if Filesize==167559:
  619.         Scen.seek(153994+n*81, 0)
  620.       f = 1000*randrange(0,6)
  621.       Scen.write(bytes([f%256]))
  622.       Scen.write(bytes([f//256]))
  623. #
  624.       if Filesize==179109:
  625.         Scen.seek(164204+n*81, 0)
  626.       if Filesize==169822:
  627.         Scen.seek(156261+n*81, 0)
  628.       if Filesize==167559:
  629.         Scen.seek(153998+n*81, 0)
  630.       f = 1000*randrange(0,6)
  631.       Scen.write(bytes([f%256]))
  632.       Scen.write(bytes([f//256]))
  633. #
  634.       if Filesize==179109:
  635.         Scen.seek(164208+n*81, 0)
  636.       if Filesize==169822:
  637.         Scen.seek(156265+n*81, 0)
  638.       if Filesize==167559:
  639.         Scen.seek(154002+n*81, 0)
  640.       f = 1000*randrange(0,6)
  641.       Scen.write(bytes([f%256]))
  642.       Scen.write(bytes([f//256]))
  643. #
  644. # SET CITY WILL AND ORDER
  645. #
  646.       if Filesize==179109:
  647.         Scen.seek(164248+n*81, 0)
  648.       if Filesize==169822:
  649.         Scen.seek(156305+n*81, 0)
  650.       if Filesize==167559:
  651.         Scen.seek(154042+n*81, 0)
  652.       f = 5*randrange(12,19)
  653.       Scen.write(bytes([f]))
  654.       f = 5*randrange(12,19)
  655.       Scen.write(bytes([f]))
  656. #
  657. # OFFICERS ASSIGNMENT
  658. #
  659.     for n in e:
  660.       try:
  661.         f = randrange(2,1+round(totaloff/41))
  662.       except:
  663.         f = 2
  664.       while f and len(offi)>fnum:
  665.         q = choice(offi)
  666.         if Filesize==179109:
  667.           Scen.seek(28098+q*152, 0)
  668.         if Filesize==169822:
  669.           Scen.seek(17855+q*152, 0)
  670.         if Filesize==167559:
  671.           Scen.seek(15404+q*152, 0)
  672.         Scen.write(bytes([b]))
  673.         Scen.write(bytes([n]))
  674.         Scen.write(bytes(1))
  675.         Scen.write(bytes([n]))
  676.         Scen.write(bytes(1))
  677.         Scen.write(bytes([3]))
  678.         if Filesize==179109:
  679.           Scen.seek(28107+q*152, 0)
  680.         if Filesize==169822:
  681.           Scen.seek(17864+q*152, 0)
  682.         if Filesize==167559:
  683.           Scen.seek(15413+q*152, 0)
  684.         Scen.write(bytes([randrange(70,100)]))
  685.         offi.remove(q)
  686.         f-=1
  687. #
  688. # MAP PREVIEW
  689. #
  690.     for q in e:
  691.       if Filesize==179109:
  692.         Scen.seek(722+q,0)
  693.       if Filesize==169822:
  694.         Scen.seek(475+q,0)
  695.       if Filesize==167559:
  696.         Scen.seek(418+q,0)
  697.       Scen.write(bytes([c]))
  698. #
  699. # FORCE RANK/TITLE
  700. #
  701.     f = len(e)
  702.     if Filesize==179109:
  703.       Scen.seek(160654+68*c,0)
  704.     if Filesize==169822:
  705.       Scen.seek(152711+68*c,0)
  706.     if Filesize==167559:
  707.       Scen.seek(150260+72*c,0)
  708.     if 1<f<4:
  709.       Scen.write(bytes([8]))
  710.     if 3<f<6:
  711.       Scen.write(bytes([7]))
  712.     if 5<f<8:
  713.       Scen.write(bytes([6]))
  714.     if 7<f<12:
  715.       Scen.write(bytes([5]))
  716.     if 11<f<14:
  717.       Scen.write(bytes([4]))
  718.     if 13<f<18:
  719.       Scen.write(bytes([3]))
  720.     if 17<f<20:
  721.       Scen.write(bytes([2]))
  722.     if 19<f<24:
  723.       Scen.write(bytes([1]))
  724. #
  725. # RESEARCHED TECHNIQUES
  726. #
  727.     if res.get():
  728.       if Filesize==179109:
  729.         Scen.seek(160667+68*c,0)
  730.       if Filesize==169822:
  731.         Scen.seek(152724+68*c,0)
  732.       if Filesize==167559:
  733.         Scen.seek(150273+72*c,0)
  734.       n = 4
  735.       while n:
  736.         if res.get()==1:
  737.           f = choice([0,1,3])*16+choice([0,1,3])
  738.         if res.get()==2:
  739.           f = choice([3,7])*16+choice([3,7])
  740.         if res.get()==3:
  741.           f = choice([7,15])*16+choice([7,15])
  742.         if res.get()==4:
  743.           f = choice([0,1,3,7,15])*16+choice([0,1,3,7,15])
  744.         Scen.write(bytes([f]))
  745.         n-=1
  746. #
  747. # RELATIONSHIPS BETWEEN FORCES
  748. #
  749.     for n in forces:
  750.       if n == c:
  751.         if Filesize==179109:
  752.           Scen.seek(160607+n+n*68,0)
  753.         if Filesize==169822:
  754.           Scen.seek(152664+n+n*68,0)
  755.         if Filesize==167559:
  756.           Scen.seek(150213+n+n*72,0)
  757.         Scen.write(bytes([50]))
  758.       else:
  759.         q = [10*randrange(0,10)]
  760.         if Filesize==179109:
  761.           Scen.seek(160607+n+c*68)
  762.         if Filesize==169822:
  763.           Scen.seek(152664+n+c*68,0)
  764.         if Filesize==167559:
  765.           Scen.seek(150213+n+c*72,0)
  766.         Scen.write(bytes(q))
  767.         if Filesize==179109:
  768.           Scen.seek(160607+c+n*68)
  769.         if Filesize==169822:
  770.           Scen.seek(152664+c+n*68,0)
  771.         if Filesize==167559:
  772.           Scen.seek(150213+c+n*72,0)
  773.         Scen.write(bytes(q))
  774. #
  775. # GATES AND PORTS ASSIGNMENT
  776. #
  777.     for n in e:
  778.       f = gates[n]
  779.       if len(f):
  780.         for t in f:
  781.           if Filesize==179109:
  782.             Scen.seek(167577+t*64, 0)
  783.           if Filesize==169822:
  784.             Scen.seek(159634+t*64, 0)
  785.           if Filesize==167559:
  786.             Scen.seek(157371+t*64, 0)
  787.           Scen.write(bytes([b]))
  788.           r = 100*randrange(5,15)
  789.           Scen.write(bytes([r%256]))
  790.           Scen.write(bytes([r//256]))
  791.           Scen.write(bytes(2))
  792.           r = 100*randrange(5,10)
  793.           Scen.write(bytes([r%256]))
  794.           Scen.write(bytes([r//256]))
  795.           Scen.write(bytes(2))
  796.           r = 1000*randrange(5,10)
  797.           Scen.write(bytes([r%256]))
  798.           Scen.write(bytes([r//256]))
  799.           r = randrange(70,95)
  800.           if Filesize==179109:
  801.             Scen.seek(167638+t*64, 0)
  802.           if Filesize==169822:
  803.             Scen.seek(159695+t*64, 0)
  804.           if Filesize==167559:
  805.             Scen.seek(157432+t*64, 0)
  806.           Scen.write(bytes([r]))
  807. #
  808. #NEXT FORCE
  809. #
  810.     b+=1
  811. #
  812. # END OF FORCE CREATION LOOP
  813. #
  814. #
  815. # ADD ALLIANCES WHEN ALL FORCES ARE CREATED
  816. #
  817.   if AllyVar.get()==1 and len(forces)>2:
  818.     n = randrange(1,len(forces))
  819.     print("There is a total of " + str(len(forces)) + " forces in this Scenario.")
  820.     ally = []
  821.     while n>0:
  822.       roll=1
  823.       while roll:
  824.         f1 = randrange(0,len(forces))
  825.         c1 = forces[f1]
  826.         f2 = randrange(0,len(forces))
  827.         while f2==f1:
  828.           f2 = randrange(0,len(forces))
  829.         c2 = forces[f2]
  830.         allynum = pow(2,c1)+pow(2,c2)
  831.         if allynum in ally:
  832.           roll=1
  833.         if allynum not in ally:
  834.           ally.append(allynum)
  835.           roll=0
  836. #
  837. # ALLY SELECTED FORCE 1 TO FORCE 2
  838. #
  839.       if Filesize==179109:
  840.         Scen.seek(160659+c1*68,0)
  841.       if Filesize==169822:
  842.         Scen.seek(152716+c1*68,0)
  843.       if Filesize==167559:
  844.         Scen.seek(150265+c1*72,0)
  845.       k1 = ord(Scen.read(1))
  846.       k2 = ord(Scen.read(1))
  847.       k3 = ord(Scen.read(1))
  848.       k4 = ord(Scen.read(1))
  849.       k5 = ord(Scen.read(1))
  850.       k6 = ord(Scen.read(1))
  851.       k = 1099511627776*k6 + 4294967296*k5 + 16777216*k4 + 65536*k3 + 256*k2 + k1 + pow(2,c2)
  852.       if Filesize==179109:
  853.         Scen.seek(160659+c1*68,0)
  854.       if Filesize==169822:
  855.         Scen.seek(152716+c1*68,0)
  856.       if Filesize==167559:
  857.         Scen.seek(150265+c1*72,0)
  858.       if k<256:
  859.         dt = [k, 0, 0, 0, 0, 0]
  860.       if 255<k<65536:
  861.         dt = [k%256, k//256, 0, 0, 0, 0]
  862.       if 65535<k<16777216:
  863.         dt = [(k-65536)%256, k%65536//256, k//65536, 0, 0, 0]
  864.       if 16777215<k<4294967296:
  865.         dt = [k%16777216%65536%256, k%16777216%65536//256, k%16777216//65536, k//16777216, 0, 0]
  866.       if 4294967295<k<1099511627776:
  867.         dt = [k%4294967296%16777216%65536%256, k%4294967296%16777216%65536//256, k%4294967296%16777216//65536, k%4294967296//16777216, k//4294967296, 0]
  868.       if k>1099511627775:
  869.         dt = [k%4294967296%16777216%65536%256, k%4294967296%16777216%65536//256, k%4294967296%16777216//65536, k%4294967296//16777216, k//4294967296%256, k//4294967296//256]
  870.       Scen.write(bytes(dt))
  871. #
  872. # ALLY FORCE 2 TO FORCE 1
  873. #
  874.       if Filesize==179109:
  875.         Scen.seek(160659+c2*68,0)
  876.       if Filesize==169822:
  877.         Scen.seek(152716+c2*68,0)
  878.       if Filesize==167559:
  879.         Scen.seek(150265+c2*72,0)
  880.       k1 = ord(Scen.read(1))
  881.       k2 = ord(Scen.read(1))
  882.       k3 = ord(Scen.read(1))
  883.       k4 = ord(Scen.read(1))
  884.       k5 = ord(Scen.read(1))
  885.       k6 = ord(Scen.read(1))
  886.       k = 1099511627776*k6 + 4294967296*k5 + 16777216*k4 + 65536*k3 + 256*k2 + k1 + pow(2,c1)
  887.       if Filesize==179109:
  888.         Scen.seek(160659+c2*68,0)
  889.       if Filesize==169822:
  890.         Scen.seek(152716+c2*68,0)
  891.       if Filesize==167559:
  892.         Scen.seek(150265+c2*72,0)
  893.       if k<256:
  894.         dt = [k, 0, 0, 0, 0, 0]
  895.       if 255<k<65536:
  896.         dt = [k%256, k//256, 0, 0, 0, 0]
  897.       if 65535<k<16777216:
  898.         dt = [(k-65536)%256, k%65536//256, k//65536, 0, 0, 0]
  899.       if 16777215<k<4294967296:
  900.         dt = [k%16777216%65536%256, k%16777216%65536//256, k%16777216//65536, k//16777216, 0, 0]
  901.       if 4294967295<k<1099511627776:
  902.         dt = [k%4294967296%16777216%65536%256, k%4294967296%16777216%65536//256, k%4294967296%16777216//65536, k%4294967296//16777216, k//4294967296, 0]
  903.       if k>1099511627775:
  904.         dt = [k%4294967296%16777216%65536%256, k%4294967296%16777216%65536//256, k%4294967296%16777216//65536, k%4294967296//16777216, k//4294967296%256, k//4294967296//256]
  905.       Scen.write(bytes(dt))
  906.       n-=1
  907. #
  908. # ITEM RANDOMIZER
  909. #
  910.   if ItemVar.get()==1:
  911.     k = 0
  912.     while k<43:
  913.       n = choice([0,0,0,1])
  914.       if n==1:
  915.         owner = choice(leaderslist)
  916.         cty = 255
  917.       if n==0:
  918.         owner = 65535
  919.         cty = randrange(0,42)
  920.       if Filesize==179109:
  921.         Scen.seek(157233+34*k,0)
  922.       if Filesize==169822:
  923.         Scen.seek(147013+57*k, 0)
  924.       if Filesize==167559:
  925.         Scen.seek(144562+57*k, 0)
  926.       Scen.write(bytes([owner%256, owner//256, cty, n]))
  927.       k+=1
  928. #
  929. #END OF RANDOMIZER
  930. #
  931.     print("There is a total of " + str(len(offi)) + " free officers available the first year.")
  932.     print(str(len(ally)) + " alliances have been created.")
  933.   Scen.close()
  934.   messagebox.showinfo("Info", "Scenario has been randomized")
  935. #
  936. # WINDOWS GUI CREATION
  937. #
  938. window = Tk()
  939. window.wm_title("RandOTK11 V1.05")
  940. rdoff = IntVar()
  941. HistVar = IntVar()
  942. LifeVar = IntVar()
  943. CityVar = IntVar()
  944. CityVar.set(1)
  945. AllyVar = IntVar()
  946. AllyVar.set(1)
  947. ItemVar = IntVar()
  948. ItemVar.set(0)
  949. IA = IntVar()
  950. IA.set(1)
  951. res = IntVar()
  952. res.set(0)
  953. equi = IntVar()
  954. equi.set(1)
  955. #
  956. # GUI : OPEN RANDOMIZE AND EXIT BUTTONS
  957. #
  958. openbtn = Button(window, text = "Open File", width = 10, command = openSCEN)
  959. openbtn.grid(row = 10, column = 1)
  960. randbtn = Button(window, text = "Randomize", width = 10, state = DISABLED, command = Randomize)
  961. randbtn.grid(row = 10, column = 2)
  962. exitbtn = Button(window, text = "Exit", width = 10, command = exitrandotk)
  963. exitbtn.grid(row = 10, column = 3)
  964. helpbtn = Button(window, text = "Info", width = 10, command = info)
  965. helpbtn.grid(row = 10, column = 4)
  966. #
  967. # GUI : STARTING DATE - FORCE NUMBER  AND FREE CITIES ENTRIES
  968. #
  969. SdateLabel = Label(window, text = "Starting Year: ")
  970. SdateLabel.grid(row = 1, column = 1)
  971. SdateEntry = Entry(window, width = 8, justify = CENTER)
  972. SdateEntry.grid(row = 1, column = 2, sticky = W)
  973. FnumLabel = Label(window, text = "Number of Forces: ")
  974. FnumLabel.grid(row = 2, column = 1)
  975. FnumEntry = Entry(window, width = 8, justify = CENTER)
  976. FnumEntry.grid(row = 2, column = 2, sticky = W)
  977. FcityLabel = Label(window, text = "Minimum free cities: ")
  978. FcityLabel.grid(row = 3, column = 1)
  979. FcityEntry = Entry(window, width = 8, justify = CENTER)
  980. FcityEntry.insert(0, "0")
  981. FcityEntry.grid(row = 3, column = 2, sticky = W)
  982. #
  983. # GUI : CHECKBOXES
  984. #
  985. OffiCkb = Checkbutton(window, text = "Randomize Officers", variable = rdoff)
  986. OffiCkb.grid(row = 1, column = 4, sticky = W)
  987. HistCkb = Checkbutton(window, text = "Historical Leaders", variable = HistVar)
  988. HistCkb.grid(row = 2, column = 4, sticky = W)
  989. LifeCkb = Checkbutton(window, text = "Randomize Lifespans", variable = LifeVar)
  990. LifeCkb.grid(row = 3, column = 4, sticky = W)
  991. CityCkb = Checkbutton(window, text = "Neighbor cities only", variable = CityVar)
  992. CityCkb.grid(row = 4, column = 4, sticky = W)
  993. AllyCkb = Checkbutton(window, text = "Add Alliances", variable = AllyVar)
  994. AllyCkb.grid(row = 5, column = 4, sticky = W)
  995. ItemCkb = Checkbutton(window, text = "Randomize Items", variable = ItemVar)
  996. ItemCkb.grid(row = 6, column = 4, sticky = W)
  997. #
  998. # GUI : LISTBOX FORCE BALANCE
  999. #
  1000. BlncLabel = Label(window, text = "Balance Setup:")
  1001. BlncLabel.grid(row = 4, column = 3, sticky = W)
  1002. BlncRdb1 = Radiobutton(window, text = "Similar sized forces", variable = equi, value = 1)
  1003. BlncRdb1.grid(row = 5, column = 3, sticky = W)
  1004. BlncRdb2 = Radiobutton(window, text = "Random sized forces", variable = equi, value = 2)
  1005. BlncRdb2.grid(row = 6, column = 3, sticky = W)
  1006. BlncRdb3 = Radiobutton(window, text = "One city forces", variable = equi, value = 3)
  1007. BlncRdb3.grid(row = 7, column = 3, sticky = W)
  1008. BlncRdb4 = Radiobutton(window, text = "Random Setup", variable = equi, value = 4)
  1009. BlncRdb4.grid(row = 8, column = 3, sticky = W)
  1010. #
  1011. # GUI : LISTBOX TECHNIQUES
  1012. #
  1013. TchqLabel = Label(window, text = "Research Setup:")
  1014. TchqLabel.grid(row = 4, column = 1, sticky = W)
  1015. TchqRdb1 = Radiobutton(window, text = "No research", variable = res, value = 0)
  1016. TchqRdb1.grid(row = 5, column = 1, sticky = W)
  1017. TchqRdb2 = Radiobutton(window, text = "Low research", variable = res, value = 1)
  1018. TchqRdb2.grid(row = 6, column = 1, sticky = W)
  1019. TchqRdb3 = Radiobutton(window, text = "Medium research", variable = res, value = 2)
  1020. TchqRdb3.grid(row = 7, column = 1, sticky = W)
  1021. TchqRdb4 = Radiobutton(window, text = "High research", variable = res, value = 3)
  1022. TchqRdb4.grid(row = 8, column = 1, sticky = W)
  1023. TchqRdb5 = Radiobutton(window, text = "Random research", variable = res, value = 4)
  1024. TchqRdb5.grid(row = 9, column = 1, sticky = W)
  1025. #
  1026. # GUI : RADIOBUTTON IA SETUP
  1027. #
  1028. IALabel = Label(window, text = "IA Setup:")
  1029. IALabel.grid(row = 4, column = 2, sticky = W)
  1030. IARdb1 = Radiobutton(window, text = "Aggressive IA", variable = IA, value = 1)
  1031. IARdb1.grid(row = 5, column = 2, sticky = W)
  1032. IARdb2 = Radiobutton(window, text = "Moderate IA", variable = IA, value = 2)
  1033. IARdb2.grid(row = 6, column = 2, sticky = W)
  1034. IARdb3 = Radiobutton(window, text = "Random IA", variable = IA, value = 3)
  1035. IARdb3.grid(row = 7, column = 2, sticky = W)
  1036. window.mainloop()
  1037. #
  1038. #END OF CODE
  1039. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement