<commit_before>// Copyright © 2011 <NAME> and <NAME>
// Licensed under the MIT License. See LICENSE for details.
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#include "../../include/mid.h"
#include "../../include/log.h"
#include "lvlgen.h"

static bool segok(Lvl *l, Path *p, Seg s);
static bool segconfl(Lvl *l, Path *p, Seg s);
static bool segclr(Lvl *l, Seg s);

Seg segmk(Loc l, Mv *m)
{
	Seg s;
	s.l0 = l;
	s.l1 = (Loc) { l.x + m->dx, l.y + m->dy, l.z + m->dz };
	s.mv = m;
	return s;
}

void segpr(Seg s)
{
	pr("seg: %d,%d -> %d,%d", s.l0.x, s.l0.y, s.l1.x, s.l1.y);
}

Path *pathnew(Lvl *l)
{
	Path *p = xalloc(1, sizeof(*p));
	p->maxsegs = l->w * l->h;
	p->segs = xalloc(p->maxsegs, sizeof(p->segs[0]));
	return p;
}

void pathfree(Path *p)
{
	free(p->segs);
	free(p);
}

bool pathadd(Lvl *l, Path *p, Seg s)
{
	if (p->nsegs == p->maxsegs || !segok(l, p, s))
		return false;

	mvblit(s.mv, l, s.l0);

	p->segs[p->nsegs] = s;
	p->nsegs++;

	return true;
}

static bool segok(Lvl *l, Path *p, Seg s)
{
	if (p->nsegs == 0 && !startonblk(s.mv))
		return false;

	bool inrange = s.l1.x > 0 && s.l1.x < l->w - 1
		&& s.l1.y > 0 && s.l1.y < l->h - 1
		&& s.l1.z >= 0 && s.l1.z < l->d;

	return inrange && segclr(l, s) && !segconfl(l, p, s)
		&& !reachable(l, s.l1.x, s.l1.y, s.l1.z);	// haven't been there yet.
}

static bool segconfl(Lvl *l, Path *p, Seg s)
{
	for (int i = 0; i < s.mv->nblkd; i++) {
		Loc *bp = &s.mv->blkd[i];
		Loc blk = (Loc) { bp->x + s.l0.x, bp->y + s.l0.y, bp->z + s.l0.z };
		if (blk.x < 0 || blk.x >= l->w
			|| blk.y < 0 || blk.y >= l->h
			|| blk.z < 0 || blk.z >= l->d
			|| reachable(l, blk.x, blk.y, blk.z))<commit_msg>segok is just a single predicate so make it one logical expression instead of using variables and some control crap.<commit_after>// Copyright © 2011 <NAME> and <NAME>
// Licensed under the MIT License. See LICENSE for details.
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#include "../../include/mid.h"
#include "../../include/log.h"
#include "lvlgen.h"

static bool segok(Lvl *l, Path *p, Seg s);
static bool segconfl(Lvl *l, Path *p, Seg s);
static bool segclr(Lvl *l, Seg s);

Seg segmk(Loc l, Mv *m)
{
	Seg s;
	s.l0 = l;
	s.l1 = (Loc) { l.x + m->dx, l.y + m->dy, l.z + m->dz };
	s.mv = m;
	return s;
}

void segpr(Seg s)
{
	pr("seg: %d,%d -> %d,%d", s.l0.x, s.l0.y, s.l1.x, s.l1.y);
}

Path *pathnew(Lvl *l)
{
	Path *p = xalloc(1, sizeof(*p));
	p->maxsegs = l->w * l->h;
	p->segs = xalloc(p->maxsegs, sizeof(p->segs[0]));
	return p;
}

void pathfree(Path *p)
{
	free(p->segs);
	free(p);
}

bool pathadd(Lvl *l, Path *p, Seg s)
{
	if (p->nsegs == p->maxsegs || !segok(l, p, s))
		return false;

	mvblit(s.mv, l, s.l0);

	p->segs[p->nsegs] = s;
	p->nsegs++;

	return true;
}

static bool segok(Lvl *l, Path *p, Seg s)
{
	return (p->nsegs != 0 || startonblk(s.mv))	// 1st seg must start on a block
		&& s.l1.x > 0 && s.l1.x < l->w - 1
		&& s.l1.y > 0 && s.l1.y < l->h - 1
		&& s.l1.z >= 0 && s.l1.z < l->d
		&& !reachable(l, s.l1.x, s.l1.y, s.l1.z)	// haven't been there yet.
		&& segclr(l, s)
		&& !segconfl(l, p, s);
}

static bool segconfl(Lvl *l, Path *p, Seg s)
{
	for (int i = 0; i < s.mv->nblkd; i++) {
		Loc *bp = &s.mv->blkd[i];
		Loc blk = (Loc) { bp->x + s.l0.x, bp->y + s.l0.y, bp->z + s.l0.z };
		if (blk.x < 0 || blk.x >= l->w
			|| blk.y < 0 || blk.y >= l->h
			|| blk.z < 0 || blk.z >= l->d
			|| reachable(l, blk.x, blk.y, blk.z))
{'fraction_non_alphanumeric': 0.24439265948150307, 'fraction_numerical': 0.020681619574715992, 'mean_word_length': 2.680600214362272, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3662035', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1723, 'n_words': 445}