aboutsummaryrefslogtreecommitdiff
path: root/src/universe.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/universe.cpp')
-rw-r--r--src/universe.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/universe.cpp b/src/universe.cpp
new file mode 100644
index 0000000..0a014f7
--- /dev/null
+++ b/src/universe.cpp
@@ -0,0 +1,75 @@
1/*
2 * UNIVERSE.C Copyright (c) 2017, Benoit Germain
3 */
4
5/*
6===============================================================================
7
8Copyright (C) 2017 Benoit Germain <bnt.germain@gmail.com>
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files (the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26THE SOFTWARE.
27
28===============================================================================
29*/
30
31#include <string.h>
32#include <assert.h>
33
34#include "universe.h"
35#include "compat.h"
36#include "macros_and_utils.h"
37#include "uniquekey.h"
38
39// crc64/we of string "UNIVERSE_REGKEY" generated at http://www.nitrxgen.net/hashgen/
40static DECLARE_CONST_UNIQUE_KEY( UNIVERSE_REGKEY, 0x9f877b2cf078f17f);
41
42// ################################################################################################
43
44Universe* universe_create( lua_State* L)
45{
46 Universe* U = (Universe*) lua_newuserdatauv( L, sizeof(Universe), 0); // universe
47 memset( U, 0, sizeof( Universe));
48 STACK_CHECK( L, 1);
49 REGISTRY_SET( L, UNIVERSE_REGKEY, lua_pushvalue(L, -2)); // universe
50 STACK_END( L, 1);
51 return U;
52}
53
54// ################################################################################################
55
56void universe_store( lua_State* L, Universe* U)
57{
58 STACK_CHECK( L, 0);
59 REGISTRY_SET( L, UNIVERSE_REGKEY, (NULL != U) ? lua_pushlightuserdata( L, U) : lua_pushnil( L));
60 STACK_END( L, 0);
61}
62
63// ################################################################################################
64
65Universe* universe_get( lua_State* L)
66{
67 Universe* universe;
68 STACK_GROW( L, 2);
69 STACK_CHECK( L, 0);
70 REGISTRY_GET( L, UNIVERSE_REGKEY);
71 universe = (Universe*) lua_touserdata( L, -1); // NULL if nil
72 lua_pop( L, 1);
73 STACK_END( L, 0);
74 return universe;
75}