lnvis

nanovg lightning network visualizer
git clone git://jb55.com/lnvis
Log | Files | Refs | README | LICENSE

ln.c (3332B)


      1 
      2 #include "ln.h"
      3 #include "grid.h"
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #include <stdio.h>
      7 
      8 void init_ln(struct ln *ln, int grid_div) {
      9 	ln->clicked = 0;
     10 	ln->grid_div = grid_div;
     11 	ln->grid = calloc(ln->grid_div * ln->grid_div, sizeof(*ln->grid));
     12 }
     13 
     14 void free_ln(struct ln *ln) {
     15 	free(ln->grid);
     16 }
     17 
     18 static double rand_0to1() {
     19 	return (double) rand() / RAND_MAX;
     20 }
     21 
     22 void init_network(int ww, int wh, struct ln *ln) {
     23 	struct node *n;
     24 
     25 	for (u32 i = 0; i < ln->node_count; ++i) {
     26 		n = &ln->nodes[i];
     27 
     28 		n->x = ww * rand_0to1();
     29 		n->y = wh * rand_0to1();
     30 		n->ax = 0.0;
     31 		n->ay = 0.0;
     32 		n->vx = 0.0;
     33 		n->vy = 0.0;
     34 		n->size = 10;
     35 	}
     36 }
     37 
     38 void random_network(int ww, int wh, int max_per_node, int num_nodes, struct ln *ln) {
     39 	int i, j;
     40 	int from, to;
     41 	int tries = 0;
     42 	struct node *n;
     43 	u8 connections[num_nodes][num_nodes];
     44 	memset(connections, 0, sizeof(connections));
     45 
     46 	ln->nodes = calloc(sizeof(*ln->nodes), num_nodes);
     47 	ln->channel_count = 0;
     48 	ln->node_count = num_nodes;
     49 	ln->channels = calloc(sizeof(*ln->channels), num_nodes * max_per_node);
     50 
     51 	printf("max channels %d\n", num_nodes * max_per_node);
     52 
     53 	for (i = 0; i < num_nodes; ++i) {
     54 		n = &ln->nodes[i];
     55 
     56 		strcpy(n->alias, "test");
     57 		n->color.r = rand_0to1();
     58 		n->color.g = rand_0to1();
     59 		n->color.b = rand_0to1();
     60 		n->color.a = 1.0;
     61 		n->x = ww * rand_0to1();
     62 		n->y = wh * rand_0to1();
     63 		n->ax = 0.0;
     64 		n->ay = 0.0;
     65 		n->vx = 0.0;
     66 		n->vy = 0.0;
     67 		n->size = 8;
     68 	}
     69 
     70 	// connect nodes randomly
     71 	for (i = 0; i < num_nodes; ++i) {
     72 
     73 		from = i;
     74 	skip:
     75 		// for each node, it can have 0 to max_per_node connections
     76 		for (j = 0; j < rand() % max_per_node; ++j) {
     77 			do {
     78 				tries++;
     79 				// if connections are way higher than the
     80 				// possible number of nodes for some reason,
     81 				// we'll need an escape hatch
     82 				if (tries > 5) {
     83 					tries = 0;
     84 					goto skip;
     85 				}
     86 				to = rand() % num_nodes;
     87 			}
     88 			while(connections[from][to]);
     89 			tries = 0;
     90 
     91 			connections[from][to] = 1;
     92 
     93 			struct channel *c =
     94 				&ln->channels[ln->channel_count++];
     95 
     96 			c->nodes[0] = &ln->nodes[from];
     97 			c->nodes[1] = &ln->nodes[to];
     98 
     99 		}
    100 
    101 		// keep trying until we find on that isn't already connected
    102 	}
    103 }
    104 
    105 
    106 static void print_node(struct node *node)
    107 {
    108 	printf("node %s #%02X%02X%02X\n", node->alias,
    109 	       (int)(node->color.r * 255.0f),
    110 	       (int)(node->color.g * 255.0f),
    111 	       (int)(node->color.b * 255.0f));
    112 }
    113 
    114 
    115 
    116 void filter_network(const char *nodeid, struct node *filter_node, struct ln *ln)
    117 {
    118 	u32 i;
    119 	int first = 1;
    120 	struct node *node = NULL;
    121 	struct channel *chan = NULL;
    122 	ln->filter = (char*)nodeid;
    123 
    124 	for (i = 0; i < ln->node_count; i++) {
    125 		node = &ln->nodes[i];
    126 
    127 		if ((filter_node == NULL && first) ||
    128 		    filter_node == node ||
    129 		    (nodeid && streq(nodeid, node->id))) {
    130 			first = 0;
    131 			node->visible = 1;
    132 			ln->filter_target = node;
    133 		}
    134 		else
    135 			node->visible = 0;
    136 	}
    137 
    138 	for (i = 0; i < ln->channel_count; i++) {
    139 		chan = &ln->channels[i];
    140 
    141 		// this should filter out duplicates?
    142 		if ((chan->flags & 1) == 0)
    143 			chan->visible = 0;
    144 		else
    145 			chan->visible = 1;
    146 
    147 		if (chan->nodes[0]->visible)
    148 			chan->nodes[1]->mark_filtered = 1;
    149 
    150 		if (chan->nodes[1]->visible)
    151 			chan->nodes[0]->mark_filtered = 1;
    152 	}
    153 
    154 	for (i = 0; i < ln->node_count; i++) {
    155 		node = &ln->nodes[i];
    156 
    157 		if (node->mark_filtered) {
    158 			node->visible = 1;
    159 			node->mark_filtered = 0;
    160 		}
    161 	}
    162 }