Clean up Variable

FossilOrigin-Name: 0ee94739cea768327664b83a23470a366e90ac45fd6c51537b5188268aa8e97c
This commit is contained in:
Jonathan Schleifer 2025-03-25 23:38:08 +00:00
parent f76b9643ec
commit fb8a872695
20 changed files with 145 additions and 125 deletions

View file

@ -6,10 +6,10 @@ OF_ASSUME_NONNULL_BEGIN
OF_CONSTRUCTOR() \
{ \
enqueueInit(^{ \
[Identifier \
addIdentifier:[Command commandWithName:@ #name \
Identifier.identifiers[@ #name] = \
[Command commandWithName:@ #name \
argumentsTypes:nargs \
block:block_]]; \
block:block_]; \
}); \
}

View file

@ -4,6 +4,7 @@
#import "Command.h"
#import "Player.h"
#import "Variable.h"
OF_APPLICATION_DELEGATE(Cube)

View file

@ -4,10 +4,9 @@ OF_ASSUME_NONNULL_BEGIN
@interface Identifier: OFObject
@property (readonly, copy, nonatomic) OFString *name;
@property (class, readonly, nonatomic)
OFMutableDictionary<OFString *, __kindof Identifier *> *identifiers;
+ (void)addIdentifier:(__kindof Identifier *)identifier;
+ (__kindof Identifier *)identifierForName:(OFString *)name;
+ (void)enumerateIdentifiersUsingBlock:(void (^)(__kindof Identifier *))block;
- (instancetype)init OF_UNAVAILABLE;
- (instancetype)initWithName:(OFString *)name;
@end

View file

@ -10,22 +10,9 @@ static OFMutableDictionary<OFString *, __kindof Identifier *> *identifiers;
identifiers = [[OFMutableDictionary alloc] init];
}
+ (void)addIdentifier:(__kindof Identifier *)identifier
+ (OFMutableDictionary<OFString *, __kindof Identifier *> *)identifiers
{
identifiers[identifier.name] = identifier;
}
+ (__kindof Identifier *)identifierForName:(OFString *)name
{
return identifiers[name];
}
+ (void)enumerateIdentifiersUsingBlock:(void (^)(__kindof Identifier *))block
{
[identifiers enumerateKeysAndObjectsUsingBlock:^(
OFString *name, __kindof Identifier *identifier, bool *stop) {
block(identifier);
}];
return identifiers;
}
- (instancetype)initWithName:(OFString *)name

View file

@ -6,6 +6,7 @@
#import "Entity.h"
#import "Player.h"
#import "Variable.h"
static OFMutableArray<Monster *> *monsters;
static int nextmonster, spawnremain, numkilled, monstertotal, mtimestart;

View file

