Clean up variables

FossilOrigin-Name: 5e43ae9916e0f61b00913c7f5c72f9349e1afecab908fd09b193edfd3f10006d
This commit is contained in:
Jonathan Schleifer 2025-03-07 21:16:47 +00:00
parent c690c2b9ed
commit 63a6c72954
5 changed files with 67 additions and 107 deletions

View file

@ -5,7 +5,7 @@ OF_ASSUME_NONNULL_BEGIN
@interface Variable : Identifier @interface Variable : Identifier
@property (readonly, nonatomic) int min, max; @property (readonly, nonatomic) int min, max;
@property (readonly, nonatomic) int *storage; @property (readonly, nonatomic) int *storage;
@property (readonly, nonatomic) void (*function)(); @property (readonly, nonatomic) void (*__cdecl function)();
@property (readonly, nonatomic) bool persisted; @property (readonly, nonatomic) bool persisted;
- (instancetype)initWithName:(OFString *)name OF_UNAVAILABLE; - (instancetype)initWithName:(OFString *)name OF_UNAVAILABLE;
@ -13,8 +13,10 @@ OF_ASSUME_NONNULL_BEGIN
min:(int)min min:(int)min
max:(int)max max:(int)max
storage:(int *)storage storage:(int *)storage
function:(void (*)())function function:(void (*__cdecl)())function
persisted:(bool)persisted; persisted:(bool)persisted;
- (void)printValue;
- (void)setValue:(int)value;
@end @end
OF_ASSUME_NONNULL_END OF_ASSUME_NONNULL_END

View file

@ -1,21 +0,0 @@
#import "Variable.h"
@implementation Variable
- (instancetype)initWithName:(OFString *)name
min:(int)min
max:(int)max
storage:(int *)storage
function:(void (*)())function
persisted:(bool)persisted
{
self = [super initWithName:name];
_min = min;
_max = max;
_storage = storage;
_function = function;
_persisted = persisted;
return self;
}
@end

57
src/Variable.mm Normal file
View file

@ -0,0 +1,57 @@
#import "Variable.h"
#include "cube.h"
@implementation Variable
- (instancetype)initWithName:(OFString *)name
min:(int)min
max:(int)max
storage:(int *)storage
function:(void (*__cdecl)())function
persisted:(bool)persisted
{
self = [super initWithName:name];
_min = min;
_max = max;
_storage = storage;
_function = function;
_persisted = persisted;
return self;
}
- (void)printValue
{
conoutf(@"%@ = %d", self.name, *_storage);
}
- (void)setValue:(int)value
{
bool outOfRange = false;
if (_min > _max) {
conoutf(@"variable is read-only");
return;
}
if (value < _min) {
value = _min;
outOfRange = true;
}
if (value > _max) {
value = _max;
outOfRange = true;
}
if (outOfRange)
conoutf(@"valid range for %@ is %d..%d", self.name, _min, _max);
*_storage = value;
if (_function != NULL)
// call trigger function if available
_function();
}
@end

View file

@ -243,89 +243,11 @@ execute(char *p, bool isdown) // all evaluation happens here, recursively
// game defined variables // game defined variables
if (isdown) { if (isdown) {
if (!w[1][0]) if (!w[1][0])
// var with no value [identifier printValue];
// just prints its else
// current value
conoutf(@"%s = %d", c,
*[identifier
storage]);
else {
if ([identifier min] >
[identifier max]) {
conoutf(
@"variable "
@"is "
@"read-"
@"only");
} else {
int i1 =
ATOI(w[1]);
if (i1 <
[identifier [identifier
min] || setValue:ATOI(
i1 > w[1])];
[identifier
max]) {
// clamp
// to
// valid
// range
i1 =
i1 < [identifier
min]
? [identifier
min]
: [identifier
max];
conoutf(
@"v"
@"a"
@"l"
@"i"
@"d"
@" "
@"r"
@"a"
@"n"
@"g"
@"e"
@" "
@"f"
@"o"
@"r"
@" "
@"%"
@"s"
@" "
@"i"
@"s"
@" "
@"%"
@"d"
@"."
@"."
@"%"
@"d",
c,
[identifier
min],
[identifier
max]);
}
*[identifier
storage] =
i1;
}
if ([identifier
function] !=
NULL)
// call trigger
// function if
// available
((void(__cdecl
*)())[identifier
function])();
}
} }
} else if ([identifier } else if ([identifier
isKindOfClass:[Alias class]]) { isKindOfClass:[Alias class]]) {

View file

@ -9,7 +9,7 @@ executable('client',
'MapModelInfo.m', 'MapModelInfo.m',
'Menu.m', 'Menu.m',
'MenuItem.m', 'MenuItem.m',
'Variable.m', 'Variable.mm',
'client.mm', 'client.mm',
'clientextras.mm', 'clientextras.mm',
'clientgame.mm', 'clientgame.mm',