BoostSimon

Untitled

Apr 24th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.87 KB | None | 0 0
  1. ################################################################################
  2. # Mystery Gift system
  3. # By Maruno
  4. ################################################################################
  5. # This url is the location of an example Mystery Gift file.
  6. # You should change it to your file's url once you upload it.
  7. ################################################################################
  8.  
  9. MYSTERYGIFTURL = "https://pastebin.com/raw/f11BxwHi"
  10.  
  11.  
  12. class PokeBattle_Trainer
  13. attr_accessor(:mysterygiftaccess) # Whether MG can be used from load screen
  14. attr_accessor(:mysterygift) # Variable that stores downloaded MG data
  15.  
  16. def mysterygiftaccess
  17. @mysterygiftaccess=false if !@mysterygiftaccess
  18. return @mysterygiftaccess
  19. end
  20.  
  21. def mysterygift
  22. @mysterygift=[] if !@mysterygift
  23. return @mysterygift
  24. end
  25. end
  26.  
  27.  
  28.  
  29. ################################################################################
  30. # Creating a new Mystery Gift for the Master file, and editing an existing one.
  31. ################################################################################
  32. # type: 0=Pokémon; 1 or higher=item (is the item's quantity).
  33. # item: The thing being turned into a Mystery Gift (Pokémon object or item ID).
  34. def pbEditMysteryGift(type,item,id=0,giftname="")
  35. begin
  36. if type==0 # Pokémon
  37. commands=[_INTL("Mystery Gift"),
  38. _INTL("Faraway place")]
  39. commands.push(item.obtainText) if item.obtainText && item.obtainText!=""
  40. commands.push(_INTL("[Custom]"))
  41. loop do
  42. command=Kernel.pbMessage(
  43. _INTL("Choose a phrase to be where the gift Pokémon was obtained from."),commands)
  44. if command>=0 && command<commands.length-1
  45. item.obtainText=commands[command]
  46. break
  47. elsif command==commands.length-1
  48. obtainname=Kernel.pbMessageFreeText(_INTL("Enter a phrase."),"",false,32)
  49. if obtainname!=""
  50. item.obtainText=obtainname
  51. break
  52. end
  53. return nil if Kernel.pbConfirmMessage(_INTL("Stop editng this gift?"))
  54. elsif command==-1
  55. return nil if Kernel.pbConfirmMessage(_INTL("Stop editng this gift?"))
  56. end
  57. end
  58. elsif type>0 # Item
  59. params=ChooseNumberParams.new
  60. params.setRange(1,99999)
  61. params.setDefaultValue(type)
  62. params.setCancelValue(0)
  63. loop do
  64. newtype=Kernel.pbMessageChooseNumber(_INTL("Choose a quantity."),params)
  65. if newtype==0
  66. return nil if Kernel.pbConfirmMessage(_INTL("Stop editng this gift?"))
  67. else
  68. type=newtype
  69. break
  70. end
  71. end
  72. end
  73. if id==0
  74. master=[]; idlist=[]
  75. if safeExists?("MysteryGiftMaster.txt")
  76. master=IO.read("MysteryGiftMaster.txt")
  77. master=pbMysteryGiftDecrypt(master)
  78. end
  79. for i in master; idlist.push(i[0]); end
  80. params=ChooseNumberParams.new
  81. params.setRange(0,99999)
  82. params.setDefaultValue(id)
  83. params.setCancelValue(0)
  84. loop do
  85. newid=Kernel.pbMessageChooseNumber(_INTL("Choose a unique ID for this gift."),params)
  86. if newid==0
  87. return nil if Kernel.pbConfirmMessage(_INTL("Stop editng this gift?"))
  88. else
  89. if idlist.include?(newid)
  90. Kernel.pbMessage(_INTL("That ID is already used by a Mystery Gift."))
  91. else
  92. id=newid
  93. break
  94. end
  95. end
  96. end
  97. end
  98. loop do
  99. newgiftname=Kernel.pbMessageFreeText(_INTL("Enter a name for the gift."),giftname,false,32)
  100. if newgiftname!=""
  101. giftname=newgiftname
  102. break
  103. end
  104. return nil if Kernel.pbConfirmMessage(_INTL("Stop editng this gift?"))
  105. end
  106. return [id,type,item,giftname]
  107. rescue
  108. Kernel.pbMessage(_INTL("Couldn't edit the gift."))
  109. return nil
  110. end
  111. end
  112.  
  113. def pbCreateMysteryGift(type,item)
  114. gift=pbEditMysteryGift(type,item)
  115. if !gift
  116. Kernel.pbMessage(_INTL("Didn't create a gift."))
  117. else
  118. begin
  119. if safeExists?("MysteryGiftMaster.txt")
  120. master=IO.read("MysteryGiftMaster.txt")
  121. master=pbMysteryGiftDecrypt(master)
  122. master.push(gift)
  123. else
  124. master=[gift]
  125. end
  126. string=pbMysteryGiftEncrypt(master)
  127. File.open("MysteryGiftMaster.txt","wb"){|f|
  128. f.write(string)
  129. }
  130. Kernel.pbMessage(_INTL("The gift was saved to MysteryGiftMaster.txt."))
  131. rescue
  132. Kernel.pbMessage(_INTL("Couldn't save the gift to MysteryGiftMaster.txt."))
  133. end
  134. end
  135. end
  136.  
  137.  
  138.  
  139. ################################################################################
  140. # Debug option for managing gifts in the Master file and exporting them to a
  141. # file to be uploaded.
  142. ################################################################################
  143. def pbManageMysteryGifts
  144. if !safeExists?("MysteryGiftMaster.txt")
  145. Kernel.pbMessage(_INTL("There are no Mystery Gifts defined."))
  146. return
  147. end
  148. # Load all gifts from the Master file.
  149. master=IO.read("MysteryGiftMaster.txt")
  150. master=pbMysteryGiftDecrypt(master)
  151. if !master || !master.is_a?(Array) || master.length==0
  152. Kernel.pbMessage(_INTL("There are no Mystery Gifts defined."))
  153. return
  154. end
  155. # Download all gifts from online
  156. msgwindow=Kernel.pbCreateMessageWindow
  157. Kernel.pbMessageDisplay(msgwindow,_INTL("Searching for online gifts...\\wtnp[0]"))
  158. online=pbDownloadToString(MYSTERYGIFTURL)
  159. Kernel.pbDisposeMessageWindow(msgwindow)
  160. if online==""
  161. Kernel.pbMessage(_INTL("No online Mystery Gifts found.\\wtnp[20]"))
  162. online=[]
  163. else
  164. Kernel.pbMessage(_INTL("Online Mystery Gifts found.\\wtnp[20]"))
  165. online=pbMysteryGiftDecrypt(online)
  166. t=[]
  167. for gift in online; t.push(gift[0]); end
  168. online=t
  169. end
  170. # Show list of all gifts.
  171. command=0
  172. loop do
  173. commands=pbRefreshMGCommands(master,online)
  174. command=Kernel.pbMessage(_INTL("\\ts[]Manage Mystery Gifts (X=online)."),commands,-1,nil,command)
  175. # Gift chosen
  176. if command==-1 || command==commands.length-1
  177. break
  178. elsif command==commands.length-2
  179. begin
  180. newfile=[]
  181. for gift in master
  182. newfile.push(gift) if online.include?(gift[0])
  183. end
  184. string=pbMysteryGiftEncrypt(newfile)
  185. File.open("MysteryGift.txt","wb"){|f|
  186. f.write(string)
  187. }
  188. Kernel.pbMessage(_INTL("The gifts were saved to MysteryGift.txt."))
  189. Kernel.pbMessage(_INTL("Upload MysteryGift.txt to the Internet."))
  190. rescue
  191. Kernel.pbMessage(_INTL("Couldn't save the gifts to MysteryGift.txt."))
  192. end
  193. elsif command>=0 && command<commands.length-2
  194. cmd=0
  195. loop do
  196. commands=pbRefreshMGCommands(master,online)
  197. gift=master[command]
  198. cmds=[_INTL("Toggle on/offline"),
  199. _INTL("Edit"),
  200. _INTL("Receive"),
  201. _INTL("Delete"),
  202. _INTL("Cancel")]
  203. cmd=Kernel.pbMessage("\\ts[]"+commands[command],cmds,-1,nil,cmd)
  204. if cmd==-1 || cmd==cmds.length-1
  205. break
  206. elsif cmd==0 # Toggle on/offline
  207. if online.include?(gift[0])
  208. for i in 0...online.length
  209. online[i]=nil if online[i]==gift[0]
  210. end
  211. online.compact!
  212. else
  213. online.push(gift[0])
  214. end
  215. elsif cmd==1 # Edit
  216. newgift=pbEditMysteryGift(gift[1],gift[2],gift[0],gift[3])
  217. master[command]=newgift if newgift
  218. elsif cmd==2 # Receive
  219. replaced=false
  220. for i in 0...$Trainer.mysterygift.length
  221. if $Trainer.mysterygift[i][0]==gift[0]
  222. $Trainer.mysterygift[i]=gift; replaced=true
  223. end
  224. end
  225. $Trainer.mysterygift.push(gift) if !replaced
  226. pbReceiveMysteryGift(gift[0])
  227. elsif cmd==3 # Delete
  228. if Kernel.pbConfirmMessage(_INTL("Are you sure you want to delete this gift?"))
  229. master[command]=nil
  230. master.compact!
  231. end
  232. break
  233. end
  234. end
  235. end
  236. end
  237. end
  238.  
  239. def pbRefreshMGCommands(master,online)
  240. commands=[]
  241. for gift in master
  242. itemname="BLANK"
  243. if gift[1]==0
  244. itemname=PBSpecies.getName(gift[2].species)
  245. elsif gift[1]>0
  246. itemname=PBItems.getName(gift[2])+sprintf(" x%d",gift[1])
  247. end
  248. ontext=["[ ]","[X]"][(online.include?(gift[0])) ? 1 : 0]
  249. commands.push(_ISPRINTF("{1:s} {2:d}: {3:s} ({4:s})",ontext,gift[0],gift[3],itemname))
  250. end
  251. commands.push(_INTL("Export selected to file"))
  252. commands.push(_INTL("Cancel"))
  253. return commands
  254. end
  255.  
  256.  
  257.  
  258. ################################################################################
  259. # Downloads all available Mystery Gifts that haven't been downloaded yet.
  260. ################################################################################
  261. # Called from the Continue/New Game screen.
  262. def pbDownloadMysteryGift(trainer)
  263. sprites={}
  264. viewport=Viewport.new(0,0,Graphics.width,Graphics.height)
  265. viewport.z=99999
  266. addBackgroundPlane(sprites,"background","mysteryGiftbg",viewport)
  267. pbFadeInAndShow(sprites)
  268. sprites["msgwindow"]=Kernel.pbCreateMessageWindow
  269. Kernel.pbMessageDisplay(sprites["msgwindow"],_INTL("Searching for a gift.\nPlease wait...\\wtnp[0]"))
  270. string=pbDownloadToString(MYSTERYGIFTURL)
  271. if string==""
  272. Kernel.pbMessageDisplay(sprites["msgwindow"],_INTL("No new gifts are available."))
  273. else
  274. online=pbMysteryGiftDecrypt(string)
  275. pending=[]
  276. for gift in online
  277. notgot=true
  278. for j in trainer.mysterygift
  279. notgot=false if j[0]==gift[0]
  280. end
  281. pending.push(gift) if notgot
  282. end
  283. if pending.length==0
  284. Kernel.pbMessageDisplay(sprites["msgwindow"],_INTL("No new gifts are available."))
  285. else
  286. loop do
  287. commands=[]
  288. for gift in pending; commands.push(gift[3]); end
  289. commands.push(_INTL("Cancel"))
  290. Kernel.pbMessageDisplay(sprites["msgwindow"],_INTL("Choose the gift you want to receive.\\wtnp[0]"))
  291. command=Kernel.pbShowCommands(sprites["msgwindow"],commands,-1)
  292. if command==-1 || command==commands.length-1
  293. break
  294. else
  295. gift=pending[command]
  296. sprites["msgwindow"].visible=false
  297. isitem=false
  298. if gift[1]==0
  299. sprite=PokemonSprite.new(viewport)
  300. sprite.setPokemonBitmap(gift[2])
  301. sprite.ox=sprite.bitmap.width/2
  302. sprite.oy=sprite.bitmap.height/2
  303. sprite.x=Graphics.width/2
  304. sprite.y=-sprite.bitmap.height/2
  305. else
  306. sprite=ItemIconSprite.new(0,0,gift[2],viewport)
  307. sprite.x=Graphics.width/2
  308. sprite.y=-sprite.height/2
  309. isitem=true
  310. end
  311. begin
  312. Graphics.update
  313. Input.update
  314. sprite.update
  315. sprite.y+=4
  316. end while sprite.y<Graphics.height/2
  317. pbMEPlay("Jingle - HMTM")
  318. 3*Graphics.frame_rate.times do
  319. Graphics.update
  320. Input.update
  321. sprite.update
  322. pbUpdateSceneMap
  323. end
  324. sprites["msgwindow"].visible=true
  325. Kernel.pbMessageDisplay(sprites["msgwindow"],_INTL("The gift has been received!")) { sprite.update }
  326. Kernel.pbMessageDisplay(sprites["msgwindow"],_INTL("Please pick up your gift from the deliveryman in any Poké Mart.")) { sprite.update }
  327. trainer.mysterygift.push(gift)
  328. pending[command]=nil; pending.compact!
  329. begin
  330. Graphics.update
  331. Input.update
  332. sprite.update
  333. sprite.opacity-=8
  334. end while sprite.opacity>0
  335. sprite.dispose
  336. end
  337. if pending.length==0
  338. Kernel.pbMessageDisplay(sprites["msgwindow"],_INTL("No new gifts are available."))
  339. break
  340. end
  341. end
  342. end
  343. end
  344. pbFadeOutAndHide(sprites)
  345. Kernel.pbDisposeMessageWindow(sprites["msgwindow"])
  346. pbDisposeSpriteHash(sprites)
  347. viewport.dispose
  348. return trainer
  349. end
  350.  
  351.  
  352.  
  353. ################################################################################
  354. # Converts an array of gifts into a string and back.
  355. ################################################################################
  356. def pbMysteryGiftEncrypt(gift)
  357. ret=[Zlib::Deflate.deflate(Marshal.dump(gift))].pack("m")
  358. return ret
  359. end
  360.  
  361. def pbMysteryGiftDecrypt(gift)
  362. return [] if gift==""
  363. ret=Marshal.restore(Zlib::Inflate.inflate(gift.unpack("m")[0]))
  364. return ret
  365. end
  366.  
  367.  
  368.  
  369. ################################################################################
  370. # Collecting a Mystery Gift from the deliveryman.
  371. ################################################################################
  372. def pbNextMysteryGiftID
  373. for i in $Trainer.mysterygift
  374. return i[0] if i.length>1
  375. end
  376. return 0
  377. end
  378.  
  379. def pbReceiveMysteryGift(id)
  380. index=-1
  381. for i in 0...$Trainer.mysterygift.length
  382. if $Trainer.mysterygift[i][0]==id && $Trainer.mysterygift[i].length>1
  383. index=i
  384. break
  385. end
  386. end
  387. if index==-1
  388. Kernel.pbMessage(_INTL("Couldn't find an unclaimed Mystery Gift with ID {1}.",id))
  389. return false
  390. end
  391. gift=$Trainer.mysterygift[index]
  392. if gift[1]==0
  393. pID=rand(256)
  394. pID|=rand(256)<<8
  395. pID|=rand(256)<<16
  396. pID|=rand(256)<<24
  397. gift[2].personalID=pID
  398. gift[2].calcStats
  399. time=pbGetTimeNow
  400. gift[2].timeReceived=time.getgm.to_i
  401. gift[2].obtainMode=4 # Fateful encounter
  402. gift[2].pbRecordFirstMoves
  403. if $game_map
  404. gift[2].obtainMap=$game_map.map_id
  405. gift[2].obtainLevel=gift[2].level
  406. else
  407. gift[2].obtainMap=0
  408. gift[2].obtainLevel=gift[2].level
  409. end
  410. if pbAddPokemonSilent(gift[2])
  411. Kernel.pbMessage(_INTL("{1} received {2}!\\se[ItemGet]\1",$Trainer.name,gift[2].name))
  412. $Trainer.mysterygift[index]=[id]
  413. return true
  414. end
  415. elsif gift[1]>0
  416. if $PokemonBag.pbCanStore?(gift[2],gift[1])
  417. $PokemonBag.pbStoreItem(gift[2],gift[1])
  418. item=gift[2]; qty=gift[1]
  419. itemname=(qty>1) ? PBItems.getNamePlural(item) : PBItems.getName(item)
  420. if $ItemData[item][ITEMUSE]==3 || $ItemData[item][ITEMUSE]==4
  421. Kernel.pbMessage(_INTL("\\se[ItemGet]{1} received \\c[1]{2}\\c[0]!\\nIt contained \\c[1]{3}\\c[0].\\wtnp[30]",
  422. $Trainer.name,itemname,PBMoves.getName($ItemData[item][ITEMMACHINE])))
  423. elsif isConst?(item,PBItems,:LEFTOVERS)
  424. Kernel.pbMessage(_INTL("\\se[ItemGet]{1} received some \\c[1]{2}\\c[0]!\\wtnp[30]",$Trainer.name,itemname))
  425. elsif qty>1
  426. Kernel.pbMessage(_INTL("\\se[ItemGet]{1} received {2} \\c[1]{3}\\c[0]!\\wtnp[30]",$Trainer.name,qty,itemname))
  427. else
  428. Kernel.pbMessage(_INTL("\\se[ItemGet]{1} received one \\c[1]{2}\\c[0]!\\wtnp[30]",$Trainer.name,itemname))
  429. end
  430. $Trainer.mysterygift[index]=[id]
  431. return true
  432. end
  433. end
  434. return false
  435. end
Advertisement
Add Comment
Please, Sign In to add comment