Coder Social home page Coder Social logo

Comments (6)

jykgod avatar jykgod commented on July 17, 2024 1

属性模型的表达和更新方式 5

模型算法用Lua脚本实现,然后通过使用json、xml或excel的方式进行配置。

优点:

  • 灵活易于修改,开发效率高
  • 修改和新增都不需要重新编译

缺点:

  • 运行效率会低一点

例子

C#Part:

LuaTable luaTable = lua.DoFile(filePath)[0] as LuaTable;//检查出脚本更新时调用或者每次都重新dofile。
...
float ret = (float)(double)((luaTable[funcName] as LuaFunction).Call(arg1, arg2)[0]);

LuaPart:

local TestClass = {}
TestClass.TestFunc1 = function(arg1, arg2)
	return arg1 + arg2
end
TestClass.TestFunc2 = function(arg1, arg2)
	return arg1 - arg2
end
return TestClass

JsonPart:

{
    "luaFilePath" : "x.lua",
    "Effects" : [
         {
              "name" : "Power",
              "func" : "TestFunc1", 
              "input":  ["Ability1","Ability2"]
         }
     ]
}

from simcivil.

tcz717 avatar tcz717 commented on July 17, 2024

属性模型的表达和更新方式 1 (基本实现 #46

通过UnitGrain内的私有方法更新

优点:

  • 方便快捷
  • 实现简单

缺点:

  • 每次更新公式需要重新编译
  • 运行中无法得知属性更新的依赖项
  • 太多重复代码

例子

        private void UpdateSpaceTimeMagicPower()
            => State.SpaceTimeMagicPower.Update(
                Min(
                    State.ReconstructionAbility,
                    State.ControllingAbility,
                    State.PerceptionAbility,
                    State.ResolvingAbility));

        private void UpdatePerceptionMagicPower() => State.PerceptionMagicPower.Update(State.PerceptionAbility);

        private void UpdateMagicTransformEfficiency()
            => State.MagicTransformEfficiency.Update(Min(State.PerceptionAbility, State.ControllingAbility));

        private void UpdateControllingMagicPower() => State.ControllingMagicPower.Update(State.ControllingAbility);
        private void UpdateMagicHitRate()          => State.MagicHitRate.Update(State.ControllingAbility);
        private void UpdateMagicLearningRate()     => State.MagicLearningRate.Update(State.ControllingAbility);
        private void UpdateAimingAccuracy()        => State.AimingAccuracy.Update(State.Vision);
        private void UpdateSightRange()            => State.SightRange.Update(State.Vision);

from simcivil.

tcz717 avatar tcz717 commented on July 17, 2024

属性模型的表达和更新方式 2

通过给UnitState内的属性标注Attribute

优点:

  • 方便快捷
  • 直观,表达式和对应属性在同一位置

缺点:

  • 每次更新公式需要重新编译
  • 可能的反射开销

例子

        [Update(state => Min(state.ReconstructionAbility, state.ControllingAbility)]
        public UnitProperty SummonMagicPower
        {
            get => Effects[(int) EffectIndex.SummonMagicPower];
            set => Effects[(int) EffectIndex.SummonMagicPower] = value;
        }

from simcivil.

tcz717 avatar tcz717 commented on July 17, 2024

属性模型的表达和更新方式 3

将模型算法抽象成一个更新表服务,并使用依赖注入获取

优点:

  • 将更新算法和数据分离并集中, 易于统一修改和替换
  • 算法和数据低耦合

缺点:

每次更新公式需要重新编译

例子

class SimpleUnitStateUpdater : IUnitStateUpdater 
{
    public void Update(UnitState state) { ... }
    private static Dictionary<AbilityIndex, Func<UnitState, float>> _model = new <AbilityIndex, Func<UnitState, float>>
    {
        [AbilityIndex.ReconstructionAbility] = state => Min(state.ReconstructionAbility, state.ControllingAbility),
        ....
    }
}

from simcivil.

tcz717 avatar tcz717 commented on July 17, 2024

属性模型的表达和更新方式 4

将模型算法抽象成一个更新表以xml或者json保存,并使用依赖注入获取

优点:

  • 将更新算法和数据分离并集中, 易于统一修改和替换
  • 算法和数据低耦合
  • 更新公式不需要重新编译

缺点:

  • 处理xml、json、宏的额外开销
  • 潜在的多服务器间模型同步开销

例子

{
  "Effects" : {
    "SummonMagicPower" : {
      "func" : "min",
      "input": ["ReconstructionAbility", "ControllingAbility"]
    },
  }
}

... or ..

{
  "Effects" : {
    "SummonMagicPower" : "min($ReconstructionAbility, $ControllingAbility)"
  }
}

from simcivil.

panyz522 avatar panyz522 commented on July 17, 2024

属性模型的表达和更新方式 6(3+5)

模型使用Json/xml etc. 配置,算法用Lua脚本实现,将模型编译为C#的data model, [并且更新相关数据表]

优点:

  • 灵活易于修改,开发效率高
  • 开发者友好,数据模型有Intellisense和refactoring支持
  • 修改算法不需要重新编译

缺点:

  • 修改模型需要重新编译
  • 运行效率低于原生算法

例子

      State4
       /  |
   State3 |
   /   \  | 
State1 State2

C#Part:

EffectState.generated.cs

public partial class EffectState
{
    private double _state1;
    private double _state2;
    private double _state3;
    private double _state4;

    public double State1 {
        get
        {
            return _state1;
        }
        set
        {
            _state1 = value;
            State3 = UpdateTable[nameof(State3)].Calculate(_state1, _state2); 
            // Evaluation tree can be optimized
        }
    }

    public double State2
    {
        get
        {
            return _state2;
        }
        set
        {
            _state2 = value;
            State3 = UpdateTable[nameof(State3)].Calculate(_state1, _state2);
            State4 = UpdateTable[nameof(State4)].Calculate(_state2, _state3);
        }
    }

    public double State3
    {
        get
        {
            return _state3;
        }
        private set
        {
            _state3 = value;
            _state4 = UpdateTable[nameof(State4)].Calculate(_state2, _state3);
        }
    }

    public double State4
    {
        get
        {
            return _state4;
        }
        private set
        {
            _state4 = value;
        }
    }

}

LuaPart:
Same as #43 (comment)

Lua scirpt template can also be generated by Json

JsonPart:
Same as #43 (comment)

{
  "luaFilePath": "x.lua",
  "Effects": [
    {
      "name": "State1",
      "type": "double",
      "summary": "The introduction of State1 is written here",
      "input": []
    },
    {
      "name": "State2",
      "type": "double",
      "summary": "The introduction of State2 is written here",
      "input": []
    },
    {
      "name": "State3",
      "type": "double",
      "summary": "The introduction of State3 is written here",
      "input": [ "State1", "State2" ] // Dependency
    },
    {
      "name": "State4",
      "type": "double",
      "summary": "The introduction of State4 is written here",
      "input": [ "State2", "State3" ] // Dependency
    }
  ]
}

from simcivil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.