golfkid

demo maker

Jun 28th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. ##What you do to create a demo is this:
  2. ##1) Decide what you want the ninja to do in each frame.
  3. ##2) Convert that into hexadecimal using this code:
  4. ##nothing=0
  5. ##left=1
  6. ##right=2
  7. ##left and right=3
  8. ##hold=4
  9. ##hold and left=5
  10. ##hold and right=6
  11. ##hold and left and right=7
  12. ##jump=C
  13. ##jump and left=D
  14. ##jump and right=E
  15. ##jump and left and right=F
  16. ##3) String together all your characters to make one long hexadecimal number.
  17. ##4) Split this number into groups of 7.
  18. ##5) Reverse each group.
  19. ##6) Put the groups back together again, separated by | symbols.
  20. ##7) Convert the numbers back into base 10.
  21.  
  22.  
  23. def demoMaker2():
  24. frames, commands = makeCommandList()
  25. for i in range(len(commands)):
  26. print commands[i],
  27. print
  28. demoMaker(frames)
  29.  
  30.  
  31. def demoMaker(framesLS):
  32. validDemo = True
  33. for i in range(len(framesLS)):
  34. framesLS[i] = str(framesLS[i])
  35. if commandToHex(framesLS[i],i+1) == 'Invalid':
  36. validDemo = False
  37. break
  38. else:
  39. framesLS[i] = commandToHex(framesLS[i],i+1)
  40.  
  41. if validDemo == True:
  42. hexToDemo(framesLS)
  43. demo = hexToDemo(framesLS)
  44. print 'Your demo data is:',demo[:-1]
  45.  
  46.  
  47.  
  48.  
  49. def commandToHex(command, frame):
  50. # This function converts each command into the correct hexadecimal code
  51. # above, and if the input is an invalid command, stops the program and
  52. # tells you what frame is wrong.
  53.  
  54. command.lower()
  55.  
  56. if command == 'n':
  57. return hex(0)
  58.  
  59. elif command == 'l':
  60. return hex(1)
  61.  
  62. elif command == 'r':
  63. return hex(2)
  64.  
  65. elif command == 'lr' or command == 'rl':
  66. return hex(3)
  67.  
  68. elif command == 'h':
  69. return hex(4)
  70.  
  71. elif command == 'hl' or command == 'lh':
  72. return hex(5)
  73.  
  74. elif command == 'hr' or command == 'rh':
  75. return hex(6)
  76.  
  77. elif command == 'hlr' or command == 'hrl' or command == 'lrh' or command == 'lhr' or command == 'rhl' or command == 'rlh':
  78. return hex(7)
  79.  
  80. elif command == 'j':
  81. return hex(12)
  82.  
  83. elif command == 'jl' or command == 'lj':
  84. return hex(13)
  85.  
  86. elif command == 'rj' or command == 'jr':
  87. return hex(14)
  88.  
  89. elif command == 'jlr' or command == 'jrl' or command == 'lrj' or command == 'ljr' or command == 'rjl' or command == 'rlj':
  90. return hex(15)
  91.  
  92. else:
  93. print 'Invalid entry for frame', frame
  94. return 'Invalid'
  95.  
  96.  
  97. def hexToDemo(frames):
  98. # This function takes the commands that have been converted into hexadecimal
  99. # codes and returns the final demo data for the original input commands.
  100.  
  101. # Creates the beginning of demo data with frame count and colon.
  102. demoLength = len(frames)
  103. totalDemo = str(demoLength)
  104. totalDemo = totalDemo + ':'
  105.  
  106. # Goes through demo 7 frames at a time.
  107. for i in range(0,len(frames),7):
  108. # Placeholder for eventual order reversal, keeping as hexadecimal code
  109. # in the computer.
  110. hexDemoFrag = '0x'
  111.  
  112. # Takes 7 command/frame chunks of the input commands that were changed
  113. # to hexadecimal. This varaible will be reused and reset each time the
  114. # program moves to the next set of 7 commands/frames.
  115. demoFrag0 = frames[i:i+7]
  116.  
  117. # This loop reverses the order of the commands.
  118. for j in range(len(demoFrag0)-1,-1,-1):
  119. hexDemoFrag = hexDemoFrag + demoFrag0[j][-1]
  120.  
  121. # Converting reversed hexadecimal code back into base-10 numbers, which
  122. # is the final form of the demo data.
  123. demoFrag = eval(hexDemoFrag)
  124.  
  125. # Formatting base-10 number into useable form for the program, and
  126. #adding the "|" character that separates each section of 7 frames
  127. # in the demo data.
  128. demoFrag = str(demoFrag)
  129. demoFrag = demoFrag + '|'
  130.  
  131. # Adding the 7-frame chunk of demo data to the entire demo.
  132. totalDemo = totalDemo + demoFrag
  133.  
  134. # Allowing the computer to display the final demo data as an output.
  135. return totalDemo
  136.  
  137.  
  138.  
  139. def makeCommandList():
  140. frameList, commandList, frameCount, done, starting = [], [], 0, False, True
  141. command = raw_input("Enter your list of commands and frames, in form of command, 'x', number of frames, and each new command separated by a space. (e.g. 'Rx4 Lx4') Use N -> nothing, J -> jump, H -> hold jump, R -> move right, L -> move left, and any combination of these commands (e.g. JR -> jump right). Warning: Only use J when starting a jump for one frame, and use H for all other frames that you would normally be holding the jump button. ")
  142. command = str(command).split()
  143. for i in range(len(command)):
  144. commandList.append(str(command[i].split('x')[0].upper() + ' x ' + str(command[i].split('x')[1]) + ' frames'))
  145. for j in range(int(command[i].split('x')[1])):
  146. frameCount =+ 1
  147. frameList.append(command[i].split('x')[0])
  148. print
  149.  
  150. return frameList, commandList
Advertisement
Add Comment
Please, Sign In to add comment