设为首页收藏本站同能贴吧 切换语言 繁体中文
开启辅助访问 切换到窄版
扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 60|回复: 0

[转载发布] DiceRoller plugin 1.0 (for MV, might work for MZ too)

[复制链接]
累计送礼:
0 个
累计收礼:
1 个
  • TA的每日心情
    开心
    2026-7-12 04:10
  • 签到天数: 209 天

    连续签到: 2 天

    [LV.7]常住居民III

    8003

    主题

    864

    回帖

    3万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    7
    卡币
    30013
    OK点
    16
    推广点
    0
    同能卷
    50
    积分
    38910

    灌水之王

    发表于 4 天前 | 显示全部楼层 |阅读模式
    Hello everyone!

    I've dipped here and there at times to check on plugins and sometimes to ask questions but I felt I needed to at least contribute a little as well. Hence, while my scripting / coding has been rusty due to being out of practice, it also gave me an opportunity to dive right back into it as well.


    INTRODUCTION

    So first off, I've often made searches for a simple plugin to emulate dice rolls and it is possible I've just been unlucky but I never did come across one. So I figured I might as well make my own and after testing it, it seems to be working pretty good for the needs I had of it.

    But then, I hear you say "Why not just use a Math.random() function to generate a random value between X and Y? My simple answer is "because of odds".

    For let's say that you want to emulate a classic 3d6 rolls for stats in a D&D game, the Math.random() method would allow you to generate a value between 3 and 18 with ease... however, that method would result in each possible value having the same odd, which is 1 in 16. This will possibly satisfy most people as you then have a desired result between 3 and 18.

    However, with a proper dice roll, the odds of getting the most extreme values on a roll are FAR lower than say getting a 10 or 11 simply because of the distribution of results from each possible outcome of each die. For example, landing a 3 or 18 with an actual dice roll? You can only achieve it if you roll all 1s or all 6s (which is about 0.4%)... but to get 10? You have a lot more combinations possible that will result in 10 ([6,3,1], [6,2,2], [6,1,3], [5,3,2], etc.) meaning that the results will more often than not be near 10-11 (about 12.5%).

    Hence the need for a proper dice roller function to emulate an actual roll of dice.


    HOW IT WORKS

    So as you might imagine, making a dice roll function isn't that difficult... it just needs to be made really to be usable and be called when needed with a few features to make it easier to use.

    Basically, I've created the diceroll() function which, when called, requires 2 parameters : the number of die and the type of die. Thus, if you wanted to perform the aforementionned "3d6", you call a script function and enter :

                    JavaScript:       
    1. diceroll(3,6);
    复制代码


    The function will then perform 3 consecutive Math.random() functions where a value between 1 and 6 will be generated then added to their previous result, returning the sum of all three. This will effectively and properly generate a dice roll with proper odds associated with that kind of roll.

    Of course, since it is a custom function, it means that you can call it in your damage formula for skills and even use battler parameters as the parameters to send to the function to generate those rolls. So you could very well end up creating some sort of "D&D-like" weapon attack roll like so :

                    JavaScript:       
    1. diceroll(2,4) + Math.Int((a.atk - 10) / 2)
    复制代码


    .. which would be the same (somewhat) to using a 2d4 damage weapon and adding your character's STR (ATK?) mod to the damage itself.

    But then, what if you wanted to make a standalone roll and use it in your game later on like, let's say, a skill check of some sort? Well I've added a plugin parameter to let you drop the result of your roll into any one game variable you want.

    As long as the plugin parameter "DiceRollVar" value is 1 or greater, then the result of your rolls will ALWAYS be copied into that game system variable so you can reserve and name a variable in your game to receive those precious dice. If the value is 0 or less then it will simply ignore that step entirely.

    GIMME!

    Here's the code in its entirety :

    Spoiler: Code
                    JavaScript:       
    1. //=============================================================================
    2. // WIEG_DiceRoller.js                                                            
    3. //=============================================================================
    4. /*:
    5. *
    6. * @author Wiegraf
    7. * @plugindesc Creates a function that replicates dice rolling.
    8. *
    9. * @param DiceRollVar
    10. * @desc Variable ID where the result is copied to (none if 0)
    11. * @default 0
    12. *
    13. * @help
    14. * | Information |
    15. * This simple plugin's purpose is to simulate actual dice rolling probabilities
    16. * instead of relying on the usual method of using a random number generator
    17. * range to obtain a similar result.
    18. *
    19. * The difference lies in the probability of each value occuring when generating
    20. * such ranges. For example, taking the example of wanting to simulate a roll of
    21. * 2 six-sided dice (which would result in a value between 2 and 12).
    22. *
    23. * Using the Math.rand() function to generate a value between 2 and 12 would mean
    24. * that each of the 11 possible values would have 1 in 11 chance (or 9.0909%) of
    25. * occuring by using "Math.rand() * 11 + 2".
    26. *
    27. * However, with a roll of 2 six-sided dice (or 2d6), the probability that you
    28. * would roll either a result of 2 or 12 are both 1 in 36 (or 2.7777%) while
    29. * rolling a result of 7 would be 6 in 36 (or 16.6667%) due * to the result being
    30. * generated by 2 random checks of ranges of 1 to 6 instead of a single check of a
    31. * range of 2 to 12.
    32. *
    33. * | Configuration |
    34. * The plugin has a single configurable parameter named "DiceRollVar" which
    35. * determines into which variable * ID the result of the dice roll will be copied.
    36. * By default, the value is set to "0" which ignores that step, meaning the result
    37. * isn't copied to a variable. If set to any value greater than 0 however, the result
    38. * will be copied into the matching variable with the ID of the value entered.
    39. *
    40. * This allows the user to simply use the diceroll() function to quickly generate
    41. * a number that can then be accessed through the ingame variables.
    42. *
    43. * | Usage |
    44. * You can execute the diceroll function though an event with the "Script" advanced
    45. * command.
    46. *
    47. * The "diceroll()" function requires 2 values to be entered in order to function :
    48. * a number of die to be rolled and the type of die. Both values must be positive
    49. * integers (aka rounded numbers). The proper syntax is as follows :
    50. *
    51. * diceroll(number,type) -> Roll "number" die with "type" faces
    52. * diceroll(2,6)         -> Roll 2 dice with 6 faces for a total between 2 and 12
    53. * diceroll(4,8)         -> Roll 4 dice with 8 faces for a total between 4 and 32
    54. *
    55. * Likewise, it is possible to use the diceroll() function in your damage formulas
    56. * as long as you provide * integer-type values as the "number" and "type". For example :
    57. *
    58. * diceroll(a.atk, 4)    -> Roll "a.atk" dice with 4 faces
    59. * diceroll(2,6) + a.atk -> Roll 2 dice with 6 faces for a total between 2 and 12
    60. *                           plus the user's Attack
    61. *
    62. */
    63. (function() {
    64.     //Registers the Plugin for use
    65.     var parameters = PluginManager.parameters("WIEG_DiceRoller");
    66.     //A place that holds all the parameters from your plugin params above
    67.     const WIEG_AdvancedSettingsParams = {
    68.       DiceRollVar: Number(parameters['DiceRollVar'])
    69.     };
    70.    
    71.   diceroll = function(dieNumb, dieType) {
    72.       var i;
    73.       var total = 0;
    74.       for (i = 0; i < dieNumb; i++) {
    75.           total += Math.floor(Math.random() * dieType + 1);
    76.       }
    77.       if(WIEG_AdvancedSettingsParams.DiceRollVar > 0) {
    78.           $gameVariables.setValue(WIEG_AdvancedSettingsParams.DiceRollVar, total);
    79.       }
    80.       return total;
    81.   };
    82. })();
    复制代码


    COPYRIGHT AND STUFF

    I only ask that you credit me if you use that plugin in your game.


    END WORD

    If you ever have questions or suggestions of features to add to this plugin, let me know and I'll consider it.


    本贴来自国际rpgmaker官方论坛作者:WiegrafDX处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/diceroller-plugin-1-0-for-mv-might-work-for-mz-too.128510/
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    简体中文
    繁體中文
    English(英语)
    日本語(日语)
    Deutsch(德语)
    Русский язык(俄语)
    بالعربية(阿拉伯语)
    Türkçe(土耳其语)
    Português(葡萄牙语)
    ภาษาไทย(泰国语)
    한어(朝鲜语/韩语)
    Français(法语)
    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2026-8-1 09:32 , Processed in 0.144629 second(s), 54 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表