Skip to content
Sriep edited this page Aug 6, 2016 · 13 revisions

Policy

Policies are code snippets that are run each tick. The code that handles the economy of each room is a policy. This where user code can be put. You can create them using the policy.framework object.

How policies are called

In the main method we call

        policy.enactPolicies();

If we look at policy.enactPolicies()

    enactPolicies: function()
    {
        this.checkRoomPolicies();
        for (var i in Memory.policies) {
            var oldPolicyModule = this.getModuleFromPolicy(Memory.policies[i]);
            var newPolicyDetails = oldPolicyModule.draftNewPolicyId(Memory.policies[i]);
            if (newPolicyDetails === null) {
                delete Memory.policies[i];
            } else if ( newPolicyDetails.id != Memory.policies[i].id) {
                var newPolicyModule = this.getModuleFromPolicy(newPolicyDetails);
                this.activatePolicy(newPolicyDetails);
                newPolicyModule.switchPolicy(Memory.policies[i], newPolicyDetails);
                delete Memory.policies[i];
                newPolicyModule.enactPolicy(newPolicyDetails);
            } else {
                oldPolicyModule.enactPolicy(Memory.policies[i]);
            } 
        } 
    },

Details of all policies are storted in Memory.polices. We can see that each policy object is expected to proved several methods to be called by the base object.

Requirments for Policies.

  • initialisePolicy(policy) This performs any tasks that a policy needs to do once only at startup. It is called from the policy.activatePolicy method.
  • draftNewPolicyId(policy) This is called to see if the policy wishes to end. If it returns null, it is deleted. If it returns details of a new policy, then the old policy is deleted and replaced by the new one. This is really useful for developing finite state machines.
  • switchPolicy(oldPolicy, newPolicy) Called from a new policy that has just been created after from the draftNewPolicyId method. It is where you put any initialise details that are dependent on the previous policy. Again useful for developing finite state machines.
  • enactPolicy(policy) This is where the custom code to be enacted each tick is put.
  • The policy-filename needs to start with "policy."
  • A policy must create a type member with a value equal to the part of the filename after "policy.". Ideally, there should be a matching POLICY_ entry in gc.js
  • A method should be added to "policy.framework" to allow the policy to be started.
Clone this wiki locally