routes
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.h
1 #ifndef ROUTES_H
2 #define ROUTES_H
3
4 #include "common.h"
5 #include "model.h"
6 #include "gate.h"
7
8 struct subworld_routes
9 {
10 struct route_node
11 {
12 v3f co, right, h;
13 u32 next[2];
14
15 u32 is_gate, gate_id;
16 }
17 *nodes;
18
19 u32 node_count,
20 node_cap;
21
22 struct route
23 {
24 const char *name;
25 v4f colour;
26
27 u32 start;
28 }
29 *routes;
30
31 u32 route_count,
32 route_cap;
33
34 struct route_gate
35 {
36 teleport_gate gate;
37
38 u32 route_ids[4]; /* Gates can be linked into up to four routes */
39 u32 route_count,
40 node_id;
41 }
42 *gates;
43
44 u32 gate_count,
45 gate_cap;
46 };
47
48 static struct subworld_routes *subworld_routes(void);
49
50 /*
51 * TODO list:
52 * when a gate is passed through it needs to trigger into an active state
53 */
54
55 static void world_routes_debug(void)
56 {
57 struct subworld_routes *r = subworld_routes();
58
59 for( int i=0; i<r->node_count; i++ )
60 {
61 struct route_node *rn = &r->nodes[i];
62 vg_line_pt3( rn->co, 1.0f, rn->is_gate? 0xffffff00: 0xff00b2ff );
63 }
64 }
65
66 static void world_routes_free(void)
67 {
68 struct subworld_routes *r = subworld_routes();
69
70 free( r->nodes );
71 free( r->routes );
72 free( r->gates );
73 }
74
75 static void world_id_fixup( u32 *uid, mdl_header *mdl )
76 {
77 if( *uid )
78 *uid = mdl_node_from_id( mdl, *uid )->sub_uid;
79 }
80
81 static void world_routes_init( mdl_header *mdl )
82 {
83 struct subworld_routes *r = subworld_routes();
84 r->nodes = NULL;
85 r->node_count = 0;
86 r->node_cap = 0;
87 r->routes = NULL;
88 r->route_count = 0;
89 r->route_cap = 0;
90 r->gates = NULL;
91 r->gate_count = 0;
92 r->gate_cap = 0;
93
94 for( int i=0; i<mdl->node_count; i++ )
95 {
96 mdl_node *pnode = mdl_node_from_id(mdl,i);
97 m4x3f transform;
98
99 if( pnode->classtype == k_classtype_route_node ||
100 pnode->classtype == k_classtype_gate )
101 {
102 mdl_node_transform( pnode, transform );
103 pnode->sub_uid = r->node_count;
104
105 r->nodes = buffer_reserve( r->nodes, r->node_count, &r->node_cap, 1,
106 sizeof( struct route_node ) );
107
108 struct route_node *rn = &r->nodes[r->node_count];
109
110 v3_copy( transform[0], rn->right );
111 v3_normalize( rn->right );
112 v3_copy( transform[2], rn->h );
113 v3_copy( transform[3], rn->co );
114
115 if( pnode->classtype == k_classtype_gate )
116 {
117 r->gates = buffer_reserve( r->gates, r->gate_count, &r->gate_cap,
118 1, sizeof( struct route_gate ) );
119
120 struct classtype_gate *inf = mdl_get_entdata( mdl, pnode );
121
122 /* H is later scaled based on link distance */
123 v3_normalize( rn->h );
124 rn->next[0] = inf->target;
125 rn->next[1] = 0;
126 rn->gate_id = r->gate_count;
127 rn->is_gate = 1;
128
129 struct route_gate *rg = &r->gates[r->gate_count];
130 rg->node_id = r->node_count;
131
132 /* TODO */
133
134 r->gate_count ++;
135 }
136 else
137 {
138 struct classtype_route_node *inf = mdl_get_entdata( mdl, pnode );
139 rn->next[0] = inf->target;
140 rn->next[1] = inf->target1;
141 rn->is_gate = 0;
142 }
143
144 r->node_count ++;
145 }
146 else if( pnode->classtype == k_classtype_route )
147 {
148 struct classtype_route *inf = mdl_get_entdata( mdl, pnode );
149 r->routes = buffer_reserve( r->routes, r->route_count, &r->route_cap,
150 1, sizeof( struct route ) );
151
152 struct route *route = &r->routes[r->route_count];
153
154 v4_zero( route->colour );
155 route->name = NULL;
156 route->start = inf->id_start;
157
158 r->route_count ++;
159 }
160 }
161
162 /*
163 * Apply correct system-local ids
164 */
165 for( int i=0; i<r->node_count; i++ )
166 {
167 struct route_node *rn = &r->nodes[i];
168
169 for( int j=0; j<2; j++ )
170 world_id_fixup( &rn->next[j], mdl );
171
172 if( rn->is_gate )
173 world_id_fixup( &rn->gate_id, mdl );
174 }
175
176 for( int i=0; i<r->gate_count; i++ )
177 {
178 struct route_gate *rg = &r->gates[i];
179 world_id_fixup( &rg->node_id, mdl );
180 }
181
182 for( int i=0; i<r->route_count; i++ )
183 {
184 struct route *route = &r->routes[i];
185 world_id_fixup( &route->start, mdl );
186 }
187 }
188
189 #endif /* ROUTES_H */