#include "brick_logic.h"


bool BrickLogicAnd::tick_body(double time) {
	bool b = true;
	for (int i = 0; i < inputs_count; ++i)
		if (inputs[i] <= 0) {b = false; break;}
	outputs[0] = dbool(b);
	return true;
}


bool BrickLogicOr::tick_body(double time) {
	bool b = false;
	for (int i = 0; i < inputs_count; ++i)
		if (inputs[i] > 0) {b = true; break;}
	outputs[0] = dbool(b);
	return true;
}


bool BrickLogicXor::tick_body(double time) {
	bool b = false;
	for (int i = 0; i < inputs_count; ++i) {
		if (inputs[i] > 0) b = (b ^ true);
		else b = b ^ false;
	}
	outputs[0] = dbool(b);
	return true;
}


bool BrickLogicNAnd::tick_body(double time) {
	bool b = true;
	for (int i = 0; i < inputs_count; ++i)
		if (inputs[i] <= 0) {b = false; break;}
	outputs[0] = 1. - dbool(b);
	return true;
}


bool BrickLogicNOr::tick_body(double time) {
	bool b = false;
	for (int i = 0; i < inputs_count; ++i)
		if (inputs[i] > 0) {b = true; break;}
	outputs[0] = 1. - dbool(b);
	return true;
}


bool BrickLogicNXor::tick_body(double time) {
	bool b = false;
	for (int i = 0; i < inputs_count; ++i) {
		if (inputs[i] > 0) b = (b ^ true);
		else b = b ^ false;
	}
	outputs[0] = 1. - dbool(b);
	return true;
}


bool BrickLogicCompare::tick_body(double time) {
	outputs[0] = outputs[1] = outputs[2] = 0.;
	if (inputs[0] > inputs[1]) outputs[0] = 1.;
	if (inputs[0] < inputs[1]) outputs[2] = 1.;
	if (fabs(inputs[0] - inputs[1]) <= inputs[Tolerance]) outputs[1] = 1.;
	return true;
}
