'adventurebook_page', 'post_parent' => $book_id, 'posts_per_page' => -1, 'post_status' => 'publish' ]); // if we are not admin, filter out pages that have not been visited by the character if (!$is_admin && $character instanceof Character) { $visited_page_ids = $character->get_visited_page_ids(); $pages = array_filter($pages, static fn($p) => in_array($p->ID, $visited_page_ids, false)); } if (empty($pages)) return ['nodes' => [], 'edges' => []]; // determine start page $start_page = $pages[0]->ID; foreach ($pages as $p) { if (get_post_meta($p->ID, '_adventurebook_startpage', true)) { $start_page = $p->ID; break; } } // directions $direction_map = [ 'north' => [0, -1], 'south' => [0, 1], 'east' => [1, 0], 'west' => [-1, 0], 'northeast' => [1, -1], 'northwest' => [-1, -1], 'southeast' => [1, 1], 'southwest' => [-1, 1], 'up' => [0, -0.5], 'down' => [0, 0.5], ]; $scale = 250; $roundness = 0.15; // Stärke der Biegung $visited = []; $positions = []; $edges = []; $queue = [['id' => $start_page, 'x' => 0, 'y' => 0]]; while (!empty($queue)) { $current = array_shift($queue); $page_id = $current['id']; if (isset($visited[$page_id])) continue; $visited[$page_id] = true; $positions[$page_id] = ['x' => $current['x'], 'y' => $current['y']]; $decisions = get_post_meta($page_id, '_adventurebook_decision_options', true); if (!is_array($decisions)) continue; foreach ($decisions as $decision) { $target_id = intval($decision['target'] ?? 0); if (!$target_id) continue; $dir = strtolower(trim($decision['decision_dir'] ?? '')); $offset = $direction_map[$dir] ?? [0, 0]; $new_x = $current['x'] + $offset[0] * $scale; $new_y = $current['y'] + $offset[1] * $scale; // Farbcodierung $color = match ($dir) { 'north' => '#4a90e2', 'south' => '#e94e77', 'east' => '#8cc152', 'west' => '#f6bb42', 'northeast' => '#7fbfff', 'northwest' => '#0074d9', 'southeast' => '#ffb347', 'southwest' => '#ff7f50', default => '#999' }; // Prüfen, ob Gegenrichtung existiert $reverse_exists = array_filter($edges, static fn($e) => $e['from'] === $target_id && $e['to'] === $page_id ); // reserve direction works because the nodes are reverse, too $smooth_type = 'curvedCW'; // Label leicht ober/unter Linie $labelVadjust = empty($reverse_exists) ? -12 : 14; // if the decision has a restriction, make the edge dashed if ($decision['restriction_type'] != 'none') { $color = '#ff0000'; } $edges[] = [ 'dashes' => $decision['restriction_type'] != 'none', 'from' => $page_id, 'to' => $target_id, 'label' => $decision['label'] ?? $dir, 'arrows' => 'to', 'color' => ['color' => $color], 'smooth' => [ 'enabled' => true, 'type' => $smooth_type, 'roundness' => $roundness, ], 'font' => [ 'color' => $color, 'align' => 'horizontal', 'vadjust' => $labelVadjust, ], ]; if (!isset($visited[$target_id])) { $queue[] = ['id' => $target_id, 'x' => $new_x, 'y' => $new_y]; } } } // Nodes erzeugen (mit Enemy_Spawn-Objekten, sichtbare Namen im Label) $nodes = []; foreach ($positions as $id => $pos) { $info_lines = []; // all enemy spawns on this page $enemy_spawns = Enemy_Spawn::get_enemy_spawns_by_page_id($id); $enemy_labels = []; if (!empty($enemy_spawns)) { foreach ($enemy_spawns as $spawn) { /* @var Enemy_Spawn $spawn */ if (!empty($spawn->code)) { $enemy_definition = Enemy_Definition::get_enemy_definition_by_code($spawn->code); $enemy_labels[] = esc_html($enemy_definition->name). ' ('.esc_html($spawn->respawn_time).'s)'; } } } $has_enemy = !empty($enemy_labels); if($has_enemy) $info_lines[] = 'E: '.implode(', ', $enemy_labels); // all item spawns on this page $item_spawns = Item_Spawn::get_item_spawns_by_page_id($id); $item_labels = []; if (!empty($item_spawns)) { foreach ($item_spawns as $spawn) { /* @var Item_Spawn $spawn */ if (!empty($spawn->code)) { $item_definition = Adventurebook\Item_Definition::get_item_definition_by_code($spawn->code); $item_labels[] = esc_html($item_definition->name); } } } $has_item = !empty($item_labels); if($has_item) $info_lines[] = 'I: '.implode(', ', $item_labels); // get all scripts on this page $script_code = get_post_meta( $id, '_adventurebook_page_script', true ); $script = Script::get_script_by_code( $script_code ); if ( $script ) { $info_lines[] = 'S: ' . esc_html( $script->name ); } // Label mehrzeilig: Seitentitel + (optional) Enemy-Liste $label = get_the_title($id); if(count($info_lines)) { $label .= "\n" . implode("\n", $info_lines); } $nodes[] = [ 'id' => $id, 'label' => $label, // mehrzeilig 'x' => $pos['x'], 'y' => $pos['y'], 'fixed' => adventurebook_page_has_directional_decisions($id), 'color' => $has_enemy ? '#f8d7da' : ($has_item ? '#aaffaa' : '#e2e3e5'), 'title' => $label, 'font' => [ 'multi' => 'html', // erlaubt mehrzeiligen Text 'size' => 12, ], 'margin' => 8, ]; } return ['nodes' => $nodes, 'edges' => array_values($edges)]; }