aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lkn/core/system.ex
blob: 53ca6c74297e188bd05a1167226917d53b81bd13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#
# lkn.core: an entity-component-system (ecs) implementation for lyxan
# Copyright (C) 2017 Thomas Letan <contact@thomasletan.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
defmodule Lkn.Core.System do
  @moduledoc """
  A behaviour module for implementing and querying a System.

  **Note:** You are not supposed to spawn a System yourself. The
    `Lkn.Core.Instance` takes care of that for you. This is why you
    will not find any `start*` function here.

  The core of this module is the `defsystem/2` macro. It is the
  recommended way to implement of System, that is a Process that
  handles one aspect of the gameplay. Before we going any further,
  lets have a look at a following minimal example.

      defsystem Sys do
        # 1. Specify which 'Component's are required by this 'System'.
        @map Sys.MapSpec
        @puppet Sys.PuppetSpec

        # 2. Implementation of the 'System' callbacks.
        def init_state(_instance_key, _map_key) do
          # return some state
        end

        def puppet_enter(state, _instance_key, _map_key, _puppets, _puppet_key)
          # we do nothing, but we could
          state
        end

        def puppet_leave(state, _instance_key, _map_key, _puppets, _puppet_key)
          # we do nothing, but we could
          state
        end

        # 3. Define some specific functions
        cast func1(a :: integer, b :: boolean) do
          # here you can use 'a' and 'b', but also 'state',
          # 'puppets', 'map_key' and 'instance_key'

          # you have to return the new state value
          state
        end
      end

  ## Map and Puppet Components

  A System handles one facet of the gameplay. Its purpose is to
  manipulate the set of “compatible” of Puppets. A System is tied to
  two Component Specification Modules (that is two modules defined
  with the `Lkn.Core.Component.defcomponent/2` macro). A Component is
  a Proxy to manipulate an Entity. It abstracts away its underlying
  structure and expose a common interface. See `Lkn.Core.Component` to
  learn how to specify then implement a Component.

  To define which Component is required for a Map and which one is
  required for a Puppet, we use the `@map` and `@puppet` annotations.

  ## The System Behaviour

  The System callbacks can be divided into two categories. The
  `init_map/2` function is called only one time to initialize the
  inner state of the System. This state implementation is at the
  discretion of the developer. The other callbacks are a set of hooks
  which are called when a particular event occures.

  ## The `cast` Keyword

  In addition to the System callbacks which are shared by all the
  Systems, each System exposes a public interface the Puppeteer can
  use to actually play. The function interface are defined using the
  `cast` keyword.

  You can use the `cast` keyword as follows: `cast
  <func_name>([arg_name :: arg_type]*) do <block> end`. Inside the
  block, you can use the arguments you have defined. In addition, you
  also can use the following variables:

  - `state` is the current state of the System, as initialized by
    `init_state/2` and modified by the System callbacks and functions
  - `instance_key` is the key to identify the Instance which spawns
    the system, it can be used to reach its other systems
  - `map_key` is the key to identify the map on which the puppets have
    been spawned
  - `puppets` is a `MapSet` of all the Puppets currently in the
    Instance and “compatible” with the System

  In addition to this variables, you also can use the `notify/1`
  function. It takes a lambda of types `(Lkn.Core.Puppeteer.k ->
  any)`. It can be used to reach all the Puppeteers that have
  populated the Instance with their Puppets.

  The block has to return the new system state.

  The `defsystem` will generate a client function to use this
  function. In addition to the specified arguments, it takes an
  additional one: an Instance key.
  """

  use Lkn.Prelude

  alias Lkn.Core.Map
  alias Lkn.Core.Name
  alias Lkn.Core.Puppet
  alias Lkn.Core.System
  alias Lkn.Core.Instance

  @typedoc "A System defined using the `defsystem/2` macro."
  @type m :: module

  @typedoc "A set of “compatible” puppets."
  @type puppets :: MapSet.t(Puppet.t)

  defmodule State do
    @moduledoc false

    defstruct [
      :map_key,
      :instance_key,
      :puppets,
    ]

    @type t :: %State{
      map_key: Map.k,
      instance_key: Instance.k,
      puppets: System.puppets,
    }

    @spec new(Instance.k, Map.k) :: t
    def new(instance_key, map_key) do
      %State{
        puppets: MapSet.new(),
        instance_key: instance_key,
        map_key: map_key,
      }
    end

    @spec put(t, Puppet.k) :: t
    def put(state, puppet_key) do
      puppets = MapSet.put(state.puppets, puppet_key)

      %State{state|
             puppets: puppets
      }
    end

    @spec delete(t, Puppet.k) :: t
    def delete(state, puppet_key) do
      %State{state|
             puppets: MapSet.delete(state.puppets, puppet_key)
      }
    end

    @spec population(t) :: integer
    def population(state) do
      MapSet.size(state.puppets)
    end

    @spec notify(t, any) :: :ok
    def notify(state, notif) do
      Lkn.Core.Instance.notify_puppeteers(state.instance_key, notif)
    end
  end

  @typedoc """
  The inner state of the Server process.
  """
  @type state :: any

  @doc """
  Initialize the state of the system.

  With the `instance_key`, you can trigger other systems of the same
  instance. With the `map_key`, you can request through the Map
  Component some information about the map.
  """
  @callback init_state(instance_key :: Instance.k, map_key :: Map.k) :: state

  @doc """
  A hook function which is called when a “compatible” puppet enters the Instance.
  """
  @callback puppet_enter(
    state :: state,
    instance_key :: Instance.k,
    map_key :: Map.k,
    puppets :: System.puppets,
    puppet_key :: Puppet.k) :: state

  @doc """
  A hook function which is called when a “compatible” puppet leaves the Instance.
  """
  @callback puppet_leave(
    state :: state,
    instance_key :: Instance.k,
    map_key :: Map.k,
    puppets :: System.puppets,
    puppet_key :: Puppet.k) :: state

  @doc """
  A macro to ease the definition of a new System which provides the
  [cast](#module-the-keyword) keyword.
  """
  defmacro defsystem(name, do: block) do
    alias Lkn.Core.Specs

    state_type = quote do Lkn.Core.System.state end
    key_type = quote do Lkn.Core.Instance.k end
    key_to_name = quote do
      &(Lkn.Core.Name.system(&1, unquote(name)))
    end

    quote do
      defmodule unquote(name) do
        use Lkn.Prelude
        use GenServer

        @behaviour Lkn.Core.System

        unquote(Specs.gen_server_from_specs(
              block,
              key_type,
              key_to_name,
              state_type,
              key_name: Specs.var_name("instance_key"),
              impl_suffix: "_impl",
              allow_impl: true,
              allow_specs: false,
              additional_args: [
                quote do unquote(Specs.var_name("map_key")) :: Lkn.Core.Map.k end,
                quote do unquote(Specs.var_name("puppets")) :: Lkn.Core.System.puppets end,
              ],
            ))

        @doc false
        @spec component(:puppet|:map) :: module
        def component(:puppet) do
          @puppet
        end
        def component(:map) do
          @map
        end

        def init(st) do
          {:ok, st}
        end

        defp priv_handle_cast({:notify, notification}, priv: priv, pub: pub) do
          State.notify(priv, notification)
          [priv: priv, pub: pub]
        end
        defp priv_handle_cast({:register_puppet, entity_key}, priv: priv, pub: pub) do
          {priv, pub} = if Lkn.Core.Entity.has_component?(entity_key, __MODULE__) do
            {State.put(priv, entity_key), puppet_enter(pub, priv.instance_key, priv.map_key, priv.puppets, entity_key)}
          else
            {priv, pub}
          end

          [priv: priv, pub: pub]
        end
        defp priv_handle_cast({:unregister_puppet, entity_key}, priv: priv, pub: pub) do
          {priv, pub} =
          if Lkn.Core.Entity.has_component?(entity_key, __MODULE__) do
            priv = State.delete(priv, entity_key)
            {priv, puppet_leave(pub, priv.instance_key, priv.map_key, priv.puppets, entity_key)}
          else
            {priv, pub}
          end

          [priv: priv, pub: pub]
        end

        defp priv_handle_call(:population_size, _from, priv: priv, pub: pub) do
          {:reply, State.population(priv), [priv: priv, pub: pub]}
        end

        def handle_cast({:priv, cmd}, state) do
          {:noreply, priv_handle_cast(cmd, state)}
        end
        def handle_cast({:spec, {name, args}}, priv: priv, pub: pub) do
          name = String.to_atom(String.replace_suffix(Atom.to_string(name), "", "_impl"))
          s = :erlang.apply(__MODULE__, name, [priv.instance_key|args] ++ [priv.map_key, priv.puppets, pub])

          {:noreply, [priv: priv, pub: s]}
        end

        def handle_call({:spec, {name, args}}, _call, priv: priv, pub: pub) do
          name = String.to_atom(String.replace_suffix(Atom.to_string(name), "", "_impl"))
          {res, s} = :erlang.apply(__MODULE__, name, [priv.instance_key|args] ++ [priv.map_key, priv.puppets, pub])

          {:reply, res, [priv: priv, pub: s]}
        end
        def handle_call({:priv, cmd}, from, priv: priv, pub: pub) do
          priv_handle_call(cmd, from, priv: priv, pub: pub)
        end

        @spec notify(any) :: :ok
        defp notify(notification) do
          GenServer.cast(self(), {:priv, {:notify, notification}})
        end
      end
    end
  end

  @doc false
  @spec start_link(m, Instance.k, Map.k) :: GenServer.on_start
  def start_link(module, instance_key, map_key) do
    GenServer.start_link(module,
      [
        priv: State.new(instance_key, map_key),
        pub: module.init_state(instance_key, map_key),
      ],
      name: Name.system(instance_key, module))
  end

  @doc false
  @spec register_puppet(Instance.k, m, Puppet.k) :: :ok
  def register_puppet(instance_key, system, puppet_key) do
    GenServer.cast(Name.system(instance_key, system), {:priv, {:register_puppet, puppet_key}})
  end

  @doc false
  @spec unregister_puppet(Instance.k, m, Puppet.k) :: :ok
  def unregister_puppet(instance_key, system, puppet_key) do
    GenServer.cast(Name.system(instance_key, system), {:priv, {:unregister_puppet, puppet_key}})
  end

  @doc """
  Returns the number of “compatible” Puppets of a given System
  registered to a given Instance.
  """
  @spec population_size(Instance.k, m) :: integer
  def population_size(instance_key, system) do
    GenServer.call(Name.system(instance_key, system), {:priv, :population_size})
  end
end