@ -2,24 +2,95 @@
OF_ASSUME_NONNULL_BEGIN
#define VARP(name, min_, cur, max_) \
int name = cur; \
\
OF_CONSTRUCTOR() \
{ \
enqueueInit(^{ \
Variable *variable = \
[Variable variableWithName:@ #name \
min:min_ \
max:max_ \
storage:&name \
function:NULL \
persisted:true]; \
Identifier.identifiers[@ #name] = variable; \
}); \
}
#define VAR(name, min_, cur, max_) \
int name = cur; \
\
OF_CONSTRUCTOR() \
{ \
enqueueInit(^{ \
Variable *variable = \
[Variable variableWithName:@ #name \
min:min_ \
max:max_ \
storage:&name \
function:NULL \
persisted:false]; \
Identifier.identifiers[@ #name] = variable; \
}); \
}
#define VARF(name, min_, cur, max_, body) \
static void var_##name(); \
static int name = cur; \
\
OF_CONSTRUCTOR() \
{ \
enqueueInit(^{ \
Variable *variable = \
[Variable variableWithName:@ #name \
min:min_ \
max:max_ \
storage:&name \
function:var_##name \
persisted:false]; \
Identifier.identifiers[@ #name] = variable; \
}); \
} \
\
static void var_##name() { body; }
#define VARFP(name, min_, cur, max_, body) \
static void var_##name(); \
static int name = cur; \
\
OF_CONSTRUCTOR() \
{ \
enqueueInit(^{ \
Variable *variable = \
[Variable variableWithName:@ #name \
min:min_ \
max:max_ \
storage:&name \
function:var_##name \
persisted:true]; \
Identifier.identifiers[@ #name] = variable; \
}); \
} \
\
static void var_##name() { body; }
@interface Variable: Identifier
@property (readonly, nonatomic) int min, max;
@property (readonly, nonatomic) int *storage;
@property (readonly, nonatomic) void (*__cdecl function)();
@property (readonly, nullable, nonatomic) void (*function)();
@property (readonly, nonatomic) bool persisted;
+ (instancetype)variableWithName:(OFString *)name
min:(int)min
max:(int)max
storage:(int *)storage
function:(void (*__cdecl)())function
function:(void (*_Nullable)())function
persisted:(bool)persisted;
- (instancetype)initWithName:(OFString *)name OF_UNAVAILABLE;
- (instancetype)initWithName:(OFString *)name
min:(int)min
max:(int)max
storage:(int *)storage
function:(void (*__cdecl)())function
function:(void (*_Nullable)())function
persisted:(bool)persisted;
- (void)printValue;
- (void)setValue:(int)value;

View file

@ -8,6 +8,7 @@
#import "Monster.h"
#import "OFString+Cube.h"
#import "Player.h"
#import "Variable.h"
int nextmode = 0; // nextmode becomes gamemode after next map load
VAR(gamemode, 1, 0, 0);

View file

@ -4,6 +4,7 @@
#import "Command.h"
#import "Player.h"
#import "Variable.h"
static ENetHost *clienthost = NULL;
static int connecting = 0;

View file

@ -18,12 +18,12 @@ cleanup(char **string)
void
alias(OFString *name, OFString *action)
{
Alias *alias = [Identifier identifierForName:name];
Alias *alias = Identifier.identifiers[name];
if (alias == nil)
[Identifier addIdentifier:[Alias aliasWithName:name
Identifier.identifiers[name] = [Alias aliasWithName:name
action:action
persisted:true]];
persisted:true];
else {
if ([alias isKindOfClass:Alias.class])
alias.action = action;
@ -37,23 +37,10 @@ COMMAND(alias, ARG_2STR, ^(OFString *name, OFString *action) {
alias(name, action);
})
int
variable(OFString *name, int min, int cur, int max, int *storage,
void (*function)(), bool persisted)
{
[Identifier addIdentifier:[Variable variableWithName:name
min:min
max:max
storage:storage
function:function
persisted:persisted]];
return cur;
}
void
setvar(OFString *name, int i)
{
Variable *variable = [Identifier identifierForName:name];
Variable *variable = Identifier.identifiers[name];
if ([variable isKindOfClass:Variable.class])
*variable.storage = i;
@ -62,7 +49,7 @@ setvar(OFString *name, int i)
int
getvar(OFString *name)
{
Variable *variable = [Identifier identifierForName:name];
Variable *variable = Identifier.identifiers[name];
if ([variable isKindOfClass:Variable.class])
return *variable.storage;
@ -73,13 +60,13 @@ getvar(OFString *name)
bool
identexists(OFString *name)
{
return ([Identifier identifierForName:name] != nil);
return (Identifier.identifiers[name] != nil);
}
OFString *
getalias(OFString *name)
{
Alias *alias = [Identifier identifierForName:name];
Alias *alias = Identifier.identifiers[name];
if ([alias isKindOfClass:Alias.class])
return alias.action;
@ -153,7 +140,7 @@ OFString *
lookup(OFString *n)
{
__kindof Identifier *identifier =
[Identifier identifierForName:[n substringFromIndex:1]];
Identifier.identifiers[[n substringFromIndex:1]];
if ([identifier isKindOfClass:Variable.class]) {
return [OFString stringWithFormat:@"%d", *[identifier storage]];
@ -260,7 +247,7 @@ execute(OFString *string, bool isDown)
if (c.length == 0)
continue;
val = executeIdentifier([Identifier identifierForName:c],
val = executeIdentifier(Identifier.identifiers[c],
[OFArray arrayWithObjects:w count:numargs], isDown);
}
@ -292,8 +279,8 @@ complete(OFMutableString *s)
}
__block int idx = 0;
[Identifier enumerateIdentifiersUsingBlock:^(
__kindof Identifier *identifier) {
[Identifier.identifiers enumerateKeysAndObjectsUsingBlock:^(
OFString *name, __kindof Identifier *identifier, bool *stop) {
if (strncmp(identifier.name.UTF8String, s.UTF8String + 1,
completesize) == 0 &&
idx++ == completeidx)
@ -359,8 +346,8 @@ writecfg()
writeclientinfo(stream);
[stream writeString:@"\n"];
[Identifier
enumerateIdentifiersUsingBlock:^(__kindof Identifier *identifier) {
[Identifier.identifiers enumerateKeysAndObjectsUsingBlock:^(
OFString *name, __kindof Identifier *identifier, bool *stop) {
if (![identifier isKindOfClass:Variable.class] ||
![identifier persisted])
return;
@ -373,8 +360,8 @@ writecfg()
writebinds(stream);
[stream writeString:@"\n"];
[Identifier
enumerateIdentifiersUsingBlock:^(__kindof Identifier *identifier) {
[Identifier.identifiers enumerateKeysAndObjectsUsingBlock:^(
OFString *name, __kindof Identifier *identifier, bool *stop) {
if (![identifier isKindOfClass:Alias.class] ||
[identifier.name hasPrefix:@"nextmap_"])
return;

View file

@ -331,51 +331,6 @@ enum {
ARG_VARI
};
// nasty macros for registering script functions, abuses globals to avoid
// excessive infrastructure
#define VARP(name, min, cur, max) \
int name; \
OF_CONSTRUCTOR() \
{ \
enqueueInit(^{ \
name = variable( \
@ #name, min, cur, max, &name, NULL, true); \
}); \
}
#define VAR(name, min, cur, max) \
int name; \
OF_CONSTRUCTOR() \
{ \
enqueueInit(^{ \
name = variable( \
@ #name, min, cur, max, &name, NULL, false); \
}); \
}
#define VARF(name, min, cur, max, body) \
void var_##name(); \
static int name; \
OF_CONSTRUCTOR() \
{ \
enqueueInit(^{ \
name = variable( \
@ #name, min, cur, max, &name, var_##name, false); \
}); \
} \
void var_##name() { body; }
#define VARFP(name, min, cur, max, body) \
void var_##name(); \
static int name; \
OF_CONSTRUCTOR() \
{ \
enqueueInit(^{ \
name = variable( \
@ #name, min, cur, max, &name, var_##name, true); \
}); \
} \
void var_##name() { body; }
#define ATOI(s) strtol(s, NULL, 0) // supports hexadecimal numbers
#ifdef WIN32
# define WIN32_LEAN_AND_MEAN
# include "windows.h"

View file

@ -8,6 +8,7 @@
#import "Monster.h"
#import "OFString+Cube.h"
#import "Player.h"
#import "Variable.h"
bool editmode = false;
@ -15,17 +16,27 @@ bool editmode = false;
// invariant: all code assumes that these are kept inside MINBORD distance of
// the edge of the map
struct block sel;
struct block sel = { 0, 0, 0, 0 };
OF_CONSTRUCTOR()
{
enqueueInit(^{
sel = (struct block) {
variable(@"selx", 0, 0, 4096, &sel.x, NULL, false),
variable(@"sely", 0, 0, 4096, &sel.y, NULL, false),
variable(@"selxs", 0, 0, 4096, &sel.xs, NULL, false),
variable(@"selys", 0, 0, 4096, &sel.ys, NULL, false),
};
static const struct {
OFString *name;
int *storage;
} vars[4] = { { @"selx", &sel.x }, { @"sely", &sel.y },
{ @"selxs", &sel.xs }, { @"selys", &sel.ys } };
for (size_t i = 0; i < 4; i++) {
Variable *variable =
[Variable variableWithName:vars[i].name
min:0
max:4096
storage:vars[i].storage
function:NULL
persisted:false];
Identifier.identifiers[vars[i].name] = variable;
}
});
}

View file

@ -11,6 +11,7 @@
#import "MapModelInfo.h"
#import "Monster.h"
#import "Player.h"
#import "Variable.h"
// collide with player or monster
static bool

View file

@ -5,8 +5,6 @@ extern "C" {
#endif
// command
extern int variable(OFString *name, int min, int cur, int max, int *storage,
void (*fun)(), bool persist);
extern void setvar(OFString *name, int i);
extern int getvar(OFString *name);
extern bool identexists(OFString *name);

View file

@ -4,6 +4,7 @@
#include "cube.h"
#import "Command.h"
#import "Variable.h"
static struct vertex *verts = NULL;
int curvert;

View file

@ -5,6 +5,7 @@
#import "Command.h"
#import "Entity.h"
#import "Player.h"
#import "Variable.h"
void
line(int x1, int y1, float z1, int x2, int y2, float z2)

View file

@ -8,6 +8,7 @@
#import "Monster.h"
#import "OFString+Cube.h"
#import "Player.h"
#import "Variable.h"
#ifdef DARWIN
# define GL_COMBINE_EXT GL_COMBINE_ARB

View file

@ -3,6 +3,7 @@
#include "cube.h"
#import "Player.h"
#import "Variable.h"
#define MAXPARTICLES 10500
const int NUMPARTCUTOFF = 20;

View file

@ -7,6 +7,7 @@
#import "Entity.h"
#import "Monster.h"
#import "Player.h"
#import "Variable.h"
#ifdef OF_BIG_ENDIAN
static const int islittleendian = 0;

View file

@ -2,6 +2,7 @@
#import "Command.h"
#import "Player.h"
#import "Variable.h"
#include <SDL_mixer.h>

View file

@ -5,6 +5,7 @@
#import "DynamicEntity.h"
#import "Entity.h"
#import "Monster.h"
#import "Variable.h"
extern bool hasoverbright;