source: golgotha/src/golg/critical_graph.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 4.9 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "critical_graph.hh"
10#include "path_api.hh"
11#include "map.hh"
12#include "map_cell.hh"
13#include "map_man.hh"
14#include <math.h>
15
16
17g1_critical_graph_class::~g1_critical_graph_class()
18//{{{
19{
20  if (pool)
21    i4_free(pool);
22  pool=0;
23}
24//}}}
25
26void g1_critical_graph_class::clear_critical_graph()
27//{{{
28{
29  if (pool)
30    i4_free(pool);
31  pool=0;
32
33  pool_connections = 0;
34
35  w32 size = sizeof(connection_class)*MAX_CRITICALS*MAX_CONNECTIONS;
36  pool = (connection_class*)i4_malloc(size, "critical_connections");
37  memset(pool,0,size);
38
39  connection_class *current = pool;
40
41  for (w32 j=1; j<MAX_CRITICALS; j++)
42  {
43    critical[j].connections=0;
44    critical[j].connection = current;
45    current += MAX_CONNECTIONS;
46    pool_connections += MAX_CONNECTIONS;
47  }
48}
49//}}}
50
51void g1_critical_graph_class::compact_critical_graph()
52//{{{
53{
54  connection_class *old_pool = pool;
55
56  w32 count=0, i,j;
57  for (j=1; j<criticals; j++)
58    count += critical[j].connections;
59
60  w32 size = sizeof(connection_class)*count;
61  pool = (connection_class*)i4_malloc(size, "critical_connections");
62
63  connection_class *current = pool;
64  pool_connections = 0;
65
66  for (j=1; j<criticals; j++)
67  {
68    for (i=0; i<critical[j].connections; i++)
69      current[i] = critical[j].connection[i];
70    critical[j].connection = current;
71    current += critical[j].connections;
72    pool_connections += critical[j].connections;
73  }
74
75  if (old_pool)
76    i4_free(old_pool);
77}
78//}}}
79
80void g1_critical_graph_class::expand_critical_graph()
81//{{{
82{
83  connection_class *old_pool = pool;
84
85  w32 size = sizeof(connection_class)*MAX_CRITICALS*MAX_CONNECTIONS;
86  pool = (connection_class*)i4_malloc(size, "critical_connections");
87  memset(pool,0,size);
88
89  connection_class *current = pool;
90  pool_connections = 0;
91
92  for (w32 j=1; j<MAX_CRITICALS; j++)
93  {
94    if (j<criticals)
95      for (w32 i=0; i<critical[j].connections; i++)
96        current[i] = critical[j].connection[i];
97    critical[j].connection = current;
98    current += MAX_CONNECTIONS;
99    pool_connections += MAX_CONNECTIONS;
100  }
101
102  if (old_pool)
103    i4_free(old_pool);
104}
105//}}}
106
107i4_bool g1_critical_graph_class::add_critical_point(i4_float x, i4_float y)
108//{{{
109{
110  if (criticals<MAX_CRITICALS)
111  {
112    critical[criticals].x=x;    // oliy change this to float - jc
113    critical[criticals].y=y;
114    critical[criticals].connections=0;
115    critical[criticals].connection=0;
116    critical[criticals].selected=0;
117    criticals++;
118
119    return i4_T;
120  }
121  else
122    return i4_F;
123}
124//}}}
125
126void g1_critical_graph_class::save_points(g1_saver_class *f)
127//{{{
128{
129  *f << criticals;
130  for (w32 j=1; j<criticals; j++)
131  {
132    f->write_16(sw16(critical[j].x));
133    f->write_16(sw16(critical[j].y));
134  }
135}
136//}}}
137
138
139void g1_critical_graph_class::save_graph(g1_saver_class *f)
140//{{{
141{
142  w32 count=0, i,j;
143  for (j=1; j<criticals; j++)
144    count += critical[j].connections;
145
146  *f << (w32)count;
147  for (j=1; j<criticals; j++)
148  {
149    connection_class *conn = &critical[j].connection[0];
150
151    *f << critical[j].connections;
152    for (i=0; i<critical[j].connections; i++, conn++)
153    {
154      *f << conn->ref << conn->dist;
155      for (int g=0; g<G1_GRADE_LEVELS; g++)
156        *f << conn->size[g];
157    }
158  }
159}
160//}}}
161
162
163void g1_critical_graph_class::load_points(g1_loader_class *f)
164//{{{
165{
166  *f >> criticals;
167
168  for (w32 j=1; j<criticals; j++)
169  {
170    critical[j].x = i4_float(f->read_16()) + 0.5;
171    critical[j].y = i4_float(f->read_16()) + 0.5;
172    critical[j].connection=0;
173    critical[j].connections=0;
174    critical[j].selected=0;
175  }
176
177  if (pool)
178    i4_free(pool);
179  pool = 0;
180  pool_connections=0;
181}
182//}}}
183
184
185void g1_critical_graph_class::load_graph(g1_loader_class *f)
186//{{{
187{
188  if (pool)
189    i4_free(pool);
190
191  pool = 0;
192
193  *f >> pool_connections;
194
195  w32 size = sizeof(connection_class)*pool_connections;
196
197  if (size)
198    pool = (connection_class*)i4_malloc(size, "critical_connections");
199  else pool=0;
200
201  connection_class *conn = pool;
202
203  for (w32 j=1; j<criticals; j++)
204  {
205    critical[j].connection = conn;
206    *f >> critical[j].connections;
207    for (w32 i=0; i<critical[j].connections; i++, conn++)
208    {
209      *f >> conn->ref >> conn->dist;
210      for (int g=0; g<G1_GRADE_LEVELS; g++)
211        *f >> conn->size[g];
212    }
213  }
214}
215//}}}
216
217
218//{{{ Emacs Locals
219// Local Variables:
220// folded-file: t
221// End:
222//}}}
Note: See TracBrowser for help on using the repository browser.