Skip to content

Commit

Permalink
fix: 修正隨機發生器的錯誤
Browse files Browse the repository at this point in the history
  • Loading branch information
northgreen committed Apr 24, 2024
1 parent 2d2cf3c commit e5ea759
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.19.2 2024-04-24T19:58:32.1577533 Language
// 1.19.2 2024-04-24T22:30:25.1810272 Language
b6d01a692d2bbaa358f7d24d26a0c53dc9f1c2ea assets\the_origin_of_magic\lang\en_us.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.19.2 2024-04-24T19:58:32.156254 Block Loot Tables
// 1.19.2 2024-04-24T22:30:25.1738852 Block Loot Tables
fe57f13449a11436bbede8c37e01bd0d1cd87191 data\the_origin_of_magic\loot_tables\blocks\magic_workbench.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.19.2 2024-04-24T19:58:32.1542548 Models
// 1.19.2 2024-04-24T22:30:25.1652519 Models
02d3ff4716d16f7c5a4dd4432d1b98f744a13f02 assets\the_origin_of_magic\models\item\blood_essence.json
105ea751d9d55fefa41f158e8e462c2e69f451ed assets\the_origin_of_magic\models\item\magic_life_time_up.json
781092579f171cf0db8317aa6267ad8e5cfbbd1f assets\the_origin_of_magic\models\item\fire_magic_element.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected void collision(HitResult hitResult) {
public void remove(RemovalReason reason) {
Vec3d pos = this.getPos();
if(reason != RemovalReason.KILLED){
if(prdRandom.getBool()){
if(prdRandom!=null && prdRandom.getBool()){
this.world.createExplosion(this, pos.getX(), pos.getY(), pos.getZ(), 5.0F * explosionRate, Explosion.DestructionType.BREAK);
}else {
this.world.createExplosion(this, pos.getX(), pos.getY(), pos.getZ(), 3.0F * explosionRate, Explosion.DestructionType.BREAK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public void tick() {

@Override
protected void onEntityHit(EntityHitResult entityHitResult) {
entityHitResult.getEntity().damage(DamageSource.thrownProjectile(this, this.getOwner()), 4);
if(prdRandom!=null && prdRandom.getBool()){
entityHitResult.getEntity().damage(DamageSource.thrownProjectile(this, this.getOwner()), 7);
}else {
entityHitResult.getEntity().damage(DamageSource.thrownProjectile(this, this.getOwner()), 4);
}
super.onEntityHit(entityHitResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public MagicSetting explosionRate (float a){
}

public MagicSetting random(PRDRandom random){
prdRandom = random.copy();
prdRandom = random;
return this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public abstract class StdStaff extends Item {
*/
float Crate;

PRDRandom random;

public StdStaff(Settings settings) {
super(settings);
// 初始化各個參數
Expand All @@ -151,6 +153,7 @@ public StdStaff(Settings settings) {
this.enchantability = 3;
this.staffAgeRate = 1;
this.Crate = 0.15f;
this.random = new PRDRandom(Crate);
}

@Override
Expand Down Expand Up @@ -260,14 +263,9 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
user.getItemCooldownManager().set(this, getCoolingTime());
}

PRDRandom random;

ItemStack staffItemStack = user.getStackInHand(hand);
NbtCompound nbt = staffItemStack.getNbt();
if(nbt != null && nbt.contains("PRDRandom", NbtElement.COMPOUND_TYPE)){
random = new PRDRandom(nbt);
} else {
random = new PRDRandom(Crate);
}
MagicInventory Magics = this.getInventoryFromNbt(nbt);

if (Magics.isEmpty()){
Expand All @@ -277,28 +275,33 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
return TypedActionResult.fail(user.getStackInHand(hand));
}
}else {
if (!world.isClient) {
// 施放解析邏輯
int count = getCastingNum(); // 可釋放的數量
List<StdThrownMagic> magicList = summonMagic(Magics,user,world,count,random);
// 生成法術實體
for(StdThrownMagic MagicEntity:magicList){

float finalSpeed = getSpeed(); // 計算最終速度
float finalScattering = getScattering(); // 計算最終散射

MagicEntity.setVelocity(user, user.getPitch(), user.getYaw(), 0.0F, finalSpeed, finalScattering); // 設置參數
MagicAbilitiesManager magicAbilitiesManager = ((PlayerEntityMixinInterfaces)user).the_origin_of_magic$getMagicAbilitiesManager();
MagicEntity.setAgeRate(this.staffAgeRate);

if (magicAbilitiesManager.cast(user, MagicEntity, world)) {
// 生成法術實體并且破壞物品
staffItemStack.damage(2, user, e -> e.sendEquipmentBreakStatus(EquipmentSlot.MAINHAND));
}
float p = random.getP();
random.setCallBack((a)->{
if (staffItemStack.getNbt() != null) {
staffItemStack.setNbt(a.writeNbt(staffItemStack.getNbt()));
}
return 0f;
});
// 施放解析邏輯
int count = getCastingNum(); // 可釋放的數量
List<StdThrownMagic> magicList = summonMagic(Magics,user,world,count,random);
// 生成法術實體
for(StdThrownMagic MagicEntity:magicList){

float finalSpeed = getSpeed(); // 計算最終速度
float finalScattering = getScattering(); // 計算最終散射

MagicEntity.setVelocity(user, user.getPitch(), user.getYaw(), 0.0F, finalSpeed, finalScattering); // 設置參數
MagicAbilitiesManager magicAbilitiesManager = ((PlayerEntityMixinInterfaces)user).the_origin_of_magic$getMagicAbilitiesManager();
MagicEntity.setAgeRate(this.staffAgeRate);

if (magicAbilitiesManager.cast(user, MagicEntity, world)) {
// 生成法術實體并且破壞物品
random.setP(p);
staffItemStack.damage(2, user, e -> e.sendEquipmentBreakStatus(EquipmentSlot.MAINHAND));
}
}
}
random.writeNbt(staffItemStack.getNbt());
return TypedActionResult.success(user.getStackInHand(hand));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,56 @@
package com.ictye.the_origin_of_magic.utils.Math;

import com.ictye.the_origin_of_magic.the_origin_of_magic;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
* PRD偽隨機事件發生器
*/
public class PRDRandom {
private int count = 1;
private float P = 0;
private PRDRandom father;
private static final Map<Float, Float> PRD_CMap = new HashMap<>();
private Function<PRDRandom, Float> CallBack = (P) -> {
return null;
};

private float p;

/**
* 構造PRD僞隨機事件發生器
* @param P 期望概率
*/
public PRDRandom(float P) {
setP(P);
this.p=P;
}

public PRDRandom copy(){
return new PRDRandom(this.toNBT());
return new PRDRandom(this.toNBT(),this);
}

private PRDRandom(NbtCompound nbt,PRDRandom father){
this(nbt);
this.father = father;
this.p = father.getP();
}

public PRDRandom(NbtCompound nbt){
if(nbt.contains("PRDRandom", NbtElement.COMPOUND_TYPE)){
NbtCompound PRD_Nbt = nbt.getCompound("PRDRandom");
setP(PRD_Nbt.getFloat("P"));
this.p = PRD_Nbt.getFloat("P");
setCount(PRD_Nbt.getInt("count"));
}else{
if (nbt.contains("P", NbtElement.FLOAT_TYPE)){
setP(nbt.getFloat("P"));
this.p = nbt.getFloat("P");
}
if (nbt.contains("count", NbtElement.INT_TYPE)){
setCount(nbt.getInt("count"));
Expand Down Expand Up @@ -70,7 +87,6 @@ public void resetCount(){
* @param P 概率
*/
public void setP(float P) {
resetCount();
float fP = Math.min(1,P);
fP = Math.max(0,fP);
fP = (float) (Math.ceil(fP * 100) / 100);
Expand All @@ -86,14 +102,31 @@ public boolean getBool(){
count = Math.max(0,count);
float c = PRD_CMap.get(getP());
if (random < count * c){
the_origin_of_magic.LOGGER.info("PRD true");
resetCount();
syncToFather();
return true;
}else{
count++;
syncToFather();
return false;
}
}

private void onCountChange(){
CallBack.apply(this);
}

public void setCallBack(Function<PRDRandom, Float> callBack) {
CallBack = callBack;
}

private void syncToFather(){
if (father != null){
father.setCount(count);
}
}

public static void init(){
// 這段是自動生產的參數,實際在代碼裏面計算非常耗費性能!!!,就像你不應該動態生產PI常量一樣
PRD_CMap.put(0.0f,0.0f);
Expand Down

0 comments on commit e5ea759

Please sign in to comment.