WolfHeretic

CharacterCust_Utilities

Sep 3rd, 2020
9,391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.06 KB | None | 0 0
  1. #===============================================================================
  2. # * Character Costumization by Wolf Heretic
  3. # * Based Heavily on a script by shiney570
  4. # Version: 2.0.0
  5. #===============================================================================
  6. # This part of the script focuses mostly on how outfits customized are saved as
  7. # well as various functions used throughout the script. Includes (In Order):
  8. # - Utility methods
  9. # - Initial saving of outfit
  10. # - Permanent Save of outfit when player saves game
  11. # - Edits how metadata is called to help distinguish between saved and non-saved
  12. # outfits.
  13. # - Edits png extention
  14. #
  15. #
  16. #==================================================================================
  17. # *Utility Methods used throughout this script
  18. #==================================================================================
  19.  
  20. # Given a string and a bool, returns an array and a var representing the array number.
  21. # accessory: A string representing an item in the array that the user wants to access.
  22. # convertation: A Bool which determines whether or not the names specific to a
  23. # players gender are returned (true), or the entire array (false).
  24. def retArrayAndNumber(accessory,convertation=true)
  25. (bodypart=HAIR_ITEMS; var=1) if cnvrtStrArr(HAIR_ITEMS).include?(accessory)
  26. (bodypart=TOP_ITEMS; var=2) if cnvrtStrArr(TOP_ITEMS).include?(accessory)
  27. (bodypart=BOTTOM_ITEMS; var=3) if cnvrtStrArr(BOTTOM_ITEMS).include?(accessory)
  28. (bodypart=HEADGEAR_ITEMS; var=4) if cnvrtStrArr(HEADGEAR_ITEMS).include?(accessory)
  29. (bodypart=ACCESSORY_ITEMS; var=5) if cnvrtStrArr(ACCESSORY_ITEMS).include?(accessory)
  30. return [cnvrtStrArr(bodypart),var] if convertation
  31. return [bodypart,var]
  32. end
  33.  
  34. # Returns a boolean indicating if the character can be customized given settings
  35. # set by the creator and the player's current state.
  36. def characterizationException
  37. return true if CHARACTER_CUSTOMIZATION==false
  38. return true if UNLOCK_CHARACTER_CUSTOMIZATION_BY_DEFAULT==false &&
  39. $Trainer.character_customization==false
  40. return true if !$Trainer
  41. return true if !$Trainer.hair
  42. return false
  43. end
  44.  
  45. # Creates a new array from the items specific to the player's gender.
  46. # array: An array representing the options for a specific component of the outfit
  47. # characterization of the character (ie. HAIR_ITEMS)
  48. def cnvrtStrArr(array)
  49. ret=[]
  50. for i in 0...array.length
  51. ret.push(array[i][0][$PokemonGlobal.playerID])
  52. end
  53. return ret
  54. end
  55.  
  56. #cnvrtBoolArr: Creates a new array with only the booleans which define the items within the array.
  57. #array: An array representing the options for a specific component of the outfit characterization of the character (ie. HAIR_ITEMS)
  58. #output: A boolean array representing which items within the component array are locked and unlocked; represented by booleans.
  59. def cnvrtBoolArr(array)
  60. ret=[]
  61. for i in 0...array.length
  62. if ((array[i][1] == true) || (array[i][1] == false))
  63. ret.push(array[i][1])
  64. else
  65. ret[i]=[]
  66. for j in 0...array[i][1].length
  67. ret[i].push(array[i][1][j][1])
  68. end
  69. end
  70. end
  71. return ret
  72. end
  73.  
  74. #Creates a new array with only the unlocked elements of the given array.
  75. #clothes: One of the arrays defining the gendered items and properties for
  76. # a specific attribute (ie. cnvrtStrArr(HEADGEAR_ITEMS))
  77. def retUnlockedAccessoryArray(clothes)
  78. arr=retArrayAndNumber(clothes[0])
  79. var=arr[1]
  80. ret=[]
  81. for i in 0...clothes.length
  82. if ($Trainer.clothesUnlocking[var-1][i]==false || $Trainer.clothesUnlocking[var-1][i]==true)
  83. ret.push clothes[i] if $Trainer.clothesUnlocking[var-1][i]==true
  84. else
  85. check=false
  86. for j in 0...$Trainer.clothesUnlocking[var-1][i].length
  87. check=(check||$Trainer.clothesUnlocking[var-1][i][j])
  88. end
  89. ret.push clothes[i] if check
  90. end
  91. end
  92. return ret
  93. end
  94.  
  95. #Creates a new array with only the unlocked elements of the given array.
  96. #clothes: One of the arrays defining the gendered items and properties for
  97. # a specific attribute (ie. cnvrtStrArr(HEADGEAR_ITEMS))
  98. def retUnlockedAccessoryArray2(clothes,itemnum)
  99. arr=retArrayAndNumber(clothes[0],false)
  100. var=arr[1]
  101. ret=[]
  102. for i in 0...arr[0][itemnum][1].length
  103. if $Trainer.clothesUnlocking[var-1][itemnum][i]
  104. ret.push arr[0][itemnum][1][i][0]
  105. end
  106. end
  107. return ret
  108. end
  109.  
  110. #Fetches the file names corresponding to a given array.
  111. #files: An array where all the file names will be stored. Should be the empty array initially
  112. #array: An array of clothing items (ie. HAIR_ITEMS)
  113. #folder: The folder from which these file names will correspond to.
  114. def individualArrayFiles(files,array,folder)
  115. for i in 0...array.length
  116. if ((array[i][1] == true) || (array[i][1] == false))
  117. files.push(folder+"/#{i}"+($PokemonGlobal.playerID+65).chr)
  118. else
  119. for j in 0...array[i][1].length
  120. files.push(folder+"/#{i}/#{j}"+($PokemonGlobal.playerID+65).chr)
  121. end
  122. end
  123. end
  124. end
  125.  
  126. # checks whether or not an inputed array is multi-dimensional at a given index.
  127. # arr: An array of clothing items (ie. HAIR_ITEMS)
  128. # num: The index numberof a specific item in the array.
  129. # single(optional): if true, checks if index at given array is singular, otherwise
  130. # checks if not singular.
  131. def checkAccessory(arr,num,single=true)
  132. if (arr[num][1]==true)||(arr[num][1]==false)
  133. return (false^single)
  134. else
  135. return (true^single)
  136. end
  137. end
  138.  
  139. # helperfunction for randomizing outfit.
  140. # bodypart: number corresponding to the specific bodypart array desired.
  141. def randomizeOutfitHelper(bodypart)
  142. arr=HAIR_ITEMS if bodypart==0
  143. arr=TOP_ITEMS if bodypart==1
  144. arr=BOTTOM_ITEMS if bodypart==2
  145. arr=HEADGEAR_ITEMS if bodypart==3
  146. arr=ACCESSORY_ITEMS if bodypart==4
  147. fv=rand(arr.length)
  148. if checkAccessory(arr,fv)
  149. case bodypart
  150. when 0
  151. $Trainer.hair=[fv, -1]
  152. when 1
  153. $Trainer.top=[fv, -1]
  154. when 2
  155. $Trainer.bottom=[fv, -1]
  156. when 3
  157. $Trainer.headgear=[fv, -1]
  158. when 4
  159. $Trainer.accessory=[fv, -1]
  160. end
  161. else
  162. case bodypart
  163. when 0
  164. $Trainer.hair=[fv, rand(arr[fv][1].length)]
  165. when 1
  166. $Trainer.top=[fv, rand(arr[fv][1].length)]
  167. when 2
  168. $Trainer.bottom=[fv, rand(arr[fv][1].length)]
  169. when 3
  170. $Trainer.headgear=[fv, rand(arr[fv][1].length)]
  171. when 4
  172. $Trainer.accessory=[fv, rand(arr[fv][1].length)]
  173. end
  174. end
  175. end
  176.  
  177. #===============================================================================
  178. # * Drawing the customized Bitmap
  179. #===============================================================================
  180.  
  181. # Method to add an additional bitmap to another bitmap.
  182. def addAdditionalBitmap(filepath,formerBitmap)
  183. if File.exists?(filepath)
  184. formerBitmap.blt(0,0,Bitmap.new(filepath),Rect.new(0,0,Graphics.width,Graphics.height))
  185. end
  186. end
  187.  
  188. #Draws a specific bitmap for the player sprite.
  189. def drawCharacterCustomizedBitmap(folder,bmp,trainerClass=$Trainer)
  190. return nil if !folder.is_a?(String)
  191. return bitmap if !trainerClass
  192. return bitmap if CHARACTER_CUSTOMIZATION==false
  193. return bitmap if UNLOCK_CHARACTER_CUSTOMIZATION_BY_DEFAULT==false &&
  194. trainerClass.character_customization==false
  195. oldfilepath = "Graphics/Characters/"+folder+"/"
  196. # Adding Bottom Bitmap
  197. if trainerClass.bottom[1] == -1
  198. addAdditionalBitmap(oldfilepath+"bottoms/"+(trainerClass.bottom[0]).to_s+
  199. ($PokemonGlobal.playerID+65).chr+".png",bmp)
  200. else
  201. addAdditionalBitmap(oldfilepath+"bottoms/"+(trainerClass.bottom[0]).to_s+
  202. "/"+(trainerClass.bottom[1]).to_s+($PokemonGlobal.playerID+65).chr+".png",bmp)
  203. end
  204. # Adding Top Bitmap
  205. if trainerClass.top[1] == -1
  206. addAdditionalBitmap(oldfilepath+"tops/"+(trainerClass.top[0]).to_s+
  207. ($PokemonGlobal.playerID+65).chr+".png",bmp)
  208. else
  209. addAdditionalBitmap(oldfilepath+"tops/"+(trainerClass.top[0]).to_s+
  210. "/"+(trainerClass.top[1]).to_s+($PokemonGlobal.playerID+65).chr+".png",bmp)
  211. end
  212. # Adding Accessory Bitmap
  213. if trainerClass.accessory[1] == -1
  214. addAdditionalBitmap(oldfilepath+"accessories/"+(trainerClass.accessory[0]).to_s+
  215. ($PokemonGlobal.playerID+65).chr+".png",bmp)
  216. else
  217. addAdditionalBitmap(oldfilepath+"accessories/"+(trainerClass.accessory[0]).to_s+
  218. "/"+(trainerClass.accessory[1]).to_s+($PokemonGlobal.playerID+65).chr+".png",bmp)
  219. end
  220. # Adding Hair Bitmap
  221. if trainerClass.hair[1] == -1
  222. addAdditionalBitmap(oldfilepath+"hair/"+(trainerClass.hair[0]).to_s+
  223. ($PokemonGlobal.playerID+65).chr+".png",bmp)
  224. else
  225. addAdditionalBitmap(oldfilepath+"hair/"+(trainerClass.hair[0]).to_s+
  226. "/"+(trainerClass.hair[1]).to_s+($PokemonGlobal.playerID+65).chr+".png",bmp)
  227. end
  228. # Adding Headgear Bitmap
  229. if trainerClass.headgear[1] == -1
  230. addAdditionalBitmap(oldfilepath+"headgear/"+(trainerClass.headgear[0]).to_s+
  231. ($PokemonGlobal.playerID+65).chr+".png",bmp)
  232. else
  233. addAdditionalBitmap(oldfilepath+"headgear/"+(trainerClass.headgear[0]).to_s+
  234. "/"+(trainerClass.headgear[1]).to_s+($PokemonGlobal.playerID+65).chr+".png",bmp)
  235. end
  236. end
  237.  
  238. # saves a specific bitmap to a given folder.
  239. def saveCustomizedBitmapToFolder(filepath,folder)
  240. return if !$Trainer
  241. return if !filepath.is_a?(String) || !folder.is_a?(String)
  242. if !USE_BASE_GRAPHIC
  243. bmp=Bitmap.new(filepath)
  244. else
  245. bmp=Bitmap.new(filepath+"_base")
  246. end
  247. # Safety Copy
  248. if !File.exists?(filepath+"_safetyCopy"+".png") && $DEBUG
  249. safetyCopy=Bitmap.new(filepath)
  250. safetyCopy.saveToPng(filepath+"_safetyCopy"+".png")
  251. end
  252. # Deleting old file
  253. if !USE_BASE_GRAPHIC
  254. bmp.clear
  255. end
  256. drawCharacterCustomizedBitmap(folder,bmp)
  257. bmp.saveToPng(filepath+"_curr.png")
  258. end
  259.  
  260. # saves the costumized bitmaps to the actual game folders.
  261. def saveAllCustomizedBitmapsToFolder
  262. return if !$Trainer
  263. # Trainer charsets
  264. metadata=pbLoadMetadata
  265. filenames=metadata[0][MetadataPlayerA+$PokemonGlobal.playerID]
  266. for i in 0...filenames.length
  267. if filenames[i].is_a?(String) && !(filenames[i]=="xxx")
  268. filepath="Graphics/Characters/#{filenames[i]}"
  269. folder=SPRITE_CONVERT_HASH[filenames[i]]
  270. saveCustomizedBitmapToFolder(filepath,folder)
  271. end
  272. end
  273. # Trainer backsprite
  274. helpr="trback00#{$PokemonGlobal.playerID}"
  275. filepath="Graphics/Trainers/"
  276. folder=SPRITE_CONVERT_HASH[helpr]
  277. saveCustomizedBitmapToFolder(filepath+helpr,folder)
  278. # Intro Image/Trainercard Image
  279. filepath="Graphics/Pictures/"
  280. helpr=$Trainer.isFemale? ? "introGirl" : "introBoy" #Modify this line if you want more than two characters.
  281. folder=SPRITE_CONVERT_HASH[helpr]
  282. saveCustomizedBitmapToFolder(filepath+helpr,folder)
  283. # Map Player
  284. helpr="mapPlayer00#{$PokemonGlobal.playerID}"
  285. folder=SPRITE_CONVERT_HASH[helpr]
  286. saveCustomizedBitmapToFolder(filepath+helpr,folder)
  287. end
  288.  
  289. #===============================================================================
  290. # Creates a method which saves a character's outfit permanently when the game is saved.
  291. #===============================================================================
  292.  
  293. #Saves a specific outfit when pbSave is called.
  294. def saveOutfit(filepath)
  295. if (File.exists?(filepath+".png") && File.exists?(filepath+"_curr.png"))
  296. File.delete(filepath+".png")
  297. bmp=Bitmap.new(filepath+"_curr.png")
  298. bmp.saveToPng(filepath+".png")
  299. else
  300. if $DEBUG
  301. p "ERROR: Unable to save file at #{filepath}"
  302. end
  303. end
  304. return
  305. end
  306.  
  307. #Saves all of the outfits when pbSave is called.
  308. def saveAllOutfits
  309. return if !$Trainer
  310. $PokemonTemp.savedoutfit = true
  311. if !reqFilesExist
  312. if $DEBUG
  313. p "Unable to replace files. One of the neccessary files does not exist"
  314. end
  315. $PokemonTemp.savedoutfit = false
  316. return
  317. end
  318. metadata=pbLoadMetadata
  319. filepath="Graphics/Characters/"
  320. filenames=metadata[0][MetadataPlayerA+$PokemonGlobal.playerID]
  321. for i in 0...filenames.length
  322. if filenames[i].is_a?(String) && !(filenames[i]=="xxx")
  323. saveOutfit(filepath+filenames[i])
  324. end
  325. end
  326. # Trainer backsprite
  327. saveOutfit("Graphics/Trainers/trback00#{$PokemonGlobal.playerID}")
  328. # Intro Image/Trainercard Image
  329. filepath="Graphics/Pictures/"
  330. filepath+= $Trainer.isFemale? ? "introGirl" : "introBoy"
  331. saveOutfit(filepath)
  332. # Map Player
  333. saveOutfit("Graphics/Pictures/mapPlayer00#{$PokemonGlobal.playerID}")
  334. return
  335. end
  336.  
  337. #Checks to see if all of the required files exist before trying to save all of the outfits.
  338. def reqFilesExist
  339. # Trainer charsets
  340. metadata=pbLoadMetadata
  341. filenames=metadata[0][MetadataPlayerA+$PokemonGlobal.playerID]
  342. for i in 0...filenames.length
  343. if filenames[i].is_a?(String) && !(filenames[i]=="xxx")
  344. if !File.exists?("Graphics/Characters/#{filenames[i]}.png")
  345. return false
  346. end
  347. if !File.exists?("Graphics/Characters/#{filenames[i]}"+"_curr.png")
  348. return false
  349. end
  350. end
  351. end
  352. # Trainer backsprite
  353. filepath="Graphics/Trainers/trback00#{$PokemonGlobal.playerID}"
  354. if (!File.exists?(filepath+".png") || !File.exists?(filepath+"_curr.png"))
  355. return false
  356. end
  357. # Intro Image/Trainercard Image
  358. filepath="Graphics/Pictures/"
  359. filepath+= $Trainer.isFemale? ? "introGirl" : "introBoy"
  360. if (!File.exists?(filepath+".png") || !File.exists?(filepath+"_curr.png"))
  361. return false
  362. end
  363. # Map Player
  364. filepath="Graphics/Pictures/mapPlayer00#{$PokemonGlobal.playerID}"
  365. if (!File.exists?(filepath+".png") || !File.exists?(filepath+"_curr.png"))
  366. return false
  367. end
  368. return true
  369. end
  370.  
  371.  
  372. #==============================================================================================
  373. # Influences how the metadata is called so that an outfit is temporarily stored when changed.
  374. #==============================================================================================
  375.  
  376. def pbGetMetadata(mapid,metadataType)
  377. meta=pbLoadMetadata
  378. if (mapid == 0) && (metadataType >= MetadataPlayerA) && (metadataType < MetadataPlayerA+7) && ($PokemonTemp.savedoutfit == false)
  379. ret=[]
  380. ret.push(meta[mapid][metadataType][0])
  381. for i in 1...meta[mapid][metadataType].length
  382. ret.push(meta[mapid][metadataType][i]+"_curr")
  383. end
  384. return ret
  385. else
  386. return meta[mapid][metadataType] if meta[mapid]
  387. return nil
  388. end
  389. end
  390.  
  391. def pbTrainerHeadFile(type)
  392. return nil if !type
  393. bitmapFileName = sprintf("Graphics/Pictures/mapPlayer%s",getConstantName(PBTrainers,type)) rescue nil
  394. if !pbResolveBitmap(bitmapFileName) && $PokemonTemp.savedoutfit
  395. bitmapFileName = sprintf("Graphics/Pictures/mapPlayer%03d",type)
  396. elsif !pbResolveBitmap(bitmapFileName)
  397. sprintf("Graphics/Pictures/mapPlayer%03d_curr",type)
  398. end
  399. return bitmapFileName
  400. end
  401.  
  402. def pbTrainerSpriteFile(type)
  403. return nil if !type
  404. bitmapFileName = sprintf("Graphics/Characters/trainer%s",getConstantName(PBTrainers,type)) rescue nil
  405. if !pbResolveBitmap(bitmapFileName) && $PokemonTemp.savedoutfit
  406. bitmapFileName = sprintf("Graphics/Characters/trainer%03d",type)
  407. elsif !pbResolveBitmap(bitmapFileName)
  408. bitmapFileName = sprintf("Graphics/Characters/trainer%03d_curr",type)
  409. end
  410. return bitmapFileName
  411. end
  412.  
  413. def pbTrainerSpriteBackFile(type)
  414. return nil if !type
  415. bitmapFileName = sprintf("Graphics/Trainers/trback%s",getConstantName(PBTrainers,type)) rescue nil
  416. if !pbResolveBitmap(bitmapFileName) && $PokemonTemp.savedoutfit
  417. bitmapFileName = sprintf("Graphics/Trainers/trback%03d",type)
  418. elsif !pbResolveBitmap(bitmapFileName)
  419. bitmapFileName = sprintf("Graphics/Trainers/trback%03d_curr",type)
  420. end
  421. return bitmapFileName
  422. end
  423.  
  424. #===============================================================================
  425. # * Edit to the pbSave function to make sure outfit is saved for future load.
  426. #===============================================================================
  427.  
  428. def pbSave(safesave=false)
  429. $Trainer.metaID=$PokemonGlobal.playerID
  430. if $PokemonTemp.savedoutfit == false #ADDED CODE
  431. saveAllOutfits #ADDED CODE
  432. end
  433. begin
  434. File.open(RTP.getSaveFileName("Game.rxdata"),"wb"){|f|
  435. Marshal.dump($Trainer,f)
  436. Marshal.dump(Graphics.frame_count,f)
  437. if $data_system.respond_to?("magic_number")
  438. $game_system.magic_number = $data_system.magic_number
  439. else
  440. $game_system.magic_number = $data_system.version_id
  441. end
  442. $game_system.save_count+=1
  443. Marshal.dump($game_system,f)
  444. Marshal.dump($PokemonSystem,f)
  445. Marshal.dump($game_map.map_id,f)
  446. Marshal.dump($game_switches,f)
  447. Marshal.dump($game_variables,f)
  448. Marshal.dump($game_self_switches,f)
  449. Marshal.dump($game_screen,f)
  450. Marshal.dump($MapFactory,f)
  451. Marshal.dump($game_player,f)
  452. $PokemonGlobal.safesave=safesave
  453. Marshal.dump($PokemonGlobal,f)
  454. Marshal.dump($PokemonMap,f)
  455. Marshal.dump($PokemonBag,f)
  456. Marshal.dump($PokemonStorage,f)
  457. }
  458. Graphics.frame_reset
  459. rescue
  460. return false
  461. end
  462. return true
  463. end
  464.  
  465. #===============================================================================
  466. # * Edit to the class Game_Player to erase the .png extension in the name.
  467. #===============================================================================
  468. =begin
  469. class Game_Player
  470. def character_name
  471. if !@defaultCharacterName
  472. @defaultCharacterName=""
  473. end
  474. if @defaultCharacterName!=""
  475. return @defaultCharacterName.gsub(/\0/,"v")#(/\.png/,"")
  476. end
  477. if !moving? && !@move_route_forcing && $PokemonGlobal
  478. meta=pbGetMetadata(0,MetadataPlayerA+$PokemonGlobal.playerID)
  479. if $PokemonGlobal.playerID>=0 && meta &&
  480. !$PokemonGlobal.bicycle && !$PokemonGlobal.diving && !$PokemonGlobal.surfing
  481. if meta[4] && meta[4]!="" && Input.dir4!=0 && passable?(@x,@y,Input.dir4) && pbCanRun?
  482. # Display running character sprite
  483. @character_name=pbGetPlayerCharset(meta,4)
  484. else
  485. # Display normal character sprite
  486. @character_name=pbGetPlayerCharset(meta,1)
  487. end
  488. end
  489. end
  490. return @character_name.gsub(/\.png/,"")
  491. end
  492. end
  493. =end
  494.  
Advertisement
Add Comment
Please, Sign In to add comment