The-Powder-Toy/newelement.py

78 lines
1.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2019-05-06 03:59:06 -05:00
import sys
import re
if len(sys.argv) != 2:
name = input('element name: ')
else:
name = sys.argv[1]
if re.search('[^A-Z0-9-]', name):
2019-06-23 05:11:01 -05:00
sys.exit('element names should only contain uppercase letters, digits and hyphens (you can change the Name property of the element to whatever later though, which is what shows up in menus)')
2019-05-06 03:59:06 -05:00
path = 'src/simulation/elements/' + name + '.cpp'
2020-01-09 03:34:15 -06:00
def get_elements():
elements = dict()
with open('src/simulation/ElementNumbers.h', 'r') as numbers:
for nm, pt in re.findall('ELEMENT_DEFINE\\s*\\(\\s*(\\S+)\\s*,\\s*(\\d+)\\s*\\)', numbers.read()):
elements[nm] = int(pt)
return elements
2019-05-06 03:59:06 -05:00
2020-01-09 03:34:15 -06:00
elements = get_elements()
if name in elements:
sys.exit('element already exists')
2019-05-06 03:59:06 -05:00
max_id = 0
2020-01-09 03:34:15 -06:00
for nm, pt in elements.items():
pt_id = int(pt)
if max_id < pt_id:
max_id = pt_id
new_id = max_id + 1
2019-05-06 03:59:06 -05:00
with open(path, 'w') as elem:
elem.write(r"""#include "simulation/ElementCommon.h"
2020-01-09 03:34:15 -06:00
static int update(UPDATE_FUNC_ARGS);
static int graphics(GRAPHICS_FUNC_ARGS);
void Element::Element_{0}()
2019-05-06 03:59:06 -05:00
{{
Identifier = "DEFAULT_PT_{0}";
Name = "{0}";
Colour = PIXPACK(0xFFFFFF);
MenuVisible = 1;
MenuSection = SC_SPECIAL;
Enabled = 1;
// element properties here
2020-01-09 03:34:15 -06:00
Update = &update;
Graphics = &graphics;
2019-05-06 03:59:06 -05:00
}}
2020-01-09 03:34:15 -06:00
static int update(UPDATE_FUNC_ARGS)
2019-05-06 03:59:06 -05:00
{{
// update code here
return 0;
}}
2020-01-09 03:34:15 -06:00
static int graphics(GRAPHICS_FUNC_ARGS)
2019-05-06 03:59:06 -05:00
{{
// graphics code here
// return 1 if nothing dymanic happens here
return 0;
}}
2020-01-09 03:34:15 -06:00
""".format(name))
2019-05-06 03:59:06 -05:00
elem.close()
2020-01-09 03:34:15 -06:00
print('element file \'{0}\' successfully created '.format(path))
input('now add \'ELEMENT_DEFINE({0}, {1});\' to \'src/simulation/ElementNumbers.h\', then press enter'.format(name, str(new_id)))
while True:
elements = get_elements()
if name in elements and elements[name] == new_id:
break
input('nope; try doing that again, then press enter')